Skip to content

Commit

Permalink
Replace Datasets DatasetSelect component on groups page with ListReso…
Browse files Browse the repository at this point in the history
…urces [#870]

* make the Resource.lastUpdated property optional
* conditionalize display of "List known update" in IndividualResource
  TooltipWrapper
* conditionalize display of "Most recent snapshot" in ResourceGroup
* conditionalize use of Showcase in ListResources based on length of
  showcaseCards prop
* conditionalize use of SortOptions in ListResources based on initial
  group lacking a lastUpdated field
* extend groups page with ListResources callback to hit
  charon/getAvailableResources endpoint
  • Loading branch information
genehack committed Jun 12, 2024
1 parent ce5729b commit 0f576aa
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const IndividualResource = ({resource, isMobile}: IndividualResourceProps

// add history if mobile and resource has version info
let history: React.JSX.Element | null = null
if (!isMobile && resource.updateCadence && resource.nVersions) {
if (!isMobile && resource.updateCadence && resource.nVersions && resource.lastUpdated) {
history = (
<TooltipWrapper description={resource.updateCadence.description +
`<br/>Last known update on ${resource.lastUpdated}` +
Expand All @@ -158,12 +158,14 @@ export const IndividualResource = ({resource, isMobile}: IndividualResourceProps
)
}

const description = resource.lastUpdated ? `Last known update on ${resource.lastUpdated}` : "";

return (
<Container ref={ref}>

<FlexRow>

<TooltipWrapper description={`Last known update on ${resource.lastUpdated}`}>
<TooltipWrapper description={description}>
<ResourceLinkWrapper onShiftClick={() => setModalResource(resource)}>
<Name displayName={resource.displayName} href={resource.url} topOfColumn={topOfColumn}/>
</ResourceLinkWrapper>
Expand Down
2 changes: 1 addition & 1 deletion static-site/src/components/ListResources/ResourceGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ResourceGroupHeader = ({group, isMobile, setCollapsed, collapsible, isColl
<TooltipWrapper description={`The most recently updated datasets in this group were last updated on ${group.lastUpdated}` +
'<br/>(however there may have been a more recent update since we indexed the data)'}>
<span>
{`Most recent snapshot: ${group.lastUpdated}`}
{group.lastUpdated && `Most recent snapshot: ${group.lastUpdated}`}
</span>
</TooltipWrapper>
)}
Expand Down
10 changes: 8 additions & 2 deletions static-site/src/components/ListResources/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@ function ListResources({
return (
<ListResourcesContainer>

<Showcase cards={showcaseCards} setSelectedFilterOptions={setSelectedFilterOptions}/>
{ showcaseCards.length > 0 && (
<Showcase cards={showcaseCards} setSelectedFilterOptions={setSelectedFilterOptions}/>
)}

<Filter options={availableFilterOptions} selectedFilterOptions={selectedFilterOptions} setSelectedFilterOptions={setSelectedFilterOptions}/>

<SortOptions sortMethod={sortMethod} changeSortMethod={changeSortMethod}/>
{ groups && groups[0]?.lastUpdated && (
<SortOptions sortMethod={sortMethod} changeSortMethod={changeSortMethod}/>
) || (
<HugeSpacer/>
)}

<SetModalResourceContext.Provider value={setModalResource}>
<ScrollableAnchor id={LIST_ANCHOR}>
Expand Down
2 changes: 1 addition & 1 deletion static-site/src/components/ListResources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Resource {
nameParts: string[]
sortingName: string
url: string
lastUpdated: string // date
lastUpdated?: string // date
firstUpdated?: string // date
dates?: string[]
nVersions?: number
Expand Down
6 changes: 1 addition & 5 deletions static-site/src/components/ListResources/useDataFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ function partitionByPathogen(pathVersions: PathVersions, pathPrefix: string, ver
return Object.entries(pathVersions).reduce((store: Partitions, [name, dates]) => {
const sortedDates = [...dates].sort();

// do nothing if resource has no dates
if (sortedDates.length < 1) return store

const nameParts = name.split('/');
// split() will always return at least 1 string
const groupName = nameParts[0]!;
Expand All @@ -77,11 +74,10 @@ function partitionByPathogen(pathVersions: PathVersions, pathPrefix: string, ver
nameParts,
sortingName: _sortableName(nameParts),
url: `/${pathPrefix}${name}`,
lastUpdated: sortedDates.at(-1)!,
lastUpdated: sortedDates.at(-1),
};
if (versioned) {
resourceDetails.firstUpdated = sortedDates[0]!;
resourceDetails.lastUpdated = sortedDates.at(-1)!;
resourceDetails.dates = sortedDates;
resourceDetails.nVersions = sortedDates.length;
resourceDetails.updateCadence = updateCadence(sortedDates.map((date)=> new Date(date)));
Expand Down
63 changes: 40 additions & 23 deletions static-site/src/pages/groups.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,50 @@ import { SmallSpacer, HugeSpacer, FlexCenter } from "../layouts/generalComponent
import * as splashStyles from "../components/splash/styles";
import { fetchAndParseJSON } from "../util/datasetsHelpers";
import DatasetSelect from "../components/Datasets/dataset-select";
import ListResources from "../components/ListResources/index";
import { GroupCards } from "../components/splash/groupCards";
import GenericPage from "../layouts/generic-page";
import { UserContext } from "../layouts/userDataWrapper";
import { DataFetchErrorParagraph } from "../components/splash/errorMessages";
import { groupsTitle, GroupsAbstract } from "../../data/SiteConfig";

const datasetColumns = ({isNarrative}) => [
const resourceListingCallback = async (response) => {
const sourceUrl = "/charon/getAvailable?prefix=/groups/";

try {
const response = await fetch(sourceUrl, {headers: {accept: "application/json"}});
if (response.status !== 200) {
throw new Error(`fetching data from "${sourceUrl}" returned status code ${response.status}`);
}
const datasets = (await response.json()).datasets;

// Use an empty array as the value side, to indicate that there
// are no dated versions associated with this data
const pathVersions = Object.assign(
...datasets.map((ds) => ({
[ds.request.replace(/^\/groups\//, "")]: []
})),
);

return { pathPrefix: "groups/", pathVersions };
} catch (err) {
console.error(`Error while fetching data from "${sourceUrl}"`);
console.error(err);
return setDataFetchError(true);
}
}

const datasetColumns = [
{
name: isNarrative ? "Narrative" : "Dataset",
name: "Narrative",
value: (d) => d.request.replace("/groups/", "").replace(/\//g, " / "),
url: (d) => d.url
url: (d) => d.url,
},
{
name: "Group Name",
value: (d) => d.request.split("/")[2],
url: (d) => `/groups/${d.request.split("/")[2]}`
}
url: (d) => `/groups/${d.request.split("/")[2]}`,
},
];

const GroupListingInfo = () => {
Expand Down Expand Up @@ -92,31 +119,21 @@ class GroupsPage extends React.Component {
<GroupCards squashed/>
<HugeSpacer />

<ScrollableAnchor id={'datasets'}>
<splashStyles.H2>Available Datasets</splashStyles.H2>
</ScrollableAnchor>
<FlexCenter>
<splashStyles.CenteredFocusParagraph>
{`Note that this listing is refreshed every ~6 hours.
To see a current listing, visit the page for that group by clicking on that group's tile, above.`}
</splashStyles.CenteredFocusParagraph>
</FlexCenter>
<HugeSpacer />
{this.state.dataLoaded && (
<DatasetSelect
datasets={this.state.datasets}
columns={datasetColumns({isNarrative: false})}
/>
)}
<ListResources
resourceType="dataset"
versioned={false}
resourceListingCallback={resourceListingCallback}/>

<HugeSpacer />

<ScrollableAnchor id={'narratives'}>
<splashStyles.H2>Available Narratives</splashStyles.H2>
</ScrollableAnchor>

{this.state.dataLoaded && (
<DatasetSelect
datasets={this.state.narratives}
columns={datasetColumns({isNarrative: true})}
/>
columns={datasetColumns}/>
)}
{ this.state.errorFetchingData && <DataFetchErrorParagraph />}

Expand Down

0 comments on commit 0f576aa

Please sign in to comment.