Skip to content

Commit

Permalink
feat: introduce swr (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumichocopengin8 authored May 20, 2024
1 parent 370eaa9 commit 69e834f
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 62 deletions.
146 changes: 145 additions & 1 deletion web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.51.4",
"recoil": "^0.7.7"
"recoil": "^0.7.7",
"swr": "^2.2.5"
},
"devDependencies": {
"@eslint/js": "^9.3.0",
Expand Down
1 change: 1 addition & 0 deletions web/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export * from './crates_api';
export * from './swr';
16 changes: 16 additions & 0 deletions web/src/api/swr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import useSWR from 'swr';
import { fetchCrateDataUsingGET, fetchDownloadDataUsingGET } from 'api/crates_api';

export const useCrateDataResultsQuery = (crateNames: string[]) => {
const { data, error, isLoading } = useSWR(['fetchCrateDataUsingGET', crateNames], ([, crateNames]) =>
Promise.all(crateNames.map((crateName) => fetchCrateDataUsingGET(crateName)))
);
return { crateDataResults: data ?? [], isLoading, error };
};

export const useCrateDownloadDataResultsQuery = (crateNames: string[]) => {
const { data, error, isLoading } = useSWR(['fetchDownloadDataUsingGET', crateNames], ([, crateNames]) =>
Promise.all(crateNames.map((crateName) => fetchDownloadDataUsingGET(crateName)))
);
return { crateDownloadDataResults: data, isLoading, error };
};
11 changes: 8 additions & 3 deletions web/src/components/[crate_names]/CratesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@

import { Table, TableBody, TableContainer, Paper, Typography } from '@mui/material';
import StatsTableHead from 'components/shared/table/StatsTableHead';
import { useRecoilValue } from 'recoil';
import { crateDataResultsState } from 'recoil/atoms';
import CrateTableRow from './CrateTableRow';
import { useCrateDataResultsQuery } from 'api';
import TableSkelton from 'components/skelton/table/TableSkelton';

interface Props {
crateNames: string[];
}

const CratesTable = ({ crateNames }: Props): JSX.Element => {
const crateDataResults = useRecoilValue(crateDataResultsState(crateNames));
const { crateDataResults, isLoading } = useCrateDataResultsQuery(crateNames);

// TODO: remove this when swr supports React Suspense
if (isLoading) {
return <TableSkelton crateNames={crateNames} />;
}

return (
<section>
Expand Down
12 changes: 9 additions & 3 deletions web/src/components/[crate_names]/DownloadChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import { useMemo } from 'react';
import { useRecoilValue } from 'recoil';
import { css } from '@emotion/react';
import { crateDownloadDataResultsState } from 'recoil/atoms';
import { Typography } from '@mui/material';
import ReactECharts from 'components/echarts/ReactEChart';
import type { EChartsOption } from 'echarts';
import { uniform_data } from 'web_assembly/pkg/web_assembly';
import { wasmInitSelector } from 'recoil/selectors';
import { useCrateDownloadDataResultsQuery } from 'api';
import ChartSkelton from 'components/skelton/chart/ChartSkelton';

interface Props {
crateNames: string[];
Expand All @@ -29,12 +30,12 @@ interface ChartData {
}

const DownloadChart = ({ crateNames }: Props): JSX.Element => {
const crateDownloadDataResults = useRecoilValue(crateDownloadDataResultsState(crateNames));
const { crateDownloadDataResults, isLoading } = useCrateDownloadDataResultsQuery(crateNames);
const isWasmLoaded = useRecoilValue(wasmInitSelector);

const uniformedData: ChartData = useMemo(() => {
const t1 = performance.now();
const data: ChartData = JSON.parse(uniform_data(crateNames, JSON.stringify(crateDownloadDataResults)));
const data: ChartData = JSON.parse(uniform_data(crateNames, JSON.stringify(crateDownloadDataResults ?? [])));
const t2 = performance.now();
console.log(t2 - t1);
return data;
Expand Down Expand Up @@ -62,6 +63,11 @@ const DownloadChart = ({ crateNames }: Props): JSX.Element => {
series: uniformedData.data.map((d) => ({ data: d.downloads, name: d.name, type: 'line' })),
};

// TODO: remove this when swr supports React Suspense
if (isLoading) {
return <ChartSkelton />;
}

return (
<section>
<Typography variant="h6" component="h3" gutterBottom>
Expand Down
14 changes: 0 additions & 14 deletions web/src/recoil/atoms.tsx

This file was deleted.

41 changes: 1 addition & 40 deletions web/src/recoil/selectors.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
import { selector, selectorFamily, waitForAll } from 'recoil';
import { CrateResponse } from 'interfaces/crate';
import { Downloads } from 'interfaces/downloads';
import { fetchCrateDataUsingGET, fetchDownloadDataUsingGET } from 'api/index';
import { selector } from 'recoil';
import init from 'web_assembly/pkg';

const crateDataResultQuery = selectorFamily<CrateResponse, string>({
key: 'crateDataResultQuery',
get: (crateName) => async () => {
const response = await fetchCrateDataUsingGET(crateName);
return response;
},
});

export const crateDataResultsQuery = selectorFamily<CrateResponse[], string[]>({
key: 'crateDataResultsQuery',
get:
(crateNames) =>
({ get }) => {
const crateDataResults = get(waitForAll(crateNames.map((crateName) => crateDataResultQuery(crateName))));
return crateDataResults.filter((d) => d);
},
});

const crateDownloadDataResultQuery = selectorFamily<Downloads, string>({
key: 'crateDownloadDataResultQuery',
get: (crateName) => async () => {
const response = await fetchDownloadDataUsingGET(crateName);
return response;
},
});

export const crateDownloadDataResultsQuery = selectorFamily<Downloads[], string[]>({
key: 'crateDownloadDataResultsQuery',
get:
(crateNames) =>
({ get }) => {
const crateDataResults = get(waitForAll(crateNames.map((crateName) => crateDownloadDataResultQuery(crateName))));
return crateDataResults.filter((d) => d);
},
});

export const wasmInitSelector = selector<boolean>({
key: 'wasmInitSelector',
get: async () => {
Expand Down

0 comments on commit 69e834f

Please sign in to comment.