Skip to content

chore: cleanup some query handling #15130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions site/.storybook/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,17 @@ function withQuery(Story, { parameters }) {

if (parameters.queries) {
for (const query of parameters.queries) {
if (query.data instanceof Error) {
// This is copied from setQueryData() but sets the error.
if (query.isError) {
// Based on `setQueryData`, but modified to set the result as an error.
const cache = queryClient.getQueryCache();
const parsedOptions = parseQueryArgs(query.key);
const defaultedOptions = queryClient.defaultQueryOptions(parsedOptions);
// Adds an uninitialized response to the cache, which we can now mutate.
const cachedQuery = cache.build(queryClient, defaultedOptions);
// Set manual data so react-query will not try to refetch.
// Setting `manual` prevents retries.
cachedQuery.setData(undefined, { manual: true });
cachedQuery.setState({ error: query.data });
// Set the `error` value and the appropriate status.
cachedQuery.setState({ error: query.data, status: "error" });
} else {
queryClient.setQueryData(query.key, query.data);
}
Expand Down
2 changes: 1 addition & 1 deletion site/src/@types/storybook.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare module "@storybook/react" {
experiments?: Experiments;
showOrganizations?: boolean;
organizations?: Organization[];
queries?: { key: QueryKey; data: unknown }[];
queries?: { key: QueryKey; data: unknown; isError?: boolean }[];
webSocket?: WebSocketEvent[];
user?: User;
permissions?: Partial<Permissions>;
Expand Down
26 changes: 10 additions & 16 deletions site/src/api/queries/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,21 @@ export const systemNotificationTemplates = () => {
export function selectTemplatesByGroup(
data: NotificationTemplate[],
): Record<string, NotificationTemplate[]> {
const grouped = data.reduce(
(acc, tpl) => {
if (!acc[tpl.group]) {
acc[tpl.group] = [];
}
acc[tpl.group].push(tpl);
return acc;
},
{} as Record<string, NotificationTemplate[]>,
);

// Sort templates within each group
for (const group in grouped) {
grouped[group].sort((a, b) => a.name.localeCompare(b.name));
const grouped: Record<string, NotificationTemplate[]> = {};
for (const template of data) {
if (!grouped[template.group]) {
grouped[template.group] = [];
}
grouped[template.group].push(template);
}

// Sort groups by name
// Sort groups by name, and sort templates within each group
const sortedGroups = Object.keys(grouped).sort((a, b) => a.localeCompare(b));
const sortedGrouped: Record<string, NotificationTemplate[]> = {};
for (const group of sortedGroups) {
sortedGrouped[group] = grouped[group];
sortedGrouped[group] = grouped[group].sort((a, b) =>
a.name.localeCompare(b.name),
);
}

return sortedGrouped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export const LoadingGroup: Story = {

export const GroupError: Story = {
parameters: {
queries: [groupQuery(new Error("test group error")), permissionsQuery({})],
queries: [
{ ...groupQuery(new Error("test group error")), isError: true },
permissionsQuery({}),
],
},
};

Expand Down Expand Up @@ -90,7 +93,7 @@ export const MembersError: Story = {
queries: [
groupQuery(MockGroup),
permissionsQuery({ canUpdateGroup: true }),
membersQuery(new Error("test members error")),
{ ...membersQuery(new Error("test members error")), isError: true },
],
},
play: async ({ canvasElement }) => {
Expand Down
Loading