@sssb/kit 0.0.0 → 0.3.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.
Files changed (227) hide show
  1. package/LICENSE +21 -0
  2. package/dist/core/webhook-hmac.d.ts +69 -0
  3. package/dist/release.json +1 -0
  4. package/dist/testing/e2e.d.ts +15 -0
  5. package/dist/testing/e2e.js +195 -0
  6. package/package.json +212 -5
  7. package/src/adapters/README.md +6 -0
  8. package/src/adapters/dev-server.ts +21 -0
  9. package/src/adapters/fake/agreements.ts +81 -0
  10. package/src/adapters/fake/analytics.ts +93 -0
  11. package/src/adapters/fake/atomic-write.ts +161 -0
  12. package/src/adapters/fake/audit.ts +58 -0
  13. package/src/adapters/fake/auth.ts +313 -0
  14. package/src/adapters/fake/clock.ts +54 -0
  15. package/src/adapters/fake/data-dir.ts +27 -0
  16. package/src/adapters/fake/db.ts +88 -0
  17. package/src/adapters/fake/email.ts +46 -0
  18. package/src/adapters/fake/jobs.ts +248 -0
  19. package/src/adapters/fake/llm.ts +230 -0
  20. package/src/adapters/fake/pglite-dialect.ts +124 -0
  21. package/src/adapters/fake/service-auth.ts +145 -0
  22. package/src/adapters/fake/simulator-admin.ts +203 -0
  23. package/src/adapters/fake/simulator.ts +93 -0
  24. package/src/adapters/fake/sms.ts +43 -0
  25. package/src/adapters/fake/storage.ts +288 -0
  26. package/src/adapters/fake/webhooks.ts +98 -0
  27. package/src/adapters/index.ts +142 -0
  28. package/src/adapters/lazy-jobs.ts +25 -0
  29. package/src/adapters/proxy.ts +34 -0
  30. package/src/adapters/real/absent.ts +41 -0
  31. package/src/adapters/real/accept-invite-form.tsx +77 -0
  32. package/src/adapters/real/analytics.ts +28 -0
  33. package/src/adapters/real/auth.ts +216 -0
  34. package/src/adapters/real/clerk-proxy.ts +111 -0
  35. package/src/adapters/real/contract-data-api.ts +104 -0
  36. package/src/adapters/real/contract-postgres.ts +167 -0
  37. package/src/adapters/real/database-transport.ts +34 -0
  38. package/src/adapters/real/db-data-api.ts +61 -0
  39. package/src/adapters/real/db-port.ts +26 -0
  40. package/src/adapters/real/db.ts +69 -0
  41. package/src/adapters/real/email.ts +22 -0
  42. package/src/adapters/real/index.ts +139 -0
  43. package/src/adapters/real/jobs-in-process.ts +118 -0
  44. package/src/adapters/real/jobs.ts +33 -0
  45. package/src/adapters/real/llm.ts +108 -0
  46. package/src/adapters/real/migrate-handler.ts +66 -0
  47. package/src/adapters/real/optional-ports.ts +78 -0
  48. package/src/adapters/real/providers.tsx +12 -0
  49. package/src/adapters/real/rds-data-codec.ts +508 -0
  50. package/src/adapters/real/rds-data-dialect.ts +470 -0
  51. package/src/adapters/real/rds-data-emulator.ts +351 -0
  52. package/src/adapters/real/s3-storage.ts +233 -0
  53. package/src/adapters/real/sign-in-form.tsx +59 -0
  54. package/src/adapters/real/storage.ts +10 -0
  55. package/src/adapters/run-mode.ts +11 -0
  56. package/src/adapters/sentry/baseline.ts +90 -0
  57. package/src/adapters/sentry/client.ts +17 -0
  58. package/src/adapters/sentry/init.ts +17 -0
  59. package/src/authz/app-exemptions.ts +226 -0
  60. package/src/authz/authorize.ts +214 -0
  61. package/src/authz/route-policy.ts +153 -0
  62. package/src/authz/route-scan.ts +1516 -0
  63. package/src/components/agreements/acceptances-section.tsx +55 -0
  64. package/src/components/agreements/agreement-advisory-banner.tsx +33 -0
  65. package/src/components/agreements/agreement-body.tsx +34 -0
  66. package/src/components/agreements/agreement-gate.tsx +45 -0
  67. package/src/components/app-header.tsx +45 -0
  68. package/src/components/auth/accept-invite-screen.tsx +126 -0
  69. package/src/components/auth/auth-icons.tsx +68 -0
  70. package/src/components/auth/org-switcher.tsx +76 -0
  71. package/src/components/auth/people-picker.tsx +50 -0
  72. package/src/components/auth/preview-row.tsx +64 -0
  73. package/src/components/auth/sign-in-screen.tsx +24 -0
  74. package/src/components/auth/user-menu.tsx +64 -0
  75. package/src/components/demo-badge.tsx +20 -0
  76. package/src/components/demo-banner.tsx +14 -0
  77. package/src/components/gates/gate-interstitial.tsx +23 -0
  78. package/src/components/job-timeline.tsx +64 -0
  79. package/src/components/notifications/notification-bell.tsx +120 -0
  80. package/src/components/notifications/notification-prefs-section.tsx +112 -0
  81. package/src/components/org-screen.tsx +197 -0
  82. package/src/components/profile-screen.tsx +100 -0
  83. package/src/components/simulator/actor-runtime.ts +180 -0
  84. package/src/components/simulator/actor-shell.tsx +275 -0
  85. package/src/components/simulator/actors-app.tsx +119 -0
  86. package/src/components/simulator/errors-app.tsx +84 -0
  87. package/src/components/simulator/events-app.tsx +210 -0
  88. package/src/components/simulator/format-when.ts +20 -0
  89. package/src/components/simulator/hooks-app.tsx +319 -0
  90. package/src/components/simulator/inbound-app.tsx +210 -0
  91. package/src/components/simulator/jobs-app.tsx +471 -0
  92. package/src/components/simulator/mail-app.tsx +290 -0
  93. package/src/components/simulator/messages-app.tsx +64 -0
  94. package/src/components/simulator/people-app.tsx +140 -0
  95. package/src/components/simulator/people-dimensions.ts +16 -0
  96. package/src/components/simulator/simulator-host.tsx +335 -0
  97. package/src/components/simulator/simulator-panel.tsx +704 -0
  98. package/src/components/simulator/snapshots-app.tsx +398 -0
  99. package/src/components/simulator/tab-mount.ts +25 -0
  100. package/src/components/simulator/tours-app.tsx +105 -0
  101. package/src/components/webhook-endpoints-card.tsx +172 -0
  102. package/src/core/abilities.ts +464 -0
  103. package/src/core/agreements.ts +139 -0
  104. package/src/core/gates.ts +157 -0
  105. package/src/core/greeting.ts +11 -0
  106. package/src/core/inbound-email.ts +129 -0
  107. package/src/core/jobs.ts +51 -0
  108. package/src/core/keyset.ts +227 -0
  109. package/src/core/llm-models.ts +10 -0
  110. package/src/core/locale.ts +3 -0
  111. package/src/core/notifications.ts +182 -0
  112. package/src/core/presets.ts +456 -0
  113. package/src/core/roles.ts +47 -0
  114. package/src/core/same-origin-path.ts +20 -0
  115. package/src/core/schedules.ts +147 -0
  116. package/src/core/standard-schema.ts +56 -0
  117. package/src/core/state-machine.ts +43 -0
  118. package/src/core/webhook-events.ts +86 -0
  119. package/src/core/webhook-hmac.ts +248 -0
  120. package/src/core/webhook-signing.ts +27 -0
  121. package/src/cutover/rows.json +101 -0
  122. package/src/db/agreements.ts +176 -0
  123. package/src/db/app-roles.ts +197 -0
  124. package/src/db/audit.ts +356 -0
  125. package/src/db/inbound-email.ts +145 -0
  126. package/src/db/jobs.ts +300 -0
  127. package/src/db/jsonb.ts +18 -0
  128. package/src/db/keyset.ts +179 -0
  129. package/src/db/migrate.ts +73 -0
  130. package/src/db/migrations/0001_tenants.ts +25 -0
  131. package/src/db/migrations/0003_organizations.ts +27 -0
  132. package/src/db/migrations/0004_jobs.ts +64 -0
  133. package/src/db/migrations/0005_service_keys.ts +33 -0
  134. package/src/db/migrations/0008_audit_events.ts +51 -0
  135. package/src/db/migrations/0009_job_schedules.ts +52 -0
  136. package/src/db/migrations/0010_webhooks.ts +100 -0
  137. package/src/db/migrations/0011_notifications.ts +98 -0
  138. package/src/db/migrations/0012_inbound_email.ts +74 -0
  139. package/src/db/migrations/0013_agreements.ts +98 -0
  140. package/src/db/migrations/0014_audit_credential.ts +29 -0
  141. package/src/db/migrations/0015_app_user_grant.ts +67 -0
  142. package/src/db/migrations/0016_audit_detail.ts +26 -0
  143. package/src/db/migrations/index.ts +49 -0
  144. package/src/db/notifications.ts +185 -0
  145. package/src/db/org-lookup.ts +104 -0
  146. package/src/db/owner-pass.ts +252 -0
  147. package/src/db/rls-coverage.ts +259 -0
  148. package/src/db/rls-proof-runner.ts +94 -0
  149. package/src/db/rls-proofs.ts +1176 -0
  150. package/src/db/schedules.ts +187 -0
  151. package/src/db/schema.ts +235 -0
  152. package/src/db/seed.ts +176 -0
  153. package/src/db/service-keys.ts +50 -0
  154. package/src/db/tenant-lookup.ts +13 -0
  155. package/src/db/webhooks.ts +483 -0
  156. package/src/db/with-tenant.ts +26 -0
  157. package/src/demo-static/actor-drivers.ts +161 -0
  158. package/src/demo-static/assistant-replay.ts +61 -0
  159. package/src/demo-static/contracts.ts +282 -0
  160. package/src/demo-static/email-html.ts +58 -0
  161. package/src/demo-static/hash-route.ts +24 -0
  162. package/src/demo-static/rows.ts +105 -0
  163. package/src/demo-static/shell.tsx +440 -0
  164. package/src/demo-static/tour/contracts.ts +97 -0
  165. package/src/demo-static/tour/driver.ts +320 -0
  166. package/src/demo-static/tour/tour-overlay.tsx +226 -0
  167. package/src/demo-static/tour/use-tours.tsx +394 -0
  168. package/src/demo-static/world.ts +1480 -0
  169. package/src/email/send.ts +12 -0
  170. package/src/email/templates/digest-email.tsx +61 -0
  171. package/src/email/templates/invite-email.tsx +57 -0
  172. package/src/email/templates/notification-email.tsx +39 -0
  173. package/src/guide.md +132 -0
  174. package/src/i18n/messages/en.json +373 -0
  175. package/src/i18n/messages/es.json +373 -0
  176. package/src/i18n/messages.ts +34 -0
  177. package/src/i18n/namespaces.ts +32 -0
  178. package/src/i18n/request.ts +34 -0
  179. package/src/i18n/routing.ts +7 -0
  180. package/src/inbound-email/handlers.ts +67 -0
  181. package/src/inbound-email/intake.ts +180 -0
  182. package/src/jobs/digest-email.ts +112 -0
  183. package/src/jobs/run-in-process.ts +82 -0
  184. package/src/observability/scrub.ts +44 -0
  185. package/src/ports/README.md +8 -0
  186. package/src/ports/analytics.ts +8 -0
  187. package/src/ports/auth.ts +87 -0
  188. package/src/ports/db.ts +27 -0
  189. package/src/ports/email.ts +14 -0
  190. package/src/ports/errors.ts +46 -0
  191. package/src/ports/jobs.ts +69 -0
  192. package/src/ports/llm.ts +85 -0
  193. package/src/ports/storage.ts +132 -0
  194. package/src/seed/contracts.ts +129 -0
  195. package/src/server-lib/assistant.ts +45 -0
  196. package/src/server-lib/defer.ts +57 -0
  197. package/src/server-lib/demo-presets.ts +85 -0
  198. package/src/server-lib/invite.ts +92 -0
  199. package/src/server-lib/notify-deps.ts +15 -0
  200. package/src/server-lib/notify.ts +252 -0
  201. package/src/server-lib/preset-operations.ts +121 -0
  202. package/src/server-lib/scheduled-tick.ts +48 -0
  203. package/src/server-lib/simulator-api.ts +311 -0
  204. package/src/server-lib/sms.ts +43 -0
  205. package/src/server-lib/webhook-dispatch.ts +75 -0
  206. package/src/service-auth/credential.ts +20 -0
  207. package/src/service-auth/mailgun.ts +70 -0
  208. package/src/service-auth/verify.ts +80 -0
  209. package/src/service-auth/webhook.ts +69 -0
  210. package/src/testing/checks.json +103 -0
  211. package/src/testing/conformance/authz.ts +46 -0
  212. package/src/testing/conformance/db.ts +258 -0
  213. package/src/testing/conformance/demo-presets.ts +295 -0
  214. package/src/testing/conformance/i18n.ts +80 -0
  215. package/src/testing/conformance/register.ts +200 -0
  216. package/src/testing/conformance/seam-map.ts +205 -0
  217. package/src/testing/conformance/suite.ts +59 -0
  218. package/src/testing/conformance/types.ts +18 -0
  219. package/src/testing/contract.ts +200 -0
  220. package/src/testing/e2e.ts +15 -0
  221. package/src/testing/index.ts +6 -0
  222. package/src/testing/seam-specifiers.json +22 -0
  223. package/src/testing/tmp-dir-marker.d.mts +4 -0
  224. package/src/testing/tmp-dir-marker.mjs +5 -0
  225. package/src/testing/tmp-dir.ts +182 -0
  226. package/src/theme.ts +28 -0
  227. package/README.md +0 -3
