-
Notifications
You must be signed in to change notification settings - Fork 185
feat: add component usage data in the ComponentDetails component [FC-0076] #1656
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
Merged
ChrisChV
merged 8 commits into
openedx:master
from
open-craft:rpenido/fal-4005/list-courses-using-library
Feb 25, 2025
Merged
Changes from all commits
Commits
Show all changes
8 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 1af6bb6
fix: change url to studio
rpenido 950fdc3
Merge branch 'master' into rpenido/fal-4005/list-courses-using-library
rpenido a641ffb
fix: only show the xblock parent one time
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 hidden or 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 hidden or 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 hidden or 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
116 changes: 116 additions & 0 deletions
116
src/library-authoring/component-info/ComponentUsage.tsx
This file contains hidden or 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,116 @@ | ||
| import { getConfig } from '@edx/frontend-platform'; | ||
| import { FormattedMessage } from '@edx/frontend-platform/i18n'; | ||
| import { Collapsible, Hyperlink, Stack } 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 = (usageKey: string) => ( | ||
| `${getConfig().STUDIO_BASE_URL}/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 = { | ||
| displayName: link.displayName, | ||
| url: getContainerUrl(link.usageKey), | ||
| }; | ||
|
|
||
| if (hit.contextKey in acc) { | ||
| if (!(link.usageKey in acc[hit.contextKey].links)) { | ||
| acc[hit.contextKey].links[link.usageKey] = linkData; | ||
| return acc; | ||
| } | ||
| } else { | ||
| acc[hit.contextKey] = { | ||
| key: hit.contextKey, | ||
| contextName: hit.breadcrumbs[0].displayName, | ||
| links: { | ||
| [link.usageKey]: linkData, | ||
| }, | ||
| }; | ||
| } | ||
| return acc; | ||
| }, {}); | ||
|
|
||
| const componentUsageList = Object.values(componentUsage); | ||
|
|
||
| return ( | ||
| <> | ||
| { | ||
| componentUsageList.map((context) => ( | ||
| <Collapsible key={context.key} title={context.contextName} styling="basic"> | ||
| <Stack> | ||
| {Object.keys(context.links).map((downstreamUsageKey: string) => ( | ||
| <Hyperlink | ||
| key={downstreamUsageKey} | ||
| destination={context.links[downstreamUsageKey].url} | ||
| target="_blank" | ||
| > | ||
| {context.links[downstreamUsageKey].displayName} | ||
| </Hyperlink> | ||
| ))} | ||
| </Stack> | ||
| </Collapsible> | ||
| )) | ||
| } | ||
| </> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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/openedx-platform#36253 (comment)
Uh oh!
There was an error while loading. Please reload this page.
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.
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 it might be a component under Problem Bank which is only supported legacy studio, so it doesn't redirect to MFE.
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're right! I may have done something wrong with my testing.
It is respecting the waffle flag as expected.