@larakit/react 0.0.0-stage → 0.5.1
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/package.json +30 -4
- package/src/Cell.tsx +35 -0
- package/src/Dashboard.tsx +79 -0
- package/src/Field.tsx +132 -0
- package/src/FileInput.tsx +76 -0
- package/src/Form.tsx +116 -0
- package/src/Layout.tsx +250 -0
- package/src/PanelPages.tsx +115 -0
- package/src/Repeater.tsx +46 -0
- package/src/ResourceIndex.tsx +271 -0
- package/src/action-upload.test.tsx +44 -0
- package/src/blocks.tsx +26 -0
- package/src/button-part.test.tsx +42 -0
- package/src/components.test.ts +22 -0
- package/src/components.tsx +435 -0
- package/src/confirm.test.tsx +68 -0
- package/src/context.tsx +19 -0
- package/src/custom-pages.test.tsx +177 -0
- package/src/errors.test.tsx +95 -0
- package/src/fixtures.ts +20 -0
- package/src/index.ts +29 -0
- package/src/kit/api.ts +305 -0
- package/src/kit/auth.tsx +109 -0
- package/src/kit/bridge.tsx +142 -0
- package/src/kit/button.tsx +398 -0
- package/src/kit/cell.tsx +260 -0
- package/src/kit/charts.tsx +638 -0
- package/src/kit/create.tsx +166 -0
- package/src/kit/dashboard.tsx +335 -0
- package/src/kit/dates.tsx +62 -0
- package/src/kit/entry.tsx +87 -0
- package/src/kit/field.tsx +449 -0
- package/src/kit/fields/CodeEditor.tsx +157 -0
- package/src/kit/fields/KeyValue.tsx +215 -0
- package/src/kit/fields/MarkdownEditor.tsx +380 -0
- package/src/kit/fields/OneTimeCode.tsx +192 -0
- package/src/kit/fields/Repeater.tsx +350 -0
- package/src/kit/fields/Slider.tsx +185 -0
- package/src/kit/fields/TagsInput.tsx +219 -0
- package/src/kit/filters.tsx +214 -0
- package/src/kit/form-shared.tsx +62 -0
- package/src/kit/form.tsx +253 -0
- package/src/kit/hooks.ts +119 -0
- package/src/kit/i18n.tsx +67 -0
- package/src/kit/icons.tsx +190 -0
- package/src/kit/infolist.tsx +452 -0
- package/src/kit/kit.test.tsx +2210 -0
- package/src/kit/layout.tsx +236 -0
- package/src/kit/loading.tsx +120 -0
- package/src/kit/notice.tsx +72 -0
- package/src/kit/page.tsx +103 -0
- package/src/kit/pageform.tsx +75 -0
- package/src/kit/picker.tsx +110 -0
- package/src/kit/plugins.tsx +206 -0
- package/src/kit/profile.tsx +86 -0
- package/src/kit/relations.tsx +146 -0
- package/src/kit/rich.tsx +276 -0
- package/src/kit/search.tsx +92 -0
- package/src/kit/select.tsx +244 -0
- package/src/kit/shell.tsx +401 -0
- package/src/kit/table-extras.tsx +457 -0
- package/src/kit/table.tsx +734 -0
- package/src/kit/theme.ts +57 -0
- package/src/layout-parts.test.tsx +159 -0
- package/src/pages/index.ts +7 -0
- package/src/part.test.tsx +56 -0
- package/src/plugins.test.tsx +159 -0
- package/src/plugins.tsx +240 -0
- package/src/preview.test.tsx +81 -0
- package/src/rail.test.tsx +56 -0
- package/src/repeater.test.tsx +48 -0
- package/src/resource-pages.test.tsx +119 -0
- package/src/screens.test.tsx +121 -0
- package/src/ssr.test.tsx +86 -0
- package/src/tenant.test.tsx +94 -0
- package/src/ui.tsx +100 -0
- package/theme.css +467 -0
- package/README.md +0 -3
package/src/Layout.tsx
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState, type MouseEvent, type ReactNode } from 'react';
|
|
2
|
+
import { Link, router, usePage } from '@inertiajs/react';
|
|
3
|
+
import type { Flash } from '@larakit/core';
|
|
4
|
+
import { type BrandProps, type NavProps, type PartOwn, type TenantSwitcherProps, type ToastProps, type TopbarProps } from './components';
|
|
5
|
+
import { LarakitProvider, type Larakit } from './context';
|
|
6
|
+
import { api, changed, fail, setLoadingDelay, setPanelPath, setPreviewRole, setTenant } from './kit/api';
|
|
7
|
+
import { Icon } from './kit/icons';
|
|
8
|
+
import { I18nProvider, setI18n, t, useT } from './kit/i18n';
|
|
9
|
+
import { complain, Notices, say } from './kit/notice';
|
|
10
|
+
import { Shell } from './kit/shell';
|
|
11
|
+
import { missingPlugins } from './plugins';
|
|
12
|
+
import { useTheme } from './kit/theme';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The panel around a page: the menu with its groups, badges and narrow form, the bar with the search, the bell,
|
|
16
|
+
* the links and the user's menu, the render hooks, and the messages after a save or an action. Every part of it
|
|
17
|
+
* can be the application's own component (LA-319).
|
|
18
|
+
*/
|
|
19
|
+
export function PanelLayout({ kit, current, title = '', children }: { kit: Larakit; current?: string; title?: string; children: ReactNode }) {
|
|
20
|
+
const { panel, path } = kit;
|
|
21
|
+
// The kit's requests and texts follow the panel the page is in. They are set while drawing, so a page drawn on
|
|
22
|
+
// the server has them too.
|
|
23
|
+
setPanelPath(path);
|
|
24
|
+
setI18n(panel.locale, panel.dir, panel.messages);
|
|
25
|
+
setLoadingDelay(panel.layout?.loadingDelay);
|
|
26
|
+
const theme = useTheme(panel.theme as never);
|
|
27
|
+
const badges = useBadges(panel);
|
|
28
|
+
const items = panel.menu.map((m) => ({
|
|
29
|
+
name: m.name, label: m.label, href: m.href, current: m.name === current,
|
|
30
|
+
icon: m.icon || undefined, group: m.group || undefined, badge: badges[m.name],
|
|
31
|
+
}));
|
|
32
|
+
const shell = useMemo(() => kitPanel(panel), [panel]);
|
|
33
|
+
useExpiredVisits(path);
|
|
34
|
+
// A plugin whose ui app.tsx doesn't pass to larakit({ plugins }) is named once in the console.
|
|
35
|
+
useEffect(() => { missingPlugins(panel.plugins); }, [panel.plugins]);
|
|
36
|
+
// A link or bookmark of the old panel opens the page at its real address, also a `#/...` link on this page.
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const go = () => { const to = hashAddress(location.hash, path); if (to) router.visit(to, { replace: true }); };
|
|
39
|
+
go();
|
|
40
|
+
addEventListener('hashchange', go);
|
|
41
|
+
return () => removeEventListener('hashchange', go);
|
|
42
|
+
}, [path]);
|
|
43
|
+
void title;
|
|
44
|
+
return (
|
|
45
|
+
<LarakitProvider value={kit}>
|
|
46
|
+
<I18nProvider locale={panel.locale} dir={panel.dir} messages={panel.messages}>
|
|
47
|
+
<div className="larakit" dir={panel.dir} lang={panel.locale} onClickCapture={follow(path)}>
|
|
48
|
+
<Shell panel={shell} theme={theme} items={items} name={current ?? ''} onTenantChange={chooseTenant}>
|
|
49
|
+
{panel.preview && <PreviewBar label={panel.preview.label} />}
|
|
50
|
+
{children}
|
|
51
|
+
</Shell>
|
|
52
|
+
<Notices />
|
|
53
|
+
<FlashMessage />
|
|
54
|
+
</div>
|
|
55
|
+
</I18nProvider>
|
|
56
|
+
</LarakitProvider>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A visit sent again after a 419, with a new token: a second 419 is a session that ended. */
|
|
61
|
+
const again = 'X-Larakit-Again';
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* An Inertia visit that a stale token turned away (419) is sent once more with a new one, as the kit's JSON requests
|
|
65
|
+
* are. When that is turned away too, the session has ended: the page stays and says so, instead of Inertia's modal
|
|
66
|
+
* with the framework's page. Every other answer that isn't a page is left to Inertia: the panel's own 403, 404 and
|
|
67
|
+
* 500 are pages (larakit/Error), and a failure in debug mode is the framework's page, which Inertia shows over this one.
|
|
68
|
+
*/
|
|
69
|
+
function useExpiredVisits(path: string) {
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
let last: (NonNullable<Parameters<typeof router.visit>[1]> & { url: URL }) | undefined;
|
|
72
|
+
const offBefore = router.on('before', (e) => { last = e.detail.visit as never; });
|
|
73
|
+
const offException = router.on('httpException', (e) => {
|
|
74
|
+
const { status, headers } = e.detail.response;
|
|
75
|
+
if (status !== 419 || headers['x-inertia']) return;
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
const visit = last;
|
|
78
|
+
if (!visit || visit.headers?.[again]) { complain(t('page.sessionExpired')); return; }
|
|
79
|
+
// Any answer of the panel brings a new token.
|
|
80
|
+
void fetch(path + '/api/panel', { headers: { Accept: 'application/json' } }).catch(() => undefined).then(() => {
|
|
81
|
+
const { url, ...options } = visit;
|
|
82
|
+
router.visit(url.pathname + url.search + url.hash, { ...options, headers: { ...visit.headers, [again]: '1' } });
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
return () => { offBefore(); offException(); };
|
|
86
|
+
}, [path]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Switches the tenant: the server keeps it in the session, then the page is visited again without keeping its state,
|
|
91
|
+
* so the list, the widgets and the record that it shows are all the new tenant's.
|
|
92
|
+
*/
|
|
93
|
+
async function chooseTenant(id: string) {
|
|
94
|
+
try {
|
|
95
|
+
await setTenant(id);
|
|
96
|
+
} catch (e) {
|
|
97
|
+
complain(fail(e));
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
router.visit(location.pathname + location.search, { preserveState: false, preserveScroll: true });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Where an address of the old panel (0.4 and before), which kept the page after `#`, is now: `/admin#/orders/7/edit`
|
|
105
|
+
* is `/admin/orders/7/edit`, `#/orders/new` is `/orders/create` and `#/orders/7/pages/history` is
|
|
106
|
+
* `/orders/7/history`. Anything else after `#`, such as `#main`, is not an old address: undefined. The old panel is gone,
|
|
107
|
+
* but bookmarks, mails and links in other apps that point into it still land on the page they meant.
|
|
108
|
+
*/
|
|
109
|
+
export function hashAddress(hash: string, path: string): string | undefined {
|
|
110
|
+
if (!hash.startsWith('#/')) return undefined;
|
|
111
|
+
const [where, query] = [hash.slice(1).split('?')[0], hash.includes('?') ? hash.slice(hash.indexOf('?')) : ''];
|
|
112
|
+
const parts = where.split('/').filter(Boolean);
|
|
113
|
+
if (parts.length === 2 && parts[1] === 'new') parts[1] = 'create';
|
|
114
|
+
if (parts.length === 4 && parts[2] === 'pages') parts.splice(2, 1);
|
|
115
|
+
return path + (parts.length ? '/' + parts.join('/') : '') + query;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Says whose eyes the panel is seen through while a role is previewed, with the way back. */
|
|
119
|
+
function PreviewBar({ label }: { label: string }) {
|
|
120
|
+
const t = useT();
|
|
121
|
+
return (
|
|
122
|
+
<div role="status" data-lk="preview-bar" className="callout tone-warning mb-4 flex flex-wrap items-center gap-3 border p-3 text-sm">
|
|
123
|
+
<Icon name="eye" className="size-4 shrink-0" /><span className="min-w-0 flex-1">{t('roles.previewing', { role: label })}</span>
|
|
124
|
+
<button type="button" data-lk="end-preview" className="btn" onClick={() => void setPreviewRole('')}>{t('roles.endPreview')}</button>
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The panel as the kit's shell reads it: its screens are the menu's, with their badges and search. */
|
|
130
|
+
export function kitPanel(panel: Larakit['panel']) {
|
|
131
|
+
const resources = panel.menu.filter((m) => m.name !== 'dashboard').map((m) => ({
|
|
132
|
+
name: m.name, label: m.label, icon: m.icon, group: m.group,
|
|
133
|
+
globalSearch: (m as { globalSearch?: boolean }).globalSearch, badge: (m as { badge?: boolean }).badge,
|
|
134
|
+
columns: [], fields: [], actions: [], view: true,
|
|
135
|
+
}));
|
|
136
|
+
return { ...panel, resources, pages: [], dashboard: [] } as never;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The numbers next to the screens of the menu that show one, read again after a change. */
|
|
140
|
+
function useBadges(panel: Larakit['panel']): Record<string, number> {
|
|
141
|
+
const [badges, setBadges] = useState<Record<string, number>>({});
|
|
142
|
+
const wanted = panel.menu.some((m) => (m as { badge?: boolean }).badge);
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
if (!wanted) return;
|
|
145
|
+
let ask = 0;
|
|
146
|
+
const read = () => { const n = ++ask; api<{ badges: Record<string, number> }>('/badges').then((b) => { if (n === ask) setBadges(b.badges ?? {}); }).catch(() => {}); };
|
|
147
|
+
read();
|
|
148
|
+
addEventListener(changed, read);
|
|
149
|
+
const off = router.on('success', read);
|
|
150
|
+
return () => { removeEventListener(changed, read); off(); };
|
|
151
|
+
}, [wanted, panel]);
|
|
152
|
+
return badges;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* A link to another page of the panel is an Inertia visit, so the layout stays. Files, exports, new tabs and
|
|
157
|
+
* other sites load as links do.
|
|
158
|
+
*/
|
|
159
|
+
function follow(path: string) {
|
|
160
|
+
return (e: MouseEvent<HTMLDivElement>) => {
|
|
161
|
+
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
|
|
162
|
+
const a = (e.target as HTMLElement).closest?.('a');
|
|
163
|
+
if (!a || !a.href || a.target || a.hasAttribute('download') || a.dataset.native !== undefined) return;
|
|
164
|
+
const to = new URL(a.href, location.href);
|
|
165
|
+
if (to.origin !== location.origin || !(to.pathname === path || to.pathname.startsWith(path + '/'))) return;
|
|
166
|
+
if (to.pathname.startsWith(path + '/api/') || to.pathname === path + '/logout') return;
|
|
167
|
+
if (to.pathname === location.pathname && to.search === location.search && to.hash) return;
|
|
168
|
+
e.preventDefault();
|
|
169
|
+
router.visit(to.pathname + to.search + to.hash);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
type Own<P> = PartOwn<P>;
|
|
174
|
+
|
|
175
|
+
/** Larakit's brand: the panel's logo, or its name. */
|
|
176
|
+
export function Brand({ name, logo, href }: Own<BrandProps>) {
|
|
177
|
+
return (
|
|
178
|
+
<Link href={href} data-lk="brand" className="mb-6 flex items-center gap-2 px-2 text-base font-semibold">
|
|
179
|
+
{logo ? <img src={logo} alt={name} className="h-7" /> : name}
|
|
180
|
+
</Link>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Larakit's menu of screens, under the headings of their groups. */
|
|
185
|
+
export function Nav({ items }: Own<NavProps>) {
|
|
186
|
+
const groups: { group: string; items: typeof items }[] = [];
|
|
187
|
+
for (const item of items) {
|
|
188
|
+
const last = groups[groups.length - 1];
|
|
189
|
+
if (last && last.group === (item.group ?? '')) last.items.push(item);
|
|
190
|
+
else groups.push({ group: item.group ?? '', items: [item] });
|
|
191
|
+
}
|
|
192
|
+
return (
|
|
193
|
+
<nav data-lk="nav" className="space-y-4">
|
|
194
|
+
{groups.map(({ group, items }, i) => (
|
|
195
|
+
<div key={group + i}>
|
|
196
|
+
{group && <p className="mb-1 px-2 text-xs font-medium text-(--lk-muted)">{group}</p>}
|
|
197
|
+
<ul className="space-y-0.5">
|
|
198
|
+
{items.map((item) => (
|
|
199
|
+
<li key={item.name}>
|
|
200
|
+
<Link href={item.href} aria-current={item.current ? 'page' : undefined} className={'block rounded-(--lk-radius) px-2 py-1.5 text-sm ' + (item.current ? 'bg-black/5 font-medium dark:bg-white/10' : 'text-(--lk-muted) hover:bg-black/5 dark:hover:bg-white/5')}>
|
|
201
|
+
{item.label}
|
|
202
|
+
</Link>
|
|
203
|
+
</li>
|
|
204
|
+
))}
|
|
205
|
+
</ul>
|
|
206
|
+
</div>
|
|
207
|
+
))}
|
|
208
|
+
</nav>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Larakit's choice of tenant: a select of the tenants the user can see. */
|
|
213
|
+
export function TenantSwitcher({ tenants, active, choose }: Own<TenantSwitcherProps>) {
|
|
214
|
+
return (
|
|
215
|
+
<select data-lk="tenant-switcher" aria-label="Tenant" value={active?.id ?? ''} onChange={(e) => { const t = tenants.find((x) => x.id === e.target.value); if (t) choose(t); }}
|
|
216
|
+
className="mb-4 block w-full rounded-(--lk-radius) border-0 bg-(--lk-surface) px-2 py-1.5 text-sm ring-1 ring-inset ring-(--lk-border)">
|
|
217
|
+
{tenants.map((t) => <option key={t.id} value={t.id}>{t.name}</option>)}
|
|
218
|
+
</select>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Larakit's bar above the page: who is signed in. */
|
|
223
|
+
export function Topbar({ user }: Own<TopbarProps>) {
|
|
224
|
+
return (
|
|
225
|
+
<div data-lk="topbar" className="flex h-12 items-center justify-end border-b border-(--lk-border) px-4 text-sm text-(--lk-muted) sm:px-8">
|
|
226
|
+
{user?.name}
|
|
227
|
+
</div>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Larakit's message after a save or an action. */
|
|
232
|
+
export function Toast({ type, message, close }: Own<ToastProps>) {
|
|
233
|
+
return (
|
|
234
|
+
<div role="status" data-lk="toast" className={'fixed right-4 bottom-4 z-50 flex items-center gap-3 rounded-(--lk-radius) px-4 py-2.5 text-sm text-white shadow-lg ' + (type === 'danger' ? 'bg-(--lk-danger)' : 'bg-(--lk-success)')}>
|
|
235
|
+
{message}
|
|
236
|
+
<button type="button" aria-label="Close" className="opacity-70 hover:opacity-100" onClick={close}>✕</button>
|
|
237
|
+
</div>
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** What the server flashed after a save or an action, as the panel's other messages show. */
|
|
242
|
+
function FlashMessage() {
|
|
243
|
+
const flash = (usePage() as unknown as { flash?: { larakit?: Flash } }).flash?.larakit;
|
|
244
|
+
useEffect(() => {
|
|
245
|
+
if (!flash?.message) return;
|
|
246
|
+
if (flash.type === 'danger') complain(flash.message);
|
|
247
|
+
else say(flash.message);
|
|
248
|
+
}, [flash]);
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { Head, router } from '@inertiajs/react';
|
|
3
|
+
import type { AuthPageProps as AuthProps, DashboardPageProps, ErrorPageProps, PanelPageProps, ProfilePageProps } from '@larakit/core';
|
|
4
|
+
import { LayoutPart, type ErrorProps, type PartOwn } from './components';
|
|
5
|
+
import { kitFor, useLarakit } from './context';
|
|
6
|
+
import { kitPanel, PanelLayout } from './Layout';
|
|
7
|
+
import type { MenuPage } from './kit/api';
|
|
8
|
+
import { AuthApp, type AuthInfo } from './kit/auth';
|
|
9
|
+
import { PageHeader } from './kit/button';
|
|
10
|
+
import { Dashboard as KitDashboard, PanelPage as KitPanelPage } from './kit/dashboard';
|
|
11
|
+
import { AppPage, PluginPage } from './kit/page';
|
|
12
|
+
import { ProfilePage } from './kit/profile';
|
|
13
|
+
|
|
14
|
+
/** The panel's start: its widgets, with the dates they cover when the panel has them. `GET {p}`. */
|
|
15
|
+
export function DashboardPage({ larakit, path, dashboard }: DashboardPageProps) {
|
|
16
|
+
const kit = useMemo(() => kitFor(larakit, path), [larakit, path]);
|
|
17
|
+
const panel = useMemo(() => ({ ...(kitPanel(larakit) as object), dashboard, dates: larakit.dates }) as never, [larakit, dashboard]);
|
|
18
|
+
return (
|
|
19
|
+
<PanelLayout kit={kit} current="dashboard">
|
|
20
|
+
<Head title={kit.t('nav.dashboard', 'Dashboard')} />
|
|
21
|
+
<KitDashboard panel={panel} />
|
|
22
|
+
</PanelLayout>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One of the panel's own pages, with its widgets, form and actions, or drawn by the application's component that
|
|
28
|
+
* the page names; a page of a record, with the way back to its list; or a plugin's page. `GET {p}/{page}`,
|
|
29
|
+
* `{p}/{screen}/{id}/{page}` and `{p}/{plugin}/{page}`.
|
|
30
|
+
*/
|
|
31
|
+
export function PanelPage({ larakit, path, page, at, back, data, rest }: PanelPageProps) {
|
|
32
|
+
const kit = useMemo(() => kitFor(larakit, path), [larakit, path]);
|
|
33
|
+
const shown = page as MenuPage & { type?: string; component?: string };
|
|
34
|
+
const panel = useMemo(() => kitPanel(larakit), [larakit]);
|
|
35
|
+
const plugin = at.startsWith('plugins/');
|
|
36
|
+
// A record's page is in the menu as its screen.
|
|
37
|
+
const current = back ? at.split('/')[0] : plugin ? shown.type : at;
|
|
38
|
+
return (
|
|
39
|
+
<PanelLayout kit={kit} current={current}>
|
|
40
|
+
<Head title={shown.label} />
|
|
41
|
+
{plugin
|
|
42
|
+
? <PluginPage page={shown as MenuPage & { type: string }} data={data} path={rest ?? ''} base={path} plugins={larakit.plugins} />
|
|
43
|
+
: shown.component && !back
|
|
44
|
+
? <AppPage key={at} page={shown as MenuPage & { component: string }} data={data} base={path} />
|
|
45
|
+
: <KitPanelPage key={at} page={shown} panel={panel} at={at} back={back} />}
|
|
46
|
+
</PanelLayout>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The signed-in user's own page: their name, address and password. `GET {p}/profile`. */
|
|
51
|
+
export function ProfileView({ larakit, path }: ProfilePageProps) {
|
|
52
|
+
const kit = useMemo(() => kitFor(larakit, path), [larakit, path]);
|
|
53
|
+
return (
|
|
54
|
+
<PanelLayout kit={kit} current="profile">
|
|
55
|
+
<Head title={kit.t('user.profile', 'Profile')} />
|
|
56
|
+
<PageHeader title={kit.t('user.profile', 'Profile')} />
|
|
57
|
+
<ProfilePage onSaved={() => router.reload({ only: ['larakit'] })} />
|
|
58
|
+
</PanelLayout>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A page of the panel that is forbidden, not found or failed, with the panel's menu around it. */
|
|
63
|
+
export function ErrorView({ larakit, path, status }: ErrorPageProps) {
|
|
64
|
+
const kit = useMemo(() => kitFor(larakit, path), [larakit, path]);
|
|
65
|
+
return (
|
|
66
|
+
<PanelLayout kit={kit}>
|
|
67
|
+
<ErrorBody status={status} />
|
|
68
|
+
</PanelLayout>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function ErrorBody({ status }: { status: number }) {
|
|
73
|
+
const { t, path } = useLarakit();
|
|
74
|
+
// A forbidden page reads as one that isn't there, so nobody learns what they may not open.
|
|
75
|
+
const failed = status >= 500;
|
|
76
|
+
const title = failed ? t('page.wentWrong', 'Something went wrong.') : t('page.notFound', 'Page not found');
|
|
77
|
+
const message = failed ? '' : t('page.notFoundBody', 'This page does not exist, or you may not open it.');
|
|
78
|
+
return (
|
|
79
|
+
<>
|
|
80
|
+
<Head title={title} />
|
|
81
|
+
<LayoutPart part="error" Default={DefaultError} props={{ status, title, message, home: path, retry: failed ? () => router.reload() : undefined }} />
|
|
82
|
+
</>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Larakit's own body of an error page: `part("error", ...)` replaces it. */
|
|
87
|
+
function DefaultError({ status, title, message, home, retry }: PartOwn<ErrorProps>) {
|
|
88
|
+
const { t } = useLarakit();
|
|
89
|
+
return (
|
|
90
|
+
<div data-lk="error-page" data-status={status} className="mx-auto max-w-md py-16 text-center">
|
|
91
|
+
<p className="muted text-sm font-semibold tabular-nums">{status}</p>
|
|
92
|
+
<h1 className="mt-2 text-2xl font-bold">{title}</h1>
|
|
93
|
+
{message && <p className="muted mt-2">{message}</p>}
|
|
94
|
+
<div className="mt-6 flex justify-center gap-2">
|
|
95
|
+
{retry && <button type="button" className="btn" onClick={retry}>{t('page.tryAgain', 'Try again')}</button>}
|
|
96
|
+
<a className="btn" href={home}>{t('page.goHome', 'Go to the start')}</a>
|
|
97
|
+
</div>
|
|
98
|
+
</div>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Larakit's sign-in page: `GET {p}/login`. */
|
|
103
|
+
export function LoginPage({ auth }: AuthProps) {
|
|
104
|
+
return <AuthApp info={auth as unknown as AuthInfo} kind="login" />;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Asks for a link to choose a new password: `GET {p}/forgot-password`. */
|
|
108
|
+
export function ForgotPasswordPage({ auth }: AuthProps) {
|
|
109
|
+
return <AuthApp info={auth as unknown as AuthInfo} kind="forgot" />;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Chooses a new password with the link's token: `GET {p}/reset-password/{token}?email=`. */
|
|
113
|
+
export function ResetPasswordPage({ auth, token, email }: AuthProps) {
|
|
114
|
+
return <AuthApp info={auth as unknown as AuthInfo} kind="reset" token={token} email={email} />;
|
|
115
|
+
}
|
package/src/Repeater.tsx
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { errorAt, holds, required, rows, starting, text } from '@larakit/core';
|
|
2
|
+
import { useLarakit } from './context';
|
|
3
|
+
import { Field, type ControlProps } from './Field';
|
|
4
|
+
import { Button } from './ui';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Larakit's repeater: a row of fields for each item. The server checks the rows and names an error by its place,
|
|
8
|
+
* `items.0.sku`, which Inertia gives back as it is; each row shows its own errors under its fields.
|
|
9
|
+
*/
|
|
10
|
+
export function Repeater({ id, field: f, value, set, invalid, describedBy, disabled, api, record, errors, path = f.key }: ControlProps) {
|
|
11
|
+
const { t } = useLarakit();
|
|
12
|
+
const look = f.look ?? {};
|
|
13
|
+
const subs = look.fields ?? [];
|
|
14
|
+
const list = rows(value);
|
|
15
|
+
const full = look.maxItems !== undefined && look.maxItems > 0 && list.length >= look.maxItems;
|
|
16
|
+
const change = (i: number, key: string, v: unknown) => set(list.map((row, j) => (j === i ? { ...row, [key]: v } : row)));
|
|
17
|
+
const columns = look.columns ?? Math.min(subs.length, 3);
|
|
18
|
+
const grid = columns >= 3 ? 'sm:grid-cols-3' : columns === 2 ? 'sm:grid-cols-2' : '';
|
|
19
|
+
const title = (row: Record<string, unknown>, i: number) => (look.itemLabel && text(row[look.itemLabel]).trim()) || `#${i + 1}`;
|
|
20
|
+
return (
|
|
21
|
+
<div id={id} data-lk="repeater" role="group" aria-invalid={invalid || undefined} aria-describedby={describedBy} className="space-y-3">
|
|
22
|
+
{list.map((row, i) => (
|
|
23
|
+
<fieldset key={i} data-lk="repeater-row" data-row={i} className="rounded-(--lk-radius) p-3 ring-1 ring-(--lk-border)">
|
|
24
|
+
<div className="mb-2 flex items-center justify-between gap-2">
|
|
25
|
+
<legend className="text-xs font-medium text-(--lk-muted)">{title(row, i)}</legend>
|
|
26
|
+
{!disabled && (
|
|
27
|
+
<Button aria-label={t('form.removeItem', 'Remove {label}', { label: title(row, i) })} onClick={() => set(list.filter((_, j) => j !== i))}>
|
|
28
|
+
{t('form.remove', 'Remove')}
|
|
29
|
+
</Button>
|
|
30
|
+
)}
|
|
31
|
+
</div>
|
|
32
|
+
<div className={'grid gap-3 ' + grid}>
|
|
33
|
+
{subs.filter((s) => holds(s.visibleWhen, row)).map((s) => (
|
|
34
|
+
<Field key={s.key} field={s} id={`${id}-${i}-${s.key}`} path={`${path}.${i}.${s.key}`} value={row[s.key]} set={(v) => change(i, s.key, v)}
|
|
35
|
+
error={errorAt(errors, path, i, s.key)} errors={errors} required={required(s, row)} disabled={disabled} api={api} record={record} />
|
|
36
|
+
))}
|
|
37
|
+
</div>
|
|
38
|
+
{errorAt(errors, path, i) && <p data-lk="row-error" className="mt-2 text-xs text-(--lk-danger)">{errorAt(errors, path, i)}</p>}
|
|
39
|
+
</fieldset>
|
|
40
|
+
))}
|
|
41
|
+
{!disabled && !full && (
|
|
42
|
+
<Button data-lk="repeater-add" onClick={() => set([...list, starting(subs)])}>+ {look.addLabel || t('form.add', 'Add')}</Button>
|
|
43
|
+
)}
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
}
|