Skip to content

Commit e74d8a7

Browse files
chore(site): refactor starter templates to use react-query (#9697)
* Remove starter templates service * Remove starter template service * Remove template x service
1 parent e1bd6dd commit e74d8a7

8 files changed

+56
-204
lines changed

site/src/pages/StarterTemplatePage/StarterTemplatePage.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
import { useMachine } from "@xstate/react";
21
import { useOrganizationId } from "hooks/useOrganizationId";
32
import { FC } from "react";
43
import { Helmet } from "react-helmet-async";
54
import { useParams } from "react-router-dom";
65
import { pageTitle } from "utils/page";
7-
import { starterTemplateMachine } from "xServices/starterTemplates/starterTemplateXService";
86
import { StarterTemplatePageView } from "./StarterTemplatePageView";
7+
import { useQuery } from "@tanstack/react-query";
8+
import { templateExamples } from "api/queries/templates";
99

1010
const StarterTemplatePage: FC = () => {
1111
const { exampleId } = useParams() as { exampleId: string };
1212
const organizationId = useOrganizationId();
13-
const [state] = useMachine(starterTemplateMachine, {
14-
context: {
15-
organizationId,
16-
exampleId,
17-
},
18-
});
13+
const templateExamplesQuery = useQuery(templateExamples(organizationId));
14+
const starterTemplate = templateExamplesQuery.data?.find(
15+
(example) => example.id === exampleId,
16+
);
1917

2018
return (
2119
<>
2220
<Helmet>
23-
<title>
24-
{pageTitle(state.context.starterTemplate?.name ?? exampleId)}
25-
</title>
21+
<title>{pageTitle(starterTemplate?.name ?? exampleId)}</title>
2622
</Helmet>
2723

28-
<StarterTemplatePageView context={state.context} />
24+
<StarterTemplatePageView
25+
starterTemplate={starterTemplate}
26+
error={templateExamplesQuery.error}
27+
/>
2928
</>
3029
);
3130
};
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
mockApiError,
3-
MockOrganization,
4-
MockTemplateExample,
5-
} from "testHelpers/entities";
1+
import { mockApiError, MockTemplateExample } from "testHelpers/entities";
62
import { StarterTemplatePageView } from "./StarterTemplatePageView";
73

84
import type { Meta, StoryObj } from "@storybook/react";
@@ -17,23 +13,15 @@ type Story = StoryObj<typeof StarterTemplatePageView>;
1713

1814
export const Default: Story = {
1915
args: {
20-
context: {
21-
exampleId: MockTemplateExample.id,
22-
organizationId: MockOrganization.id,
23-
error: undefined,
24-
starterTemplate: MockTemplateExample,
25-
},
16+
error: undefined,
17+
starterTemplate: MockTemplateExample,
2618
},
2719
};
2820
export const Error: Story = {
2921
args: {
30-
context: {
31-
exampleId: MockTemplateExample.id,
32-
organizationId: MockOrganization.id,
33-
error: mockApiError({
34-
message: `Example ${MockTemplateExample.id} not found.`,
35-
}),
36-
starterTemplate: undefined,
37-
},
22+
error: mockApiError({
23+
message: `Example ${MockTemplateExample.id} not found.`,
24+
}),
25+
starterTemplate: undefined,
3826
},
3927
};

site/src/pages/StarterTemplatePage/StarterTemplatePageView.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,28 @@ import {
99
PageHeaderTitle,
1010
} from "components/PageHeader/PageHeader";
1111
import { FC } from "react";
12-
import { StarterTemplateContext } from "xServices/starterTemplates/starterTemplateXService";
1312
import ViewCodeIcon from "@mui/icons-material/OpenInNewOutlined";
1413
import PlusIcon from "@mui/icons-material/AddOutlined";
1514
import { Stack } from "components/Stack/Stack";
1615
import { Link } from "react-router-dom";
1716
import { ErrorAlert } from "components/Alert/ErrorAlert";
17+
import { TemplateExample } from "api/typesGenerated";
1818

1919
export interface StarterTemplatePageViewProps {
20-
context: StarterTemplateContext;
20+
starterTemplate?: TemplateExample;
21+
error?: unknown;
2122
}
2223

