Skip to content

Commit 6921c76

Browse files
committed
Switch to Tanstack Query
1 parent 734a177 commit 6921c76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+714
-534
lines changed

biome.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@
1717
"crates/**",
1818
"frontend/src/gql/**",
1919
"frontend/src/routeTree.gen.ts",
20+
"frontend/.storybook/locales.ts",
21+
"frontend/locales/*.json",
2022
"**/coverage/**",
2123
"**/dist/**"
2224
]
2325
},
2426
"formatter": {
2527
"enabled": true,
26-
"useEditorconfig": true,
27-
"ignore": ["frontend/.storybook/locales.ts", "frontend/locales/*.json"]
28+
"useEditorconfig": true
2829
},
2930
"linter": {
3031
"enabled": true,
3132
"rules": {
32-
"recommended": true
33+
"recommended": true,
34+
"correctness": {
35+
"noUnusedImports": "warn",
36+
"noUnusedVariables": "warn"
37+
}
3338
}
3439
}
3540
}

frontend/.storybook/preview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ const withThemeProvider: Decorator = (Story, context) => {
7474
);
7575
};
7676

77-
const withDummyRouter: Decorator = (Story, context) => {
77+
const withDummyRouter: Decorator = (Story, _context) => {
7878
return (
7979
<DummyRouter>
8080
<Story />
8181
</DummyRouter>
8282
);
8383
};
8484

85-
const withTooltipProvider: Decorator = (Story, context) => {
85+
const withTooltipProvider: Decorator = (Story, _context) => {
8686
return (
8787
<TooltipProvider>
8888
<Story />

frontend/codegen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const config: CodegenConfig = {
1818
enumsAsTypes: true,
1919
// By default, unknown scalars are generated as `any`. This is not ideal for catching potential bugs.
2020
defaultScalarType: "unknown",
21+
maybeValue: "T | null | undefined",
2122
scalars: {
2223
DateTime: "string",
2324
Url: "string",

frontend/package-lock.json

Lines changed: 76 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@fontsource/inter": "^5.1.0",
2222
"@radix-ui/react-collapsible": "^1.1.1",
2323
"@radix-ui/react-dialog": "^1.1.2",
24+
"@tanstack/react-query": "^5.59.20",
2425
"@tanstack/react-router": "^1.81.4",
2526
"@tanstack/router-zod-adapter": "^1.81.4",
2627
"@urql/core": "^5.0.8",
@@ -34,6 +35,7 @@
3435
"classnames": "^2.5.1",
3536
"date-fns": "^4.1.0",
3637
"graphql": "^16.9.0",
38+
"graphql-request": "^7.1.2",
3739
"i18next": "^23.16.5",
3840
"react": "^18.3.1",
3941
"react-dom": "^18.3.1",
@@ -56,6 +58,7 @@
5658
"@storybook/addon-essentials": "^8.4.2",
5759
"@storybook/react": "^8.4.2",
5860
"@storybook/react-vite": "^8.4.2",
61+
"@tanstack/react-query-devtools": "^5.59.20",
5962
"@tanstack/router-devtools": "^1.81.4",
6063
"@tanstack/router-vite-plugin": "^1.79.0",
6164
"@testing-library/react": "^16.0.1",

frontend/src/components/BrowserSession.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { Badge } from "@vector-im/compound-web";
1111
import { parseISO } from "date-fns";
1212
import { useCallback } from "react";
1313
import { useTranslation } from "react-i18next";
14-
import { useMutation } from "urql";
1514

1615
import { type FragmentType, graphql, useFragment } from "../gql";
17-
import type { DeviceType } from "../gql/graphql";
1816

17+
import { useMutation, useQueryClient } from "@tanstack/react-query";
18+
import { graphqlClient } from "../graphql";
1919
import DateTime from "./DateTime";
2020
import EndSessionButton from "./Session/EndSessionButton";
2121
import LastActive from "./Session/LastActive";
@@ -58,14 +58,26 @@ export const useEndBrowserSession = (
5858
sessionId: string,
5959
isCurrent: boolean,
6060
): (() => Promise<void>) => {
61-
const [, endSession] = useMutation(END_SESSION_MUTATION);
61+
const queryClient = useQueryClient();
62+
const endSession = useMutation({
63+
mutationFn: (id: string) =>
64+
graphqlClient.request(END_SESSION_MUTATION, { id }),
65+
onSuccess: (data) => {
66+
queryClient.invalidateQueries({ queryKey: ["sessionsOverview"] });
67+
queryClient.invalidateQueries({ queryKey: ["browserSessionList"] });
68+
queryClient.invalidateQueries({
69+
queryKey: ["sessionDetail", data.endBrowserSession.browserSession?.id],
70+
});
71+
72+
if (isCurrent) {
73+
window.location.reload();
74+
}
75+
},
76+
});
6277

6378
const onSessionEnd = useCallback(async (): Promise<void> => {
64-
await endSession({ id: sessionId });
65-
if (isCurrent) {
66-
window.location.reload();
67-
}
68-
}, [isCurrent, endSession, sessionId]);
79+
await endSession.mutateAsync(sessionId);
80+
}, [endSession.mutateAsync, sessionId]);
6981

7082
return onSessionEnd;
7183
};

frontend/src/components/CompatSession.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
// SPDX-License-Identifier: AGPL-3.0-only
55
// Please see LICENSE in the repository root for full details.
66

7+
import { useMutation, useQueryClient } from "@tanstack/react-query";
78
import { parseISO } from "date-fns";
89
import { useTranslation } from "react-i18next";
9-
import { useMutation } from "urql";
10-
1110
import { type FragmentType, graphql, useFragment } from "../gql";
12-
import { DeviceType } from "../gql/graphql";
13-
11+
import { graphqlClient } from "../graphql";
1412
import { browserLogoUri } from "./BrowserSession";
1513
import DateTime from "./DateTime";
1614
import EndSessionButton from "./Session/EndSessionButton";
@@ -44,7 +42,6 @@ export const END_SESSION_MUTATION = graphql(/* GraphQL */ `
4442
status
4543
compatSession {
4644
id
47-
finishedAt
4845
}
4946
}
5047
}
@@ -54,7 +51,7 @@ export const simplifyUrl = (url: string): string => {
5451
let parsed: URL;
5552
try {
5653
parsed = new URL(url);
57-
} catch (e) {
54+
} catch (_e) {
5855
// Not a valid URL, return the original
5956
return url;
6057
}
@@ -76,10 +73,21 @@ const CompatSession: React.FC<{
7673
}> = ({ session }) => {
7774
const { t } = useTranslation();
7875
const data = useFragment(FRAGMENT, session);
79-
const [, endCompatSession] = useMutation(END_SESSION_MUTATION);
76+
const queryClient = useQueryClient();
77+
const endSession = useMutation({
78+
mutationFn: (id: string) =>
79+
graphqlClient.request(END_SESSION_MUTATION, { id }),
80+
onSuccess: (data) => {
81+
queryClient.invalidateQueries({ queryKey: ["sessionsOverview"] });
82+
queryClient.invalidateQueries({ queryKey: ["appSessionList"] });
83+
queryClient.invalidateQueries({
84+
queryKey: ["sessionDetail", data.endCompatSession.compatSession?.id],
85+
});
86+
},
87+
});
8088

8189
const onSessionEnd = async (): Promise<void> => {
82-
await endCompatSession({ id: data.id });
90+
await endSession.mutateAsync(data.id);
8391
};
8492

8593
const clientName = data.ssoLogin?.redirectUri

frontend/src/components/Layout/Layout.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
import cx from "classnames";
88
import { Suspense } from "react";
9-
import { useQuery } from "urql";
109

1110
import { graphql } from "../../gql";
1211
import Footer from "../Footer";
1312

13+
import { queryOptions, useQuery } from "@tanstack/react-query";
14+
import { graphqlClient } from "../../graphql";
1415
import styles from "./Layout.module.css";
1516

1617
const QUERY = graphql(/* GraphQL */ `
@@ -22,10 +23,13 @@ const QUERY = graphql(/* GraphQL */ `
2223
}
2324
`);
2425

26+
export const query = queryOptions({
27+
queryKey: ["footer"],
28+
queryFn: ({ signal }) => graphqlClient.request({ document: QUERY, signal }),
29+
});
30+
2531
const AsyncFooter: React.FC = () => {
26-
const [result] = useQuery({
27-
query: QUERY,
28-
});
32+
const result = useQuery(query);
2933

3034
if (result.error) {
3135
// We probably prefer to render an empty footer in case of an error

frontend/src/components/Layout/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
// SPDX-License-Identifier: AGPL-3.0-only
55
// Please see LICENSE in the repository root for full details.
66

7-
export { default } from "./Layout";
7+
export { default, query } from "./Layout";

0 commit comments

Comments
 (0)