create-freakpunch 0.0.0-stage → 0.1.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/README.md +22 -2
- package/bin/create-freakpunch.mjs +227 -0
- package/files/README.md +15 -0
- package/files/app.config.ts +58 -0
- package/package.json +21 -4
- package/template/.dockerignore +17 -0
- package/template/.oxlintrc.json +9 -0
- package/template/AGENTS.md +6 -0
- package/template/BACKEND.md +86 -0
- package/template/CLAUDE.md +292 -0
- package/template/Dockerfile +19 -0
- package/template/_gitignore +26 -0
- package/template/components.json +27 -0
- package/template/design/COMPONENTS.md +145 -0
- package/template/design/DESIGN.md +212 -0
- package/template/design/PATTERNS.md +110 -0
- package/template/index.html +25 -0
- package/template/nginx.conf +32 -0
- package/template/package-lock.json +6484 -0
- package/template/package.json +51 -0
- package/template/public/favicon.svg +1 -0
- package/template/public/robots.txt +2 -0
- package/template/src/app.config.ts +61 -0
- package/template/src/app.roles.ts +24 -0
- package/template/src/components/ui/alert-dialog.tsx +181 -0
- package/template/src/components/ui/avatar.tsx +116 -0
- package/template/src/components/ui/badge.tsx +57 -0
- package/template/src/components/ui/breadcrumb.tsx +130 -0
- package/template/src/components/ui/button.tsx +58 -0
- package/template/src/components/ui/calendar.tsx +277 -0
- package/template/src/components/ui/card.tsx +99 -0
- package/template/src/components/ui/chart.tsx +370 -0
- package/template/src/components/ui/checkbox.tsx +28 -0
- package/template/src/components/ui/combobox.tsx +329 -0
- package/template/src/components/ui/command.tsx +191 -0
- package/template/src/components/ui/dialog.tsx +162 -0
- package/template/src/components/ui/dropdown-menu.tsx +271 -0
- package/template/src/components/ui/empty.tsx +103 -0
- package/template/src/components/ui/field.tsx +236 -0
- package/template/src/components/ui/input-group.tsx +263 -0
- package/template/src/components/ui/input.tsx +45 -0
- package/template/src/components/ui/kbd.tsx +31 -0
- package/template/src/components/ui/label.tsx +17 -0
- package/template/src/components/ui/pagination.tsx +134 -0
- package/template/src/components/ui/popover.tsx +87 -0
- package/template/src/components/ui/radio-group.tsx +35 -0
- package/template/src/components/ui/select.tsx +204 -0
- package/template/src/components/ui/separator.tsx +25 -0
- package/template/src/components/ui/sheet.tsx +139 -0
- package/template/src/components/ui/sidebar.tsx +741 -0
- package/template/src/components/ui/skeleton.tsx +13 -0
- package/template/src/components/ui/sonner.tsx +78 -0
- package/template/src/components/ui/spinner.tsx +42 -0
- package/template/src/components/ui/switch.tsx +29 -0
- package/template/src/components/ui/table.tsx +121 -0
- package/template/src/components/ui/tabs.tsx +191 -0
- package/template/src/components/ui/textarea.tsx +17 -0
- package/template/src/components/ui/toggle-group.tsx +94 -0
- package/template/src/components/ui/toggle.tsx +48 -0
- package/template/src/components/ui/tooltip.tsx +91 -0
- package/template/src/core/api/http.ts +76 -0
- package/template/src/core/api/mock.ts +88 -0
- package/template/src/core/api/rest.ts +67 -0
- package/template/src/core/api/types.ts +21 -0
- package/template/src/core/audit/audit.service.ts +125 -0
- package/template/src/core/audit/hooks.ts +19 -0
- package/template/src/core/auth/auth-store.ts +52 -0
- package/template/src/core/auth/auth.service.ts +75 -0
- package/template/src/core/auth/can.tsx +39 -0
- package/template/src/core/auth/dev-role-switcher.tsx +64 -0
- package/template/src/core/auth/permissions.ts +115 -0
- package/template/src/core/auth/require-auth.tsx +16 -0
- package/template/src/core/auth/require-permission.tsx +24 -0
- package/template/src/core/auth/roles.ts +9 -0
- package/template/src/core/auth/types.ts +20 -0
- package/template/src/core/auth/use-auth.ts +19 -0
- package/template/src/core/company/company.service.ts +92 -0
- package/template/src/core/company/hooks.ts +40 -0
- package/template/src/core/company/password-policy.ts +40 -0
- package/template/src/core/company/types.ts +46 -0
- package/template/src/core/components/button-link.tsx +14 -0
- package/template/src/core/components/confirm-dialog.tsx +116 -0
- package/template/src/core/components/data-table/data-table.tsx +297 -0
- package/template/src/core/components/data-table/features.ts +24 -0
- package/template/src/core/components/data-table/table-header-cell.tsx +13 -0
- package/template/src/core/components/data-table/toolbar.tsx +100 -0
- package/template/src/core/components/data-table/use-list-url-state.ts +108 -0
- package/template/src/core/components/date-picker.tsx +71 -0
- package/template/src/core/components/file-attachments.tsx +84 -0
- package/template/src/core/components/form-error.tsx +9 -0
- package/template/src/core/components/form-page.tsx +93 -0
- package/template/src/core/components/image-upload.tsx +86 -0
- package/template/src/core/components/page.tsx +74 -0
- package/template/src/core/components/phone-input.tsx +58 -0
- package/template/src/core/components/search-select.tsx +57 -0
- package/template/src/core/components/segmented.tsx +41 -0
- package/template/src/core/components/states.tsx +97 -0
- package/template/src/core/components/status-badge.tsx +26 -0
- package/template/src/core/components/user-avatar.tsx +23 -0
- package/template/src/core/config/runtime.ts +14 -0
- package/template/src/core/config/types.ts +36 -0
- package/template/src/core/i18n/i18n.ts +69 -0
- package/template/src/core/i18n/i18next.d.ts +12 -0
- package/template/src/core/i18n/keys.ts +37 -0
- package/template/src/core/i18n/language-switcher.tsx +45 -0
- package/template/src/core/i18n/types.ts +17 -0
- package/template/src/core/i18n/use-format.ts +31 -0
- package/template/src/core/layout/app-shell.tsx +50 -0
- package/template/src/core/layout/app-sidebar.tsx +268 -0
- package/template/src/core/layout/command-menu.tsx +117 -0
- package/template/src/core/layout/top-bar.tsx +100 -0
- package/template/src/core/lib/csv.ts +27 -0
- package/template/src/core/lib/format.ts +87 -0
- package/template/src/core/lib/phone.ts +50 -0
- package/template/src/core/lib/storage.ts +27 -0
- package/template/src/core/modules/lazy-page.ts +9 -0
- package/template/src/core/modules/navigation.ts +39 -0
- package/template/src/core/modules/types.ts +55 -0
- package/template/src/core/notifications/hooks.ts +84 -0
- package/template/src/core/notifications/notification-center.tsx +251 -0
- package/template/src/core/notifications/notifications.service.ts +237 -0
- package/template/src/core/notifications/types.ts +65 -0
- package/template/src/core/preferences/preferences-store.ts +70 -0
- package/template/src/core/theme/brand-context.tsx +53 -0
- package/template/src/core/theme/brand.ts +29 -0
- package/template/src/core/theme/theme-context.tsx +62 -0
- package/template/src/core/theme/theme-options.ts +8 -0
- package/template/src/core/theme/theme-toggle.tsx +45 -0
- package/template/src/core/users/hooks.ts +51 -0
- package/template/src/core/users/types.ts +37 -0
- package/template/src/core/users/users.mock.ts +24 -0
- package/template/src/core/users/users.service.ts +167 -0
- package/template/src/hooks/use-mobile.ts +19 -0
- package/template/src/index.css +321 -0
- package/template/src/lib/utils.ts +1 -0
- package/template/src/locales/en/account.ts +180 -0
- package/template/src/locales/en/auth.ts +17 -0
- package/template/src/locales/en/common.ts +150 -0
- package/template/src/locales/en/company.ts +378 -0
- package/template/src/locales/en/dashboard.ts +41 -0
- package/template/src/locales/en/errors.ts +18 -0
- package/template/src/locales/en/landing.ts +15 -0
- package/template/src/locales/en/notifications.ts +43 -0
- package/template/src/locales/en/settings.ts +25 -0
- package/template/src/locales/index.ts +58 -0
- package/template/src/locales/tr/account.ts +184 -0
- package/template/src/locales/tr/auth.ts +21 -0
- package/template/src/locales/tr/common.ts +153 -0
- package/template/src/locales/tr/company.ts +382 -0
- package/template/src/locales/tr/dashboard.ts +45 -0
- package/template/src/locales/tr/errors.ts +22 -0
- package/template/src/locales/tr/landing.ts +19 -0
- package/template/src/locales/tr/notifications.ts +47 -0
- package/template/src/locales/tr/settings.ts +29 -0
- package/template/src/main.tsx +16 -0
- package/template/src/modules/locales.ts +14 -0
- package/template/src/modules/products/api/products.mock.ts +46 -0
- package/template/src/modules/products/api/products.service.ts +84 -0
- package/template/src/modules/products/components/product-status.tsx +21 -0
- package/template/src/modules/products/hooks.ts +73 -0
- package/template/src/modules/products/index.ts +50 -0
- package/template/src/modules/products/locales/en.ts +78 -0
- package/template/src/modules/products/locales/tr.ts +82 -0
- package/template/src/modules/products/pages/product-detail-page.tsx +108 -0
- package/template/src/modules/products/pages/product-form-page.tsx +211 -0
- package/template/src/modules/products/pages/products-list-page.tsx +286 -0
- package/template/src/modules/products/types.ts +61 -0
- package/template/src/modules/registry.ts +19 -0
- package/template/src/pages/dashboard/dashboard-page.tsx +270 -0
- package/template/src/pages/dashboard/dashboard.service.ts +92 -0
- package/template/src/pages/landing-page.tsx +79 -0
- package/template/src/pages/login-page.tsx +140 -0
- package/template/src/pages/no-access-page.tsx +11 -0
- package/template/src/pages/not-found-page.tsx +62 -0
- package/template/src/pages/settings/account/account.service.ts +147 -0
- package/template/src/pages/settings/account/components/mock-qr.tsx +50 -0
- package/template/src/pages/settings/account/components/password-strength.tsx +38 -0
- package/template/src/pages/settings/account/components/two-factor-dialog.tsx +140 -0
- package/template/src/pages/settings/account/hooks.ts +73 -0
- package/template/src/pages/settings/account/notifications-page.tsx +103 -0
- package/template/src/pages/settings/account/preferences-page.tsx +157 -0
- package/template/src/pages/settings/account/profile-page.tsx +242 -0
- package/template/src/pages/settings/account/security-page.tsx +364 -0
- package/template/src/pages/settings/account/types.ts +37 -0
- package/template/src/pages/settings/company/audit-log-page.tsx +126 -0
- package/template/src/pages/settings/company/branding-page.tsx +278 -0
- package/template/src/pages/settings/company/components/invite-dialog.tsx +106 -0
- package/template/src/pages/settings/company/components/read-only-notice.tsx +13 -0
- package/template/src/pages/settings/company/danger-zone-page.tsx +127 -0
- package/template/src/pages/settings/company/general-page.tsx +232 -0
- package/template/src/pages/settings/company/integrations/api-keys-panel.tsx +170 -0
- package/template/src/pages/settings/company/integrations/hooks.ts +37 -0
- package/template/src/pages/settings/company/integrations/integrations-page.tsx +57 -0
- package/template/src/pages/settings/company/integrations/integrations.service.ts +173 -0
- package/template/src/pages/settings/company/integrations/settings-forms.tsx +299 -0
- package/template/src/pages/settings/company/integrations/webhooks-panel.tsx +179 -0
- package/template/src/pages/settings/company/modules-page.tsx +57 -0
- package/template/src/pages/settings/company/roles-page.tsx +115 -0
- package/template/src/pages/settings/company/security-policies-page.tsx +164 -0
- package/template/src/pages/settings/company/users-page.tsx +262 -0
- package/template/src/pages/settings/settings-layout.tsx +57 -0
- package/template/src/pages/settings/settings-nav.ts +69 -0
- package/template/src/pages/settings/settings-routes.tsx +49 -0
- package/template/src/providers.tsx +68 -0
- package/template/src/router.tsx +54 -0
- package/template/tools/check-design.mjs +73 -0
- package/template/tools/new-module.mjs +90 -0
- package/template/tools/templates/module/api/__id__.mock.ts.tpl +17 -0
- package/template/tools/templates/module/api/__id__.service.ts.tpl +51 -0
- package/template/tools/templates/module/hooks.ts.tpl +57 -0
- package/template/tools/templates/module/index.ts.tpl +37 -0
- package/template/tools/templates/module/locales/en.ts.tpl +45 -0
- package/template/tools/templates/module/locales/tr.ts.tpl +49 -0
- package/template/tools/templates/module/pages/__id__-form-page.tsx.tpl +121 -0
- package/template/tools/templates/module/pages/__id__-list-page.tsx.tpl +237 -0
- package/template/tools/templates/module/types.ts.tpl +32 -0
- package/template/tsconfig.app.json +32 -0
- package/template/tsconfig.json +12 -0
- package/template/tsconfig.node.json +23 -0
- package/template/vite.config.ts +44 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { ApiError } from '@/core/api/http'
|
|
2
|
+
import { createMockDocument, delay, seededRandom } from '@/core/api/mock'
|
|
3
|
+
import { authStore } from '@/core/auth/auth-store'
|
|
4
|
+
import {
|
|
5
|
+
type ActiveSession,
|
|
6
|
+
type LoginEvent,
|
|
7
|
+
type PasswordChange,
|
|
8
|
+
type SecurityOverview,
|
|
9
|
+
type TwoFactorSetup,
|
|
10
|
+
} from './types'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The signed-in user's own security settings (notification settings live in
|
|
14
|
+
* `core/notifications`). A real
|
|
15
|
+
* backend implements this with `http` (e.g. PUT /me/password,
|
|
16
|
+
* POST /me/2fa/setup, GET /me/sessions); screens stay the same.
|
|
17
|
+
*/
|
|
18
|
+
export interface AccountService {
|
|
19
|
+
security(): Promise<SecurityOverview>
|
|
20
|
+
changePassword(input: PasswordChange): Promise<void>
|
|
21
|
+
startTwoFactorSetup(): Promise<TwoFactorSetup>
|
|
22
|
+
confirmTwoFactor(code: string): Promise<{ recoveryCodes: string[] }>
|
|
23
|
+
disableTwoFactor(): Promise<void>
|
|
24
|
+
sessions(): Promise<ActiveSession[]>
|
|
25
|
+
revokeSession(id: string): Promise<void>
|
|
26
|
+
revokeOtherSessions(): Promise<void>
|
|
27
|
+
loginHistory(): Promise<LoginEvent[]>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Error codes in ApiError.message; screens map them to translations. */
|
|
31
|
+
export const accountErrors = { wrongPassword: 'wrong_password', invalidCode: 'invalid_code' } as const
|
|
32
|
+
|
|
33
|
+
type AccountDoc = {
|
|
34
|
+
security: SecurityOverview
|
|
35
|
+
sessions: ActiveSession[]
|
|
36
|
+
history: LoginEvent[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const hour = 3_600_000
|
|
40
|
+
|
|
41
|
+
function seed(userId: string): AccountDoc {
|
|
42
|
+
const now = Date.now()
|
|
43
|
+
const rand = seededRandom(userId.length * 7919 + userId.charCodeAt(userId.length - 1))
|
|
44
|
+
const at = (hoursAgo: number) => new Date(now - hoursAgo * hour).toISOString()
|
|
45
|
+
const devices = [
|
|
46
|
+
{ browser: 'Chrome', os: 'macOS', kind: 'desktop', location: 'İstanbul, TR', ip: '88.241.12.40' },
|
|
47
|
+
{ browser: 'Safari', os: 'iOS', kind: 'mobile', location: 'İstanbul, TR', ip: '176.88.31.7' },
|
|
48
|
+
{ browser: 'Firefox', os: 'Windows', kind: 'desktop', location: 'Ankara, TR', ip: '78.180.5.122' },
|
|
49
|
+
{ browser: 'Edge', os: 'Windows', kind: 'desktop', location: 'Berlin, DE', ip: '91.64.200.18' },
|
|
50
|
+
] as const
|
|
51
|
+
return {
|
|
52
|
+
security: { passwordChangedAt: at(24 * 64), twoFactorEnabled: false },
|
|
53
|
+
sessions: devices.slice(0, 3).map((d, i) => ({
|
|
54
|
+
...d,
|
|
55
|
+
id: `s_${i + 1}`,
|
|
56
|
+
lastActiveAt: at(i === 0 ? 0 : i * 20 + rand() * 10),
|
|
57
|
+
current: i === 0,
|
|
58
|
+
})),
|
|
59
|
+
history: Array.from({ length: 8 }, (_, i) => {
|
|
60
|
+
const d = devices[Math.floor(rand() * devices.length)]!
|
|
61
|
+
return {
|
|
62
|
+
id: `l_${i + 1}`,
|
|
63
|
+
at: at(i * 19 + rand() * 6),
|
|
64
|
+
browser: d.browser,
|
|
65
|
+
os: d.os,
|
|
66
|
+
location: d.location,
|
|
67
|
+
ip: d.ip,
|
|
68
|
+
success: i !== 2 && i !== 5,
|
|
69
|
+
}
|
|
70
|
+
}),
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function createMockAccountService(): AccountService {
|
|
75
|
+
const docs = new Map<string, ReturnType<typeof createMockDocument<AccountDoc>>>()
|
|
76
|
+
const doc = () => {
|
|
77
|
+
const userId = authStore.getSession()?.user.id
|
|
78
|
+
if (!userId) throw new ApiError(401, 'unauthorized')
|
|
79
|
+
if (!docs.has(userId)) docs.set(userId, createMockDocument(`account:${userId}`, () => seed(userId)))
|
|
80
|
+
return docs.get(userId)!
|
|
81
|
+
}
|
|
82
|
+
const update = (patch: Partial<AccountDoc>) => doc().save({ ...doc().get(), ...patch })
|
|
83
|
+
let pendingSecret: string | null = null
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
async security() {
|
|
87
|
+
await delay(150, 300)
|
|
88
|
+
return doc().get().security
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
async changePassword({ current }) {
|
|
92
|
+
await delay(500, 800)
|
|
93
|
+
// Demo rule so the error path can be tried: "wrong" is never the current password.
|
|
94
|
+
if (current === 'wrong') throw new ApiError(400, accountErrors.wrongPassword)
|
|
95
|
+
update({ security: { ...doc().get().security, passwordChangedAt: new Date().toISOString() } })
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
async startTwoFactorSetup() {
|
|
99
|
+
await delay(200, 400)
|
|
100
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
|
|
101
|
+
pendingSecret = Array.from({ length: 32 }, () => alphabet[Math.floor(Math.random() * 32)]).join('')
|
|
102
|
+
const email = authStore.getSession()?.user.email ?? ''
|
|
103
|
+
return {
|
|
104
|
+
secret: pendingSecret.match(/.{4}/g)!.join(' '),
|
|
105
|
+
otpauthUrl: `otpauth://totp/freakpunch:${encodeURIComponent(email)}?secret=${pendingSecret}&issuer=freakpunch`,
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
async confirmTwoFactor(code) {
|
|
110
|
+
await delay(400, 700)
|
|
111
|
+
if (!pendingSecret || !/^\d{6}$/.test(code)) throw new ApiError(400, accountErrors.invalidCode)
|
|
112
|
+
pendingSecret = null
|
|
113
|
+
update({
|
|
114
|
+
security: { ...doc().get().security, twoFactorEnabled: true, twoFactorEnabledAt: new Date().toISOString() },
|
|
115
|
+
})
|
|
116
|
+
const chunk = () => Math.random().toString(36).slice(2, 7)
|
|
117
|
+
return { recoveryCodes: Array.from({ length: 8 }, () => `${chunk()}-${chunk()}`) }
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
async disableTwoFactor() {
|
|
121
|
+
await delay()
|
|
122
|
+
update({ security: { ...doc().get().security, twoFactorEnabled: false, twoFactorEnabledAt: undefined } })
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
async sessions() {
|
|
126
|
+
await delay(150, 350)
|
|
127
|
+
return doc().get().sessions
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
async revokeSession(id) {
|
|
131
|
+
await delay()
|
|
132
|
+
update({ sessions: doc().get().sessions.filter((s) => s.id !== id || s.current) })
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
async revokeOtherSessions() {
|
|
136
|
+
await delay()
|
|
137
|
+
update({ sessions: doc().get().sessions.filter((s) => s.current) })
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
async loginHistory() {
|
|
141
|
+
await delay(150, 350)
|
|
142
|
+
return doc().get().history
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export const accountService: AccountService = createMockAccountService()
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useMemo } from 'react'
|
|
2
|
+
|
|
3
|
+
import { seededRandom } from '@/core/api/mock'
|
|
4
|
+
|
|
5
|
+
const SIZE = 25
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A QR-looking placeholder derived from `value` (deterministic). A real
|
|
9
|
+
* backend returns the otpauth URL and a QR library or the API renders it.
|
|
10
|
+
*/
|
|
11
|
+
export function MockQr({ value, label, className }: { value: string; label: string; className?: string }) {
|
|
12
|
+
const cells = useMemo(() => {
|
|
13
|
+
let seed = 0
|
|
14
|
+
for (const ch of value) seed = (seed * 31 + ch.charCodeAt(0)) % 2147483647
|
|
15
|
+
const rand = seededRandom(seed || 1)
|
|
16
|
+
const finder = (x: number, y: number) =>
|
|
17
|
+
[
|
|
18
|
+
[0, 0],
|
|
19
|
+
[SIZE - 7, 0],
|
|
20
|
+
[0, SIZE - 7],
|
|
21
|
+
].some(([fx, fy]) => x >= fx! && x < fx! + 7 && y >= fy! && y < fy! + 7)
|
|
22
|
+
const out: [number, number][] = []
|
|
23
|
+
for (let y = 0; y < SIZE; y++) {
|
|
24
|
+
for (let x = 0; x < SIZE; x++) {
|
|
25
|
+
if (finder(x, y)) continue
|
|
26
|
+
if (rand() > 0.52) out.push([x, y])
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return out
|
|
30
|
+
}, [value])
|
|
31
|
+
|
|
32
|
+
const finderAt = (x: number, y: number) => (
|
|
33
|
+
<g key={`${x}-${y}`}>
|
|
34
|
+
<rect x={x} y={y} width={7} height={7} fill="currentColor" />
|
|
35
|
+
<rect x={x + 1} y={y + 1} width={5} height={5} className="fill-card" />
|
|
36
|
+
<rect x={x + 2} y={y + 2} width={3} height={3} fill="currentColor" />
|
|
37
|
+
</g>
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<svg viewBox={`-1 -1 ${SIZE + 2} ${SIZE + 2}`} role="img" aria-label={label} className={className} shapeRendering="crispEdges">
|
|
42
|
+
{cells.map(([x, y]) => (
|
|
43
|
+
<rect key={`${x}:${y}`} x={x} y={y} width={1} height={1} fill="currentColor" />
|
|
44
|
+
))}
|
|
45
|
+
{finderAt(0, 0)}
|
|
46
|
+
{finderAt(SIZE - 7, 0)}
|
|
47
|
+
{finderAt(0, SIZE - 7)}
|
|
48
|
+
</svg>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useTranslation } from 'react-i18next'
|
|
2
|
+
import { Check, Minus } from 'lucide-react'
|
|
3
|
+
|
|
4
|
+
import { checkPassword, passwordStrength, type PasswordPolicy } from '@/core/company/password-policy'
|
|
5
|
+
import { cn } from '@/lib/utils'
|
|
6
|
+
|
|
7
|
+
const barColor = ['bg-danger', 'bg-danger', 'bg-warning', 'bg-success', 'bg-success'] as const
|
|
8
|
+
const levels = ['tooWeak', 'weak', 'fair', 'good', 'strong'] as const
|
|
9
|
+
|
|
10
|
+
/** Four-segment strength bar + the company's password rules, checked live. */
|
|
11
|
+
export function PasswordStrength({ password, policy }: { password: string; policy: PasswordPolicy }) {
|
|
12
|
+
const { t } = useTranslation('account')
|
|
13
|
+
const score = passwordStrength(password)
|
|
14
|
+
const { rules } = checkPassword(policy, password)
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<div className="flex flex-col gap-2.5">
|
|
18
|
+
<div className="flex items-center gap-3">
|
|
19
|
+
<div className="flex flex-1 gap-1" aria-hidden>
|
|
20
|
+
{[1, 2, 3, 4].map((i) => (
|
|
21
|
+
<span key={i} className={cn('h-1 flex-1 rounded-full', password && score >= i ? barColor[score] : 'bg-fill-3')} />
|
|
22
|
+
))}
|
|
23
|
+
</div>
|
|
24
|
+
<span className="w-20 text-right text-xs text-muted-foreground" aria-live="polite">
|
|
25
|
+
{password ? t(`security.password.strength.${levels[score]}`) : t('security.password.strength.label')}
|
|
26
|
+
</span>
|
|
27
|
+
</div>
|
|
28
|
+
<ul className="grid gap-1 text-xs sm:grid-cols-2">
|
|
29
|
+
{rules.map(({ rule, met }) => (
|
|
30
|
+
<li key={rule} className={cn('flex items-center gap-1.5', met ? 'text-foreground' : 'text-muted-foreground')}>
|
|
31
|
+
{met ? <Check className="size-3.5 text-success" /> : <Minus className="size-3.5" />}
|
|
32
|
+
{t(`security.password.rules.${rule}`, { count: policy.minLength })}
|
|
33
|
+
</li>
|
|
34
|
+
))}
|
|
35
|
+
</ul>
|
|
36
|
+
</div>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import { useTranslation } from 'react-i18next'
|
|
3
|
+
import { Copy } from 'lucide-react'
|
|
4
|
+
import { toast } from 'sonner'
|
|
5
|
+
|
|
6
|
+
import { Button } from '@/components/ui/button'
|
|
7
|
+
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
|
8
|
+
import { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field'
|
|
9
|
+
import { Input } from '@/components/ui/input'
|
|
10
|
+
import { Spinner } from '@/components/ui/spinner'
|
|
11
|
+
import { LoadingState } from '@/core/components/states'
|
|
12
|
+
import { useConfirmTwoFactor, useTwoFactorSetup } from '../hooks'
|
|
13
|
+
import { MockQr } from './mock-qr'
|
|
14
|
+
|
|
15
|
+
/** Two steps: scan + verify a code, then show recovery codes once. */
|
|
16
|
+
export function TwoFactorDialog({ open, onOpenChange }: { open: boolean; onOpenChange: (open: boolean) => void }) {
|
|
17
|
+
const { t } = useTranslation(['account', 'common'])
|
|
18
|
+
const setup = useTwoFactorSetup()
|
|
19
|
+
const confirm = useConfirmTwoFactor()
|
|
20
|
+
const [code, setCode] = useState('')
|
|
21
|
+
const [invalid, setInvalid] = useState(false)
|
|
22
|
+
const [recoveryCodes, setRecoveryCodes] = useState<string[] | null>(null)
|
|
23
|
+
|
|
24
|
+
const { mutate: start, reset: resetSetup } = setup
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (open) start()
|
|
27
|
+
}, [open, start])
|
|
28
|
+
|
|
29
|
+
function close() {
|
|
30
|
+
onOpenChange(false)
|
|
31
|
+
setCode('')
|
|
32
|
+
setInvalid(false)
|
|
33
|
+
setRecoveryCodes(null)
|
|
34
|
+
resetSetup()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function verify() {
|
|
38
|
+
if (!/^\d{6}$/.test(code)) return setInvalid(true)
|
|
39
|
+
try {
|
|
40
|
+
const result = await confirm.mutateAsync(code)
|
|
41
|
+
setRecoveryCodes(result.recoveryCodes)
|
|
42
|
+
toast.success(t('security.twoFactor.dialog.enabledToast'))
|
|
43
|
+
} catch {
|
|
44
|
+
setInvalid(true)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<Dialog open={open} onOpenChange={(next) => !next && close()}>
|
|
50
|
+
<DialogContent showCloseButton={false} className="sm:max-w-md">
|
|
51
|
+
<DialogHeader>
|
|
52
|
+
<DialogTitle>
|
|
53
|
+
{recoveryCodes ? t('security.twoFactor.dialog.recoveryTitle') : t('security.twoFactor.dialog.title')}
|
|
54
|
+
</DialogTitle>
|
|
55
|
+
<DialogDescription>
|
|
56
|
+
{recoveryCodes ? t('security.twoFactor.dialog.recoveryDescription') : t('security.twoFactor.dialog.scanStep')}
|
|
57
|
+
</DialogDescription>
|
|
58
|
+
</DialogHeader>
|
|
59
|
+
|
|
60
|
+
{recoveryCodes ? (
|
|
61
|
+
<ul className="grid grid-cols-2 gap-2 rounded-xl border bg-fill-1 p-3 text-sm tabular-nums">
|
|
62
|
+
{recoveryCodes.map((c) => (
|
|
63
|
+
<li key={c}>{c}</li>
|
|
64
|
+
))}
|
|
65
|
+
</ul>
|
|
66
|
+
) : !setup.data ? (
|
|
67
|
+
<LoadingState className="py-10" />
|
|
68
|
+
) : (
|
|
69
|
+
<form
|
|
70
|
+
id="two-factor-form"
|
|
71
|
+
className="flex flex-col gap-4"
|
|
72
|
+
onSubmit={(e) => {
|
|
73
|
+
e.preventDefault()
|
|
74
|
+
void verify()
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
<div className="flex flex-col items-center gap-3 sm:flex-row sm:items-start">
|
|
78
|
+
<div className="rounded-xl border bg-card p-2 text-foreground shadow-(--shadow-card)">
|
|
79
|
+
<MockQr value={setup.data.otpauthUrl} label={t('security.twoFactor.dialog.qrLabel')} className="size-36" />
|
|
80
|
+
</div>
|
|
81
|
+
<div className="min-w-0 text-xs text-muted-foreground">
|
|
82
|
+
<p>{t('security.twoFactor.dialog.manual')}</p>
|
|
83
|
+
<p className="mt-1.5 rounded-lg bg-fill-2 px-2 py-1.5 tabular-nums text-[11px] break-all text-foreground">
|
|
84
|
+
{setup.data.secret}
|
|
85
|
+
</p>
|
|
86
|
+
</div>
|
|
87
|
+
</div>
|
|
88
|
+
<Field data-invalid={invalid}>
|
|
89
|
+
<FieldLabel htmlFor="totp">{t('security.twoFactor.dialog.codeLabel')}</FieldLabel>
|
|
90
|
+
<Input
|
|
91
|
+
id="totp"
|
|
92
|
+
inputMode="numeric"
|
|
93
|
+
autoComplete="one-time-code"
|
|
94
|
+
maxLength={6}
|
|
95
|
+
autoFocus
|
|
96
|
+
value={code}
|
|
97
|
+
aria-invalid={invalid}
|
|
98
|
+
className="tabular-nums tracking-[0.3em]"
|
|
99
|
+
onChange={(e) => {
|
|
100
|
+
setCode(e.target.value.replace(/\D/g, '').slice(0, 6))
|
|
101
|
+
setInvalid(false)
|
|
102
|
+
}}
|
|
103
|
+
/>
|
|
104
|
+
<FieldDescription>{t('security.twoFactor.dialog.codeHint')}</FieldDescription>
|
|
105
|
+
{invalid && <FieldError>{t('security.twoFactor.dialog.codeInvalid')}</FieldError>}
|
|
106
|
+
</Field>
|
|
107
|
+
</form>
|
|
108
|
+
)}
|
|
109
|
+
|
|
110
|
+
<DialogFooter>
|
|
111
|
+
{recoveryCodes ? (
|
|
112
|
+
<>
|
|
113
|
+
<Button
|
|
114
|
+
variant="outline"
|
|
115
|
+
onClick={() => {
|
|
116
|
+
void navigator.clipboard?.writeText(recoveryCodes.join('\n'))
|
|
117
|
+
toast.success(t('common:actions.copied'))
|
|
118
|
+
}}
|
|
119
|
+
>
|
|
120
|
+
<Copy data-icon="inline-start" />
|
|
121
|
+
{t('security.twoFactor.dialog.copyCodes')}
|
|
122
|
+
</Button>
|
|
123
|
+
<Button onClick={close}>{t('security.twoFactor.dialog.done')}</Button>
|
|
124
|
+
</>
|
|
125
|
+
) : (
|
|
126
|
+
<>
|
|
127
|
+
<Button variant="ghost" onClick={close}>
|
|
128
|
+
{t('common:actions.cancel')}
|
|
129
|
+
</Button>
|
|
130
|
+
<Button type="submit" form="two-factor-form" disabled={!setup.data || confirm.isPending}>
|
|
131
|
+
{confirm.isPending && <Spinner data-icon="inline-start" />}
|
|
132
|
+
{t('security.twoFactor.dialog.verify')}
|
|
133
|
+
</Button>
|
|
134
|
+
</>
|
|
135
|
+
)}
|
|
136
|
+
</DialogFooter>
|
|
137
|
+
</DialogContent>
|
|
138
|
+
</Dialog>
|
|
139
|
+
)
|
|
140
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
2
|
+
|
|
3
|
+
import { useSession } from '@/core/auth/use-auth'
|
|
4
|
+
import { accountService } from './account.service'
|
|
5
|
+
import type { PasswordChange } from './types'
|
|
6
|
+
|
|
7
|
+
/** Keys are scoped to the signed-in user so switching users never shows stale data. */
|
|
8
|
+
export function useAccountKeys() {
|
|
9
|
+
const userId = useSession()?.user.id ?? 'anonymous'
|
|
10
|
+
const all = ['account', userId] as const
|
|
11
|
+
return {
|
|
12
|
+
all,
|
|
13
|
+
security: [...all, 'security'] as const,
|
|
14
|
+
sessions: [...all, 'sessions'] as const,
|
|
15
|
+
history: [...all, 'history'] as const,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useSecurityOverview() {
|
|
20
|
+
const keys = useAccountKeys()
|
|
21
|
+
return useQuery({ queryKey: keys.security, queryFn: () => accountService.security() })
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function useChangePassword() {
|
|
25
|
+
const qc = useQueryClient()
|
|
26
|
+
const keys = useAccountKeys()
|
|
27
|
+
return useMutation({
|
|
28
|
+
mutationFn: (input: PasswordChange) => accountService.changePassword(input),
|
|
29
|
+
onSuccess: () => qc.invalidateQueries({ queryKey: keys.security }),
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function useTwoFactorSetup() {
|
|
34
|
+
return useMutation({ mutationFn: () => accountService.startTwoFactorSetup() })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function useConfirmTwoFactor() {
|
|
38
|
+
const qc = useQueryClient()
|
|
39
|
+
const keys = useAccountKeys()
|
|
40
|
+
return useMutation({
|
|
41
|
+
mutationFn: (code: string) => accountService.confirmTwoFactor(code),
|
|
42
|
+
onSuccess: () => qc.invalidateQueries({ queryKey: keys.security }),
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function useDisableTwoFactor() {
|
|
47
|
+
const qc = useQueryClient()
|
|
48
|
+
const keys = useAccountKeys()
|
|
49
|
+
return useMutation({
|
|
50
|
+
mutationFn: () => accountService.disableTwoFactor(),
|
|
51
|
+
onSuccess: () => qc.invalidateQueries({ queryKey: keys.security }),
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function useSessions() {
|
|
56
|
+
const keys = useAccountKeys()
|
|
57
|
+
return useQuery({ queryKey: keys.sessions, queryFn: () => accountService.sessions() })
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useRevokeSession() {
|
|
61
|
+
const qc = useQueryClient()
|
|
62
|
+
const keys = useAccountKeys()
|
|
63
|
+
return useMutation({
|
|
64
|
+
mutationFn: (id: string | 'others') =>
|
|
65
|
+
id === 'others' ? accountService.revokeOtherSessions() : accountService.revokeSession(id),
|
|
66
|
+
onSuccess: () => qc.invalidateQueries({ queryKey: keys.sessions }),
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function useLoginHistory() {
|
|
71
|
+
const keys = useAccountKeys()
|
|
72
|
+
return useQuery({ queryKey: keys.history, queryFn: () => accountService.loginHistory() })
|
|
73
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Fragment } from 'react'
|
|
2
|
+
import { Controller, useForm } from 'react-hook-form'
|
|
3
|
+
import { useTranslation } from 'react-i18next'
|
|
4
|
+
import { toast } from 'sonner'
|
|
5
|
+
|
|
6
|
+
import { Switch } from '@/components/ui/switch'
|
|
7
|
+
import { FormLayout } from '@/core/components/form-page'
|
|
8
|
+
import { Page } from '@/core/components/page'
|
|
9
|
+
import { ErrorState, LoadingState } from '@/core/components/states'
|
|
10
|
+
import { useNotificationSettings, useUpdateNotificationSettings } from '@/core/notifications/hooks'
|
|
11
|
+
import { notificationEvents, type NotificationGroup, type NotificationSettings } from '@/core/notifications/types'
|
|
12
|
+
|
|
13
|
+
const channels = ['email', 'inApp'] as const
|
|
14
|
+
const groups = [...new Set(notificationEvents.map((e) => e.group))] as NotificationGroup[]
|
|
15
|
+
|
|
16
|
+
export default function NotificationsPage() {
|
|
17
|
+
const { t } = useTranslation('account')
|
|
18
|
+
const query = useNotificationSettings()
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<Page title={t('notifications.title')} description={t('notifications.description')}>
|
|
22
|
+
{query.isPending ? (
|
|
23
|
+
<LoadingState />
|
|
24
|
+
) : query.error ? (
|
|
25
|
+
<ErrorState error={query.error} onRetry={() => query.refetch()} />
|
|
26
|
+
) : (
|
|
27
|
+
<NotificationsForm settings={query.data} />
|
|
28
|
+
)}
|
|
29
|
+
</Page>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function NotificationsForm({ settings }: { settings: NotificationSettings }) {
|
|
34
|
+
const { t } = useTranslation('account')
|
|
35
|
+
const update = useUpdateNotificationSettings()
|
|
36
|
+
const { control, handleSubmit, formState, reset } = useForm<NotificationSettings>({ defaultValues: settings })
|
|
37
|
+
|
|
38
|
+
const onSubmit = handleSubmit(async (values) => {
|
|
39
|
+
const saved = await update.mutateAsync(values)
|
|
40
|
+
reset(saved)
|
|
41
|
+
toast.success(t('notifications.saved'))
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<FormLayout
|
|
46
|
+
onSubmit={onSubmit}
|
|
47
|
+
saveBar="dirty"
|
|
48
|
+
isDirty={formState.isDirty}
|
|
49
|
+
isSubmitting={formState.isSubmitting}
|
|
50
|
+
onCancel={() => reset()}
|
|
51
|
+
>
|
|
52
|
+
<div role="table" aria-label={t('notifications.title')} className="flex flex-col">
|
|
53
|
+
<div role="row" className="grid grid-cols-[1fr_4rem_4rem] items-center gap-2 bg-fill-1 px-4 py-2.5 sm:grid-cols-[1fr_5rem_5rem] md:px-6">
|
|
54
|
+
<span role="columnheader" className="label-caps">
|
|
55
|
+
{t('notifications.channels.event')}
|
|
56
|
+
</span>
|
|
57
|
+
{channels.map((c) => (
|
|
58
|
+
<span key={c} role="columnheader" className="label-caps text-center">
|
|
59
|
+
{t(`notifications.channels.${c}`)}
|
|
60
|
+
</span>
|
|
61
|
+
))}
|
|
62
|
+
</div>
|
|
63
|
+
{groups.map((group) => (
|
|
64
|
+
<Fragment key={group}>
|
|
65
|
+
<div role="row" className="border-t px-4 pt-4 pb-1 md:px-6">
|
|
66
|
+
<span role="rowheader" className="label-caps">
|
|
67
|
+
{t(`notifications.groups.${group}`)}
|
|
68
|
+
</span>
|
|
69
|
+
</div>
|
|
70
|
+
{notificationEvents
|
|
71
|
+
.filter((e) => e.group === group)
|
|
72
|
+
.map(({ id }) => {
|
|
73
|
+
const label = t(`notifications.events.${id}.label`)
|
|
74
|
+
return (
|
|
75
|
+
<div key={id} role="row" className="grid grid-cols-[1fr_4rem_4rem] items-center gap-2 px-4 py-3 sm:grid-cols-[1fr_5rem_5rem] md:px-6">
|
|
76
|
+
<div role="cell" className="min-w-0">
|
|
77
|
+
<p className="text-sm font-medium">{label}</p>
|
|
78
|
+
<p className="text-xs text-muted-foreground">{t(`notifications.events.${id}.description`)}</p>
|
|
79
|
+
</div>
|
|
80
|
+
{channels.map((channel) => (
|
|
81
|
+
<div key={channel} role="cell" className="flex justify-center">
|
|
82
|
+
<Controller
|
|
83
|
+
control={control}
|
|
84
|
+
name={`${id}.${channel}`}
|
|
85
|
+
render={({ field }) => (
|
|
86
|
+
<Switch
|
|
87
|
+
checked={field.value}
|
|
88
|
+
onCheckedChange={field.onChange}
|
|
89
|
+
aria-label={t('notifications.toggle', { channel: t(`notifications.channels.${channel}`), event: label })}
|
|
90
|
+
/>
|
|
91
|
+
)}
|
|
92
|
+
/>
|
|
93
|
+
</div>
|
|
94
|
+
))}
|
|
95
|
+
</div>
|
|
96
|
+
)
|
|
97
|
+
})}
|
|
98
|
+
</Fragment>
|
|
99
|
+
))}
|
|
100
|
+
</div>
|
|
101
|
+
</FormLayout>
|
|
102
|
+
)
|
|
103
|
+
}
|