2324
export const StarterTemplatePageView: FC<StarterTemplatePageViewProps> = ({
24-
context,
25+
starterTemplate,
26+
error,
2527
}) => {
2628
const styles = useStyles();
27-
const { starterTemplate } = context;
2829

29-
if (context.error) {
30+
if (error) {
3031
return (
3132
<Margins>
32-
<ErrorAlert error={context.error} />
33+
<ErrorAlert error={error} />
3334
</Margins>
3435
);
3536
}

site/src/pages/StarterTemplatesPage/StarterTemplatesPage.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
import { useMachine } from "@xstate/react";
21
import { useOrganizationId } from "hooks/useOrganizationId";
32
import { FC } from "react";
43
import { Helmet } from "react-helmet-async";
54
import { pageTitle } from "utils/page";
6-
import { starterTemplatesMachine } from "xServices/starterTemplates/starterTemplatesXService";
75
import { StarterTemplatesPageView } from "./StarterTemplatesPageView";
6+
import { useQuery } from "@tanstack/react-query";
7+
import { templateExamples } from "api/queries/templates";
8+
import { getTemplatesByTag } from "utils/starterTemplates";
89

910
const StarterTemplatesPage: FC = () => {
1011
const organizationId = useOrganizationId();
11-
const [state] = useMachine(starterTemplatesMachine, {
12-
context: { organizationId },
13-
});
12+
const templateExamplesQuery = useQuery(templateExamples(organizationId));
13+
const starterTemplatesByTag = templateExamplesQuery.data
14+
? getTemplatesByTag(templateExamplesQuery.data)
15+
: undefined;
1416

1517
return (
1618
<>
1719
<Helmet>
1820
<title>{pageTitle("Starter Templates")}</title>
1921
</Helmet>
2022

21-
<StarterTemplatesPageView context={state.context} />
23+
<StarterTemplatesPageView
24+
error={templateExamplesQuery.error}
25+
starterTemplatesByTag={starterTemplatesByTag}
26+
/>
2227
</>
2328
);
2429
};
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
mockApiError,
3-
MockOrganization,
43
MockTemplateExample,
54
MockTemplateExample2,
65
} from "testHelpers/entities";
@@ -18,25 +17,19 @@ type Story = StoryObj<typeof StarterTemplatesPageView>;
1817

1918
export const Default: Story = {
2019
args: {
21-
context: {
22-
organizationId: MockOrganization.id,
23-
error: undefined,
24-
starterTemplatesByTag: getTemplatesByTag([
25-
MockTemplateExample,
26-
MockTemplateExample2,
27-
]),
28-
},
20+
error: undefined,
21+
starterTemplatesByTag: getTemplatesByTag([
22+
MockTemplateExample,
23+
MockTemplateExample2,
24+
]),
2925
},
3026
};
3127

3228
export const Error: Story = {
3329
args: {
34-
context: {
35-
organizationId: MockOrganization.id,
36-
error: mockApiError({
37-
message: "Error on loading the template examples",
38-
}),
39-
starterTemplatesByTag: undefined,
40-
},
30+
error: mockApiError({
31+
message: "Error on loading the template examples",
32+
}),
33+
starterTemplatesByTag: undefined,
4134
},
4235
};

site/src/pages/StarterTemplatesPage/StarterTemplatesPageView.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { TemplateExampleCard } from "components/TemplateExampleCard/TemplateExam
1313
import { FC } from "react";
1414
import { Link, useSearchParams } from "react-router-dom";
1515
import { combineClasses } from "utils/combineClasses";
16-
import { StarterTemplatesContext } from "xServices/starterTemplates/starterTemplatesXService";
16+
import { StarterTemplatesByTag } from "utils/starterTemplates";
1717

1818
const getTagLabel = (tag: string) => {
1919
const labelByTag: Record<string, string> = {
@@ -26,22 +26,25 @@ const getTagLabel = (tag: string) => {
2626
return labelByTag[tag] ?? tag;
2727
};
2828

29-
const selectTags = ({ starterTemplatesByTag }: StarterTemplatesContext) => {
29+
const selectTags = (starterTemplatesByTag: StarterTemplatesByTag) => {
3030
return starterTemplatesByTag
3131
? Object.keys(starterTemplatesByTag).sort((a, b) => a.localeCompare(b))
3232
: undefined;
3333
};
3434
export interface StarterTemplatesPageViewProps {
35-
context: StarterTemplatesContext;
35+
starterTemplatesByTag?: StarterTemplatesByTag;
36+
error?: unknown;
3637
}
3738

3839
export const StarterTemplatesPageView: FC<StarterTemplatesPageViewProps> = ({
39-
context,
40+
starterTemplatesByTag,
41+
error,
4042
}) => {
4143
const [urlParams] = useSearchParams();
4244
const styles = useStyles();
43-
const { starterTemplatesByTag } = context;
44-
const tags = selectTags(context);
45+
const tags = starterTemplatesByTag
46+
? selectTags(starterTemplatesByTag)
47+
: undefined;
4548
const activeTag = urlParams.get("tag") ?? "all";
4649
const visibleTemplates = starterTemplatesByTag
4750
? starterTemplatesByTag[activeTag]
@@ -56,8 +59,8 @@ export const StarterTemplatesPageView: FC<StarterTemplatesPageViewProps> = ({
5659
</PageHeaderSubtitle>
5760
</PageHeader>
5861

59-
<Maybe condition={Boolean(context.error)}>
60-
<ErrorAlert error={context.error} />
62+
<Maybe condition={Boolean(error)}>
63+
<ErrorAlert error={error} />
6164
</Maybe>
6265

6366
<Maybe condition={Boolean(!starterTemplatesByTag)}>

site/src/xServices/starterTemplates/starterTemplateXService.ts

Lines changed: 0 additions & 71 deletions
This file was deleted.

site/src/xServices/starterTemplates/starterTemplatesXService.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)