@passui/tanstack 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +76 -2
- package/package.json +165 -5
- package/src/auth-shell/auth-shell.tsx +26 -0
- package/src/auth-shell/index.ts +1 -0
- package/src/document/compose-viewport.tsx +96 -0
- package/src/document/index.ts +1 -0
- package/src/form-fields/fields.tsx +192 -0
- package/src/form-fields/index.ts +2 -0
- package/src/form-fields/selection.tsx +149 -0
- package/src/form-fields/submit.tsx +40 -0
- package/src/forms/contexts.ts +8 -0
- package/src/forms/elements/form-alert.tsx +30 -0
- package/src/forms/elements/submit.tsx +1 -0
- package/src/forms/fields/avatar-picker.tsx +399 -0
- package/src/forms/fields/avatar.tsx +299 -0
- package/src/forms/fields/checkbox.tsx +1 -0
- package/src/forms/fields/index.ts +32 -0
- package/src/forms/fields/input.tsx +1 -0
- package/src/forms/fields/multiselect.tsx +1 -0
- package/src/forms/fields/otp.tsx +1 -0
- package/src/forms/fields/radio-card.tsx +1 -0
- package/src/forms/fields/switch.tsx +1 -0
- package/src/forms/fields/textarea.tsx +1 -0
- package/src/forms/index.ts +30 -0
- package/src/mutation/index.ts +1 -0
- package/src/mutation/use-app-mutation.ts +156 -0
- package/src/route-boundary/default-boundary.tsx +61 -0
- package/src/route-boundary/default-loader.tsx +19 -0
- package/src/route-boundary/index.ts +3 -0
- package/src/route-boundary/not-founder.tsx +50 -0
- package/src/settings/index.ts +1 -0
- package/src/settings/setting-list.tsx +252 -0
- package/src/shared/index.ts +2 -0
- package/src/shared/optimistic.ts +7 -0
- package/src/shared/table.ts +30 -0
- package/src/solid/auth-shell/auth-shell.tsx +20 -0
- package/src/solid/auth-shell/index.ts +1 -0
- package/src/solid/document/compose-viewport.tsx +69 -0
- package/src/solid/document/index.ts +1 -0
- package/src/solid/form-fields/avatar.tsx +370 -0
- package/src/solid/form-fields/fields.tsx +336 -0
- package/src/solid/form-fields/index.ts +3 -0
- package/src/solid/form-fields/submit.tsx +31 -0
- package/src/solid/forms/contexts.ts +7 -0
- package/src/solid/forms/form-alert.tsx +19 -0
- package/src/solid/forms/index.ts +32 -0
- package/src/solid/mutation/index.ts +5 -0
- package/src/solid/mutation/use-app-mutation.ts +171 -0
- package/src/solid/route-boundary/boundaries.tsx +104 -0
- package/src/solid/route-boundary/index.ts +1 -0
- package/src/solid/settings/index.ts +1 -0
- package/src/solid/settings/setting-list.tsx +185 -0
- package/src/solid/styles.css +2 -0
- package/src/solid/tables/columns.tsx +40 -0
- package/src/solid/tables/data-table.tsx +448 -0
- package/src/solid/tables/filter-row.tsx +161 -0
- package/src/solid/tables/index.ts +4 -0
- package/src/solid/tables/pagination.tsx +78 -0
- package/src/styles.css +12 -0
- package/src/tables/columns.tsx +39 -0
- package/src/tables/data-table.tsx +617 -0
- package/src/tables/filter-row.tsx +260 -0
- package/src/tables/index.ts +3 -0
- package/src/tables/pagination.tsx +93 -0
- package/src/tables/utils.ts +5 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ProfileImage,
|
|
3
|
+
ProfileImageFallback,
|
|
4
|
+
ProfileImageContent,
|
|
5
|
+
} from "@passui/react/profile-image"
|
|
6
|
+
import { HugeiconsIcon } from "@hugeicons/react"
|
|
7
|
+
import { Delete02Icon } from "@hugeicons/core-free-icons"
|
|
8
|
+
import { Button } from "@passui/react/button"
|
|
9
|
+
import { useFieldContext } from "../contexts"
|
|
10
|
+
import { useEffect, useRef, useState } from "react"
|
|
11
|
+
import type { DragEvent, ReactNode } from "react"
|
|
12
|
+
import { cn } from "@passui/react/cn"
|
|
13
|
+
import { Ambient } from "@passui/react/ambient"
|
|
14
|
+
import { AvatarPicker } from "./avatar-picker"
|
|
15
|
+
import type { AvatarPickerLabels } from "./avatar-picker"
|
|
16
|
+
|
|
17
|
+
// A field's value has three real states, not two: `null` (untouched, keep
|
|
18
|
+
// whatever's already there), a `File` (a new one was picked), and `false`
|
|
19
|
+
// (explicitly cleared, distinct from untouched — only reachable when
|
|
20
|
+
// `clearable` is on). The submitting form reads this to decide whether to
|
|
21
|
+
// upload a new file, send an explicit clear, or omit the field entirely.
|
|
22
|
+
export type FieldAvatarValue = File | null | false
|
|
23
|
+
|
|
24
|
+
export type FieldAvatarLabels = AvatarPickerLabels & {
|
|
25
|
+
edit: string
|
|
26
|
+
remove: string
|
|
27
|
+
/** Shown under a picked photo until it's kept or dropped. */
|
|
28
|
+
confirmHint: string
|
|
29
|
+
save: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type FieldAvatarProps = {
|
|
33
|
+
/** Compact identity row for account settings. */
|
|
34
|
+
compact?: boolean
|
|
35
|
+
existingImage?: string | null
|
|
36
|
+
label: string
|
|
37
|
+
accept?: string
|
|
38
|
+
/** Offer removing the image once there is one. Off by default: a
|
|
39
|
+
* personal avatar usually just gets replaced, not cleared to nothing. */
|
|
40
|
+
clearable?: boolean
|
|
41
|
+
/** Sit the photo in a surface lit by its own colors, with this under it. */
|
|
42
|
+
caption?: ReactNode
|
|
43
|
+
/** For a translated UI; English otherwise. */
|
|
44
|
+
labels?: Partial<FieldAvatarLabels>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const DEFAULT_LABELS: FieldAvatarLabels = {
|
|
48
|
+
edit: "Edit",
|
|
49
|
+
remove: "Remove",
|
|
50
|
+
confirmHint:
|
|
51
|
+
"Press Enter to use this photo, Esc to cancel",
|
|
52
|
+
save: "Use photo",
|
|
53
|
+
cancel: "Cancel",
|
|
54
|
+
photo: "Photo",
|
|
55
|
+
photoDescription: "Choose how you appear.",
|
|
56
|
+
choosePhoto: "Choose Photo…",
|
|
57
|
+
takePhoto: "Take Photo",
|
|
58
|
+
capture: "Take photo",
|
|
59
|
+
monogram: "Monogram",
|
|
60
|
+
removePhoto: "Remove Photo",
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const imageFrom = (files: FileList | null | undefined) =>
|
|
64
|
+
[...(files ?? [])].find((file) =>
|
|
65
|
+
file.type.startsWith("image/")
|
|
66
|
+
) ?? null
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A photo you click to replace: a small menu (choose, take, monogram), then
|
|
70
|
+
* the new image shows in place and Enter keeps it (Esc drops it); no cropping dialog.
|
|
71
|
+
*/
|
|
72
|
+
export function FieldAvatar({
|
|
73
|
+
compact = false,
|
|
74
|
+
existingImage,
|
|
75
|
+
label,
|
|
76
|
+
accept = "image/png,image/webp,image/jpeg,image/svg+xml",
|
|
77
|
+
clearable = false,
|
|
78
|
+
caption,
|
|
79
|
+
labels,
|
|
80
|
+
}: FieldAvatarProps) {
|
|
81
|
+
const text = { ...DEFAULT_LABELS, ...labels }
|
|
82
|
+
const field = useFieldContext<FieldAvatarValue>()
|
|
83
|
+
const fileInputRef = useRef<HTMLInputElement>(null)
|
|
84
|
+
const [pending, setPending] = useState<File | null>(null)
|
|
85
|
+
const [sheetOpen, setSheetOpen] = useState(false)
|
|
86
|
+
const [dragOver, setDragOver] = useState(false)
|
|
87
|
+
const [preview, setPreview] = useState<string | null>(
|
|
88
|
+
null
|
|
89
|
+
)
|
|
90
|
+
const isCleared = field.state.value === false
|
|
91
|
+
const shown = pending ?? field.state.value
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (!(shown instanceof File)) {
|
|
95
|
+
setPreview(null)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
const url = URL.createObjectURL(shown)
|
|
99
|
+
setPreview(url)
|
|
100
|
+
return () => URL.revokeObjectURL(url)
|
|
101
|
+
}, [shown])
|
|
102
|
+
|
|
103
|
+
// while a picked photo waits, Enter keeps it and Escape drops it
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (!pending) return
|
|
106
|
+
const onKey = (event: KeyboardEvent) => {
|
|
107
|
+
if (event.key === "Enter") {
|
|
108
|
+
event.preventDefault()
|
|
109
|
+
keep()
|
|
110
|
+
}
|
|
111
|
+
if (event.key === "Escape") setPending(null)
|
|
112
|
+
}
|
|
113
|
+
window.addEventListener("keydown", onKey)
|
|
114
|
+
return () =>
|
|
115
|
+
window.removeEventListener("keydown", onKey)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
function keep() {
|
|
119
|
+
if (!pending) return
|
|
120
|
+
field.handleChange(pending)
|
|
121
|
+
setPending(null)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const currentImage = isCleared
|
|
125
|
+
? undefined
|
|
126
|
+
: (preview ?? existingImage ?? undefined)
|
|
127
|
+
const canClear =
|
|
128
|
+
clearable &&
|
|
129
|
+
!pending &&
|
|
130
|
+
!isCleared &&
|
|
131
|
+
Boolean(currentImage)
|
|
132
|
+
|
|
133
|
+
function onDrop(event: DragEvent<HTMLButtonElement>) {
|
|
134
|
+
event.preventDefault()
|
|
135
|
+
setDragOver(false)
|
|
136
|
+
const file = imageFrom(event.dataTransfer.files)
|
|
137
|
+
if (file) setPending(file)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const photo = (
|
|
141
|
+
<div className="flex w-fit flex-col items-center gap-3">
|
|
142
|
+
<button
|
|
143
|
+
type="button"
|
|
144
|
+
onClick={() => setSheetOpen(true)}
|
|
145
|
+
onDragOver={(event) => {
|
|
146
|
+
event.preventDefault()
|
|
147
|
+
setDragOver(true)
|
|
148
|
+
}}
|
|
149
|
+
onDragLeave={() => setDragOver(false)}
|
|
150
|
+
onDrop={onDrop}
|
|
151
|
+
onPaste={(event) => {
|
|
152
|
+
const file = imageFrom(
|
|
153
|
+
event.clipboardData.files
|
|
154
|
+
)
|
|
155
|
+
if (file) setPending(file)
|
|
156
|
+
}}
|
|
157
|
+
className={cn(
|
|
158
|
+
"group relative rounded-full outline-none",
|
|
159
|
+
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
|
|
160
|
+
)}
|
|
161
|
+
aria-label={`${text.edit} ${label}`}
|
|
162
|
+
>
|
|
163
|
+
<ProfileImage
|
|
164
|
+
className={cn(
|
|
165
|
+
"relative ring-1 ring-white/40",
|
|
166
|
+
compact ? "size-16!" : "size-43!",
|
|
167
|
+
caption &&
|
|
168
|
+
!compact &&
|
|
169
|
+
"shadow-[0_16px_40px_-16px_rgb(0_0_0/0.5)]",
|
|
170
|
+
"transition-transform duration-300 ease-out group-hover:scale-[1.02] group-active:scale-[0.98]",
|
|
171
|
+
(dragOver || pending) &&
|
|
172
|
+
"scale-[1.04]"
|
|
173
|
+
)}
|
|
174
|
+
>
|
|
175
|
+
<ProfileImageContent
|
|
176
|
+
src={currentImage}
|
|
177
|
+
alt={label}
|
|
178
|
+
className="object-cover transition-[filter] duration-300 group-hover:brightness-90"
|
|
179
|
+
/>
|
|
180
|
+
<ProfileImageFallback
|
|
181
|
+
className={
|
|
182
|
+
compact ? "text-xl" : "text-4xl"
|
|
183
|
+
}
|
|
184
|
+
>
|
|
185
|
+
{(
|
|
186
|
+
label.charAt(0) || "?"
|
|
187
|
+
).toUpperCase()}
|
|
188
|
+
</ProfileImageFallback>
|
|
189
|
+
</ProfileImage>
|
|
190
|
+
{/* the frosted "Edit" capsule sitting on the photo's lower edge */}
|
|
191
|
+
{!pending && (
|
|
192
|
+
<span
|
|
193
|
+
className={cn(
|
|
194
|
+
"absolute bottom-3 left-1/2 -translate-x-1/2 rounded-full px-3 py-1",
|
|
195
|
+
"bg-black/35 text-xs font-medium text-white backdrop-blur-md",
|
|
196
|
+
"opacity-0 transition-opacity duration-200 group-hover:opacity-100 group-focus-visible:opacity-100",
|
|
197
|
+
dragOver && "opacity-100"
|
|
198
|
+
)}
|
|
199
|
+
>
|
|
200
|
+
{text.edit}
|
|
201
|
+
</span>
|
|
202
|
+
)}
|
|
203
|
+
</button>
|
|
204
|
+
{pending && (
|
|
205
|
+
<div className="flex flex-col items-center gap-2 animate-in fade-in-0 slide-in-from-top-1 duration-300">
|
|
206
|
+
<p className="text-muted-foreground">
|
|
207
|
+
{text.confirmHint}
|
|
208
|
+
</p>
|
|
209
|
+
<div className="flex items-center gap-1">
|
|
210
|
+
<Button
|
|
211
|
+
type="button"
|
|
212
|
+
variant="ghost"
|
|
213
|
+
size="sm"
|
|
214
|
+
onClick={() => setPending(null)}
|
|
215
|
+
>
|
|
216
|
+
{text.cancel}
|
|
217
|
+
</Button>
|
|
218
|
+
<Button
|
|
219
|
+
variant="default"
|
|
220
|
+
type="button"
|
|
221
|
+
size="sm"
|
|
222
|
+
onClick={keep}
|
|
223
|
+
>
|
|
224
|
+
{text.save}
|
|
225
|
+
</Button>
|
|
226
|
+
</div>
|
|
227
|
+
</div>
|
|
228
|
+
)}
|
|
229
|
+
{canClear && !caption && (
|
|
230
|
+
<Button
|
|
231
|
+
type="button"
|
|
232
|
+
variant="ghost"
|
|
233
|
+
size="sm"
|
|
234
|
+
className="text-muted-foreground"
|
|
235
|
+
onClick={() => field.handleChange(false)}
|
|
236
|
+
>
|
|
237
|
+
<HugeiconsIcon icon={Delete02Icon} />
|
|
238
|
+
{text.remove}
|
|
239
|
+
</Button>
|
|
240
|
+
)}
|
|
241
|
+
</div>
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
return (
|
|
245
|
+
<>
|
|
246
|
+
{compact ? (
|
|
247
|
+
<div className="flex min-w-0 flex-wrap items-center gap-4">
|
|
248
|
+
{photo}
|
|
249
|
+
{!pending && caption}
|
|
250
|
+
</div>
|
|
251
|
+
) : caption ? (
|
|
252
|
+
<Ambient
|
|
253
|
+
src={currentImage}
|
|
254
|
+
className="flex w-full flex-col items-center gap-4 pt-6 pb-2 text-center"
|
|
255
|
+
>
|
|
256
|
+
{photo}
|
|
257
|
+
{!pending && caption}
|
|
258
|
+
</Ambient>
|
|
259
|
+
) : (
|
|
260
|
+
photo
|
|
261
|
+
)}
|
|
262
|
+
<input
|
|
263
|
+
ref={fileInputRef}
|
|
264
|
+
type="file"
|
|
265
|
+
accept={accept}
|
|
266
|
+
className="hidden"
|
|
267
|
+
onChange={(e) => {
|
|
268
|
+
const file = e.target.files?.[0]
|
|
269
|
+
e.target.value = ""
|
|
270
|
+
if (file) setPending(file)
|
|
271
|
+
}}
|
|
272
|
+
/>
|
|
273
|
+
<AvatarPicker
|
|
274
|
+
open={sheetOpen}
|
|
275
|
+
onOpenChange={setSheetOpen}
|
|
276
|
+
label={label}
|
|
277
|
+
image={currentImage}
|
|
278
|
+
clearable={clearable}
|
|
279
|
+
labels={text}
|
|
280
|
+
onChooseFile={() => {
|
|
281
|
+
setSheetOpen(false)
|
|
282
|
+
fileInputRef.current?.click()
|
|
283
|
+
}}
|
|
284
|
+
onCapture={(file) => {
|
|
285
|
+
setSheetOpen(false)
|
|
286
|
+
setPending(file)
|
|
287
|
+
}}
|
|
288
|
+
onChoose={(file) => {
|
|
289
|
+
setSheetOpen(false)
|
|
290
|
+
field.handleChange(file)
|
|
291
|
+
}}
|
|
292
|
+
onRemove={() => {
|
|
293
|
+
setSheetOpen(false)
|
|
294
|
+
field.handleChange(false)
|
|
295
|
+
}}
|
|
296
|
+
/>
|
|
297
|
+
</>
|
|
298
|
+
)
|
|
299
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldCheckbox } from "../../form-fields/fields"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FieldInput,
|
|
3
|
+
FieldCheckbox,
|
|
4
|
+
FieldTextarea,
|
|
5
|
+
FieldSwitch,
|
|
6
|
+
FieldOtp,
|
|
7
|
+
} from "../../form-fields/fields"
|
|
8
|
+
import {
|
|
9
|
+
FieldRadioCard,
|
|
10
|
+
FieldMultiselect,
|
|
11
|
+
} from "../../form-fields/selection"
|
|
12
|
+
import { FieldAvatar } from "./avatar"
|
|
13
|
+
|
|
14
|
+
export const fields = {
|
|
15
|
+
input: FieldInput,
|
|
16
|
+
checkbox: FieldCheckbox,
|
|
17
|
+
textarea: FieldTextarea,
|
|
18
|
+
switch: FieldSwitch,
|
|
19
|
+
radioCard: FieldRadioCard,
|
|
20
|
+
multiselect: FieldMultiselect,
|
|
21
|
+
avatar: FieldAvatar,
|
|
22
|
+
otp: FieldOtp,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export * from "./input"
|
|
26
|
+
export * from "./checkbox"
|
|
27
|
+
export * from "./textarea"
|
|
28
|
+
export * from "./switch"
|
|
29
|
+
export * from "./radio-card"
|
|
30
|
+
export * from "./multiselect"
|
|
31
|
+
export * from "./avatar"
|
|
32
|
+
export * from "./otp"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldInput } from "../../form-fields/fields"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldMultiselect } from "../../form-fields/selection"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldOtp } from "../../form-fields/fields"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldRadioCard } from "../../form-fields/selection"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldSwitch } from "../../form-fields/fields"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldTextarea } from "../../form-fields/fields"
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createFormHook,
|
|
3
|
+
useSelector,
|
|
4
|
+
useStore,
|
|
5
|
+
} from "@tanstack/react-form"
|
|
6
|
+
import { fieldContext, formContext } from "./contexts"
|
|
7
|
+
import { Submit } from "./elements/submit"
|
|
8
|
+
import { fields } from "./fields/index"
|
|
9
|
+
|
|
10
|
+
export const { useAppForm, withForm } = createFormHook({
|
|
11
|
+
fieldComponents: {
|
|
12
|
+
...fields,
|
|
13
|
+
},
|
|
14
|
+
formComponents: {
|
|
15
|
+
submit: Submit,
|
|
16
|
+
},
|
|
17
|
+
fieldContext,
|
|
18
|
+
formContext,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
// for reading another field's live value from within a form — e.g. a
|
|
22
|
+
// radioCard whose options depend on what's currently selected elsewhere in
|
|
23
|
+
// the same form — without subscribing the whole component to every field
|
|
24
|
+
export { useSelector, useStore }
|
|
25
|
+
|
|
26
|
+
// field prop/value types (e.g. FieldAvatarValue), for a caller that needs to
|
|
27
|
+
// type a value it hands to a field before the form itself is submitted
|
|
28
|
+
export * from "./fields/index"
|
|
29
|
+
|
|
30
|
+
export { FormAlert } from "./elements/form-alert"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./use-app-mutation"
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useMutation,
|
|
3
|
+
useQueryClient,
|
|
4
|
+
} from "@tanstack/react-query"
|
|
5
|
+
import type {
|
|
6
|
+
QueryKey,
|
|
7
|
+
UseMutationOptions,
|
|
8
|
+
} from "@tanstack/react-query"
|
|
9
|
+
import { toast } from "@passui/react/toast"
|
|
10
|
+
|
|
11
|
+
type OptimisticUpdate<TOptimisticData, TVariables> = {
|
|
12
|
+
queryKey: QueryKey
|
|
13
|
+
/** Applied to the cache immediately, before the mutation resolves. Rolled back if it errors. */
|
|
14
|
+
updater: (
|
|
15
|
+
old: TOptimisticData | undefined,
|
|
16
|
+
variables: TVariables
|
|
17
|
+
) => TOptimisticData
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type MutationContext<TOptimisticData> =
|
|
21
|
+
| {
|
|
22
|
+
previous: TOptimisticData | undefined
|
|
23
|
+
next: TOptimisticData
|
|
24
|
+
}
|
|
25
|
+
| undefined
|
|
26
|
+
|
|
27
|
+
import { shouldRestoreOptimisticSnapshot } from "../shared/optimistic"
|
|
28
|
+
export { shouldRestoreOptimisticSnapshot } from "../shared/optimistic"
|
|
29
|
+
|
|
30
|
+
type AppMutationOptions<TData, TVariables, TOptimisticData> =
|
|
31
|
+
Omit<
|
|
32
|
+
UseMutationOptions<
|
|
33
|
+
TData,
|
|
34
|
+
Error,
|
|
35
|
+
TVariables,
|
|
36
|
+
MutationContext<TOptimisticData>
|
|
37
|
+
>,
|
|
38
|
+
"onSuccess" | "onError" | "onMutate" | "onSettled"
|
|
39
|
+
> & {
|
|
40
|
+
/** Query keys to invalidate once the mutation settles, win or lose. */
|
|
41
|
+
invalidates?: QueryKey[]
|
|
42
|
+
optimisticUpdate?: OptimisticUpdate<
|
|
43
|
+
TOptimisticData,
|
|
44
|
+
TVariables
|
|
45
|
+
>
|
|
46
|
+
successMessage?:
|
|
47
|
+
| string
|
|
48
|
+
| ((data: TData, variables: TVariables) => string)
|
|
49
|
+
successDescription?:
|
|
50
|
+
| string
|
|
51
|
+
| ((
|
|
52
|
+
data: TData,
|
|
53
|
+
variables: TVariables
|
|
54
|
+
) => string | undefined)
|
|
55
|
+
errorMessage?: string
|
|
56
|
+
onSuccess?: (
|
|
57
|
+
data: TData,
|
|
58
|
+
variables: TVariables
|
|
59
|
+
) => void
|
|
60
|
+
onError?: (
|
|
61
|
+
error: Error,
|
|
62
|
+
variables: TVariables
|
|
63
|
+
) => void
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// every domain mutation hook in both infra and www builds on this instead
|
|
67
|
+
// of calling useMutation directly; optimisticUpdate's rollback only
|
|
68
|
+
// restores the snapshot if nothing else wrote to the same queryKey in the
|
|
69
|
+
// meantime
|
|
70
|
+
export function useAppMutation<
|
|
71
|
+
TData,
|
|
72
|
+
TVariables = void,
|
|
73
|
+
TOptimisticData = unknown,
|
|
74
|
+
>({
|
|
75
|
+
invalidates = [],
|
|
76
|
+
optimisticUpdate,
|
|
77
|
+
successMessage,
|
|
78
|
+
successDescription,
|
|
79
|
+
errorMessage,
|
|
80
|
+
onSuccess,
|
|
81
|
+
onError,
|
|
82
|
+
...options
|
|
83
|
+
}: AppMutationOptions<TData, TVariables, TOptimisticData>) {
|
|
84
|
+
const q = useQueryClient()
|
|
85
|
+
return useMutation<
|
|
86
|
+
TData,
|
|
87
|
+
Error,
|
|
88
|
+
TVariables,
|
|
89
|
+
MutationContext<TOptimisticData>
|
|
90
|
+
>({
|
|
91
|
+
...options,
|
|
92
|
+
onMutate: async (variables) => {
|
|
93
|
+
if (!optimisticUpdate) return undefined
|
|
94
|
+
const { queryKey, updater } = optimisticUpdate
|
|
95
|
+
await q.cancelQueries({ queryKey })
|
|
96
|
+
const previous =
|
|
97
|
+
q.getQueryData<TOptimisticData>(queryKey)
|
|
98
|
+
const next = updater(previous, variables)
|
|
99
|
+
q.setQueryData<TOptimisticData>(queryKey, next)
|
|
100
|
+
return { previous, next }
|
|
101
|
+
},
|
|
102
|
+
onSuccess: (data, variables) => {
|
|
103
|
+
if (successMessage) {
|
|
104
|
+
toast.success(
|
|
105
|
+
typeof successMessage === "function"
|
|
106
|
+
? successMessage(data, variables)
|
|
107
|
+
: successMessage,
|
|
108
|
+
{
|
|
109
|
+
description:
|
|
110
|
+
typeof successDescription ===
|
|
111
|
+
"function"
|
|
112
|
+
? successDescription(
|
|
113
|
+
data,
|
|
114
|
+
variables
|
|
115
|
+
)
|
|
116
|
+
: successDescription,
|
|
117
|
+
}
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
onSuccess?.(data, variables)
|
|
121
|
+
},
|
|
122
|
+
onError: (error, variables, context) => {
|
|
123
|
+
if (optimisticUpdate && context) {
|
|
124
|
+
const current = q.getQueryData(
|
|
125
|
+
optimisticUpdate.queryKey
|
|
126
|
+
)
|
|
127
|
+
// only roll back if nothing else has written to this key
|
|
128
|
+
// since our optimistic value landed, otherwise this would
|
|
129
|
+
// silently discard a concurrent mutation's newer change
|
|
130
|
+
if (
|
|
131
|
+
shouldRestoreOptimisticSnapshot(
|
|
132
|
+
current,
|
|
133
|
+
context.next
|
|
134
|
+
)
|
|
135
|
+
) {
|
|
136
|
+
q.setQueryData(
|
|
137
|
+
optimisticUpdate.queryKey,
|
|
138
|
+
context.previous
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
toast.error(
|
|
143
|
+
errorMessage ?? "Something went wrong",
|
|
144
|
+
{
|
|
145
|
+
description: error.message,
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
onError?.(error, variables)
|
|
149
|
+
},
|
|
150
|
+
onSettled: () => {
|
|
151
|
+
for (const queryKey of invalidates) {
|
|
152
|
+
void q.invalidateQueries({ queryKey })
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
})
|
|
156
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ErrorComponentProps } from "@tanstack/react-router"
|
|
2
|
+
import { Button, buttonStyle } from "@passui/react/button"
|
|
3
|
+
import {
|
|
4
|
+
ErrorComponent,
|
|
5
|
+
Link,
|
|
6
|
+
useLocation,
|
|
7
|
+
useRouter,
|
|
8
|
+
} from "@tanstack/react-router"
|
|
9
|
+
import { cn } from "@passui/react/cn"
|
|
10
|
+
|
|
11
|
+
export function DefaultBoundary({
|
|
12
|
+
error,
|
|
13
|
+
}: ErrorComponentProps) {
|
|
14
|
+
const router = useRouter()
|
|
15
|
+
const isRoot = useLocation({
|
|
16
|
+
select: (location) => location.pathname === "/",
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<div className="flex min-h-100 w-full flex-col items-center justify-center gap-6 p-6 text-center">
|
|
21
|
+
<div className="w-full max-w-md overflow-hidden rounded-md border border-destructive/20 bg-destructive/5 p-4 text-left">
|
|
22
|
+
<p className="mb-2 text-xs font-semibold tracking-wider text-destructive uppercase">
|
|
23
|
+
Application Error
|
|
24
|
+
</p>
|
|
25
|
+
<ErrorComponent error={error} />
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
<div className="flex flex-wrap items-center justify-center gap-2">
|
|
29
|
+
<Button
|
|
30
|
+
onClick={() => router.invalidate()}
|
|
31
|
+
variant="secondary"
|
|
32
|
+
size="sm"
|
|
33
|
+
>
|
|
34
|
+
Try Again
|
|
35
|
+
</Button>
|
|
36
|
+
|
|
37
|
+
{isRoot ? (
|
|
38
|
+
<Link
|
|
39
|
+
to="/"
|
|
40
|
+
className={cn(
|
|
41
|
+
buttonStyle({
|
|
42
|
+
variant: "default",
|
|
43
|
+
size: "sm",
|
|
44
|
+
})
|
|
45
|
+
)}
|
|
46
|
+
>
|
|
47
|
+
Go Home
|
|
48
|
+
</Link>
|
|
49
|
+
) : (
|
|
50
|
+
<Button
|
|
51
|
+
onClick={() => router.history.back()}
|
|
52
|
+
variant="default"
|
|
53
|
+
size="sm"
|
|
54
|
+
>
|
|
55
|
+
Go Back
|
|
56
|
+
</Button>
|
|
57
|
+
)}
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { HugeiconsIcon } from "@hugeicons/react"
|
|
2
|
+
import { Loading03Icon } from "@hugeicons/core-free-icons"
|
|
3
|
+
import { cn } from "@passui/react/cn"
|
|
4
|
+
|
|
5
|
+
export function DefaultLoader() {
|
|
6
|
+
return (
|
|
7
|
+
<section
|
|
8
|
+
className={cn(
|
|
9
|
+
"flex h-screen w-screen items-center justify-center",
|
|
10
|
+
"bg-background"
|
|
11
|
+
)}
|
|
12
|
+
>
|
|
13
|
+
<HugeiconsIcon
|
|
14
|
+
icon={Loading03Icon}
|
|
15
|
+
className="size-5! animate-spin duration-500"
|
|
16
|
+
/>
|
|
17
|
+
</section>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Link, useRouter } from "@tanstack/react-router"
|
|
2
|
+
import { Button, buttonStyle } from "@passui/react/button"
|
|
3
|
+
import type { PropsWithChildren } from "react"
|
|
4
|
+
import { cn } from "@passui/react/cn"
|
|
5
|
+
|
|
6
|
+
type NotFoundProps = PropsWithChildren
|
|
7
|
+
|
|
8
|
+
export function NotFound({ children }: NotFoundProps) {
|
|
9
|
+
const router = useRouter()
|
|
10
|
+
|
|
11
|
+
return (
|
|
12
|
+
<div className="flex min-h-[80svh] w-full flex-col items-center justify-center text-center">
|
|
13
|
+
<section className="mx-auto flex w-full max-w-sm flex-col items-center justify-center gap-4">
|
|
14
|
+
{children ?? (
|
|
15
|
+
<div className="flex flex-col items-center gap-2">
|
|
16
|
+
<span className="font-mono text-xs font-semibold tracking-widest text-destructive uppercase">
|
|
17
|
+
Error 404
|
|
18
|
+
</span>
|
|
19
|
+
<h2 className="text-2xl font-bold tracking-tight text-foreground">
|
|
20
|
+
Page Not Found
|
|
21
|
+
</h2>
|
|
22
|
+
<p className="max-w-70 text-sm text-muted-foreground">
|
|
23
|
+
The page you are looking for might
|
|
24
|
+
have been moved, renamed, or
|
|
25
|
+
deleted.
|
|
26
|
+
</p>
|
|
27
|
+
</div>
|
|
28
|
+
)}
|
|
29
|
+
|
|
30
|
+
<div className="mt-2 flex items-center gap-2">
|
|
31
|
+
<Button
|
|
32
|
+
onClick={() => router.history.back()}
|
|
33
|
+
variant="secondary"
|
|
34
|
+
size="sm"
|
|
35
|
+
>
|
|
36
|
+
Go back
|
|
37
|
+
</Button>
|
|
38
|
+
<Link
|
|
39
|
+
to="/"
|
|
40
|
+
className={cn(
|
|
41
|
+
buttonStyle({ size: "sm" })
|
|
42
|
+
)}
|
|
43
|
+
>
|
|
44
|
+
Start Over
|
|
45
|
+
</Link>
|
|
46
|
+
</div>
|
|
47
|
+
</section>
|
|
48
|
+
</div>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./setting-list"
|