-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add component usage data in the ComponentDetails component [FC-0076] #1656
Open
rpenido
wants to merge
5
commits into
openedx:master
Choose a base branch
from
open-craft:rpenido/fal-4005/list-courses-using-library
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0c35fe8
feat: add component usage data in the ComponentDetails component
rpenido 586c3d8
refactor: construct container url on frontend
rpenido a3d86b4
fix: stuck loading issue
rpenido f462f23
fix: use correct url
rpenido 42a4949
fix: open in new tab
rpenido File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/library-authoring/component-info/ComponentUsage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { getConfig, getPath } from '@edx/frontend-platform'; | ||
import { FormattedMessage } from '@edx/frontend-platform/i18n'; | ||
import { Collapsible, Hyperlink } from '@openedx/paragon'; | ||
|
||
import AlertError from '../../generic/alert-error'; | ||
import Loading from '../../generic/Loading'; | ||
import { useFetchIndexDocuments } from '../../search-manager'; | ||
import { useComponentDownstreamLinks } from '../data/apiHooks'; | ||
import messages from './messages'; | ||
|
||
interface ComponentUsageProps { | ||
usageKey: string; | ||
} | ||
|
||
type ComponentUsageTree = Record<string, { | ||
key: string, | ||
contextName: string, | ||
links: { | ||
usageKey: string, | ||
displayName: string, | ||
url: string, | ||
}[] | ||
}>; | ||
|
||
const getContainerUrl = (contextKey: string, usageKey: string) => ( | ||
`${getPath(getConfig().PUBLIC_PATH)}course/${contextKey}/container/${usageKey}` | ||
); | ||
|
||
export const ComponentUsage = ({ usageKey }: ComponentUsageProps) => { | ||
const { | ||
data: dataDownstreamLinks, | ||
isError: isErrorDownstreamLinks, | ||
error: errorDownstreamLinks, | ||
isLoading: isLoadingDownstreamLinks, | ||
} = useComponentDownstreamLinks(usageKey); | ||
|
||
const downstreamKeys = dataDownstreamLinks || []; | ||
|
||
const { | ||
data: downstreamHits, | ||
isError: isErrorIndexDocuments, | ||
error: errorIndexDocuments, | ||
isLoading: isLoadingIndexDocuments, | ||
} = useFetchIndexDocuments({ | ||
filter: [`usage_key IN ["${downstreamKeys.join('","')}"]`], | ||
limit: downstreamKeys.length, | ||
attributesToRetrieve: ['usage_key', 'breadcrumbs', 'context_key'], | ||
enabled: !!downstreamKeys.length, | ||
}); | ||
|
||
if (isErrorDownstreamLinks || isErrorIndexDocuments) { | ||
return <AlertError error={errorDownstreamLinks || errorIndexDocuments} />; | ||
} | ||
|
||
if (isLoadingDownstreamLinks || (isLoadingIndexDocuments && !!downstreamKeys.length)) { | ||
return <Loading />; | ||
} | ||
|
||
if (!downstreamKeys.length || !downstreamHits) { | ||
return <FormattedMessage {...messages.detailsTabUsageEmpty} />; | ||
} | ||
|
||
const componentUsage = downstreamHits.reduce<ComponentUsageTree>((acc, hit) => { | ||
const link = hit.breadcrumbs.at(-1); | ||
// istanbul ignore if: this should never happen. it is a type guard for the breadcrumb last item | ||
if (!(link && ('usageKey' in link))) { | ||
return acc; | ||
} | ||
|
||
const linkData = { | ||
...link, | ||
url: getContainerUrl(hit.contextKey, link.usageKey), | ||
}; | ||
|
||
if (hit.contextKey in acc) { | ||
acc[hit.contextKey].links.push(linkData); | ||
} else { | ||
acc[hit.contextKey] = { | ||
key: hit.contextKey, | ||
contextName: hit.breadcrumbs[0].displayName, | ||
links: [linkData], | ||
}; | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
const componentUsageList = Object.values(componentUsage); | ||
|
||
return ( | ||
<> | ||
{ | ||
componentUsageList.map((context) => ( | ||
<Collapsible key={context.key} title={context.contextName} styling="basic"> | ||
{context.links.map(({ usageKey: downstreamUsageKey, displayName, url }) => ( | ||
<Hyperlink key={downstreamUsageKey} destination={url} target="_blank">{displayName}</Hyperlink> | ||
))} | ||
</Collapsible> | ||
)) | ||
} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ | ||
"comment": "This is a mock of the response from Meilisearch for downstream links", | ||
"estimatedTotalHits": 3, | ||
"query": "", | ||
"limit": 3, | ||
"offset": 0, | ||
"processingTimeMs": 1, | ||
"hits": [ | ||
{ | ||
"usageKey": "block-v1:org+course1+run+type@html+block@blockid1", | ||
"contextKey": "course-v1:org+course1+run", | ||
"breadcrumbs": [ | ||
{ | ||
"display_name": "Course 1" | ||
}, | ||
{ | ||
"usage_key": "unit-v1:org+course1+run+section1", | ||
"display_name": "Section 1" | ||
}, | ||
{ | ||
"usage_key": "unit-v1:org+course1+run+subsection1", | ||
"display_name": "Sub Section 1" | ||
}, | ||
{ | ||
"usage_key": "block-v1:org+course1+run+type@vertical+block@verticalId1", | ||
"display_name": "Unit 1" | ||
} | ||
] | ||
}, | ||
{ | ||
"usage_key": "block-v1:org+course1+run+type@html+block@blockid2", | ||
"contextKey": "course-v1:org+course1+run", | ||
"breadcrumbs": [ | ||
{ | ||
"display_name": "Course 1" | ||
}, | ||
{ | ||
"usage_key": "unit-v1:org+course1+run+section1", | ||
"display_name": "Section 1" | ||
}, | ||
{ | ||
"usage_key": "unit-v1:org+course1+run+subsection1", | ||
"display_name": "Sub Section 1" | ||
}, | ||
{ | ||
"usage_key": "block-v1:org+course1+run+type@vertical+block@verticalId2", | ||
"display_name": "Unit 2" | ||
} | ||
] | ||
}, | ||
{ | ||
"usage_key": "block-v1:org+course2+run+type@html+block@blockid1", | ||
"contextKey": "course-v1:org+course2+run", | ||
"breadcrumbs": [ | ||
{ | ||
"display_name": "Course 2" | ||
}, | ||
{ | ||
"usage_key": "unit-v1:org+course2+run+section1", | ||
"display_name": "Section 1" | ||
}, | ||
{ | ||
"usage_key": "unit-v1:org+course2+run+subsection1", | ||
"display_name": "Sub Section 1" | ||
}, | ||
{ | ||
"usage_key": "block-v1:org+course1+run+type@vertical+block@verticalId3", | ||
"display_name": "Unit 3" | ||
}, | ||
{ | ||
"usage_key": "block-v1:org+course2+run+type@itembank+block@itembankId3", | ||
"display_name": "Problem Bank 3" | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to build url to link to the units, like here. Currently, I just see the unit names and links don't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch this, I missed to reindex and test. Will come back to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think building the url here is simple enough to avoid adding the link data to index. See openedx/edx-platform#36253 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @navinkarkera! Fixed!
I made it a bit different because the link on the Library page redirects me to Legacy Studio, despite the waffle flag.