Skip to content

Add markdown support #6

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions app/components/common/md-content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Box, Skeleton, SkeletonText } from '@chakra-ui/react';
import React, { Suspense, useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { getMdFn } from '$utils/api';

const Markdown = React.lazy(() => import('react-markdown'));
const remarkGfm = import('remark-gfm');

function usePromise<T>(promise: Promise<T>) {
const [value, setValue] = useState<T | null>(null);

useEffect(() => {
promise.then((v) => setValue(v));
}, [promise]);

return value;
}

interface MdContentProps {
url?: string;
}

export function MdContent(props: MdContentProps) {
const { url } = props;

const remarkGfmPlugin = usePromise(remarkGfm);

const { data, error, isLoading } = useQuery({
enabled: !!url,
queryKey: ['markdown', url || 'n/a'],
queryFn: getMdFn(url || '')
});

if (isLoading || !remarkGfmPlugin) {
return <Loading />;
}

if (error) {
return (
<Box
p={2}
bg='danger.50'
color='danger.700'
border='1px'
borderColor='danger.200'
borderRadius='md'
>
Error loading the content: {error.message}
</Box>
);
}

if (!data) {
return 'No content is available. Check back later.';
}

return (
<Suspense fallback={<Loading />}>
<Markdown remarkPlugins={[remarkGfmPlugin.default]}>{data}</Markdown>
</Suspense>
);
}

function Loading() {
return (
<Box>
<Skeleton height='4' width='10' />
<SkeletonText mt='4' noOfLines={4} spacing='4' skeletonHeight='2' />
</Box>
);
}
5 changes: 3 additions & 2 deletions app/components/individual/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { changeValue } from '$utils/format';
import { useRafEffect } from '$utils/use-raf-effect-hook';
import { StringChart } from '$components/common/chart-string';
import { getPDFColorLegend } from '$utils/data/color';
import { MdContent } from '$components/common/md-content';

interface SpeciesComponentProps {
params: {
Expand Down Expand Up @@ -132,7 +133,7 @@ export default function Component(props: SpeciesComponentProps) {
}
/>

<Tabs size='sm' colorScheme='base' mx={-4}>
<Tabs size='sm' colorScheme='base' mx={-4} isLazy>
<TabList>
<Tab fontWeight='bold'>Visualize</Tab>
<Tab fontWeight='bold'>Learn</Tab>
Expand Down Expand Up @@ -205,7 +206,7 @@ export default function Component(props: SpeciesComponentProps) {
)}
</TabPanel>
<TabPanel>
<p>two!</p>
<MdContent url={`/data/${id}/${id}.md`} />
</TabPanel>
</TabPanels>
</Tabs>
Expand Down
6 changes: 4 additions & 2 deletions app/components/species/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DataSectionHead } from '$components/common/data-section-head';
import { RouteErrorHandler } from '$components/common/error';
import { useSpeciesContext } from '$components/common/app-context';
import { getPDFColorLegend } from '$utils/data/color';
import { MdContent } from '$components/common/md-content';

interface SpeciesComponentProps {
params: {
Expand All @@ -45,6 +46,7 @@ export default function Component(props: SpeciesComponentProps) {
queryFn: getJsonFn(`/api/species/${id}.json`)
});


useEffect(() => {
if (data?.groups?.length) {
setGroup(data.groups[0]);
Expand Down Expand Up @@ -89,7 +91,7 @@ export default function Component(props: SpeciesComponentProps) {
)
}
/>
<Tabs size='sm' colorScheme='base' mx={-4}>
<Tabs size='sm' colorScheme='base' mx={-4} isLazy>
<TabList>
<Tab fontWeight='bold'>Visualize</Tab>
<Tab fontWeight='bold'>Learn</Tab>
Expand Down Expand Up @@ -118,7 +120,7 @@ export default function Component(props: SpeciesComponentProps) {
) : null}
</TabPanel>
<TabPanel>
<p>two!</p>
<MdContent url={data?.descriptionMdSrc} />
</TabPanel>
</TabPanels>
</Tabs>
Expand Down
19 changes: 18 additions & 1 deletion app/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Species {
name: string;
region: string;
image: string;
descriptionMdSrc: string;
coords: [number, number];
groups: SpeciesGroup[];
}
Expand All @@ -35,7 +36,7 @@ export interface IndividualParquetItem {
latitude: number;
}

export function getJsonFn(url) {
export function getJsonFn(url: string) {
return async () => {
const response = await fetch(`${process.env.DATA_API || ''}${url}`);

Expand All @@ -51,3 +52,19 @@ export function getJsonFn(url) {
}
};
}

export function getMdFn(url: string) {
return async () => {
const response = await fetch(`${process.env.DATA_API || ''}${url}`);

try {
if (!response.ok) {
throw new Error();
}
const text = await response.text();
return text;
} catch (error) {
throw new Error(`Failed to load the md file for "${url}".`);
}
};
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
"react-cool-dimensions": "^3.0.1",
"react-dom": "^18.2.0",
"react-map-gl": "^7.1.7",
"react-markdown": "^10.1.0",
"remark-gfm": "^4.0.1",
"threads": "^1.7.0",
"use-pan-and-zoom": "^0.6.5",
"wouter": "^3.3.5"
Expand Down
11 changes: 0 additions & 11 deletions static/api/species/cod.json

This file was deleted.

31 changes: 1 addition & 30 deletions static/api/species/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"name": "Sea Bass",
"region": "Mediterranean",
"image": "/api/species/seabass.jpg",
"descriptionMdSrc": "/api/species/seabass.md",
"coords": [
-6,
47
Expand Down Expand Up @@ -39,36 +40,6 @@
60.3498
]
},
{
"id": "tuna",
"name": "Tuna",
"region": "Pacific Ocean",
"image": "http://picsum.photos/150?v=5",
"coords": [
150.6917,
35.6895
]
},
{
"id": "cod",
"name": "Cod",
"region": "North Sea",
"image": "http://picsum.photos/150?v=6",
"coords": [
1.3522,
58.8566
]
},
{
"id": "mackerel",
"name": "Mackerel",
"region": "Indian Ocean",
"image": "http://picsum.photos/150?v=7",
"coords": [
77.2090,
-20.6139
]
},
{
"id": "null-fish",
"name": "Null Fish",
Expand Down
11 changes: 0 additions & 11 deletions static/api/species/mackerel.json

This file was deleted.

1 change: 1 addition & 0 deletions static/api/species/sea-bass.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"name": "Sea Bass",
"region": "Mediterranean",
"image": "/api/species/seabass.jpg",
"descriptionMdSrc": "/api/species/seabass.md",
"coords": [
-6,
47
Expand Down
2 changes: 2 additions & 0 deletions static/api/species/seabass.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is some information about the **seabass** species.
Seabass is commonly found in both saltwater and freshwater environments, making it a versatile species for fishing and aquaculture. It is also known for its firm flesh and ability to hold up well in various cooking methods.
11 changes: 0 additions & 11 deletions static/api/species/tuna.json

This file was deleted.

Loading
Loading