forked from wdingx/pan-genome-visualization
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch data index, setup routing
- Loading branch information
1 parent
a2ed5c2
commit 299c42f
Showing
19 changed files
with
402 additions
and
188 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,12 +1,44 @@ | ||
import React from 'react' | ||
import React, { useMemo } from 'react' | ||
import { Col, Row } from 'reactstrap' | ||
|
||
import { Layout } from 'src/components/Layout/Layout' | ||
import { Link } from 'src/components/Link/Link' | ||
import MainPageContent from 'src/components/Main/MainPage.mdx' | ||
import { useDataIndexQuery } from 'src/hooks/useDataIndexQuery' | ||
|
||
export function MainPage() { | ||
const indexJson = useDataIndexQuery() | ||
|
||
const speciesLinks = useMemo(() => { | ||
return indexJson.datasets.map(({ pathogenName }) => ( | ||
<li key={pathogenName}> | ||
<Link href={`/species/${pathogenName}`}>{`/species/${pathogenName}`}</Link> | ||
</li> | ||
)) | ||
}, [indexJson.datasets]) | ||
|
||
return ( | ||
<Layout> | ||
<MainPageContent /> | ||
|
||
<Row noGutters> | ||
<Col> | ||
<ul> | ||
<li> | ||
<Link href={'/doesnotexist'}>{'/doesnotexist'}</Link> | ||
</li> | ||
<li> | ||
<Link href={'/species/doesnotexist'}>{'/species/doesnotexist'}</Link> | ||
</li> | ||
</ul> | ||
</Col> | ||
</Row> | ||
|
||
<Row noGutters> | ||
<Col> | ||
<ul>{speciesLinks}</ul> | ||
</Col> | ||
</Row> | ||
</Layout> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { useMemo } from 'react' | ||
|
||
import { axiosFetch } from 'src/io/axiosFetch' | ||
|
||
export type QueryOptions< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
> = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'initialData'> & { | ||
initialData?: () => undefined | ||
} | ||
|
||
export interface UseAxiosQueryOptions<TData = unknown> extends QueryOptions<TData, Error, TData, string[]> { | ||
delay?: number | ||
} | ||
|
||
export function useAxiosQuery<TData = unknown>(url: string, options?: UseAxiosQueryOptions<TData>): TData { | ||
const newOptions = useMemo(() => { | ||
let newOptions: UseAxiosQueryOptions<TData> = { | ||
staleTime: Number.POSITIVE_INFINITY, | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
refetchOnReconnect: true, | ||
refetchInterval: Number.POSITIVE_INFINITY, | ||
} | ||
if (options) { | ||
newOptions = { ...newOptions, ...options } | ||
} | ||
return newOptions | ||
}, [options]) | ||
|
||
const res = useQuery<TData, Error, TData, string[]>( | ||
[url], | ||
async () => { | ||
if (options?.delay) { | ||
await new Promise((resolve) => { | ||
setInterval(resolve, options.delay) | ||
}) | ||
} | ||
return axiosFetch(url) | ||
}, | ||
newOptions, | ||
) | ||
|
||
return useMemo(() => { | ||
if (!res.data) { | ||
throw new Error('Fetch failed: index.json') | ||
} | ||
return res.data | ||
}, [res.data]) | ||
} |
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,13 @@ | ||
import { useAxiosQuery, UseAxiosQueryOptions } from 'src/hooks/useAxiosQuery' | ||
|
||
export interface SpeciesDesc { | ||
pathogenName: string | ||
} | ||
|
||
export interface DataIndex { | ||
datasets: SpeciesDesc[] | ||
} | ||
|
||
export function useDataIndexQuery(options?: UseAxiosQueryOptions<DataIndex>): DataIndex { | ||
return useAxiosQuery<DataIndex>('https://data.master.pangenome.org/index.json', options) | ||
} |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { useCallback } from 'react' | ||
|
||
export function useReloadPage(url?: string | URL) { | ||
return () => { | ||
return useCallback(() => { | ||
window.history.replaceState(null, '', url) | ||
window.location.reload() | ||
// trigger React suspense forever, to display loading spinner until the page is refreshed | ||
throw new Promise(() => {}) // eslint-disable-line @typescript-eslint/no-throw-literal | ||
} | ||
}, [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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react' | ||
import { useRouter } from 'next/router' | ||
import ErrorPage from './_error' | ||
|
||
export default function NotFoundPage() { | ||
const { asPath } = useRouter() | ||
const error: Error & { statusCode?: number } = new Error(`Page not found: ${asPath}`) | ||
error.statusCode = 404 | ||
return <ErrorPage statusCode={error.statusCode} title={error.message} error={error} showDetails={false} /> | ||
} |
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,8 @@ | ||
import React from 'react' | ||
import ErrorPage from './_error' | ||
|
||
export default function InternalServerErrorPage() { | ||
const error: Error & { statusCode?: number } = new Error('Internal server error') | ||
error.statusCode = 500 | ||
return <ErrorPage statusCode={error.statusCode} title={error.message} error={error} showDetails={false} /> | ||
} |
Oops, something went wrong.