1 import { toPosix } from "./filename"
2
3 const ADMIN_FILE_RE = /(?:^|\/)src\/admin\//
4 const WIDGET_RE = /(?:^|\/)src\/admin\/widgets\/[^/]+\.(?:tsx|jsx)$/
5 const WIDGET_RE_NO_SRC = /(?:^|\/)admin\/widgets\/[^/]+\.(?:tsx|jsx)$/
6 const UI_ROUTE_RE = /(?:^|\/)src\/admin\/routes\/.+\/page\.(?:tsx|jsx)$/
7 const UI_ROUTE_RE_NO_SRC = /(?:^|\/)admin\/routes\/.+\/page\.(?:tsx|jsx)$/
8
9 /**
10 * True when `filename` lives anywhere under the admin dashboard tree
11 * (`src/admin/**`). Deliberately requires the `src/admin` segment pair so it
12 * does NOT match API admin routes (`src/api/admin/**`), which are server code
13 * where `process.env` is legitimate. Bails on synthetic / empty filenames.
14 */
15 export function isAdminFile(filename: string | undefined): boolean {
16 if (!filename || filename.startsWith("<")) {
17 return false
18 }
19 return ADMIN_FILE_RE.test(toPosix(filename))
20 }
21
22 /**
23 * True when `filename` is an admin widget file —
24 * `src/admin/widgets/<name>.tsx` (or `.jsx`) or the bare `admin/widgets/...`
25 * variant for nested project layouts.
26 */
27 export function isAdminWidgetFile(filename: string): boolean {
28 const posix = toPosix(filename)
29 return WIDGET_RE.test(posix) || WIDGET_RE_NO_SRC.test(posix)
30 }
31
32 /**
33 * True when `filename` is an admin UI route page —
34 * `src/admin/routes/**​/page.tsx` (or `.jsx`) or the bare `admin/routes/...`
35 * variant.
36 */
37 export function isAdminUiRoutePageFile(filename: string): boolean {
38 const posix = toPosix(filename)
39 return UI_ROUTE_RE.test(posix) || UI_ROUTE_RE_NO_SRC.test(posix)
40 }
41
42 /**
43 * True when `filename` hosts an admin component that the framework loads —
44 * a widget file or a UI route `page.tsx`. Bails on synthetic / empty
45 * filenames.
46 */
47 export function isAdminComponentFile(filename: string | undefined): boolean {
48 if (!filename || filename.startsWith("<")) {
49 return false
50 }
51 return isAdminWidgetFile(filename) || isAdminUiRoutePageFile(filename)
52 }
53
54 /**
55 * True when `filename` sits under a dynamic route segment — any path segment
56 * wrapped in brackets (`[id]`, `[...slug]`, `[[id]]`). Dynamic UI routes aren't
57 * added to the sidebar, so route `config` doesn't apply to them.
58 */
59 export function isDynamicUiRoutePath(filename: string | undefined): boolean {
60 if (!filename) {
61 return false
62 }
63 return toPosix(filename)
64 .split("/")
65 .some((segment) => /^\[.+\]$/.test(segment))
66 }