-
Notifications
You must be signed in to change notification settings - Fork 2
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
NickAkhmetov / HMP-235 Add support for text-only publication vignettes #3159
Merged
NickAkhmetov
merged 6 commits into
main
from
nickakhmetov/hmp-235-enable-display-of-text-only-vignettes
Jul 12, 2023
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2947640
HMP-235 Add support for text-only publication vignettes
NickAkhmetov 94b91f8
Explanatory comments
NickAkhmetov e489b1f
Revert contexts to `jsx` for now to avoid breaking storybook
NickAkhmetov 6a38a9d
Update CHANGELOG-hmp-235.md
NickAkhmetov f1e9f76
Review fixes
NickAkhmetov bb25aab
Don't use fallback value for urls so `useSWR` properly handles condit…
NickAkhmetov 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add support for text-only publication vignettes without Vitessce visualizations. |
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
43 changes: 43 additions & 0 deletions
43
context/app/static/js/components/publications/PublicationVignette/hooks.ts
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,43 @@ | ||
import useSWR from 'swr'; | ||
import { useAppContext } from 'js/components/Contexts'; | ||
import { multiFetcher } from 'js/helpers/multiFetcher'; | ||
import { fillUrls } from './utils'; | ||
import { PublicationVignette } from '../types'; | ||
|
||
type PublicationVignetteConfsInput = { | ||
uuid: string; | ||
vignette: PublicationVignette; | ||
vignetteDirName: string; | ||
}; | ||
|
||
export function usePublicationVignetteConfs({ uuid, vignetteDirName, vignette }: PublicationVignetteConfsInput) { | ||
const { assetsEndpoint, groupsToken } = useAppContext(); | ||
// Extract file paths from the vignette object to form the urls to fetch for this vignette | ||
const urls = | ||
vignette.figures?.map( | ||
({ file }) => `${assetsEndpoint}/${uuid}/vignettes/${vignetteDirName}/${file}?token=${groupsToken}`, | ||
) ?? []; | ||
const { data } = useSWR(urls, multiFetcher, { | ||
revalidateOnFocus: false, | ||
revalidateOnReconnect: false, | ||
}); | ||
|
||
if (data) { | ||
const urlHandler = (url: string, isZarr: boolean) => { | ||
return `${url.replace('{{ base_url }}', `${assetsEndpoint}/${uuid}/data`)}${ | ||
isZarr ? '' : `?token=${groupsToken}` | ||
}`; | ||
}; | ||
|
||
const requestInitHandler = () => { | ||
return { | ||
headers: { Authorization: `Bearer ${groupsToken}` }, | ||
}; | ||
}; | ||
// Formats the vitessce config data to replace the {{ base_url }} placeholder with the actual url. | ||
// TODO: Improve this `unknown`; I couldn't figure out how to import the appropriate `VitessceConfig` type from Vitessce. | ||
const formattedData: unknown[] = data.map((d) => fillUrls(d, urlHandler, requestInitHandler)); | ||
return formattedData; | ||
} | ||
return undefined; | ||
} |
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,11 @@ | ||
export type VignetteFigure = { | ||
file: string; | ||
name: string; | ||
}; | ||
|
||
export type PublicationVignette = { | ||
name: string; | ||
description: string; | ||
figures?: VignetteFigure[]; | ||
directory_name: string; | ||
}; |
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,5 @@ | ||
// Fetcher function that lets SWR fetch multiple urls at once | ||
export const multiFetcher = (...urls: string[]) => { | ||
const f = (url: string) => fetch(url).then((response) => response.json()); | ||
return Promise.all(urls.map((url) => f(url))); | ||
}; |
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 |
---|---|---|
|
@@ -20,8 +20,8 @@ | |
"sourceMap": true, | ||
"noEmit": true, | ||
"paths": { | ||
"@/*": [ | ||
"./app/static/js/*" | ||
"js/*": [ | ||
"./*" | ||
] | ||
} | ||
}, | ||
|
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.
What happens when figures is undefined and in turn urls are undefined?
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 nullish coalescing fallback on line 19 ensures that
urls
is always at least an empty list; even if that wasn't there, swr's conditional fetching functionality would ensure that the fetch doesn't run,data
remains undefined, and thereturn undefined
case on line 42 would be reached. https://swr.vercel.app/docs/conditional-fetchingThere 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.
Ah sorry didn't catch that!
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.
Actually, on second thought, that
?? []
shouldn't be there since an empty array is truthy. One moment.