-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathrouter.tsx
More file actions
392 lines (351 loc) · 11.7 KB
/
router.tsx
File metadata and controls
392 lines (351 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import { lazy, Suspense, type ComponentType } from 'react'
import { createRouter, createRoute, createRootRoute, redirect } from '@tanstack/react-router'
import { Layout } from './layout'
import { getCurrentUser } from '@/api/client'
import { RoleGuard } from '@/shared/components/role-guard'
import { normalizeSearchQuery } from '@/shared/lib/search-query'
// Capture original URL before TanStack Router rewrites it
const ORIGINAL_URL_SEARCH = typeof window !== 'undefined' ? window.location.search : ''
// Export for use in cli-auth page
export { ORIGINAL_URL_SEARCH }
function createLazyRouteComponent<TModule extends Record<string, unknown>>(
importer: () => Promise<TModule>,
exportName: keyof TModule,
) {
const LazyComponent = lazy(async () => {
const module = await importer()
return { default: module[exportName] as ComponentType<any> }
})
return function LazyRouteComponent(props: Record<string, unknown>) {
return (
<Suspense
fallback={
<div className="flex min-h-[40vh] items-center justify-center text-sm text-muted-foreground">
Loading...
</div>
}
>
<LazyComponent {...props} />
</Suspense>
)
}
}
function createRoleProtectedRouteComponent<TModule extends Record<string, unknown>>(
importer: () => Promise<TModule>,
exportName: keyof TModule,
allowedRoles: readonly string[],
) {
const RouteComponent = createLazyRouteComponent(importer, exportName)
return function RoleProtectedRouteComponent(props: Record<string, unknown>) {
return (
<RoleGuard allowedRoles={allowedRoles}>
<RouteComponent {...props} />
</RoleGuard>
)
}
}
const LandingPage = createLazyRouteComponent(() => import('@/pages/landing'), 'LandingPage')
const HomePage = createLazyRouteComponent(() => import('@/pages/home'), 'HomePage')
const LoginPage = createLazyRouteComponent(() => import('@/pages/login'), 'LoginPage')
const RegisterPage = createLazyRouteComponent(() => import('@/pages/register'), 'RegisterPage')
const PrivacyPolicyPage = createLazyRouteComponent(() => import('@/pages/privacy'), 'PrivacyPolicyPage')
const SearchPage = createLazyRouteComponent(() => import('@/pages/search'), 'SearchPage')
const TermsOfServicePage = createLazyRouteComponent(() => import('@/pages/terms'), 'TermsOfServicePage')
const NamespacePage = createLazyRouteComponent(() => import('@/pages/namespace'), 'NamespacePage')
const SkillDetailPage = createLazyRouteComponent(() => import('@/pages/skill-detail'), 'SkillDetailPage')
const DashboardPage = createLazyRouteComponent(() => import('@/pages/dashboard'), 'DashboardPage')
const MySkillsPage = createLazyRouteComponent(() => import('@/pages/dashboard/my-skills'), 'MySkillsPage')
const PublishPage = createLazyRouteComponent(() => import('@/pages/dashboard/publish'), 'PublishPage')
const MyNamespacesPage = createLazyRouteComponent(
() => import('@/pages/dashboard/my-namespaces'),
'MyNamespacesPage',
)
const NamespaceMembersPage = createLazyRouteComponent(
() => import('@/pages/dashboard/namespace-members'),
'NamespaceMembersPage',
)
const NamespaceReviewsPage = createLazyRouteComponent(
() => import('@/pages/dashboard/namespace-reviews'),
'NamespaceReviewsPage',
)
const GovernancePage = createLazyRouteComponent(() => import('@/pages/dashboard/governance'), 'GovernancePage')
const ReviewsPage = createRoleProtectedRouteComponent(
() => import('@/pages/dashboard/reviews'),
'ReviewsPage',
['SKILL_ADMIN', 'NAMESPACE_ADMIN', 'SUPER_ADMIN'],
)
const ReportsPage = createRoleProtectedRouteComponent(
() => import('@/pages/dashboard/reports'),
'ReportsPage',
['SKILL_ADMIN', 'SUPER_ADMIN'],
)
const ReviewDetailPage = createRoleProtectedRouteComponent(
() => import('@/pages/dashboard/review-detail'),
'ReviewDetailPage',
['SKILL_ADMIN', 'NAMESPACE_ADMIN', 'SUPER_ADMIN'],
)
const PromotionsPage = createRoleProtectedRouteComponent(
() => import('@/pages/dashboard/promotions'),
'PromotionsPage',
['SKILL_ADMIN', 'SUPER_ADMIN'],
)
const MyStarsPage = createLazyRouteComponent(() => import('@/pages/dashboard/stars'), 'MyStarsPage')
const TokensPage = createLazyRouteComponent(() => import('@/pages/dashboard/tokens'), 'TokensPage')
const CliAuthPage = createLazyRouteComponent(() => import('@/pages/cli-auth'), 'CliAuthPage')
const SecuritySettingsPage = createLazyRouteComponent(
() => import('@/pages/settings/security'),
'SecuritySettingsPage',
)
const AdminUsersPage = createRoleProtectedRouteComponent(
() => import('@/pages/admin/users'),
'AdminUsersPage',
['USER_ADMIN', 'SUPER_ADMIN'],
)
const AuditLogPage = createRoleProtectedRouteComponent(
() => import('@/pages/admin/audit-log'),
'AuditLogPage',
['AUDITOR', 'SUPER_ADMIN'],
)
function DefaultNotFound() {
return (
<div className="flex min-h-[40vh] items-center justify-center text-sm text-muted-foreground">
Not Found
</div>
)
}
const rootRoute = createRootRoute({
component: Layout,
notFoundComponent: DefaultNotFound,
})
function buildReturnTo(location: { pathname: string; searchStr?: string; hash?: string }) {
return `${location.pathname}${location.searchStr ?? ''}${location.hash ?? ''}`
}
async function requireAuth({ location }: { location: { pathname: string; searchStr?: string; hash?: string } }) {
const user = await getCurrentUser()
if (!user) {
throw redirect({
to: '/login',
search: { returnTo: buildReturnTo(location) },
})
}
return { user }
}
const landingRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: LandingPage,
})
const skillsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'skills',
component: HomePage,
})
const loginRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'login',
validateSearch: (search: Record<string, unknown>): { returnTo: string; reason?: string } => ({
returnTo: typeof search.returnTo === 'string' ? search.returnTo : '',
reason: typeof search.reason === 'string' ? search.reason : undefined,
}),
component: LoginPage,
})
const registerRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'register',
validateSearch: (search: Record<string, unknown>) => ({
returnTo: typeof search.returnTo === 'string' ? search.returnTo : '',
}),
component: RegisterPage,
})
const privacyRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'privacy',
component: PrivacyPolicyPage,
})
const searchRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'search',
component: SearchPage,
validateSearch: (search: Record<string, unknown>) => {
return {
q: normalizeSearchQuery(typeof search.q === 'string' ? search.q : ''),
sort: (search.sort as string) || 'newest',
page: Number(search.page) || 0,
starredOnly: search.starredOnly === true || search.starredOnly === 'true',
}
},
})
const termsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'terms',
component: TermsOfServicePage,
})
const namespaceRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/space/$namespace',
component: NamespacePage,
})
const skillDetailRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/space/$namespace/$slug',
validateSearch: (search: Record<string, unknown>): { returnTo?: string } => ({
returnTo: typeof search.returnTo === 'string' && search.returnTo.startsWith('/') ? search.returnTo : undefined,
}),
component: SkillDetailPage,
})
const dashboardRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard',
beforeLoad: requireAuth,
component: DashboardPage,
})
const dashboardSkillsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/skills',
beforeLoad: requireAuth,
component: MySkillsPage,
})
const dashboardPublishRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/publish',
beforeLoad: requireAuth,
component: PublishPage,
})
const dashboardNamespacesRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/namespaces',
beforeLoad: requireAuth,
component: MyNamespacesPage,
})
const dashboardNamespaceMembersRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/namespaces/$slug/members',
beforeLoad: requireAuth,
component: NamespaceMembersPage,
})
const dashboardNamespaceReviewsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/namespaces/$slug/reviews',
beforeLoad: requireAuth,
component: NamespaceReviewsPage,
})
const dashboardGovernanceRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/governance',
beforeLoad: requireAuth,
component: GovernancePage,
})
const dashboardReviewsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/reviews',
beforeLoad: requireAuth,
component: ReviewsPage,
})
const dashboardReportsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/reports',
beforeLoad: requireAuth,
component: ReportsPage,
})
const dashboardReviewDetailRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/reviews/$id',
beforeLoad: requireAuth,
component: ReviewDetailPage,
})
const dashboardPromotionsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/promotions',
beforeLoad: requireAuth,
component: PromotionsPage,
})
const dashboardStarsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/stars',
beforeLoad: requireAuth,
component: MyStarsPage,
})
const dashboardTokensRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'dashboard/tokens',
beforeLoad: requireAuth,
component: TokensPage,
})
const cliAuthRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'cli/auth',
component: CliAuthPage,
validateSearch: (search: Record<string, unknown>): Record<string, string> => {
// Preserve all CLI auth parameters - use empty string instead of undefined to prevent TanStack Router from removing them
return {
redirect_uri: typeof search.redirect_uri === 'string' ? search.redirect_uri : '',
label_b64: typeof search.label_b64 === 'string' ? search.label_b64 : '',
label: typeof search.label === 'string' ? search.label : '',
state: typeof search.state === 'string' ? search.state : '',
}
},
})
const settingsSecurityRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'settings/security',
beforeLoad: requireAuth,
component: SecuritySettingsPage,
})
const settingsAccountsRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'settings/accounts',
beforeLoad: async (ctx) => {
await requireAuth(ctx)
throw redirect({ to: '/settings/security' })
},
})
const adminUsersRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'admin/users',
beforeLoad: requireAuth,
component: AdminUsersPage,
})
const adminAuditLogRoute = createRoute({
getParentRoute: () => rootRoute,
path: 'admin/audit-log',
beforeLoad: requireAuth,
component: AuditLogPage,
})
const routeTree = rootRoute.addChildren([
landingRoute,
skillsRoute,
loginRoute,
registerRoute,
privacyRoute,
searchRoute,
termsRoute,
namespaceRoute,
skillDetailRoute,
dashboardRoute,
dashboardSkillsRoute,
dashboardPublishRoute,
dashboardNamespacesRoute,
dashboardNamespaceMembersRoute,
dashboardNamespaceReviewsRoute,
dashboardGovernanceRoute,
dashboardReviewsRoute,
dashboardReportsRoute,
dashboardReviewDetailRoute,
dashboardPromotionsRoute,
dashboardStarsRoute,
dashboardTokensRoute,
cliAuthRoute,
settingsSecurityRoute,
settingsAccountsRoute,
adminUsersRoute,
adminAuditLogRoute,
])
export const router = createRouter({
routeTree,
defaultNotFoundComponent: DefaultNotFound,
})
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}