create-signet-app 0.9.3 → 0.9.5

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.
@@ -102,7 +102,10 @@ export const templates = defineTemplates({
102
102
 
103
103
  /**
104
104
  * Where the client IP comes from: server.ts overwrites x-forwarded-for with the
105
- * socket's address, so one hop. Behind your own proxy, see server.ts.
105
+ * socket's address, so one hop. Behind your own proxy, see server.ts. Behind
106
+ * Cloudflare with the origin firewalled to Cloudflare's IP ranges, use
107
+ * `{ cloudflare: { originLocked: true } }`; plain `"cloudflare"` is believed only
108
+ * on Workers, because anywhere else a client can send `cf-connecting-ip` itself.
106
109
  */
107
110
  export const TRUST = { forwardedHops: 1 } as const
108
111
  // admin:begin
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.5",
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}
@@ -65,6 +65,11 @@
65
65
  "addItem": "Add",
66
66
  "removeItem": "Remove",
67
67
  "chooseOption": "Choose…",
68
- "skipToContent": "Skip to content"
68
+ "skipToContent": "Skip to content",
69
+ "stageDone": "done",
70
+ "stageCurrent": "in progress",
71
+ "stageUpcoming": "not started",
72
+ "showMore": "Show more",
73
+ "showLess": "Show less"
69
74
  }
70
75
  }
@@ -2,6 +2,11 @@ import { expect, test } from "bun:test"
2
2
  import { memoryStore } from "@dapplelight/signet-server/middleware/rate-limit"
3
3
  import { createApp } from "../src/app.ts"
4
4
 
5
+ // trust "cloudflare" is believed only on Workers (a Node or Bun origin can be hit
6
+ // directly, where a client sets cf-connecting-ip itself). The cloudflare target
7
+ // runs there, so pose as Workers here.
8
+ Object.defineProperty(navigator, "userAgent", { value: "Cloudflare-Workers", configurable: true })
9
+
5
10
  // Requests that reach the app without cf-connecting-ip (a proxy that strips it,
6
11
  // a health checker, a local call) all share one bucket. Once it is full, the
7
12
  // next caller without the header is told to back off, whoever they are; callers
@@ -18,10 +18,15 @@ Mount `clientLoginRoutes` from `@dapplelight/signet-server/auth/hono` at
18
18
  `/auth/client-login` and register this CLI as a client:
19
19
 
20
20
  ```ts
21
+ import type { ClientIpTrust } from "@dapplelight/signet-server/middleware/rate-limit"
22
+
23
+ // "cloudflare": on Workers · { cloudflare: { originLocked: true } }: origin accepts only Cloudflare IPs · { forwardedHops: n }: behind n proxies (Vercel: 1) · "none": no proxy, one shared bucket
24
+ const TRUST: ClientIpTrust = "none"
25
+
21
26
  app.route(
22
27
  "/auth/client-login",
23
28
  clientLoginRoutes({
24
- trust: "cloudflare",
29
+ trust: TRUST,
25
30
  clients: { __APP_NAME__: {} },
26
31
  verificationUri: "https://your.app/device",
27
32
  }),