-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use meilisearch to get downstream links data
- Loading branch information
Showing
14 changed files
with
260 additions
and
154 deletions.
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
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,93 @@ | ||
import { FormattedMessage } from '@edx/frontend-platform/i18n'; | ||
import { Collapsible } from '@openedx/paragon'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
import AlertError from '../../generic/alert-error'; | ||
import Loading from '../../generic/Loading'; | ||
import { useFetchIndexDocuments, type ContentHit } 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: ContentHit['breadcrumbs'][1][], | ||
}>; | ||
|
||
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) { | ||
return <Loading />; | ||
} | ||
|
||
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; | ||
} | ||
|
||
if (hit.contextKey in acc) { | ||
acc[hit.contextKey].links.push(link); | ||
} else { | ||
acc[hit.contextKey] = { | ||
key: hit.contextKey, | ||
contextName: hit.breadcrumbs[0].displayName, | ||
links: [link], | ||
}; | ||
} | ||
return acc; | ||
}, {}); | ||
|
||
console.error(componentUsage); | ||
|
||
const componentUsageList = Object.values(componentUsage); | ||
|
||
return ( | ||
componentUsageList.length ? ( | ||
<> | ||
{ | ||
componentUsageList.map((context) => ( | ||
<Collapsible key={context.key} title={context.contextName} styling="basic"> | ||
{context.links.map(({ usageKey: downstreamUsageKey, url, displayName }) => ( | ||
url ? ( | ||
<Link key={downstreamUsageKey} to={url}>{displayName}</Link> | ||
) : ( | ||
<span key={downstreamUsageKey}>{displayName}</span> | ||
) | ||
))} | ||
</Collapsible> | ||
)) | ||
} | ||
</> | ||
) : <FormattedMessage {...messages.detailsTabUsageEmpty} /> | ||
); | ||
}; |
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
Oops, something went wrong.