-
-
Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathupdate.tsx
More file actions
206 lines (197 loc) · 6.44 KB
/
update.tsx
File metadata and controls
206 lines (197 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Link } from "@/components/common/link";
import { Note, NoteButton } from "@/components/common/note";
import { FormCardGroup } from "@/components/forms/form-card";
import { useTRPC } from "@/lib/trpc/client";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Info } from "lucide-react";
import { useParams, useRouter } from "next/navigation";
import { FormAppearance } from "./form-appearance";
import { FormCustomDomain } from "./form-custom-domain";
import { FormDangerZone } from "./form-danger-zone";
import { FormGeneral } from "./form-general";
import { FormLinks } from "./form-links";
import { FormLocale } from "./form-locale";
import { FormPageAccess } from "./form-page-access";
export function FormStatusPageUpdate() {
const { id } = useParams<{ id: string }>();
const router = useRouter();
const trpc = useTRPC();
const { data: statusPage, refetch } = useQuery(
trpc.page.get.queryOptions({ id: Number.parseInt(id) }),
);
const queryClient = useQueryClient();
const { data: monitors } = useQuery(trpc.monitor.list.queryOptions());
const { data: workspace } = useQuery(trpc.workspace.get.queryOptions());
const updateStatusPageMutation = useMutation(
trpc.page.updateGeneral.mutationOptions({
onSuccess: () => {
refetch();
// NOTE: invalidate status page list to update name
queryClient.invalidateQueries({
queryKey: trpc.page.list.queryKey(),
});
},
}),
);
const updatePasswordProtectionMutation = useMutation(
trpc.page.updatePasswordProtection.mutationOptions({
onSuccess: () => refetch(),
}),
);
const updateCustomDomainMutation = useMutation(
trpc.page.updateCustomDomain.mutationOptions({
onSuccess: () => refetch(),
}),
);
const updatePageAppearanceMutation = useMutation(
trpc.page.updateAppearance.mutationOptions({
onSuccess: () => refetch(),
}),
);
const deleteStatusPageMutation = useMutation(
trpc.page.delete.mutationOptions({
onSuccess: () => {
router.push("/status-pages");
// NOTE: invalidate workspace to update the usage
queryClient.invalidateQueries({
queryKey: trpc.workspace.get.queryKey(),
});
// NOTE: invalidate status page list to update the usage
queryClient.invalidateQueries({
queryKey: trpc.page.list.queryKey(),
});
},
}),
);
const updateLinksMutation = useMutation(
trpc.page.updateLinks.mutationOptions({
onSuccess: () => refetch(),
}),
);
const updateLocalesMutation = useMutation(
trpc.page.updateLocales.mutationOptions({
onSuccess: () => refetch(),
}),
);
if (!statusPage || !monitors || !workspace) return null;
return (
<FormCardGroup>
<Note color="warning">
<Info />
<p className="text-sm">
Looking to connect monitors to your status page? The setup now has a
separate page{" "}
<Link href={`/status-pages/${id}/components`}>components</Link>.
</p>
<NoteButton variant="default" asChild>
<Link href="https://openstatus.dev/blog/status-page-components">
Learn more
</Link>
</NoteButton>
</Note>
<FormGeneral
defaultValues={{
title: statusPage.title,
slug: statusPage.slug,
description: statusPage.description,
icon: statusPage.icon ?? undefined,
}}
onSubmit={async (values) => {
await updateStatusPageMutation.mutateAsync({
id: Number.parseInt(id),
title: values.title,
slug: values.slug,
description: values.description ?? "",
icon: values.icon ?? "",
});
}}
/>
<FormCustomDomain
locked={workspace.limits["custom-domain"] === false}
defaultValues={{
domain: statusPage.customDomain ?? undefined,
}}
onSubmit={async (values) => {
await updateCustomDomainMutation.mutateAsync({
id: Number.parseInt(id),
customDomain: values.domain,
});
}}
/>
<FormLinks
defaultValues={{
homepageUrl: statusPage.homepageUrl ?? "",
contactUrl: statusPage.contactUrl ?? "",
}}
onSubmit={async (values) => {
await updateLinksMutation.mutateAsync({
id: Number.parseInt(id),
homepageUrl: values.homepageUrl ?? undefined,
contactUrl: values.contactUrl ?? undefined,
});
}}
/>
<FormAppearance
defaultValues={{
forceTheme: statusPage.forceTheme ?? "system",
configuration: {
theme: statusPage.configuration?.theme ?? "default",
},
}}
onSubmit={async (values) => {
await updatePageAppearanceMutation.mutateAsync({
id: Number.parseInt(id),
forceTheme: values.forceTheme,
configuration: values.configuration,
});
}}
/>
<FormLocale
defaultValues={{
defaultLocale: statusPage.defaultLocale ?? "en",
locales: statusPage.locales,
}}
onSubmit={async (values) => {
await updateLocalesMutation.mutateAsync({
id: Number.parseInt(id),
defaultLocale: values.defaultLocale,
locales: values.locales,
});
}}
/>
<FormPageAccess
lockedMap={
new Map([
["public", false],
["password", workspace.limits["password-protection"] === false],
[
"email-domain",
workspace.limits["email-domain-protection"] === false,
],
])
}
defaultValues={{
accessType: statusPage.accessType,
password: statusPage.password ?? undefined,
authEmailDomains: statusPage.authEmailDomains ?? [],
}}
onSubmit={async (values) => {
await updatePasswordProtectionMutation.mutateAsync({
id: Number.parseInt(id),
accessType: values.accessType,
password: values.password,
authEmailDomains: values.authEmailDomains,
});
}}
/>
<FormDangerZone
title={statusPage.title}
onSubmit={async () => {
await deleteStatusPageMutation.mutateAsync({
id: Number.parseInt(id),
});
}}
/>
</FormCardGroup>
);
}