Skip to content

Commit

Permalink
Sammyt/install react query (#301)
Browse files Browse the repository at this point in the history
* chore: install react query

* feature: add query client in loader

* chore: install eslint react query

* refactor: rename config -> loaderData

* feature: use query loader for config

* feature: use react query for config

* feature: add query on commit list and add a loader in nodes
  • Loading branch information
Samox committed Jan 12, 2024
1 parent 234138d commit 5e30a45
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 206 deletions.
42 changes: 42 additions & 0 deletions frontend/package-lock.json

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

2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"@emotion/styled": "^11.11.0",
"@tanstack/react-query": "^5.17.9",
"axios": "^1.4.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -39,6 +40,7 @@
"@storybook/react": "^7.4.6",
"@storybook/react-vite": "^7.4.6",
"@storybook/testing-library": "^0.2.2",
"@tanstack/eslint-plugin-query": "^5.17.7",
"@types/jest": "^29.5.3",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import "./App.css";
import { RouterProvider } from "react-router-dom";
import { router } from "./router";
import { router, queryClient } from "./router";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { QueryClientProvider } from "@tanstack/react-query";

function App() {
return (
<>
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
<ToastContainer />
</>
</QueryClientProvider>
);
}

Expand Down
10 changes: 7 additions & 3 deletions frontend/src/components/Lineage/Lineage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import type { LineageEvent } from "./MetricNode";
import styled from "@emotion/styled";

const CustomMetricNode: ComponentType<
NodeProps<{ label: string; events: LineageEvent[] }>
NodeProps<{ label: string; events: LineageEvent[]; eventsLoading: boolean }>
> = ({ data, sourcePosition, targetPosition }) => {
return (
<div>
{targetPosition && <Handle type="target" position={targetPosition} />}
<MetricNode metricName={data.label} events={data.events} />
<MetricNode
metricName={data.label}
events={data.events}
eventsLoading={data.eventsLoading}
/>

{sourcePosition && <Handle type="source" position={sourcePosition} />}
</div>
Expand All @@ -33,7 +37,7 @@ const StyledContainer = styled.div`
}
`;

function Lineage({ nodes: nodes, edges }: LineageProps) {
function Lineage({ nodes, edges }: LineageProps) {
const nodeTypes = useMemo(() => ({ metricNode: CustomMetricNode }), []);

return (
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/Lineage/MetricNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ export type LineageEvent = {
type MetricNodeProps = {
metricName: string;
events?: LineageEvent[];
eventsLoading?: boolean;
};

export const MetricNode = ({
metricName = "",
events = [],
eventsLoading,
}: MetricNodeProps) => {
const [expandedEvent, setExpandedEvent] = useState<
LineageEvent["type"] | null
Expand All @@ -104,7 +106,9 @@ export const MetricNode = ({
return (
<StyledMetricNode>
<strong>{metricName}</strong>
{events.length > 0 ? (
{eventsLoading ? (
<small>Loading</small>
) : events.length > 0 ? (
<ul>
{events.map((event, index) => (
<li key={index}>
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/pages/DisplayCommit/DisplayCommit.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parsePatch } from "../../services/patch.mapper";
import { Params, useLoaderData, defer } from "react-router";
import { getConfig, getPatchAndHeader } from "../../services/data-drift";
import { getPatchAndHeader } from "../../services/data-drift";
import styled from "@emotion/styled";
import { DiffTable } from "./DiffTable";
import { toast } from "react-toastify";
Expand Down Expand Up @@ -37,7 +37,6 @@ const getPatchFromApi = async ({ owner, repo, commitSHA }: CommitParam) => {
repo,
commitSHA,
}),
getConfig({ owner, repo }),
]);

if (patchToLarge) {
Expand Down
64 changes: 34 additions & 30 deletions frontend/src/pages/DriftList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Params, useLoaderData } from "react-router";
import { DDConfig, getCommitList, getConfig } from "../services/data-drift";
import { DDConfig, configQuery, getCommitList } from "../services/data-drift";
import { CommitList } from "../components/Commits/CommitList";
import DriftCard from "../components/Commits/DriftCard";
import styled from "@emotion/styled";
import { QueryClient } from "@tanstack/react-query";

function assertParamsIsDefined(
params: Params<"owner" | "repo">
Expand Down Expand Up @@ -34,41 +35,44 @@ function queryParamsAreDefined(params: Record<string, string>): params is {
return "periodKey" in params && "filepath" in params && "driftDate" in params;
}

const loader = async ({ params }: { params: Params<"owner" | "repo"> }) => {
assertParamsIsDefined(params);
const [result, config] = await Promise.all([
getCommitList(params),
getConfig(params),
]);
const urlParams = Object.fromEntries(
new URLSearchParams(window.location.search)
);
if (queryParamsAreDefined(urlParams)) {
const urlParamsWithParent = {
...urlParams,
parentData: extractParentsFromConfig(config, urlParams.filepath),
};
const loader =
(queryClient: QueryClient) =>
async ({ params }: { params: Params<"owner" | "repo"> }) => {
assertParamsIsDefined(params);
const [result] = await Promise.all([getCommitList(params)]);
const query = configQuery(params);
const maybeConfig = queryClient.getQueryData<DDConfig>(query.queryKey);
const config =
maybeConfig !== undefined
? maybeConfig
: await queryClient.fetchQuery(query);
const urlParams = Object.fromEntries(
new URLSearchParams(window.location.search)
);
if (queryParamsAreDefined(urlParams)) {
const urlParamsWithParent = {
...urlParams,
parentData: extractParentsFromConfig(config, urlParams.filepath),
};
return {
data: result.data,
params,
urlParams: urlParamsWithParent,
};
}
return {
data: result.data,
params,
config,
urlParams: urlParamsWithParent,
urlParams: {
periodKey: "",
filepath: "",
driftDate: "",
parentData: [],
},
};
}
return {
data: result.data,
params,
config,
urlParams: {
periodKey: "",
filepath: "",
driftDate: "",
parentData: [],
},
};
};

type LoaderData = Awaited<ReturnType<typeof loader>>;
type LoaderData = Awaited<ReturnType<ReturnType<typeof loader>>>;

const DriftListContainer = styled.div`
display: flex;
Expand Down
Loading

0 comments on commit 5e30a45

Please sign in to comment.