Skip to content

Commit d2c1562

Browse files
authored
chore: cleanup some query handling (coder#15130)
1 parent aaa1223 commit d2c1562

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

site/.storybook/preview.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ function withQuery(Story, { parameters }) {
104104

105105
if (parameters.queries) {
106106
for (const query of parameters.queries) {
107-
if (query.data instanceof Error) {
108-
// This is copied from setQueryData() but sets the error.
107+
if (query.isError) {
108+
// Based on `setQueryData`, but modified to set the result as an error.
109109
const cache = queryClient.getQueryCache();
110110
const parsedOptions = parseQueryArgs(query.key);
111111
const defaultedOptions = queryClient.defaultQueryOptions(parsedOptions);
112+
// Adds an uninitialized response to the cache, which we can now mutate.
112113
const cachedQuery = cache.build(queryClient, defaultedOptions);
113-
// Set manual data so react-query will not try to refetch.
114+
// Setting `manual` prevents retries.
114115
cachedQuery.setData(undefined, { manual: true });
115-
cachedQuery.setState({ error: query.data });
116+
// Set the `error` value and the appropriate status.
117+
cachedQuery.setState({ error: query.data, status: "error" });
116118
} else {
117119
queryClient.setQueryData(query.key, query.data);
118120
}

site/src/@types/storybook.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ declare module "@storybook/react" {
1919
experiments?: Experiments;
2020
showOrganizations?: boolean;
2121
organizations?: Organization[];
22-
queries?: { key: QueryKey; data: unknown }[];
22+
queries?: { key: QueryKey; data: unknown; isError?: boolean }[];
2323
webSocket?: WebSocketEvent[];
2424
user?: User;
2525
permissions?: Partial<Permissions>;

site/src/api/queries/notifications.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,21 @@ export const systemNotificationTemplates = () => {
6565
export function selectTemplatesByGroup(
6666
data: NotificationTemplate[],
6767
): Record<string, NotificationTemplate[]> {
68-
const grouped = data.reduce(
69-
(acc, tpl) => {
70-
if (!acc[tpl.group]) {
71-
acc[tpl.group] = [];
72-
}
73-
acc[tpl.group].push(tpl);
74-
return acc;
75-
},
76-
{} as Record<string, NotificationTemplate[]>,
77-
);
78-
79-
// Sort templates within each group
80-
for (const group in grouped) {
81-
grouped[group].sort((a, b) => a.name.localeCompare(b.name));
68+
const grouped: Record<string, NotificationTemplate[]> = {};
69+
for (const template of data) {
70+
if (!grouped[template.group]) {
71+
grouped[template.group] = [];
72+
}
73+
grouped[template.group].push(template);
8274
}
8375

84-
// Sort groups by name
76+
// Sort groups by name, and sort templates within each group
8577
const sortedGroups = Object.keys(grouped).sort((a, b) => a.localeCompare(b));
8678
const sortedGrouped: Record<string, NotificationTemplate[]> = {};
8779
for (const group of sortedGroups) {
88-
sortedGrouped[group] = grouped[group];
80+
sortedGrouped[group] = grouped[group].sort((a, b) =>
81+
a.name.localeCompare(b.name),
82+
);
8983
}
9084

9185
return sortedGrouped;

site/src/pages/ManagementSettingsPage/GroupsPage/GroupPage.stories.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export const LoadingGroup: Story = {
5353

5454
export const GroupError: Story = {
5555
parameters: {
56-
queries: [groupQuery(new Error("test group error")), permissionsQuery({})],
56+
queries: [
57+
{ ...groupQuery(new Error("test group error")), isError: true },
58+
permissionsQuery({}),
59+
],
5760
},
5861
};
5962

@@ -90,7 +93,7 @@ export const MembersError: Story = {
9093
queries: [
9194
groupQuery(MockGroup),
9295
permissionsQuery({ canUpdateGroup: true }),
93-
membersQuery(new Error("test members error")),
96+
{ ...membersQuery(new Error("test members error")), isError: true },
9497
],
9598
},
9699
play: async ({ canvasElement }) => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy