@alepha/ui 0.25.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 +12 -0
- package/components.json +20 -0
- package/package.json +85 -0
- package/src/components/action-error-toaster/action-error-toaster.tsx +70 -0
- package/src/components/admin/admin-audits.tsx +247 -0
- package/src/components/admin/admin-files.tsx +346 -0
- package/src/components/admin/admin-jobs-executions-panel.tsx +235 -0
- package/src/components/admin/admin-jobs-status-badge.tsx +33 -0
- package/src/components/admin/admin-jobs.tsx +294 -0
- package/src/components/admin/admin-keys.tsx +162 -0
- package/src/components/admin/admin-notifications.tsx +130 -0
- package/src/components/admin/admin-page.tsx +24 -0
- package/src/components/admin/admin-parameters.tsx +864 -0
- package/src/components/admin/admin-payments.tsx +93 -0
- package/src/components/admin/admin-sessions.tsx +174 -0
- package/src/components/admin/admin-user-detail-audits-tab.tsx +82 -0
- package/src/components/admin/admin-user-detail-identity-aside.tsx +218 -0
- package/src/components/admin/admin-user-detail-overview-tab.tsx +87 -0
- package/src/components/admin/admin-user-detail-password-dialog.tsx +85 -0
- package/src/components/admin/admin-user-detail-password-schema.ts +11 -0
- package/src/components/admin/admin-user-detail-profile-schema.ts +19 -0
- package/src/components/admin/admin-user-detail-provider-labels.ts +15 -0
- package/src/components/admin/admin-user-detail-security-tab.tsx +146 -0
- package/src/components/admin/admin-user-detail-sessions-tab.tsx +93 -0
- package/src/components/admin/admin-user-detail.tsx +606 -0
- package/src/components/admin/admin-users-roles-picker.tsx +126 -0
- package/src/components/admin/admin-users-status-filter.tsx +51 -0
- package/src/components/admin/admin-users.tsx +518 -0
- package/src/components/admin/diff-lines.spec.ts +58 -0
- package/src/components/admin/diff-lines.ts +58 -0
- package/src/components/admin/parameter-diff-dialog.tsx +119 -0
- package/src/components/admin/parameter-history-item.tsx +252 -0
- package/src/components/admin/parameter-json-dialog.tsx +93 -0
- package/src/components/admin/parameter-save-dialog.tsx +126 -0
- package/src/components/admin/use-confirmed-action.tsx +93 -0
- package/src/components/alepha-table/__tests__/use-table-selection.browser.spec.tsx +92 -0
- package/src/components/alepha-table/alepha-table.tsx +1158 -0
- package/src/components/alepha-table/use-table-selection.ts +95 -0
- package/src/components/app-shell/app-shell.tsx +568 -0
- package/src/components/auth/__tests__/auth-router.spec.ts +52 -0
- package/src/components/auth/auth-login.tsx +382 -0
- package/src/components/auth/auth-register.tsx +786 -0
- package/src/components/auth/auth-reset-password.tsx +349 -0
- package/src/components/auth/auth-router.tsx +126 -0
- package/src/components/auth/auth-verify-email.tsx +179 -0
- package/src/components/auto-form/auto-form.tsx +867 -0
- package/src/components/brand-icon/brand-icon.tsx +104 -0
- package/src/components/button-dark/button-dark.tsx +78 -0
- package/src/components/button-language/button-language.tsx +98 -0
- package/src/components/button-theme/button-theme.tsx +148 -0
- package/src/components/button-user/button-user.tsx +214 -0
- package/src/components/captcha/turnstile-widget.tsx +157 -0
- package/src/components/control/control.tsx +735 -0
- package/src/components/control-array/control-array.tsx +700 -0
- package/src/components/control-base/__tests__/form-field-a11y.browser.spec.tsx +93 -0
- package/src/components/control-base/field-i18n.ts +60 -0
- package/src/components/control-base/form-field.tsx +275 -0
- package/src/components/control-base/grid.tsx +59 -0
- package/src/components/control-base/icon-hint.tsx +64 -0
- package/src/components/control-date/control-date.tsx +190 -0
- package/src/components/control-number/control-number.tsx +136 -0
- package/src/components/control-object/control-object.tsx +200 -0
- package/src/components/control-select/control-select.tsx +774 -0
- package/src/components/control-upload/control-upload.tsx +475 -0
- package/src/components/file-image/file-image.tsx +52 -0
- package/src/components/language-toggle/language-toggle.tsx +66 -0
- package/src/components/markdown-view/markdown-view.css +114 -0
- package/src/components/markdown-view/markdown-view.tsx +92 -0
- package/src/components/nav-shell/nav-page.tsx +56 -0
- package/src/components/nav-shell/nav-shell.tsx +72 -0
- package/src/components/nav-shell/nav-tree-util.ts +62 -0
- package/src/components/nav-shell/spotlight.tsx +163 -0
- package/src/components/nav-shell/use-nav-breadcrumbs.ts +52 -0
- package/src/components/nav-shell/use-nav-entries.ts +101 -0
- package/src/components/nav-shell/use-nav-tree.ts +61 -0
- package/src/components/ui/accordion.tsx +77 -0
- package/src/components/ui/alert-dialog.tsx +186 -0
- package/src/components/ui/alert.tsx +75 -0
- package/src/components/ui/aspect-ratio.tsx +22 -0
- package/src/components/ui/avatar.tsx +106 -0
- package/src/components/ui/badge.tsx +51 -0
- package/src/components/ui/breadcrumb.tsx +121 -0
- package/src/components/ui/button-group.tsx +86 -0
- package/src/components/ui/button.tsx +83 -0
- package/src/components/ui/calendar.tsx +235 -0
- package/src/components/ui/card.tsx +102 -0
- package/src/components/ui/carousel.tsx +241 -0
- package/src/components/ui/chart.tsx +370 -0
- package/src/components/ui/checkbox.tsx +27 -0
- package/src/components/ui/collapsible.tsx +19 -0
- package/src/components/ui/combobox.tsx +298 -0
- package/src/components/ui/command.tsx +196 -0
- package/src/components/ui/context-menu.tsx +271 -0
- package/src/components/ui/dialog.tsx +153 -0
- package/src/components/ui/direction.tsx +4 -0
- package/src/components/ui/drawer.tsx +130 -0
- package/src/components/ui/dropdown-menu.tsx +269 -0
- package/src/components/ui/empty.tsx +100 -0
- package/src/components/ui/hover-card.tsx +50 -0
- package/src/components/ui/input-group.tsx +157 -0
- package/src/components/ui/input-otp.tsx +83 -0
- package/src/components/ui/input.tsx +19 -0
- package/src/components/ui/item.tsx +200 -0
- package/src/components/ui/kbd.tsx +26 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/menubar.tsx +283 -0
- package/src/components/ui/navigation-menu.tsx +170 -0
- package/src/components/ui/pagination.tsx +133 -0
- package/src/components/ui/popover.tsx +87 -0
- package/src/components/ui/progress.tsx +82 -0
- package/src/components/ui/radio-group.tsx +35 -0
- package/src/components/ui/resizable.tsx +49 -0
- package/src/components/ui/scroll-area.tsx +51 -0
- package/src/components/ui/segmented.tsx +232 -0
- package/src/components/ui/select.tsx +201 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/sheet.tsx +133 -0
- package/src/components/ui/sidebar.tsx +723 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/slider.tsx +51 -0
- package/src/components/ui/sonner.tsx +46 -0
- package/src/components/ui/spinner.tsx +15 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +115 -0
- package/src/components/ui/tabs.tsx +79 -0
- package/src/components/ui/textarea.tsx +17 -0
- package/src/components/ui/toggle-group.tsx +86 -0
- package/src/components/ui/toggle.tsx +44 -0
- package/src/components/ui/tooltip.tsx +63 -0
- package/src/components/use-dialog/use-dialog.tsx +244 -0
- package/src/components/use-toast/use-toast.tsx +89 -0
- package/src/hooks/use-mobile.ts +21 -0
- package/src/lib/__tests__/i18n-fr.spec.ts +72 -0
- package/src/lib/i18n-fr.ts +500 -0
- package/src/lib/utils.ts +6 -0
- package/src/styles.css +129 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
void React;
|
|
4
|
+
|
|
5
|
+
import "./markdown-view.css";
|
|
6
|
+
import ReactMarkdown from "react-markdown";
|
|
7
|
+
import rehypeHighlight from "rehype-highlight";
|
|
8
|
+
import remarkGfm from "remark-gfm";
|
|
9
|
+
|
|
10
|
+
export interface MarkdownViewProps {
|
|
11
|
+
content: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const MarkdownView = (props: MarkdownViewProps) => {
|
|
15
|
+
return (
|
|
16
|
+
<div className="max-w-none text-sm leading-relaxed">
|
|
17
|
+
<ReactMarkdown
|
|
18
|
+
remarkPlugins={[remarkGfm]}
|
|
19
|
+
rehypePlugins={[
|
|
20
|
+
[rehypeHighlight, { detect: true, ignoreMissing: true }],
|
|
21
|
+
]}
|
|
22
|
+
components={{
|
|
23
|
+
h1: ({ children }) => (
|
|
24
|
+
<h1 className="mt-6 mb-3 text-2xl font-semibold tracking-tight">
|
|
25
|
+
{children}
|
|
26
|
+
</h1>
|
|
27
|
+
),
|
|
28
|
+
h2: ({ children }) => (
|
|
29
|
+
<h2 className="mt-6 mb-2 text-xl font-semibold tracking-tight">
|
|
30
|
+
{children}
|
|
31
|
+
</h2>
|
|
32
|
+
),
|
|
33
|
+
h3: ({ children }) => (
|
|
34
|
+
<h3 className="mt-4 mb-2 text-base font-semibold">{children}</h3>
|
|
35
|
+
),
|
|
36
|
+
p: ({ children }) => <p className="my-3">{children}</p>,
|
|
37
|
+
ul: ({ children }) => (
|
|
38
|
+
<ul className="my-3 list-disc space-y-1 pl-5">{children}</ul>
|
|
39
|
+
),
|
|
40
|
+
ol: ({ children }) => (
|
|
41
|
+
<ol className="my-3 list-decimal space-y-1 pl-5">{children}</ol>
|
|
42
|
+
),
|
|
43
|
+
li: ({ children }) => <li className="pl-1">{children}</li>,
|
|
44
|
+
a: ({ href, children }) => (
|
|
45
|
+
<a
|
|
46
|
+
href={href}
|
|
47
|
+
className="text-primary underline decoration-primary/50 underline-offset-4 hover:decoration-primary"
|
|
48
|
+
>
|
|
49
|
+
{children}
|
|
50
|
+
</a>
|
|
51
|
+
),
|
|
52
|
+
code: ({ children, className }) => {
|
|
53
|
+
const isBlock =
|
|
54
|
+
className?.includes("language-") || className?.includes("hljs");
|
|
55
|
+
if (isBlock) {
|
|
56
|
+
return <code className={`${className} text-xs`}>{children}</code>;
|
|
57
|
+
}
|
|
58
|
+
return (
|
|
59
|
+
<code className="bg-muted rounded px-1.5 py-0.5 text-[0.85em] font-mono">
|
|
60
|
+
{children}
|
|
61
|
+
</code>
|
|
62
|
+
);
|
|
63
|
+
},
|
|
64
|
+
pre: ({ children }) => (
|
|
65
|
+
<pre className="border-border my-4 overflow-auto rounded-md border bg-transparent p-3 text-xs">
|
|
66
|
+
{children}
|
|
67
|
+
</pre>
|
|
68
|
+
),
|
|
69
|
+
blockquote: ({ children }) => (
|
|
70
|
+
<blockquote className="border-muted-foreground/30 my-3 border-l-2 pl-4 italic">
|
|
71
|
+
{children}
|
|
72
|
+
</blockquote>
|
|
73
|
+
),
|
|
74
|
+
hr: () => <hr className="border-border my-6" />,
|
|
75
|
+
table: ({ children }) => (
|
|
76
|
+
<table className="my-4 border-collapse text-xs">{children}</table>
|
|
77
|
+
),
|
|
78
|
+
th: ({ children }) => (
|
|
79
|
+
<th className="border-border border px-2 py-1 text-left font-medium">
|
|
80
|
+
{children}
|
|
81
|
+
</th>
|
|
82
|
+
),
|
|
83
|
+
td: ({ children }) => (
|
|
84
|
+
<td className="border-border border px-2 py-1">{children}</td>
|
|
85
|
+
),
|
|
86
|
+
}}
|
|
87
|
+
>
|
|
88
|
+
{props.content}
|
|
89
|
+
</ReactMarkdown>
|
|
90
|
+
</div>
|
|
91
|
+
);
|
|
92
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
$page,
|
|
3
|
+
type PageConfigSchema,
|
|
4
|
+
type PagePrimitive,
|
|
5
|
+
type PagePrimitiveOptions,
|
|
6
|
+
type TPropsDefault,
|
|
7
|
+
type TPropsParentDefault,
|
|
8
|
+
} from "alepha/react/router";
|
|
9
|
+
import { $secure } from "alepha/security";
|
|
10
|
+
|
|
11
|
+
export interface NavPageOptions<
|
|
12
|
+
TConfig extends PageConfigSchema = PageConfigSchema,
|
|
13
|
+
TProps extends object = TPropsDefault,
|
|
14
|
+
TPropsParent extends object = TPropsParentDefault,
|
|
15
|
+
> extends PagePrimitiveOptions<TConfig, TProps, TPropsParent> {
|
|
16
|
+
/**
|
|
17
|
+
* Permission(s) required for this page — wired into BOTH the route gate
|
|
18
|
+
* (`use: [$secure({ permissions })]`) and the nav-entry gate
|
|
19
|
+
* (`nav.permission`) so the two can never drift. A single string requires
|
|
20
|
+
* that permission; an array requires ALL of them (AND), matching `$secure`.
|
|
21
|
+
*
|
|
22
|
+
* For OR / custom logic use `can` instead. An explicit `nav.permission`
|
|
23
|
+
* still takes precedence over this when both are set.
|
|
24
|
+
*/
|
|
25
|
+
permission?: string | string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* `$page` sugar for shell pages: declares the page's `nav` metadata and its
|
|
30
|
+
* permission in one place. The single `permission` value feeds both the real
|
|
31
|
+
* route gate and the UI nav gate, eliminating the repeated permission string
|
|
32
|
+
* that the two would otherwise both need.
|
|
33
|
+
*
|
|
34
|
+
* Pages declared this way are picked up by {@link useNavTree} /
|
|
35
|
+
* {@link NavShell} purely from their `nav` field — no separate nav list.
|
|
36
|
+
*/
|
|
37
|
+
export const navPage = <
|
|
38
|
+
TConfig extends PageConfigSchema = PageConfigSchema,
|
|
39
|
+
TProps extends object = TPropsDefault,
|
|
40
|
+
TPropsParent extends object = TPropsParentDefault,
|
|
41
|
+
>(
|
|
42
|
+
options: NavPageOptions<TConfig, TProps, TPropsParent>,
|
|
43
|
+
): PagePrimitive<TConfig, TProps, TPropsParent> => {
|
|
44
|
+
const { permission, use, nav, ...rest } = options;
|
|
45
|
+
const permissions = permission
|
|
46
|
+
? Array.isArray(permission)
|
|
47
|
+
? permission
|
|
48
|
+
: [permission]
|
|
49
|
+
: undefined;
|
|
50
|
+
|
|
51
|
+
return $page<TConfig, TProps, TPropsParent>({
|
|
52
|
+
...rest,
|
|
53
|
+
use: permissions ? [$secure({ permissions }), ...(use ?? [])] : use,
|
|
54
|
+
nav: nav ? { ...nav, permission: nav.permission ?? permission } : nav,
|
|
55
|
+
});
|
|
56
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AppShell,
|
|
3
|
+
type AppShellProps,
|
|
4
|
+
type NavGroup,
|
|
5
|
+
type NavItem,
|
|
6
|
+
} from "@alepha/ui/components/app-shell/app-shell";
|
|
7
|
+
import { useRouterState } from "alepha/react/router";
|
|
8
|
+
import { useMemo } from "react";
|
|
9
|
+
import { isActivePath } from "./nav-tree-util.ts";
|
|
10
|
+
import { useNavBreadcrumbs } from "./use-nav-breadcrumbs.ts";
|
|
11
|
+
import { useNavTree } from "./use-nav-tree.ts";
|
|
12
|
+
|
|
13
|
+
export interface NavShellProps
|
|
14
|
+
extends Omit<AppShellProps, "nav" | "breadcrumbs"> {
|
|
15
|
+
/**
|
|
16
|
+
* Route name that anchors this shell — typically the layout `$page` mounted
|
|
17
|
+
* at the shell's base path (e.g. `/admin`). The sidebar and breadcrumbs are
|
|
18
|
+
* derived from this page's subtree, so an app can host several shells (a `/`
|
|
19
|
+
* app shell and a `/admin` shell), each scoped to its own routes.
|
|
20
|
+
*/
|
|
21
|
+
root: string;
|
|
22
|
+
/**
|
|
23
|
+
* Infer nav groups appended after the route-derived ones, for entries that
|
|
24
|
+
* don't map to routes (external links, grouped sub-menus). Their `active`
|
|
25
|
+
* state is computed from the current path (matching `href`), so callers only
|
|
26
|
+
* supply structure. Most shells won't need this.
|
|
27
|
+
*/
|
|
28
|
+
extraNav?: NavGroup[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Data-driven {@link AppShell}: the sidebar nav and breadcrumb trail are
|
|
33
|
+
* derived from the route tree under `root` (via each `$page`'s `nav`
|
|
34
|
+
* metadata), instead of hand-maintained lists. Everything else — brand,
|
|
35
|
+
* top-bar actions, layout variant, fill — passes straight through to
|
|
36
|
+
* `AppShell`.
|
|
37
|
+
*/
|
|
38
|
+
export const NavShell = (props: NavShellProps) => {
|
|
39
|
+
const { root, extraNav, ...appShellProps } = props;
|
|
40
|
+
const derived = useNavTree({ root });
|
|
41
|
+
const breadcrumbs = useNavBreadcrumbs({ root });
|
|
42
|
+
const state = useRouterState();
|
|
43
|
+
const current = state.url?.pathname ?? "/";
|
|
44
|
+
|
|
45
|
+
const nav = useMemo(() => {
|
|
46
|
+
if (!extraNav?.length) return derived;
|
|
47
|
+
return [
|
|
48
|
+
...derived,
|
|
49
|
+
...extraNav.map((group) => ({
|
|
50
|
+
...group,
|
|
51
|
+
items: markActive(group.items, current),
|
|
52
|
+
})),
|
|
53
|
+
];
|
|
54
|
+
}, [derived, extraNav, current]);
|
|
55
|
+
|
|
56
|
+
return <AppShell {...appShellProps} nav={nav} breadcrumbs={breadcrumbs} />;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Recursively set `active` on static nav items by matching their `href`
|
|
61
|
+
* against the current path (a parent group highlights when a descendant is
|
|
62
|
+
* active).
|
|
63
|
+
*/
|
|
64
|
+
function markActive(items: NavItem[], current: string): NavItem[] {
|
|
65
|
+
return items.map((item) => ({
|
|
66
|
+
...item,
|
|
67
|
+
active: item.href ? isActivePath(current, item.href) : item.active,
|
|
68
|
+
children: item.children
|
|
69
|
+
? markActive(item.children, current)
|
|
70
|
+
: item.children,
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { PageRoute } from "alepha/react/router";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Shared helpers for deriving navigation surfaces (sidebar, breadcrumbs,
|
|
6
|
+
* command palette) from the route tree. Each `$page` carries its own `nav`
|
|
7
|
+
* metadata, so there is no separate hand-maintained nav list to keep in sync
|
|
8
|
+
* — see {@link useNavTree} and {@link useNavBreadcrumbs}.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Resolve the display label for a page: `nav.label`, then the page `label`,
|
|
13
|
+
* then a static `head.title`, then the route name as a last resort.
|
|
14
|
+
*/
|
|
15
|
+
export function navLabel(page: PageRoute): ReactNode {
|
|
16
|
+
if (page.nav?.label != null) return page.nav.label;
|
|
17
|
+
if (page.label != null) return page.label;
|
|
18
|
+
const head = page.head;
|
|
19
|
+
// `head` may be a `(props) => Head` function — only a static object carries a
|
|
20
|
+
// usable title here (the nav builder has no props to call it with).
|
|
21
|
+
if (head && typeof head === "object" && "title" in head && head.title) {
|
|
22
|
+
return head.title;
|
|
23
|
+
}
|
|
24
|
+
return page.name;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Walk the `parent` chain to decide whether `page` is a (strict) descendant of
|
|
29
|
+
* the route named `root`. The root itself is excluded — it anchors the shell
|
|
30
|
+
* but is not a nav entry.
|
|
31
|
+
*/
|
|
32
|
+
export function isDescendantOf(page: PageRoute, root: string): boolean {
|
|
33
|
+
let parent = page.parent;
|
|
34
|
+
while (parent) {
|
|
35
|
+
if (parent.name === root) return true;
|
|
36
|
+
parent = parent.parent;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* AND-semantics permission probe matching `$secure({ permissions })`: a single
|
|
43
|
+
* string requires that one permission; an array requires ALL of them. No
|
|
44
|
+
* permission means "always visible".
|
|
45
|
+
*/
|
|
46
|
+
export function hasNavPermission(
|
|
47
|
+
permission: string | string[] | undefined,
|
|
48
|
+
has: (permission: string) => boolean,
|
|
49
|
+
): boolean {
|
|
50
|
+
if (!permission) return true;
|
|
51
|
+
const list = Array.isArray(permission) ? permission : [permission];
|
|
52
|
+
return list.every(has);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Whether `current` (the active pathname) is on or under `href`. Mirrors the
|
|
57
|
+
* legacy hand-rolled check — exact match, or a path segment below it — so the
|
|
58
|
+
* sidebar highlights `/admin/users` while viewing `/admin/users/:id`.
|
|
59
|
+
*/
|
|
60
|
+
export function isActivePath(current: string, href: string): boolean {
|
|
61
|
+
return current === href || current.startsWith(`${href}/`);
|
|
62
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Command,
|
|
3
|
+
CommandDialog,
|
|
4
|
+
CommandEmpty,
|
|
5
|
+
CommandGroup,
|
|
6
|
+
CommandInput,
|
|
7
|
+
CommandItem,
|
|
8
|
+
CommandList,
|
|
9
|
+
} from "@alepha/ui/components/ui/command";
|
|
10
|
+
import { useRouter } from "alepha/react/router";
|
|
11
|
+
import {
|
|
12
|
+
type ReactNode,
|
|
13
|
+
useCallback,
|
|
14
|
+
useEffect,
|
|
15
|
+
useMemo,
|
|
16
|
+
useState,
|
|
17
|
+
} from "react";
|
|
18
|
+
import { type NavEntry, useNavEntries } from "./use-nav-entries.ts";
|
|
19
|
+
|
|
20
|
+
export interface SpotlightProps {
|
|
21
|
+
/**
|
|
22
|
+
* Route name anchoring the palette — the same `root` passed to
|
|
23
|
+
* {@link NavShell}. The palette lists the navigable pages of that subtree,
|
|
24
|
+
* derived from each page's `nav` metadata (label / icon / group / keywords /
|
|
25
|
+
* description). One source for sidebar, breadcrumbs and palette.
|
|
26
|
+
*/
|
|
27
|
+
root: string;
|
|
28
|
+
/**
|
|
29
|
+
* Controlled open state. Omit to let the palette manage its own state (it
|
|
30
|
+
* still toggles on the keyboard shortcut).
|
|
31
|
+
*/
|
|
32
|
+
open?: boolean;
|
|
33
|
+
onOpenChange?: (open: boolean) => void;
|
|
34
|
+
/**
|
|
35
|
+
* Search input placeholder.
|
|
36
|
+
*/
|
|
37
|
+
placeholder?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Message shown when nothing matches the query.
|
|
40
|
+
*/
|
|
41
|
+
emptyMessage?: ReactNode;
|
|
42
|
+
/**
|
|
43
|
+
* Bind the global ⌘K / Ctrl+K shortcut to toggle the palette. Defaults to
|
|
44
|
+
* `true`.
|
|
45
|
+
*/
|
|
46
|
+
shortcut?: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Command palette over the navigation tree. Opens on ⌘K / Ctrl+K (or via
|
|
51
|
+
* controlled `open`), searches page labels / keywords / descriptions, and
|
|
52
|
+
* navigates on select. Reads the same {@link useNavEntries} source as the
|
|
53
|
+
* sidebar, so it never drifts from the routes.
|
|
54
|
+
*/
|
|
55
|
+
export const Spotlight = (props: SpotlightProps) => {
|
|
56
|
+
const { root, placeholder = "Search…", shortcut = true } = props;
|
|
57
|
+
const router = useRouter<any>();
|
|
58
|
+
const entries = useNavEntries({ root });
|
|
59
|
+
|
|
60
|
+
const isControlled = props.open !== undefined;
|
|
61
|
+
const [internalOpen, setInternalOpen] = useState(false);
|
|
62
|
+
const open = isControlled ? !!props.open : internalOpen;
|
|
63
|
+
|
|
64
|
+
const setOpen = useCallback(
|
|
65
|
+
(next: boolean) => {
|
|
66
|
+
if (!isControlled) setInternalOpen(next);
|
|
67
|
+
props.onOpenChange?.(next);
|
|
68
|
+
},
|
|
69
|
+
[isControlled, props.onOpenChange],
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (!shortcut) return;
|
|
74
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
75
|
+
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
|
|
76
|
+
event.preventDefault();
|
|
77
|
+
setOpen(!open);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
document.addEventListener("keydown", onKeyDown);
|
|
81
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
82
|
+
}, [shortcut, open, setOpen]);
|
|
83
|
+
|
|
84
|
+
// Bucket the already-sorted entries into their groups, preserving order.
|
|
85
|
+
const groups = useMemo(() => {
|
|
86
|
+
const out: { key: string; label?: string; items: NavEntry[] }[] = [];
|
|
87
|
+
const byKey = new Map<string, NavEntry[]>();
|
|
88
|
+
for (const entry of entries) {
|
|
89
|
+
const key = entry.group ?? "";
|
|
90
|
+
let items = byKey.get(key);
|
|
91
|
+
if (!items) {
|
|
92
|
+
items = [];
|
|
93
|
+
byKey.set(key, items);
|
|
94
|
+
out.push({ key, label: entry.group || undefined, items });
|
|
95
|
+
}
|
|
96
|
+
items.push(entry);
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
}, [entries]);
|
|
100
|
+
|
|
101
|
+
const onSelect = useCallback(
|
|
102
|
+
(entry: NavEntry) => {
|
|
103
|
+
if (entry.disabled) return;
|
|
104
|
+
setOpen(false);
|
|
105
|
+
router.push(entry.name);
|
|
106
|
+
},
|
|
107
|
+
[router, setOpen],
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<CommandDialog
|
|
112
|
+
open={open}
|
|
113
|
+
onOpenChange={setOpen}
|
|
114
|
+
title="Search"
|
|
115
|
+
description="Jump to a page"
|
|
116
|
+
>
|
|
117
|
+
<Command>
|
|
118
|
+
<CommandInput placeholder={placeholder} />
|
|
119
|
+
<CommandList>
|
|
120
|
+
<CommandEmpty>{props.emptyMessage ?? "No results."}</CommandEmpty>
|
|
121
|
+
{groups.map((group) => (
|
|
122
|
+
<CommandGroup key={group.key || "_ungrouped"} heading={group.label}>
|
|
123
|
+
{group.items.map((entry) => (
|
|
124
|
+
<CommandItem
|
|
125
|
+
key={entry.name}
|
|
126
|
+
// cmdk matches the query against `value` + `keywords`;
|
|
127
|
+
// include the human label plus any explicit synonyms.
|
|
128
|
+
value={`${entry.name} ${toText(entry.label)}`}
|
|
129
|
+
keywords={[
|
|
130
|
+
toText(entry.label),
|
|
131
|
+
toText(entry.description),
|
|
132
|
+
...(entry.keywords ?? []),
|
|
133
|
+
entry.group ?? "",
|
|
134
|
+
].filter(Boolean)}
|
|
135
|
+
disabled={entry.disabled}
|
|
136
|
+
onSelect={() => onSelect(entry)}
|
|
137
|
+
>
|
|
138
|
+
{entry.icon}
|
|
139
|
+
<span>{entry.label}</span>
|
|
140
|
+
{entry.description ? (
|
|
141
|
+
<span className="text-muted-foreground ml-2 truncate text-xs">
|
|
142
|
+
{entry.description}
|
|
143
|
+
</span>
|
|
144
|
+
) : null}
|
|
145
|
+
</CommandItem>
|
|
146
|
+
))}
|
|
147
|
+
</CommandGroup>
|
|
148
|
+
))}
|
|
149
|
+
</CommandList>
|
|
150
|
+
</Command>
|
|
151
|
+
</CommandDialog>
|
|
152
|
+
);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* cmdk filters on plain text; coerce a ReactNode label to a searchable string
|
|
157
|
+
* (component labels contribute nothing — callers should add `nav.keywords`).
|
|
158
|
+
*/
|
|
159
|
+
function toText(node: ReactNode): string {
|
|
160
|
+
return typeof node === "string" || typeof node === "number"
|
|
161
|
+
? String(node)
|
|
162
|
+
: "";
|
|
163
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useRouterState } from "alepha/react/router";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { navLabel } from "./nav-tree-util.ts";
|
|
5
|
+
|
|
6
|
+
export interface UseNavBreadcrumbsOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Route name anchoring the shell. The trail starts at this layer and walks
|
|
9
|
+
* down to the active leaf; ancestors above it (e.g. the global app layout)
|
|
10
|
+
* are dropped.
|
|
11
|
+
*/
|
|
12
|
+
root: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface NavCrumb {
|
|
16
|
+
label: ReactNode;
|
|
17
|
+
href?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Derive the breadcrumb trail from the active route layers. Each rendered
|
|
22
|
+
* layer that carries a route becomes a crumb (labelled via {@link navLabel});
|
|
23
|
+
* the last one is the current page and is rendered without a link.
|
|
24
|
+
*
|
|
25
|
+
* Because the trail is the genuine nested-route chain, a detail page mounted
|
|
26
|
+
* directly under the shell layout (rather than under its list page) yields
|
|
27
|
+
* `Shell > Detail` — there is no synthetic "list" crumb in between.
|
|
28
|
+
*/
|
|
29
|
+
export function useNavBreadcrumbs(
|
|
30
|
+
options: UseNavBreadcrumbsOptions,
|
|
31
|
+
): NavCrumb[] {
|
|
32
|
+
const state = useRouterState();
|
|
33
|
+
|
|
34
|
+
return useMemo(() => {
|
|
35
|
+
const layers = state.layers ?? [];
|
|
36
|
+
const rootIndex = layers.findIndex((l) => l.route?.name === options.root);
|
|
37
|
+
const chain = rootIndex >= 0 ? layers.slice(rootIndex) : layers;
|
|
38
|
+
|
|
39
|
+
const crumbs: NavCrumb[] = [];
|
|
40
|
+
for (const layer of chain) {
|
|
41
|
+
const route = layer.route;
|
|
42
|
+
if (!route) continue;
|
|
43
|
+
crumbs.push({ label: navLabel(route), href: route.match });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The current page is a label, not a link.
|
|
47
|
+
if (crumbs.length > 0) {
|
|
48
|
+
crumbs[crumbs.length - 1] = { label: crumbs[crumbs.length - 1].label };
|
|
49
|
+
}
|
|
50
|
+
return crumbs;
|
|
51
|
+
}, [state.layers, options.root]);
|
|
52
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { useAuth } from "alepha/react/auth";
|
|
2
|
+
import { useRouter, useRouterState } from "alepha/react/router";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { useMemo } from "react";
|
|
5
|
+
import {
|
|
6
|
+
hasNavPermission,
|
|
7
|
+
isActivePath,
|
|
8
|
+
isDescendantOf,
|
|
9
|
+
navLabel,
|
|
10
|
+
} from "./nav-tree-util.ts";
|
|
11
|
+
|
|
12
|
+
export interface UseNavEntriesOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Route name anchoring the shell — only descendant pages contribute entries.
|
|
15
|
+
* See {@link UseNavTreeOptions}.
|
|
16
|
+
*/
|
|
17
|
+
root: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A single resolved navigation entry derived from a `$page`'s `nav` metadata.
|
|
22
|
+
* Shared by the sidebar ({@link useNavTree}) and the command palette
|
|
23
|
+
* ({@link Spotlight}) so both stay in sync from one source.
|
|
24
|
+
*/
|
|
25
|
+
export interface NavEntry {
|
|
26
|
+
/** Route name — used for navigation (`router.push(name)`) and as a stable key. */
|
|
27
|
+
name: string;
|
|
28
|
+
/** Resolved pathname (the page's full `match`). */
|
|
29
|
+
href: string;
|
|
30
|
+
label: ReactNode;
|
|
31
|
+
icon?: ReactNode;
|
|
32
|
+
group?: string;
|
|
33
|
+
/** `nav.order` within the group. */
|
|
34
|
+
order: number;
|
|
35
|
+
/** Smallest `order` in this entry's group — drives group ordering. */
|
|
36
|
+
groupOrder: number;
|
|
37
|
+
keywords?: string[];
|
|
38
|
+
description?: ReactNode;
|
|
39
|
+
badge?: ReactNode;
|
|
40
|
+
/** `can()` returned `"disabled"` — show muted, block navigation. */
|
|
41
|
+
disabled: boolean;
|
|
42
|
+
/** The current route is on or under this entry. */
|
|
43
|
+
active: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Flat, filtered, sorted list of nav entries for the subtree under `root`.
|
|
48
|
+
* Filtering (hidden / permission / `can`), labelling and active-state matching
|
|
49
|
+
* are applied here; consumers decide how to present them. Entries are sorted by
|
|
50
|
+
* group order, then by item order.
|
|
51
|
+
*/
|
|
52
|
+
export function useNavEntries(options: UseNavEntriesOptions): NavEntry[] {
|
|
53
|
+
const router = useRouter<any>();
|
|
54
|
+
const state = useRouterState();
|
|
55
|
+
const { has } = useAuth();
|
|
56
|
+
const pages = router.pages;
|
|
57
|
+
const current = state.url?.pathname ?? "/";
|
|
58
|
+
|
|
59
|
+
return useMemo(() => {
|
|
60
|
+
const entries: NavEntry[] = [];
|
|
61
|
+
// Smallest order seen per group, so a group sorts by its earliest item.
|
|
62
|
+
const groupOrders = new Map<string, number>();
|
|
63
|
+
|
|
64
|
+
for (const page of pages) {
|
|
65
|
+
const nav = page.nav;
|
|
66
|
+
if (!nav || nav.hidden) continue;
|
|
67
|
+
if (!isDescendantOf(page, options.root)) continue;
|
|
68
|
+
if (!hasNavPermission(nav.permission, has)) continue;
|
|
69
|
+
|
|
70
|
+
// `can` escape hatch: `false` hides, `"disabled"` shows muted.
|
|
71
|
+
const canResult = page.can ? page.can({ has }) : true;
|
|
72
|
+
if (canResult === false) continue;
|
|
73
|
+
|
|
74
|
+
const order = nav.order ?? 0;
|
|
75
|
+
const key = nav.group ?? "";
|
|
76
|
+
const prev = groupOrders.get(key);
|
|
77
|
+
if (prev === undefined || order < prev) groupOrders.set(key, order);
|
|
78
|
+
|
|
79
|
+
entries.push({
|
|
80
|
+
name: page.name,
|
|
81
|
+
href: page.match,
|
|
82
|
+
label: navLabel(page),
|
|
83
|
+
icon: nav.icon,
|
|
84
|
+
group: nav.group,
|
|
85
|
+
order,
|
|
86
|
+
groupOrder: 0,
|
|
87
|
+
keywords: nav.keywords,
|
|
88
|
+
description: nav.description,
|
|
89
|
+
badge: nav.badge,
|
|
90
|
+
disabled: canResult === "disabled",
|
|
91
|
+
active: isActivePath(current, page.match),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const entry of entries) {
|
|
96
|
+
entry.groupOrder = groupOrders.get(entry.group ?? "") ?? 0;
|
|
97
|
+
}
|
|
98
|
+
entries.sort((a, b) => a.groupOrder - b.groupOrder || a.order - b.order);
|
|
99
|
+
return entries;
|
|
100
|
+
}, [pages, current, has, options.root]);
|
|
101
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NavGroup,
|
|
3
|
+
NavItem,
|
|
4
|
+
} from "@alepha/ui/components/app-shell/app-shell";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import { type NavEntry, useNavEntries } from "./use-nav-entries.ts";
|
|
7
|
+
|
|
8
|
+
export interface UseNavTreeOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Route name that anchors the shell (e.g. the `/admin` layout page). Only
|
|
11
|
+
* its descendant pages with `nav` metadata become entries — this is what
|
|
12
|
+
* lets an app host several shells (a `/` app shell and a `/admin` shell),
|
|
13
|
+
* each scoped to its own subtree.
|
|
14
|
+
*/
|
|
15
|
+
root: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Derive sidebar navigation groups from the route tree. Built on the shared
|
|
20
|
+
* {@link useNavEntries} (which handles filtering, permissions, `can`, labelling
|
|
21
|
+
* and active-state); this hook only buckets the already-sorted entries into
|
|
22
|
+
* `nav.group` sections for {@link AppShell}.
|
|
23
|
+
*
|
|
24
|
+
* This replaces the hand-synced nav list that used to live alongside the
|
|
25
|
+
* router and breadcrumb definitions.
|
|
26
|
+
*/
|
|
27
|
+
export function useNavTree(options: UseNavTreeOptions): NavGroup[] {
|
|
28
|
+
const entries = useNavEntries(options);
|
|
29
|
+
|
|
30
|
+
return useMemo(() => {
|
|
31
|
+
const groups: NavGroup[] = [];
|
|
32
|
+
const byKey = new Map<string, NavItem[]>();
|
|
33
|
+
|
|
34
|
+
for (const entry of entries) {
|
|
35
|
+
const key = entry.group ?? "";
|
|
36
|
+
let items = byKey.get(key);
|
|
37
|
+
if (!items) {
|
|
38
|
+
items = [];
|
|
39
|
+
byKey.set(key, items);
|
|
40
|
+
// Entries are pre-sorted by (groupOrder, order), so the first time we
|
|
41
|
+
// see a group it lands in the right position.
|
|
42
|
+
groups.push({ label: entry.group || undefined, items });
|
|
43
|
+
}
|
|
44
|
+
items.push(toNavItem(entry));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return groups;
|
|
48
|
+
}, [entries]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function toNavItem(entry: NavEntry): NavItem {
|
|
52
|
+
return {
|
|
53
|
+
label: entry.label,
|
|
54
|
+
href: entry.href,
|
|
55
|
+
icon: entry.icon,
|
|
56
|
+
badge: entry.badge,
|
|
57
|
+
tooltip: entry.description,
|
|
58
|
+
disabled: entry.disabled,
|
|
59
|
+
active: entry.active,
|
|
60
|
+
};
|
|
61
|
+
}
|