@@ -0,0 +1,64 @@
1
+ 'use client'
2
+
3
+ import { Avatar, Menu, UnstyledButton, VisuallyHidden } from '@mantine/core'
4
+ import { useTranslations } from 'next-intl'
5
+ import { SignOutIcon, UserIcon } from './auth-icons'
6
+ import { PreviewRow } from './preview-row'
7
+
8
+ export interface MenuUser {
9
+ name: string
10
+ email: string
11
+ }
12
+
13
+ /**
14
+ * Lean take on Clerk's UserButton (ADR-0003): an avatar trigger opening a popover whose header is
15
+ * the user preview row (name + email), then the account actions with leading icons.
16
+ */
17
+ export function UserMenu({
18
+ user,
19
+ onProfile,
20
+ onSignOut,
21
+ }: {
22
+ user: MenuUser
23
+ /** Omit in an app that mounts no profile route — the entry is then not offered at all, which is
24
+ * the UI form of ADR-0012's "unused capability = empty registration". */
25
+ onProfile?: () => void
26
+ onSignOut: () => void
27
+ }) {
28
+ const t = useTranslations('auth')
29
+
30
+ return (
31
+ <Menu position="bottom-end" width={260}>
32
+ <Menu.Target>
33
+ {/* The trigger shows only initials. They are a symbol, like a "B" on a Bold button, not a
34
+ label (WCAG 2.5.3's carve-out), so they are aria-hidden and a visually hidden line names
35
+ the button with the user's full name (the email when the name is blank). Named by its
36
+ content, never an aria-label: axe 4.14 compares an aria-label with the on-screen
37
+ initials. A checker that compares the computed name instead may still flag this. */}
38
+ <UnstyledButton data-testid="user-menu">
39
+ <VisuallyHidden>{t('userMenuLabel', { name: user.name.trim() || user.email })}</VisuallyHidden>
40
+ <Avatar
41
+ name={user.name}
42
+ aria-hidden
43
+ radius="xl"
44
+ styles={{ placeholder: { color: '#fff', background: 'var(--mantine-color-dark-6)' } }}
45
+ />
46
+ </UnstyledButton>
47
+ </Menu.Target>
48
+ <Menu.Dropdown>
49
+ <Menu.Label>
50
+ <PreviewRow name={user.name} title={user.name} subtitle={user.email} rounded />
51
+ </Menu.Label>
52
+ <Menu.Divider />
53
+ {onProfile ? (
54
+ <Menu.Item leftSection={<UserIcon />} onClick={onProfile} data-testid="menu-profile">
55
+ {t('profileLink')}
56
+ </Menu.Item>
57
+ ) : null}
58
+ <Menu.Item leftSection={<SignOutIcon />} onClick={onSignOut} data-testid="menu-signout">
59
+ {t('signOut')}
60
+ </Menu.Item>
61
+ </Menu.Dropdown>
62
+ </Menu>
63
+ )
64
+ }
@@ -0,0 +1,20 @@
1
+ 'use client'
2
+
3
+ import { Badge } from '@mantine/core'
4
+ import { useTranslations } from 'next-intl'
5
+
6
+ /** Fixed corner badge every DEMO_MODE page shows — demo builds must be unmistakable. */
7
+ export function DemoBadge() {
8
+ const t = useTranslations('shell')
9
+ return (
10
+ <Badge
11
+ color="orange"
12
+ variant="filled"
13
+ radius="sm"
14
+ data-testid="demo-badge"
15
+ style={{ position: 'fixed', bottom: 12, right: 12, zIndex: 1000 }}
16
+ >
17
+ {t('demoBadge')}
18
+ </Badge>
19
+ )
20
+ }
@@ -0,0 +1,14 @@
1
+ 'use client'
2
+
3
+ import { Alert } from '@mantine/core'
4
+ import { useTranslations } from 'next-intl'
5
+
6
+ /** Presentational banner gated by the demo-banner feature flag (analytics port). */
7
+ export function DemoBanner() {
8
+ const t = useTranslations('banner')
9
+ return (
10
+ <Alert variant="light" color="blue" radius={0} data-testid="demo-banner-flag">
11
+ {t('demo')}
12
+ </Alert>
13
+ )
14
+ }
@@ -0,0 +1,23 @@
1
+ import { Card, Container, Stack, Title } from '@mantine/core'
2
+ import type { ReactNode } from 'react'
3
+
4
+ /**
5
+ * The GENERIC blocking-gate resolution shell — the reusable UI half of the gate seam
6
+ * (packages/kit/src/core/gates.ts). Domain-agnostic: it lays out a gate's title and a content slot (the resolution
7
+ * flow itself). The worked example fills it with an agreement body + accept button
8
+ * (packages/kit/src/components/agreements/agreement-gate.tsx); a future rider (MFA enrollment, onboarding) supplies
9
+ * its own content here with no change to this shell. Router-agnostic and hook-free, so it renders
10
+ * identically in the real app and the static-demo twin (ADR-0006). Mirrors the AcceptInviteShell shape.
11
+ */
12
+ export function GateInterstitial({ title, children }: { title: string; children: ReactNode }) {
13
+ return (
14
+ <Container component="main" size="sm" py="xl" data-testid="gate-interstitial">
15
+ <Stack gap="md">
16
+ <Title order={1}>{title}</Title>
17
+ <Card withBorder padding="lg" radius="md">
18
+ {children}
19
+ </Card>
20
+ </Stack>
21
+ </Container>
22
+ )
23
+ }
@@ -0,0 +1,64 @@
1
+ 'use client'
2
+
3
+ import { Group, Stack, Text } from '@mantine/core'
4
+ import { useLocale, useTranslations } from 'next-intl'
5
+ import { formatWhen } from './simulator/format-when'
6
+
7
+ /** Presentational shape — the timeline entry as both the product route and the world view expose it. */
8
+ export interface JobTimelineItem {
9
+ status: string
10
+ at: string
11
+ message?: string | null
12
+ }
13
+
14
+ /**
15
+ * Reusable, router-agnostic status timeline: one line per transition (queued → running → …), each
16
+ * with a localized status label and the moment it happened. Colour is inherited (never set), so the
17
+ * same component reads correctly on the light product card AND inside the dark Simulator panel — the
18
+ * host controls the text colour, the muted timestamp just drops opacity on whatever that colour is.
19
+ */
20
+ export function JobTimeline({ timeline }: { timeline: JobTimelineItem[] }) {
21
+ const t = useTranslations('jobTimeline')
22
+ const locale = useLocale()
23
+
24
+ function statusLabel(status: string): string {
25
+ switch (status) {
26
+ case 'queued':
27
+ return t('statusQueued')
28
+ case 'running':
29
+ return t('statusRunning')
30
+ case 'completed':
31
+ return t('statusCompleted')
32
+ case 'failed':
33
+ return t('statusFailed')
34
+ default:
35
+ return status
36
+ }
37
+ }
38
+
39
+ return (
40
+ <Stack gap={2} data-testid="job-timeline">
41
+ {timeline.map((entry) => (
42
+ <Group
43
+ key={`${entry.status}-${entry.at}`}
44
+ gap={8}
45
+ wrap="nowrap"
46
+ data-testid={`job-timeline-entry-${entry.status}`}
47
+ style={{ color: 'inherit' }}
48
+ >
49
+ <Text size="xs" fw={600} style={{ color: 'inherit', flexShrink: 0 }}>
50
+ {statusLabel(entry.status)}
51
+ </Text>
52
+ <Text size="xs" style={{ color: 'inherit', opacity: 0.7, whiteSpace: 'nowrap' }}>
53
+ {formatWhen(entry.at, locale)}
54
+ </Text>
55
+ {entry.message ? (
56
+ <Text size="xs" truncate style={{ color: 'inherit', opacity: 0.7, minWidth: 0 }}>
57
+ {entry.message}
58
+ </Text>
59
+ ) : null}
60
+ </Group>
61
+ ))}
62
+ </Stack>
63
+ )
64
+ }
@@ -0,0 +1,120 @@
1
+ 'use client'
2
+
3
+ import { Box, Menu, ScrollArea, Stack, Text, UnstyledButton } from '@mantine/core'
4
+ import { useLocale, useTranslations } from 'next-intl'
5
+ import {
6
+ FRAMEWORK_NOTIFICATION_NAMESPACE,
7
+ type NotificationKind,
8
+ type NotificationPayload,
9
+ notificationCopy,
10
+ } from '../../core/notifications'
11
+ import { BellIcon } from '../auth/auth-icons'
12
+ import { formatWhen } from '../simulator/format-when'
13
+
14
+ export interface NotificationItem {
15
+ id: string
16
+ kind: NotificationKind
17
+ payload: NotificationPayload
18
+ readAt: string | null
19
+ createdAt: string
20
+ }
21
+
22
+ /**
23
+ * The header notification bell — router-agnostic (no fetch, no navigation): the glue owns the
24
+ * poll + the mark-read POST and passes data + callbacks. An unread pill sits on the bell (the
25
+ * simulator-panel count-pill pattern); opening the popover fires `onOpen` (the glue marks all read, so
26
+ * the badge clears on open — the recorded mark-all-on-open decision). Copy is rendered from each item's
27
+ * kind + payload via the SHARED core `notificationCopy` mapper, so the list, the emails, and the SMS can
28
+ * never drift. Reused verbatim by the static-demo twin.
29
+ */
30
+ export function NotificationBell({
31
+ items,
32
+ unread,
33
+ onOpen,
34
+ }: {
35
+ items: NotificationItem[]
36
+ unread: number
37
+ onOpen: () => void
38
+ }) {
39
+ const t = useTranslations('notifications')
40
+ // Root translator too: an APP-registered kind's copy lives in the app catalog, and the namespace to
41
+ // resolve it in comes from notificationCopy (core/notifications.ts).
42
+ const tRoot = useTranslations()
43
+ const locale = useLocale()
44
+
45
+ return (
46
+ <Menu position="bottom-end" width={360} onOpen={onOpen}>
47
+ <Menu.Target>
48
+ <UnstyledButton
49
+ aria-label={t('bellLabel', { count: unread })}
50
+ data-testid="notification-bell"
51
+ style={{ position: 'relative', display: 'inline-flex', alignItems: 'center' }}
52
+ >
53
+ <BellIcon />
54
+ {unread > 0 ? (
55
+ <Text
56
+ component="span"
57
+ size="xs"
58
+ fw={700}
59
+ data-testid="notification-unread-count"
60
+ style={{
61
+ position: 'absolute',
62
+ top: -6,
63
+ right: -8,
64
+ background: 'var(--mantine-color-red-9)',
65
+ color: 'var(--mantine-color-white)',
66
+ // The theme's largest radius step, not '50%': a percentage radius turns
67
+ // a two-digit count into an ellipse, while xl keeps a pill at any width.
68
+ borderRadius: 'var(--mantine-radius-xl)',
69
+ padding: '0 6px',
70
+ lineHeight: '16px',
71
+ }}
72
+ >
73
+ {unread}
74
+ </Text>
75
+ ) : null}
76
+ </UnstyledButton>
77
+ </Menu.Target>
78
+ <Menu.Dropdown>
79
+ <Menu.Label>{t('title')}</Menu.Label>
80
+ {items.length === 0 ? (
81
+ <Text c="gray.6" size="sm" px="sm" py="xs" data-testid="notification-empty">
82
+ {t('empty')}
83
+ </Text>
84
+ ) : (
85
+ <ScrollArea.Autosize mah={360} type="auto">
86
+ <Stack gap={0}>
87
+ {items.map((item) => {
88
+ const copy = notificationCopy(item.kind, item.payload)
89
+ const ns = copy.namespace ?? FRAMEWORK_NOTIFICATION_NAMESPACE
90
+ return (
91
+ <Box
92
+ key={item.id}
93
+ data-testid="notification-item"
94
+ px="sm"
95
+ py="xs"
96
+ style={{
97
+ borderTop: '1px solid var(--mantine-color-gray-2)',
98
+ background:
99
+ item.readAt === null ? 'var(--mantine-color-blue-0)' : undefined,
100
+ }}
101
+ >
102
+ <Text size="sm" fw={item.readAt === null ? 700 : 500}>
103
+ {tRoot(`${ns}.${copy.titleKey}`, copy.values)}
104
+ </Text>
105
+ <Text size="xs" c="gray.7">
106
+ {tRoot(`${ns}.${copy.bodyKey}`, copy.values)}
107
+ </Text>
108
+ <Text size="xs" c="gray.5">
109
+ {formatWhen(item.createdAt, locale)}
110
+ </Text>
111
+ </Box>
112
+ )
113
+ })}
114
+ </Stack>
115
+ </ScrollArea.Autosize>
116
+ )}
117
+ </Menu.Dropdown>
118
+ </Menu>
119
+ )
120
+ }
@@ -0,0 +1,112 @@
1
+ 'use client'
2
+
3
+ import { Card, Switch, Table, Text, Title } from '@mantine/core'
4
+ import { useTranslations } from 'next-intl'
5
+ import { useState } from 'react'
6
+ import {
7
+ NOTIFICATION_CHANNELS,
8
+ NOTIFICATION_KINDS,
9
+ type NotificationChannel,
10
+ type NotificationKind,
11
+ type NotificationPrefRow,
12
+ isChannelEnabled,
13
+ notificationNamespace,
14
+ } from '../../core/notifications'
15
+
16
+ /**
17
+ * i18n label key for a notification kind, in the `notifications` namespace.
18
+ *
19
+ * DERIVED, not enumerated. This was a `Record<NotificationKind, string>` literal — which cannot be
20
+ * written by the framework at all, because `NotificationKind` includes whatever kinds the APP
21
+ * registers through the seam, so the literal cannot enumerate the app kinds it does not know
22
+ * hard-coded into a framework file. The convention it already followed is a total function:
23
+ * `org.invited` → `kindOrgInvited`, `<app>.<event>` → `kindAppEvent`. Any registered
24
+ * kind now resolves without the framework knowing its name.
25
+ */
26
+ function kindLabelKey(kind: NotificationKind): string {
27
+ return kind.split(/[._-]/).reduce((key, part) => key + part.charAt(0).toUpperCase() + part.slice(1), 'kind')
28
+ }
29
+
30
+ /** Fully-qualified label path: the derived key, resolved in whichever CATALOG owns that kind's copy. */
31
+ function kindLabelPath(kind: NotificationKind): string {
32
+ return `${notificationNamespace(kind)}.${kindLabelKey(kind)}`
33
+ }
34
+
35
+ const CHANNEL_LABEL: Record<NotificationChannel, string> = {
36
+ in_app: 'channelInApp',
37
+ email: 'channelEmail',
38
+ sms: 'channelSms',
39
+ }
40
+
41
+ /**
42
+ * The notification-preferences grid — a kind × channel toggle matrix on the profile screen.
43
+ * Router-agnostic: the glue owns the GET/POST; this renders the current prefs and calls `onToggle`.
44
+ * OPT-OUT default: a switch is ON unless the user's stored prefs disable that (kind, channel) — the
45
+ * SHARED core resolver (isChannelEnabled) decides, so the UI and the fan-out agree by construction.
46
+ * Reused verbatim by the static-demo twin.
47
+ */
48
+ export function NotificationPrefsSection({
49
+ prefs,
50
+ onToggle,
51
+ }: {
52
+ prefs: NotificationPrefRow[]
53
+ onToggle: (kind: NotificationKind, channel: NotificationChannel, enabled: boolean) => Promise<void>
54
+ }) {
55
+ const t = useTranslations('notifications')
56
+ // Root translator for kind labels: an app-registered kind's label lives in the app's catalog.
57
+ const tRoot = useTranslations()
58
+ const [busy, setBusy] = useState<string | null>(null)
59
+
60
+ return (
61
+ <Card withBorder padding="lg" radius="md" data-testid="notification-prefs">
62
+ <Title order={2} size="h3" mb="xs">
63
+ {t('prefsSection')}
64
+ </Title>
65
+ <Text c="gray.7" size="sm" mb="md">
66
+ {t('prefsHint')}
67
+ </Text>
68
+ <Table>
69
+ <Table.Thead>
70
+ <Table.Tr>
71
+ <Table.Th>{t('prefsKindColumn')}</Table.Th>
72
+ {NOTIFICATION_CHANNELS.map((channel) => (
73
+ <Table.Th key={channel} ta="center">
74
+ {t(CHANNEL_LABEL[channel])}
75
+ </Table.Th>
76
+ ))}
77
+ </Table.Tr>
78
+ </Table.Thead>
79
+ <Table.Tbody>
80
+ {NOTIFICATION_KINDS.map((kind) => (
81
+ <Table.Tr key={kind}>
82
+ <Table.Td>{tRoot(kindLabelPath(kind))}</Table.Td>
83
+ {NOTIFICATION_CHANNELS.map((channel) => {
84
+ const key = `${kind}:${channel}`
85
+ return (
86
+ <Table.Td key={channel} ta="center">
87
+ <Switch
88
+ checked={isChannelEnabled(prefs, kind, channel)}
89
+ disabled={busy !== null}
90
+ aria-label={t('prefToggleAria', {
91
+ kind: tRoot(kindLabelPath(kind)),
92
+ channel: t(CHANNEL_LABEL[channel]),
93
+ })}
94
+ data-testid={`pref-${kind}-${channel}`}
95
+ onChange={(event) => {
96
+ setBusy(key)
97
+ void onToggle(kind, channel, event.currentTarget.checked).finally(() =>
98
+ setBusy(null),
99
+ )
100
+ }}
101
+ style={{ display: 'inline-block' }}
102
+ />
103
+ </Table.Td>
104
+ )
105
+ })}
106
+ </Table.Tr>
107
+ ))}
108
+ </Table.Tbody>
109
+ </Table>
110
+ </Card>
111
+ )
112
+ }
@@ -0,0 +1,197 @@
1
+ 'use client'
2
+
3
+ import { Badge, Button, Card, Container, Group, Select, Stack, Table, Text, TextInput, Title } from '@mantine/core'
4
+ import { useForm } from '@mantine/form'
5
+ import { useTranslations } from 'next-intl'
6
+ import type { ReactNode } from 'react'
7
+ import { useState } from 'react'
8
+
9
+ export interface OrgMember {
10
+ id: string
11
+ name: string | null
12
+ email: string
13
+ role: string
14
+ status: 'active' | 'invited'
15
+ }
16
+
17
+ /**
18
+ * Role → Badge hue. Restricted to dark-based hues whose `light` variant clears WCAG AA at this small
19
+ * bold size — yellow/orange/lime/green/teal land at ~2.7–4.4:1 (axe: color-contrast). Unknown roles
20
+ * fall back to gray.
21
+ */
22
+ const ROLE_COLORS: Record<string, string> = {
23
+ admin: 'grape',
24
+ staff: 'indigo',
25
+ member: 'blue',
26
+ guest: 'gray',
27
+ restricted: 'violet',
28
+ }
29
+
30
+ function RoleBadge({ role }: { role: string }) {
31
+ return (
32
+ <Badge color={ROLE_COLORS[role] ?? 'gray'} variant="light" radius="sm">
33
+ {role}
34
+ </Badge>
35
+ )
36
+ }
37
+
38
+ /**
39
+ * Lean take on Clerk's OrganizationProfile (ADR-0003): an active-members section and a separate
40
+ * pending-invitations section, plus the invite flow (exercises auth + email + db ports together).
41
+ */
42
+ export function OrgScreen({
43
+ orgName,
44
+ members,
45
+ roles,
46
+ canManage,
47
+ onInvite,
48
+ error,
49
+ children,
50
+ }: {
51
+ orgName: string
52
+ members: OrgMember[]
53
+ roles: string[]
54
+ canManage: boolean
55
+ onInvite: (invite: { email: string; role: string }) => Promise<void>
56
+ /** Already-localized failure message from the caller (e.g. duplicate invite) — see org-glue. */
57
+ error?: string | null
58
+ /** Admin-only extra cards rendered inside the same main landmark (e.g. webhook endpoints). */
59
+ children?: ReactNode
60
+ }) {
61
+ const t = useTranslations('org')
62
+ const [busy, setBusy] = useState(false)
63
+ const [sentTo, setSentTo] = useState<string | null>(null)
64
+ const form = useForm({
65
+ initialValues: { email: '', role: roles.includes('member') ? 'member' : roles[0] },
66
+ validate: { email: (value) => (/.+@.+\..+/.test(value) ? null : t('emailInvalid')) },
67
+ })
68
+
69
+ const active = members.filter((m) => m.status === 'active')
70
+ const pending = members.filter((m) => m.status === 'invited')
71
+
72
+ return (
73
+ <Container component="main" size="md" py="xl">
74
+ <Stack gap="lg">
75
+ <Title order={1}>{t('title', { org: orgName })}</Title>
76
+
77
+ <Card withBorder padding="lg" radius="md">
78
+ <Group justify="space-between" mb="sm">
79
+ <Title order={2} size="h3">
80
+ {t('membersHeading')}
81
+ </Title>
82
+ <Badge variant="default" radius="sm">
83
+ {active.length}
84
+ </Badge>
85
+ </Group>
86
+ <Table data-testid="member-table" verticalSpacing="sm">
87
+ <Table.Thead>
88
+ <Table.Tr>
89
+ <Table.Th>{t('colName')}</Table.Th>
90
+ <Table.Th>{t('colEmail')}</Table.Th>
91
+ <Table.Th>{t('colRole')}</Table.Th>
92
+ </Table.Tr>
93
+ </Table.Thead>
94
+ <Table.Tbody>
95
+ {active.map((member) => (
96
+ <Table.Tr key={member.id} data-testid={`member-${member.email}`}>
97
+ <Table.Td>{member.name ?? '—'}</Table.Td>
98
+ <Table.Td>{member.email}</Table.Td>
99
+ <Table.Td>
100
+ <RoleBadge role={member.role} />
101
+ </Table.Td>
102
+ </Table.Tr>
103
+ ))}
104
+ </Table.Tbody>
105
+ </Table>
106
+ </Card>
107
+
108
+ {pending.length > 0 && (
109
+ <Card withBorder padding="lg" radius="md">
110
+ <Title order={2} size="h3" mb="sm">
111
+ {t('pendingHeading')}
112
+ </Title>
113
+ <Table data-testid="pending-table" verticalSpacing="sm">
114
+ <Table.Thead>
115
+ <Table.Tr>
116
+ <Table.Th>{t('colEmail')}</Table.Th>
117
+ <Table.Th>{t('colRole')}</Table.Th>
118
+ <Table.Th>{t('colStatus')}</Table.Th>
119
+ </Table.Tr>
120
+ </Table.Thead>
121
+ <Table.Tbody>
122
+ {pending.map((member) => (
123
+ <Table.Tr key={member.id} data-testid={`invite-${member.email}`}>
124
+ <Table.Td>{member.email}</Table.Td>
125
+ <Table.Td>
126
+ <RoleBadge role={member.role} />
127
+ </Table.Td>
128
+ <Table.Td>
129
+ <Badge variant="default" radius="sm">
130
+ {t('statusInvited')}
131
+ </Badge>
132
+ </Table.Td>
133
+ </Table.Tr>
134
+ ))}
135
+ </Table.Tbody>
136
+ </Table>
137
+ </Card>
138
+ )}
139
+
140
+ {canManage && (
141
+ <Card withBorder padding="lg" radius="md">
142
+ <Title order={2} size="h3" mb="sm">
143
+ {t('inviteHeading')}
144
+ </Title>
145
+ <form
146
+ onSubmit={form.onSubmit((values) => {
147
+ setBusy(true)
148
+ setSentTo(null)
149
+ onInvite({ email: values.email, role: values.role ?? 'member' })
150
+ .then(() => {
151
+ setSentTo(values.email)
152
+ form.reset()
153
+ })
154
+ .catch(() => {
155
+ // the caller surfaced the failure through the `error` prop
156
+ })
157
+ .finally(() => setBusy(false))
158
+ })}
159
+ >
160
+ <Group align="end" gap="sm">
161
+ <TextInput
162
+ label={t('emailLabel')}
163
+ flex={1}
164
+ data-testid="invite-email"
165
+ {...form.getInputProps('email')}
166
+ />
167
+ <Select
168
+ label={t('roleLabel')}
169
+ data={roles}
170
+ w={160}
171
+ allowDeselect={false}
172
+ data-testid="invite-role"
173
+ {...form.getInputProps('role')}
174
+ />
175
+ <Button type="submit" loading={busy} data-testid="invite-submit">
176
+ {t('inviteButton')}
177
+ </Button>
178
+ </Group>
179
+ {sentTo ? (
180
+ <Text mt="sm" c="green.8" size="sm" data-testid="invite-sent">
181
+ {t('inviteSent', { email: sentTo })}
182
+ </Text>
183
+ ) : null}
184
+ {error ? (
185
+ <Text mt="sm" c="red.8" size="sm" data-testid="invite-error">
186
+ {error}
187
+ </Text>
188
+ ) : null}
189
+ </form>
190
+ </Card>
191
+ )}
192
+
193
+ {children}
194
+ </Stack>
195
+ </Container>
196
+ )
197
+ }