create-signet-app 0.9.3 → 0.9.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-signet-app",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "license": "MIT",
5
5
  "description": "Scaffold a Vite + React + Tailwind app wired to Signet.",
6
6
  "keywords": [
@@ -1,10 +1,14 @@
1
+ import { ErrorState } from "@dapplelight/signet-ui/components/error-state"
2
+ import { Skeleton } from "@dapplelight/signet-ui/components/skeleton"
1
3
  import {
2
4
  NotificationInbox,
3
5
  NotificationPreferences,
4
6
  } from "@dapplelight/signet-ui/screens/notification-screens"
5
7
  import { useInfiniteQuery, useQuery, useQueryClient } from "@tanstack/react-query"
6
8
  import { createFileRoute, useNavigate } from "@tanstack/react-router"
7
- import { api } from "../../lib/api"
9
+ import type { ReactNode } from "react"
10
+ import { toast } from "sonner"
11
+ import { ApiError, api } from "../../lib/api"
8
12
 
9
13
  export const Route = createFileRoute("/_app/notifications")({
10
14
  component: Notifications,
@@ -16,6 +20,12 @@ const CATEGORIES = [
16
20
  { id: "billing", label: "Billing", description: "Plan changes and receipts", required: true }, // @payments
17
21
  ]
18
22
 
23
+ const SURFACE = "rounded-xl bg-bg-surface p-2 ring-1 ring-foreground/10"
24
+
25
+ /** Toast the ApiError's message, else the fallback. */
26
+ const fail = (e: unknown, fallback: string) =>
27
+ toast.error(e instanceof ApiError && e.message ? e.message : fallback)
28
+
19
29
  function Notifications() {
20
30
  const qc = useQueryClient()
21
31
  const navigate = useNavigate()
@@ -26,44 +36,102 @@ function Notifications() {
26
36
  (await api.notifications.$get({ query: pageParam ? { cursor: pageParam } : {} })).json(),
27
37
  getNextPageParam: (last) => last.nextCursor,
28
38
  })
29
- const prefs = useQuery({
39
+ const prefsQuery = useQuery({
30
40
  queryKey: ["notification-prefs"],
31
41
  queryFn: async () => (await api.notifications.prefs.$get()).json(),
32
- }).data
42
+ })
43
+ const prefs = prefsQuery.data
33
44
  const refresh = () => qc.invalidateQueries({ queryKey: ["notifications"] })
45
+ // Toast, then swallow: the inbox's inline line stays empty, so the user sees one message.
46
+ const act = async (fallback: string, run: () => Promise<unknown>) => {
47
+ try {
48
+ await run()
49
+ await refresh()
50
+ } catch (e) {
51
+ fail(e, fallback)
52
+ }
53
+ }
34
54
 
35
- return (
36
- <div className="space-y-10">
55
+ let inboxView: ReactNode
56
+ if (inbox.isPending) {
57
+ inboxView = (
58
+ <div
59
+ data-state="loading"
60
+ role="status"
61
+ aria-label="Loading notifications"
62
+ className={`flex flex-col gap-2 ${SURFACE}`}
63
+ >
64
+ {[0, 1, 2].map((i) => (
65
+ <Skeleton key={i} className="h-16" />
66
+ ))}
67
+ </div>
68
+ )
69
+ } else if (!inbox.data) {
70
+ // a failed refetch or load-more keeps the old data: only show the error when there is none
71
+ inboxView = (
72
+ <div data-state="error" className={SURFACE}>
73
+ <ErrorState message="Your notifications did not load." onRetry={() => inbox.refetch()} />
74
+ </div>
75
+ )
76
+ } else {
77
+ inboxView = (
37
78
  <NotificationInbox
38
- items={inbox.data?.pages.flatMap((p) => p.items) ?? []}
39
- unreadCount={inbox.data?.pages[0]?.unreadCount}
79
+ items={inbox.data.pages.flatMap((p) => p.items)}
80
+ unreadCount={inbox.data.pages[0]?.unreadCount}
40
81
  hasMore={inbox.hasNextPage}
41
- onLoadMore={() => inbox.fetchNextPage()}
42
- onMarkRead={async (ids) => {
43
- await api.notifications.read.$post({ json: { ids } })
44
- await refresh()
45
- }}
46
- onMarkAllRead={async () => {
47
- await api.notifications["read-all"].$post()
48
- await refresh()
49
- }}
50
- onArchive={async (item) => {
51
- await api.notifications[":id"].archive.$post({ param: { id: item.id } })
52
- await refresh()
82
+ onLoadMore={async () => {
83
+ try {
84
+ await inbox.fetchNextPage({ throwOnError: true })
85
+ } catch (e) {
86
+ fail(e, "Couldn't load more notifications.")
87
+ }
53
88
  }}
89
+ onMarkRead={(ids) =>
90
+ act("Couldn't mark as read.", () => api.notifications.read.$post({ json: { ids } }))
91
+ }
92
+ onMarkAllRead={() =>
93
+ act("Couldn't mark all as read.", () => api.notifications["read-all"].$post())
94
+ }
95
+ onArchive={(item) =>
96
+ act("Couldn't archive that notification.", () =>
97
+ api.notifications[":id"].archive.$post({ param: { id: item.id } }),
98
+ )
99
+ }
54
100
  onOpen={(item) => {
55
101
  if (item.link) navigate({ to: item.link })
56
102
  }}
57
103
  />
58
- {prefs ? (
104
+ )
105
+ }
106
+
107
+ return (
108
+ <div className="space-y-10" aria-busy={inbox.isPending || prefsQuery.isPending || undefined}>
109
+ {inboxView}
110
+ {prefsQuery.isPending ? (
111
+ <div data-state="loading" role="status" aria-label="Loading notification settings">
112
+ <Skeleton className="h-40" />
113
+ </div>
114
+ ) : prefsQuery.isError && !prefs ? (
115
+ <div data-state="error" className={SURFACE}>
116
+ <ErrorState
117
+ message="Your notification settings did not load."
118
+ onRetry={() => prefsQuery.refetch()}
119
+ />
120
+ </div>
121
+ ) : prefs ? (
59
122
  <NotificationPreferences
60
123
  categories={CATEGORIES}
61
124
  channels={["inApp", "email", "sms"]}
62
125
  defaultValue={prefs.channels}
63
126
  onToggle={async ({ category, channel, on }) => {
64
- await api.notifications.prefs.$put({
65
- json: { channels: { [category]: { [channel]: on } } },
66
- })
127
+ try {
128
+ await api.notifications.prefs.$put({
129
+ json: { channels: { [category]: { [channel]: on } } },
130
+ })
131
+ } catch (e) {
132
+ fail(e, "Couldn't save that setting.")
133
+ throw e // rethrow: the switch flips back
134
+ }
67
135
  }}
68
136
  />
69
137
  ) : null}