-
Notifications
You must be signed in to change notification settings - Fork 0
Add alternate term names for facet terms #731
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
Draft
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/add-alternate-term-names
base: main
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.
+612
−15
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # Alternate Term Names | ||
|
|
||
| This feature allows consuming applications to provide alternate display names for facet terms in the UI filtering system. | ||
|
|
||
| ## Overview | ||
|
|
||
| The alternate term names system provides a way to display user-friendly names for facet terms while maintaining the original term values for filtering and data operations. | ||
|
|
||
| ### Use Cases | ||
| - **Synonyms**: Display "Human" instead of "Homo sapiens" | ||
| - **UI Localization**: Show translated terms in different languages | ||
| - **User-friendly phrasing**: Replace technical terms with more accessible language | ||
|
|
||
| ## Setup | ||
|
|
||
| ### 1. Add the Provider | ||
|
|
||
| Wrap your application with the `AlternateTermNamesProvider` at a high level (typically alongside other providers): | ||
|
|
||
| ```tsx | ||
| import { AlternateTermNamesProvider } from "@databiosphere/findable-ui/lib/providers/alternateTermNames/alternateTermNames"; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <AlternateTermNamesProvider> | ||
| {/* Your app components */} | ||
| </AlternateTermNamesProvider> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Create the Alternate Names File | ||
|
|
||
| Create a file at `/public/fe-api/alternateTermNames.json` in your app with the following structure: | ||
|
|
||
| ```json | ||
| { | ||
| "facetName": { | ||
| "originalTermName": "Alternate Display Name" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Example:** | ||
|
|
||
| ```json | ||
| { | ||
| "species": { | ||
| "Homo sapiens": "Human", | ||
| "Mus musculus": "Mouse", | ||
| "Rattus norvegicus": "Rat" | ||
| }, | ||
| "tissue": { | ||
| "cerebral cortex": "Brain Cortex", | ||
| "myocardium": "Heart Muscle" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Behavior | ||
|
|
||
| ### File Loading | ||
| - The file is loaded **once** when the app initializes | ||
| - The file is **optional** - if it doesn't exist, the system continues without errors | ||
| - If the file is missing or fails to load, facet terms display their original names | ||
|
|
||
| ### Term Display | ||
| - When an alternate name is defined, it will be displayed in facet selection UI | ||
| - The original term name is still used for filtering and data operations | ||
| - If no alternate name is found for a term, the original name is displayed | ||
|
|
||
| ### Fallback Behavior | ||
| The system is robust to various error conditions: | ||
| - **File not found**: No errors, terms display original names | ||
| - **Empty file**: Terms display original names | ||
| - **Missing facet**: Terms for that facet display original names | ||
| - **Missing term**: That specific term displays its original name | ||
| - **JSON parse error**: No errors, terms display original names | ||
|
|
||
| ## Data Flow | ||
|
|
||
| 1. **Term Creation**: Terms are created in `bindFacetTerms` with both `name` (original) and `alternateName` (if available) | ||
| 2. **Term Display**: UI components use `term.alternateName ?? term.name` to display the user-friendly name | ||
| 3. **Term Operations**: Filtering and data operations continue to use `term.name` (the original value) | ||
|
|
||
| ## Important Notes | ||
|
|
||
| - **Facet UI Only**: Alternate names are used only in facet selection UI, not in result tables or data rows | ||
| - **Case Sensitive**: Term name matching is case-sensitive | ||
| - **Performance**: The file is loaded once and cached in memory for the session | ||
| - **No Retries**: There are no automatic retries if the file fails to load | ||
|
|
||
| ## Testing | ||
|
|
||
| When testing alternate term names: | ||
|
|
||
| ```typescript | ||
| import { AlternateTermNamesProvider } from "@databiosphere/findable-ui/lib/providers/alternateTermNames/alternateTermNames"; | ||
| import { useAlternateTermNames } from "@databiosphere/findable-ui/lib/providers/alternateTermNames/useAlternateTermNames"; | ||
|
|
||
| // In your test | ||
| const wrapper = ({ children }) => ( | ||
| <AlternateTermNamesProvider>{children}</AlternateTermNamesProvider> | ||
| ); | ||
|
|
||
| // Mock the fetch call | ||
| global.fetch = jest.fn().mockResolvedValue({ | ||
| ok: true, | ||
| json: async () => ({ | ||
| species: { "Homo sapiens": "Human" } | ||
| }) | ||
| }); | ||
| ``` | ||
|
|
||
| ## Migrating Existing Applications | ||
|
|
||
| 1. Add the `AlternateTermNamesProvider` to your app's provider hierarchy | ||
| 2. Create the `/public/fe-api/alternateTermNames.json` file (optional) | ||
| 3. No code changes are required in components that display facet terms | ||
|
|
||
| The system is backward compatible - applications that don't provide alternate names will continue to work exactly as before. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import React, { createContext, ReactNode, useEffect, useState } from "react"; | ||
|
|
||
| /** | ||
| * Alternate term names map structure: { facetName: { termName: alternateName } } | ||
| */ | ||
| export type AlternateTermNamesMap = Record<string, Record<string, string>>; | ||
|
|
||
| /** | ||
| * Context props for alternate term names. | ||
| */ | ||
| export type AlternateTermNamesContextProps = { | ||
| alternateTermNames: AlternateTermNamesMap | null; | ||
| }; | ||
|
|
||
| /** | ||
| * Provider props for alternate term names. | ||
| */ | ||
| export interface AlternateTermNamesProps { | ||
| children: ReactNode | ReactNode[]; | ||
| } | ||
|
|
||
| /** | ||
| * Context for alternate term names. | ||
| */ | ||
| export const AlternateTermNamesContext = | ||
| createContext<AlternateTermNamesContextProps>({ | ||
| alternateTermNames: null, | ||
| }); | ||
|
|
||
| /** | ||
| * Provider for loading alternate term names from /fe-api/alternateTermNames.json. | ||
| * Loads the file once on mount and caches it in memory for the session. | ||
| * If the file is missing or fails to load, falls back to an empty map. | ||
| * @param props - Provider props. | ||
| * @param props.children - Child components. | ||
| * @returns JSX element. | ||
| */ | ||
| export function AlternateTermNamesProvider({ | ||
| children, | ||
| }: AlternateTermNamesProps): JSX.Element { | ||
| const [alternateTermNames, setAlternateTermNames] = | ||
| useState<AlternateTermNamesMap | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| // Load alternate term names once on mount | ||
| fetch("/fe-api/alternateTermNames.json") | ||
| .then((response) => { | ||
| if (response.ok) { | ||
| return response.json(); | ||
| } | ||
| // If file doesn't exist or isn't ok, return empty object | ||
| return {}; | ||
| }) | ||
| .then((data: AlternateTermNamesMap) => { | ||
| setAlternateTermNames(data); | ||
| }) | ||
| .catch(() => { | ||
| // On any error, use empty map (file missing, parse error, etc.) | ||
| setAlternateTermNames({}); | ||
| }); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <AlternateTermNamesContext.Provider value={{ alternateTermNames }}> | ||
| {children} | ||
| </AlternateTermNamesContext.Provider> | ||
| ); | ||
| } |
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,13 @@ | ||
| import { useContext } from "react"; | ||
| import { | ||
| AlternateTermNamesContext, | ||
| AlternateTermNamesContextProps, | ||
| } from "./alternateTermNames"; | ||
|
|
||
| /** | ||
| * Hook to access alternate term names from context. | ||
| * @returns Context props containing the alternate term names map. | ||
| */ | ||
| export function useAlternateTermNames(): AlternateTermNamesContextProps { | ||
| return useContext(AlternateTermNamesContext); | ||
| } |
Oops, something went wrong.
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.
The return description "something" is not descriptive. It should be updated to accurately describe what the function returns, such as "file facet with populated terms and selected state" or similar.
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.
Fixed in commit ded05c8. Updated the JSDoc return description to "File facet with populated terms and selected state."