@eskais/app-sdk 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +74 -2
- package/dist/action/action.d.ts +133 -0
- package/dist/action/action.js +224 -0
- package/dist/action/bulk.d.ts +24 -0
- package/dist/action/bulk.js +24 -0
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.js +51 -0
- package/dist/cli/routes.d.ts +17 -0
- package/dist/cli/routes.js +36 -0
- package/dist/core/assume.d.ts +1 -0
- package/dist/core/assume.js +8 -0
- package/dist/core/constants.d.ts +359 -0
- package/dist/core/constants.js +187 -0
- package/dist/core/contracts.d.ts +107 -0
- package/dist/core/contracts.js +3 -0
- package/dist/core/data-labels.d.ts +210 -0
- package/dist/core/data-labels.js +385 -0
- package/dist/core/errors.d.ts +52 -0
- package/dist/core/errors.js +209 -0
- package/dist/core/guards.d.ts +9 -0
- package/dist/core/guards.js +14 -0
- package/dist/core/refusal.d.ts +30 -0
- package/dist/core/refusal.js +53 -0
- package/dist/dialogs/dialogs.d.ts +75 -0
- package/dist/dialogs/dialogs.js +66 -0
- package/dist/draft/draft.d.ts +36 -0
- package/dist/draft/draft.js +87 -0
- package/dist/errors/adapters.d.ts +21 -0
- package/dist/errors/adapters.js +81 -0
- package/dist/errors/define-errors.d.ts +88 -0
- package/dist/errors/define-errors.js +168 -0
- package/dist/errors/idempotency.d.ts +2 -0
- package/dist/errors/idempotency.js +13 -0
- package/dist/export/csv.d.ts +25 -0
- package/dist/export/csv.js +59 -0
- package/dist/i18n/adapters.d.ts +53 -0
- package/dist/i18n/adapters.js +56 -0
- package/dist/i18n/format.d.ts +10 -0
- package/dist/i18n/format.js +273 -0
- package/dist/i18n/labels.d.ts +10 -0
- package/dist/i18n/labels.js +227 -0
- package/dist/i18n/locale.d.ts +73 -0
- package/dist/i18n/locale.js +74 -0
- package/dist/i18n/messages.d.ts +93 -0
- package/dist/i18n/messages.js +16 -0
- package/dist/import/csv-parse.d.ts +11 -0
- package/dist/import/csv-parse.js +70 -0
- package/dist/import/import.d.ts +104 -0
- package/dist/import/import.js +266 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +45 -0
- package/dist/navigation/nav.d.ts +128 -0
- package/dist/navigation/nav.js +301 -0
- package/dist/navigation/router.d.ts +81 -0
- package/dist/navigation/router.js +249 -0
- package/dist/navigation/shortcuts.d.ts +37 -0
- package/dist/navigation/shortcuts.js +93 -0
- package/dist/next/index.d.ts +3 -0
- package/dist/next/index.js +3 -0
- package/dist/next/screen-hydration.d.ts +12 -0
- package/dist/next/screen-hydration.js +26 -0
- package/dist/next/screen.d.ts +25 -0
- package/dist/next/screen.js +44 -0
- package/dist/next/server/handler.d.ts +39 -0
- package/dist/next/server/handler.js +190 -0
- package/dist/next/server/index.d.ts +6 -0
- package/dist/next/server/index.js +6 -0
- package/dist/next/server/page.d.ts +63 -0
- package/dist/next/server/page.js +110 -0
- package/dist/next/server/prefetch.d.ts +29 -0
- package/dist/next/server/prefetch.js +97 -0
- package/dist/next/server/session.d.ts +25 -0
- package/dist/next/server/session.js +23 -0
- package/dist/next/use-next-router.d.ts +21 -0
- package/dist/next/use-next-router.js +104 -0
- package/dist/permissions/permissions.d.ts +134 -0
- package/dist/permissions/permissions.js +121 -0
- package/dist/react/data-layer.d.ts +68 -0
- package/dist/react/data-layer.js +76 -0
- package/dist/react/hooks/use-action-dialog.d.ts +25 -0
- package/dist/react/hooks/use-action-dialog.js +31 -0
- package/dist/react/hooks/use-action.d.ts +37 -0
- package/dist/react/hooks/use-action.js +125 -0
- package/dist/react/hooks/use-better-auth.d.ts +12 -0
- package/dist/react/hooks/use-better-auth.js +11 -0
- package/dist/react/hooks/use-bulk-action.d.ts +20 -0
- package/dist/react/hooks/use-bulk-action.js +24 -0
- package/dist/react/hooks/use-dialogs.d.ts +14 -0
- package/dist/react/hooks/use-dialogs.js +20 -0
- package/dist/react/hooks/use-draft.d.ts +5 -0
- package/dist/react/hooks/use-draft.js +17 -0
- package/dist/react/hooks/use-nav.d.ts +17 -0
- package/dist/react/hooks/use-nav.js +35 -0
- package/dist/react/hooks/use-new-rows.d.ts +22 -0
- package/dist/react/hooks/use-new-rows.js +24 -0
- package/dist/react/hooks/use-permissions.d.ts +13 -0
- package/dist/react/hooks/use-permissions.js +19 -0
- package/dist/react/hooks/use-reauthenticate.d.ts +6 -0
- package/dist/react/hooks/use-reauthenticate.js +11 -0
- package/dist/react/hooks/use-resource.d.ts +92 -0
- package/dist/react/hooks/use-resource.js +255 -0
- package/dist/react/hooks/use-view.d.ts +6 -0
- package/dist/react/hooks/use-view.js +24 -0
- package/dist/react/index.d.ts +15 -0
- package/dist/react/index.js +15 -0
- package/dist/react/keys.d.ts +60 -0
- package/dist/react/keys.js +71 -0
- package/dist/react/new-rows.d.ts +22 -0
- package/dist/react/new-rows.js +72 -0
- package/dist/refresh/new-rows.d.ts +13 -0
- package/dist/refresh/new-rows.js +18 -0
- package/dist/resource/form-schema.d.ts +11 -0
- package/dist/resource/form-schema.js +51 -0
- package/dist/resource/relations.d.ts +99 -0
- package/dist/resource/relations.js +194 -0
- package/dist/resource/resource-relations.d.ts +47 -0
- package/dist/resource/resource-relations.js +154 -0
- package/dist/resource/resource-schema.d.ts +9 -0
- package/dist/resource/resource-schema.js +73 -0
- package/dist/resource/resource-texts.d.ts +18 -0
- package/dist/resource/resource-texts.js +13 -0
- package/dist/resource/resource-types.d.ts +420 -0
- package/dist/resource/resource-types.js +1 -0
- package/dist/resource/resource-words.d.ts +25 -0
- package/dist/resource/resource-words.js +221 -0
- package/dist/resource/resource.d.ts +15 -0
- package/dist/resource/resource.js +280 -0
- package/dist/resource/schema.d.ts +57 -0
- package/dist/resource/schema.js +115 -0
- package/dist/services/from-page-source.d.ts +15 -0
- package/dist/services/from-page-source.js +27 -0
- package/dist/services/memory-query.d.ts +8 -0
- package/dist/services/memory-query.js +165 -0
- package/dist/services/memory-service.d.ts +33 -0
- package/dist/services/memory-service.js +291 -0
- package/dist/services/read-only-service.d.ts +18 -0
- package/dist/services/read-only-service.js +25 -0
- package/dist/services/rest-service.d.ts +65 -0
- package/dist/services/rest-service.js +222 -0
- package/dist/services/rng.d.ts +9 -0
- package/dist/services/rng.js +22 -0
- package/dist/services/spring-service.d.ts +58 -0
- package/dist/services/spring-service.js +132 -0
- package/dist/session/better-auth.d.ts +83 -0
- package/dist/session/better-auth.js +86 -0
- package/dist/session/session.d.ts +45 -0
- package/dist/session/session.js +36 -0
- package/dist/view/preferences.d.ts +35 -0
- package/dist/view/preferences.js +93 -0
- package/dist/view/view-types.d.ts +124 -0
- package/dist/view/view-types.js +1 -0
- package/dist/view/view-url.d.ts +7 -0
- package/dist/view/view-url.js +54 -0
- package/dist/view/view.d.ts +4 -0
- package/dist/view/view.js +392 -0
- package/locales/en.json +193 -0
- package/locales/es.json +193 -0
- package/package.json +88 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export * from "./core/constants.js";
|
|
2
|
+
export { isPagePosition } from "./core/contracts.js";
|
|
3
|
+
export { fieldErrorsOf, httpServiceError, isServiceError, kindOfStatus, problemDetailError, ServiceFailure, serviceError, toServiceError, } from "./core/errors.js";
|
|
4
|
+
export { AppActionRefusedError, firstRefusal, isRefusal, refuse, refusedError, toAllowed, unavailableError, clause, failedError, } from "./core/refusal.js";
|
|
5
|
+
export { createSession, fromCasl, } from "./session/session.js";
|
|
6
|
+
export { allows, definePermissions, GrantScope, hasRole, permitted, sessionPayload, } from "./permissions/permissions.js";
|
|
7
|
+
export { holdNewRows } from "./refresh/new-rows.js";
|
|
8
|
+
export { createDialogQueue, DialogKind, } from "./dialogs/dialogs.js";
|
|
9
|
+
export { betterAuthPermissions, betterAuthServerSession, betterAuthSession, betterAuthStoreSession, } from "./session/better-auth.js";
|
|
10
|
+
export { hashRouter, historyRouter, joinPath, memoryRouter, splitPath, tanstackRouter, } from "./navigation/router.js";
|
|
11
|
+
export { localPreferences, localViews, memoryPreferences, remotePreferences, } from "./view/preferences.js";
|
|
12
|
+
export { createRng } from "./services/rng.js";
|
|
13
|
+
export { isStandardSchema, issuePath, jsonSchemaProperties, validate, zodIntrospect, } from "./resource/schema.js";
|
|
14
|
+
export { applyQuery, matchesFilter, matchesSearch, sortRows, valueAt } from "./services/memory-query.js";
|
|
15
|
+
export { memoryService } from "./services/memory-service.js";
|
|
16
|
+
export { HttpMethod, restService } from "./services/rest-service.js";
|
|
17
|
+
export { fromPageSource } from "./services/from-page-source.js";
|
|
18
|
+
export { readOnlyService } from "./services/read-only-service.js";
|
|
19
|
+
export { defineResource, dependentsOf, refreshPolicy, searchKey, } from "./resource/resource.js";
|
|
20
|
+
export { actionsOf, defineAction, isAction, messageOf, } from "./action/action.js";
|
|
21
|
+
export { defineView, } from "./view/view.js";
|
|
22
|
+
export { count, defineNav, isNavCount, navItem, screenUse, } from "./navigation/nav.js";
|
|
23
|
+
export { defineDraft, isStoredDraft, } from "./draft/draft.js";
|
|
24
|
+
export { fieldsSchema, formSchemaOf, issueText } from "./resource/form-schema.js";
|
|
25
|
+
export { schemaFields } from "./resource/resource-schema.js";
|
|
26
|
+
export { exportCsv, toCsv } from "./export/csv.js";
|
|
27
|
+
export { dataLabelsEn, dataLabelsEs, } from "./core/data-labels.js";
|
|
28
|
+
export { isOneOf, isRecord, valuesOf } from "./core/guards.js";
|
|
29
|
+
export { springFilter, springParams, springService, } from "./services/spring-service.js";
|
|
30
|
+
export { appShortcuts, createShortcutRegistry, normalizeShortcut, shortcutConflicts, warnInDevelopment, warnShortcutConflict, } from "./navigation/shortcuts.js";
|
|
31
|
+
export { parseCsv } from "./import/csv-parse.js";
|
|
32
|
+
export { defineImport, invalidRowsText, } from "./import/import.js";
|
|
33
|
+
export { belongsTo, childChanges, chunkIds, embeds, hasMany, includeOf, keyRelation, manyToMany, registerResource, resolveRelations, } from "./resource/relations.js";
|
|
34
|
+
export { isListSchema } from "./resource/schema.js";
|
|
35
|
+
export { defineLocale, flattenCatalog, isMessageRef, lookup, msg, refusalIn, resolveText, } from "./i18n/locale.js";
|
|
36
|
+
export { Gender, IssueCode, } from "./i18n/messages.js";
|
|
37
|
+
export { checkMessage, formatMessage } from "./i18n/format.js";
|
|
38
|
+
export { dataLabelsOf } from "./i18n/labels.js";
|
|
39
|
+
export { fromI18next, fromIntl, fromNextIntl, } from "./i18n/adapters.js";
|
|
40
|
+
export { isErrorReason, reasonOf } from "./core/errors.js";
|
|
41
|
+
export { defineErrors, } from "./errors/define-errors.js";
|
|
42
|
+
export { appError, firebaseErrors, responseErrors } from "./errors/adapters.js";
|
|
43
|
+
export { idempotencyKey } from "./errors/idempotency.js";
|
|
44
|
+
export { defineBulkAction } from "./action/action.js";
|
|
45
|
+
export { failedTarget, isBulkResult } from "./action/bulk.js";
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { NavMatch, ResourceAction, ResourceScreen } from "../core/constants.js";
|
|
2
|
+
import type { PreferencesStore } from "../view/preferences.js";
|
|
3
|
+
import { type Allowed, type Refusal } from "../core/refusal.js";
|
|
4
|
+
import type { AnyResource, ToneName } from "../resource/resource.js";
|
|
5
|
+
import { type RouterAdapter } from "./router.js";
|
|
6
|
+
import type { Session } from "../session/session.js";
|
|
7
|
+
import { type Locale, type Text } from "../i18n/locale.js";
|
|
8
|
+
export type NavCount = {
|
|
9
|
+
readonly resource: AnyResource;
|
|
10
|
+
readonly filter: Record<string, unknown>;
|
|
11
|
+
readonly tone?: ToneName;
|
|
12
|
+
/** Polls every so many ms besides; off by default. */
|
|
13
|
+
readonly refresh?: number;
|
|
14
|
+
};
|
|
15
|
+
export type NavBadge = NavCount | {
|
|
16
|
+
count: () => Promise<number>;
|
|
17
|
+
refresh?: number;
|
|
18
|
+
tone?: ToneName;
|
|
19
|
+
} | string | number;
|
|
20
|
+
export type NavItem = {
|
|
21
|
+
id: string;
|
|
22
|
+
/** Shown with `resolveText(item.label, locale)`; `navItem` gives the resource's name in the locale. */
|
|
23
|
+
label: Text;
|
|
24
|
+
to: string;
|
|
25
|
+
icon?: unknown;
|
|
26
|
+
/** `NavMatch.prefix` by default: `/customers/7` keeps Customers current. */
|
|
27
|
+
match?: NavMatch | ((pathname: string) => boolean);
|
|
28
|
+
children?: readonly NavItem[];
|
|
29
|
+
badge?: NavBadge;
|
|
30
|
+
/** Without permission the item doesn't show. A string asks the session's `can`. */
|
|
31
|
+
can?: ResourceAction | string | (() => boolean | Refusal);
|
|
32
|
+
shortcut?: string;
|
|
33
|
+
keywords?: readonly string[];
|
|
34
|
+
mobile?: boolean | {
|
|
35
|
+
order: number;
|
|
36
|
+
};
|
|
37
|
+
placements?: readonly string[];
|
|
38
|
+
crumb?: (params: Record<string, string>) => string | Promise<string>;
|
|
39
|
+
resource?: AnyResource;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* A section of the sidebar. A `collapsible` one folds under its label; whether it's folded is kept in `preferences`,
|
|
43
|
+
* and it opens by itself when the current page is in it.
|
|
44
|
+
*/
|
|
45
|
+
export type NavGroup = {
|
|
46
|
+
group: string;
|
|
47
|
+
label?: Text;
|
|
48
|
+
collapsible?: boolean;
|
|
49
|
+
items: readonly NavItem[];
|
|
50
|
+
};
|
|
51
|
+
export type TrailEntry = {
|
|
52
|
+
id: string;
|
|
53
|
+
label: Text;
|
|
54
|
+
to: string;
|
|
55
|
+
item?: NavItem;
|
|
56
|
+
record?: {
|
|
57
|
+
resource: AnyResource;
|
|
58
|
+
id: string;
|
|
59
|
+
};
|
|
60
|
+
screen?: typeof ResourceScreen.create | typeof ResourceScreen.edit;
|
|
61
|
+
};
|
|
62
|
+
export type NavRouteBuilder<Route> = (nav: Nav<Route>) => Route[];
|
|
63
|
+
export type NavOptions<Route = unknown> = {
|
|
64
|
+
/**
|
|
65
|
+
* Where the app's paths live. Leave it out in a definition shared with server code (Next.js), where the router only
|
|
66
|
+
* exists in a client component, and bind one there with `nav.withRouter(router)` or `useRoutedNav(nav)`.
|
|
67
|
+
*/
|
|
68
|
+
router?: RouterAdapter;
|
|
69
|
+
items: readonly (NavGroup | NavItem)[];
|
|
70
|
+
session?: Session;
|
|
71
|
+
preferences?: PreferencesStore;
|
|
72
|
+
title?: (trail: readonly TrailEntry[], locale: Locale | undefined) => string;
|
|
73
|
+
/** Builds `nav.routes()`: `tanstackRoutes({ parent: () => rootRoute })` from the `resource-routes` item. */
|
|
74
|
+
routes?: NavRouteBuilder<Route>;
|
|
75
|
+
/** How many recent pages to keep. Default 5. */
|
|
76
|
+
recents?: number;
|
|
77
|
+
};
|
|
78
|
+
export type NavSnapshot = {
|
|
79
|
+
favorites: readonly string[];
|
|
80
|
+
recents: readonly string[];
|
|
81
|
+
collapsed: readonly string[];
|
|
82
|
+
};
|
|
83
|
+
export type Nav<Route = unknown> = {
|
|
84
|
+
readonly router: RouterAdapter;
|
|
85
|
+
readonly session: Session | undefined;
|
|
86
|
+
readonly options: NavOptions<Route>;
|
|
87
|
+
groups(): readonly NavGroup[];
|
|
88
|
+
visibleGroups(): readonly NavGroup[];
|
|
89
|
+
items(): readonly NavItem[];
|
|
90
|
+
visibleItems(): readonly NavItem[];
|
|
91
|
+
find(id: string): NavItem | undefined;
|
|
92
|
+
check(item: NavItem): Allowed;
|
|
93
|
+
/**
|
|
94
|
+
* Whether the session may open a path: a resource's screen asks for its use (`list`, `create`, `get`, `update`),
|
|
95
|
+
* any other page for its item's `can`. The nav's session by default; a route guard passes the `DataLayer`'s.
|
|
96
|
+
*/
|
|
97
|
+
checkPath(pathname?: string, session?: Session, notAllowed?: string): Allowed;
|
|
98
|
+
checkNavigate(id: string): Allowed;
|
|
99
|
+
navigate(id: string, options?: {
|
|
100
|
+
replace?: boolean;
|
|
101
|
+
}): void;
|
|
102
|
+
current(pathname?: string): NavItem | undefined;
|
|
103
|
+
trail(pathname?: string): readonly TrailEntry[];
|
|
104
|
+
documentTitle(pathname?: string, locale?: Locale): string | undefined;
|
|
105
|
+
resources(): readonly AnyResource[];
|
|
106
|
+
routes(): Route[];
|
|
107
|
+
getSnapshot(): NavSnapshot;
|
|
108
|
+
subscribe(listener: () => void): () => void;
|
|
109
|
+
isFavorite(id: string): boolean;
|
|
110
|
+
pin(id: string): Allowed;
|
|
111
|
+
unpin(id: string): Allowed;
|
|
112
|
+
isCollapsed(group: string): boolean;
|
|
113
|
+
setCollapsed(group: string, collapsed: boolean): void;
|
|
114
|
+
/** The group an item is in, nested items included. */
|
|
115
|
+
groupOf(id: string): NavGroup | undefined;
|
|
116
|
+
attach(): () => void;
|
|
117
|
+
/** The same manifest over another router, with its own favorites and recents. */
|
|
118
|
+
withRouter(router: RouterAdapter): Nav<Route>;
|
|
119
|
+
};
|
|
120
|
+
/** The use a resource's screen needs: its list `list`, `new` `create`, a record `get` and its edit `update`. */
|
|
121
|
+
export declare function screenUse(screen: ResourceScreen): ResourceAction;
|
|
122
|
+
export declare function navItem(resource: AnyResource, overrides?: Partial<NavItem>): NavItem;
|
|
123
|
+
export declare function count(resource: AnyResource, filter?: Record<string, unknown>, options?: {
|
|
124
|
+
tone?: ToneName;
|
|
125
|
+
refresh?: number;
|
|
126
|
+
}): NavCount;
|
|
127
|
+
export declare function isNavCount(badge: unknown): badge is NavCount;
|
|
128
|
+
export declare function defineNav<Route = unknown>(options: NavOptions<Route>): Nav<Route>;
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { NavMatch, ResourceAction, ResourceScreen } from "../core/constants.js";
|
|
2
|
+
import { isFunction, isRecord, isString } from "../core/guards.js";
|
|
3
|
+
import { firstRefusal, refuse, toAllowed } from "../core/refusal.js";
|
|
4
|
+
import { memoryRouter } from "./router.js";
|
|
5
|
+
import { resolveText } from "../i18n/locale.js";
|
|
6
|
+
const screenUses = {
|
|
7
|
+
[ResourceScreen.list]: ResourceAction.list,
|
|
8
|
+
[ResourceScreen.create]: ResourceAction.create,
|
|
9
|
+
[ResourceScreen.detail]: ResourceAction.get,
|
|
10
|
+
[ResourceScreen.edit]: ResourceAction.update,
|
|
11
|
+
};
|
|
12
|
+
/** The use a resource's screen needs: its list `list`, `new` `create`, a record `get` and its edit `update`. */
|
|
13
|
+
export function screenUse(screen) {
|
|
14
|
+
return screenUses[screen];
|
|
15
|
+
}
|
|
16
|
+
export function navItem(resource, overrides = {}) {
|
|
17
|
+
return {
|
|
18
|
+
id: resource.key,
|
|
19
|
+
label: (_t, locale) => resource.in(locale).label(2),
|
|
20
|
+
to: resource.paths.list(),
|
|
21
|
+
icon: resource.icon,
|
|
22
|
+
can: ResourceAction.list,
|
|
23
|
+
resource,
|
|
24
|
+
...(resource.shortcuts.list === undefined ? {} : { shortcut: resource.shortcuts.list }),
|
|
25
|
+
...overrides,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export function count(resource, filter = {}, options = {}) {
|
|
29
|
+
return { resource, filter, ...options };
|
|
30
|
+
}
|
|
31
|
+
export function isNavCount(badge) {
|
|
32
|
+
return isRecord(badge) && "resource" in badge && "filter" in badge;
|
|
33
|
+
}
|
|
34
|
+
function isGroup(entry) {
|
|
35
|
+
return "group" in entry;
|
|
36
|
+
}
|
|
37
|
+
function flatten(items) {
|
|
38
|
+
return items.flatMap((item) => [item, ...flatten(item.children ?? [])]);
|
|
39
|
+
}
|
|
40
|
+
function trimSlash(path) {
|
|
41
|
+
return path.length > 1 ? path.replace(/\/+$/u, "") : path;
|
|
42
|
+
}
|
|
43
|
+
function pathOf(to) {
|
|
44
|
+
const at = to.search(/[?#]/u);
|
|
45
|
+
return trimSlash(at === -1 ? to : to.slice(0, at));
|
|
46
|
+
}
|
|
47
|
+
function matches(item, pathname) {
|
|
48
|
+
if (isFunction(item.match))
|
|
49
|
+
return item.match(pathname);
|
|
50
|
+
const target = pathOf(item.to);
|
|
51
|
+
const path = trimSlash(pathname);
|
|
52
|
+
if (item.match === NavMatch.exact)
|
|
53
|
+
return path === target;
|
|
54
|
+
return path === target || (target === "/" ? false : path.startsWith(`${target}/`));
|
|
55
|
+
}
|
|
56
|
+
const unique = (ids) => [...new Set(ids)];
|
|
57
|
+
function checkWith(item, asking, notAllowed = "") {
|
|
58
|
+
const can = item.can;
|
|
59
|
+
if (can === undefined)
|
|
60
|
+
return true;
|
|
61
|
+
if (isFunction(can))
|
|
62
|
+
return toAllowed(can(), notAllowed);
|
|
63
|
+
if (item.resource && can === ResourceAction.list)
|
|
64
|
+
return item.resource.check(ResourceAction.list, undefined, asking, notAllowed);
|
|
65
|
+
return toAllowed(asking?.can(can), notAllowed);
|
|
66
|
+
}
|
|
67
|
+
const readIds = (value) => (Array.isArray(value) ? value.filter(isString) : []);
|
|
68
|
+
export function defineNav(options) {
|
|
69
|
+
const { session, preferences } = options;
|
|
70
|
+
const router = options.router ?? memoryRouter();
|
|
71
|
+
const limit = options.recents ?? 5;
|
|
72
|
+
let snapshot = { favorites: [], recents: [], collapsed: [] };
|
|
73
|
+
const listeners = new Set();
|
|
74
|
+
const write = (next) => {
|
|
75
|
+
snapshot = { ...snapshot, ...next };
|
|
76
|
+
for (const listener of listeners)
|
|
77
|
+
listener();
|
|
78
|
+
};
|
|
79
|
+
const persist = (key) => void preferences?.set(`nav:${key}`, snapshot[key]);
|
|
80
|
+
const groups = () => {
|
|
81
|
+
const result = [];
|
|
82
|
+
let loose = [];
|
|
83
|
+
const flush = () => {
|
|
84
|
+
if (loose.length > 0)
|
|
85
|
+
result.push({ group: `items-${String(result.length)}`, items: loose });
|
|
86
|
+
loose = [];
|
|
87
|
+
};
|
|
88
|
+
for (const entry of options.items) {
|
|
89
|
+
if (isGroup(entry)) {
|
|
90
|
+
flush();
|
|
91
|
+
result.push(entry);
|
|
92
|
+
}
|
|
93
|
+
else
|
|
94
|
+
loose.push(entry);
|
|
95
|
+
}
|
|
96
|
+
flush();
|
|
97
|
+
return result;
|
|
98
|
+
};
|
|
99
|
+
const check = (item) => checkWith(item, session);
|
|
100
|
+
const visible = (items) => items
|
|
101
|
+
.filter((item) => check(item) === true)
|
|
102
|
+
.map((item) => (item.children ? { ...item, children: visible(item.children) } : item));
|
|
103
|
+
const allItems = () => flatten(groups().flatMap((group) => group.items));
|
|
104
|
+
const find = (id) => allItems().find((item) => item.id === id);
|
|
105
|
+
const pathname = (given) => given ?? router.location().pathname;
|
|
106
|
+
function current(given) {
|
|
107
|
+
const path = pathname(given);
|
|
108
|
+
let best;
|
|
109
|
+
for (const item of allItems()) {
|
|
110
|
+
if (!matches(item, path))
|
|
111
|
+
continue;
|
|
112
|
+
if (!best || pathOf(item.to).length > pathOf(best.to).length)
|
|
113
|
+
best = item;
|
|
114
|
+
}
|
|
115
|
+
return best;
|
|
116
|
+
}
|
|
117
|
+
function parents(target) {
|
|
118
|
+
const walk = (items, chain) => {
|
|
119
|
+
for (const item of items) {
|
|
120
|
+
if (item === target)
|
|
121
|
+
return [...chain, item];
|
|
122
|
+
const found = walk(item.children ?? [], [...chain, item]);
|
|
123
|
+
if (found)
|
|
124
|
+
return found;
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
};
|
|
128
|
+
return (walk(groups().flatMap((group) => group.items), []) ?? [target]);
|
|
129
|
+
}
|
|
130
|
+
function trail(given) {
|
|
131
|
+
const path = pathname(given);
|
|
132
|
+
const item = current(path);
|
|
133
|
+
if (!item)
|
|
134
|
+
return [];
|
|
135
|
+
const entries = parents(item).map((entry) => ({
|
|
136
|
+
id: entry.id,
|
|
137
|
+
label: entry.label,
|
|
138
|
+
to: entry.to,
|
|
139
|
+
item: entry,
|
|
140
|
+
}));
|
|
141
|
+
const resource = item.resource;
|
|
142
|
+
const match = resource?.match(path);
|
|
143
|
+
if (!resource || !match || match.screen === ResourceScreen.list)
|
|
144
|
+
return entries;
|
|
145
|
+
if (match.screen === ResourceScreen.create)
|
|
146
|
+
return [
|
|
147
|
+
...entries,
|
|
148
|
+
{ id: `${item.id}:new`, label: "", to: resource.paths.create(), item, screen: ResourceScreen.create },
|
|
149
|
+
];
|
|
150
|
+
const id = match.id ?? "";
|
|
151
|
+
const parsed = resource.parseId(id);
|
|
152
|
+
const record = {
|
|
153
|
+
id: `${item.id}:${id}`,
|
|
154
|
+
label: id,
|
|
155
|
+
to: resource.paths.detail(parsed),
|
|
156
|
+
record: { resource, id },
|
|
157
|
+
};
|
|
158
|
+
if (match.screen === ResourceScreen.edit)
|
|
159
|
+
return [
|
|
160
|
+
...entries,
|
|
161
|
+
record,
|
|
162
|
+
{ id: `${item.id}:${id}:edit`, label: "", to: resource.paths.edit(parsed), item, screen: ResourceScreen.edit },
|
|
163
|
+
];
|
|
164
|
+
return [...entries, record];
|
|
165
|
+
}
|
|
166
|
+
function groupOf(id) {
|
|
167
|
+
return groups().find((group) => flatten(group.items).some((item) => item.id === id));
|
|
168
|
+
}
|
|
169
|
+
function setCollapsed(group, collapsed) {
|
|
170
|
+
const next = collapsed
|
|
171
|
+
? unique([...snapshot.collapsed, group])
|
|
172
|
+
: snapshot.collapsed.filter((entry) => entry !== group);
|
|
173
|
+
if (next.length === snapshot.collapsed.length && next.every((entry, index) => entry === snapshot.collapsed[index]))
|
|
174
|
+
return;
|
|
175
|
+
write({ collapsed: next });
|
|
176
|
+
persist("collapsed");
|
|
177
|
+
}
|
|
178
|
+
function openCurrent() {
|
|
179
|
+
const item = current();
|
|
180
|
+
const group = item ? groupOf(item.id) : undefined;
|
|
181
|
+
if (group?.collapsible === true && snapshot.collapsed.includes(group.group))
|
|
182
|
+
setCollapsed(group.group, false);
|
|
183
|
+
}
|
|
184
|
+
function recordRecent() {
|
|
185
|
+
openCurrent();
|
|
186
|
+
const item = current();
|
|
187
|
+
if (!item || snapshot.recents[0] === item.id)
|
|
188
|
+
return;
|
|
189
|
+
write({ recents: [item.id, ...snapshot.recents.filter((entry) => entry !== item.id)].slice(0, limit) });
|
|
190
|
+
persist("recents");
|
|
191
|
+
}
|
|
192
|
+
function checkNavigate(id) {
|
|
193
|
+
const item = find(id);
|
|
194
|
+
if (!item)
|
|
195
|
+
return refuse(`no page named "${id}"`);
|
|
196
|
+
const allowed = check(item);
|
|
197
|
+
return allowed === true ? true : firstRefusal(allowed);
|
|
198
|
+
}
|
|
199
|
+
const nav = {
|
|
200
|
+
router,
|
|
201
|
+
session,
|
|
202
|
+
options,
|
|
203
|
+
groups,
|
|
204
|
+
visibleGroups: () => groups()
|
|
205
|
+
.map((group) => ({ ...group, items: visible(group.items) }))
|
|
206
|
+
.filter((group) => group.items.length > 0),
|
|
207
|
+
items: allItems,
|
|
208
|
+
visibleItems: () => flatten(visible(groups().flatMap((group) => group.items))),
|
|
209
|
+
find,
|
|
210
|
+
check,
|
|
211
|
+
checkPath(given, asking = session, notAllowed = "") {
|
|
212
|
+
const path = pathname(given);
|
|
213
|
+
for (const item of allItems()) {
|
|
214
|
+
const match = item.resource?.match(path);
|
|
215
|
+
if (item.resource && match)
|
|
216
|
+
return item.resource.check(screenUse(match.screen), undefined, asking, notAllowed);
|
|
217
|
+
}
|
|
218
|
+
const item = current(path);
|
|
219
|
+
return item ? checkWith(item, asking, notAllowed) : true;
|
|
220
|
+
},
|
|
221
|
+
checkNavigate,
|
|
222
|
+
navigate(id, navigateOptions) {
|
|
223
|
+
const item = find(id);
|
|
224
|
+
if (!item || checkNavigate(id) !== true)
|
|
225
|
+
return;
|
|
226
|
+
router.navigate(item.to, navigateOptions);
|
|
227
|
+
},
|
|
228
|
+
current,
|
|
229
|
+
trail,
|
|
230
|
+
documentTitle(given, locale) {
|
|
231
|
+
const entries = trail(given);
|
|
232
|
+
if (options.title)
|
|
233
|
+
return options.title(entries, locale);
|
|
234
|
+
const last = entries.at(-1)?.label;
|
|
235
|
+
const text = last === undefined ? undefined : resolveText(last, locale);
|
|
236
|
+
return text === undefined || text === "" ? undefined : text;
|
|
237
|
+
},
|
|
238
|
+
resources() {
|
|
239
|
+
const found = allItems().flatMap((item) => (item.resource ? [item.resource] : []));
|
|
240
|
+
return [...new Set(found)];
|
|
241
|
+
},
|
|
242
|
+
routes() {
|
|
243
|
+
if (!options.routes)
|
|
244
|
+
throw new Error("nav.routes() needs a route builder: defineNav({ routes: tanstackRoutes({ parent: () => rootRoute }) }).");
|
|
245
|
+
return options.routes(nav);
|
|
246
|
+
},
|
|
247
|
+
getSnapshot: () => snapshot,
|
|
248
|
+
subscribe(listener) {
|
|
249
|
+
listeners.add(listener);
|
|
250
|
+
return () => {
|
|
251
|
+
listeners.delete(listener);
|
|
252
|
+
};
|
|
253
|
+
},
|
|
254
|
+
isFavorite: (id) => snapshot.favorites.includes(id),
|
|
255
|
+
pin(id) {
|
|
256
|
+
const allowed = checkNavigate(id);
|
|
257
|
+
if (allowed !== true)
|
|
258
|
+
return allowed;
|
|
259
|
+
if (!snapshot.favorites.includes(id)) {
|
|
260
|
+
write({ favorites: [...snapshot.favorites, id] });
|
|
261
|
+
persist("favorites");
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
},
|
|
265
|
+
unpin(id) {
|
|
266
|
+
if (!find(id))
|
|
267
|
+
return refuse(`no page named "${id}"`);
|
|
268
|
+
if (snapshot.favorites.includes(id)) {
|
|
269
|
+
write({ favorites: snapshot.favorites.filter((entry) => entry !== id) });
|
|
270
|
+
persist("favorites");
|
|
271
|
+
}
|
|
272
|
+
return true;
|
|
273
|
+
},
|
|
274
|
+
isCollapsed: (group) => snapshot.collapsed.includes(group),
|
|
275
|
+
setCollapsed,
|
|
276
|
+
groupOf,
|
|
277
|
+
attach() {
|
|
278
|
+
let active = true;
|
|
279
|
+
if (preferences) {
|
|
280
|
+
void Promise.all([
|
|
281
|
+
preferences.get("nav:favorites"),
|
|
282
|
+
preferences.get("nav:recents"),
|
|
283
|
+
preferences.get("nav:collapsed"),
|
|
284
|
+
]).then(([favorites, recents, collapsed]) => {
|
|
285
|
+
if (!active)
|
|
286
|
+
return;
|
|
287
|
+
write({ favorites: readIds(favorites), recents: readIds(recents), collapsed: readIds(collapsed) });
|
|
288
|
+
openCurrent();
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
recordRecent();
|
|
292
|
+
const unsubscribe = router.subscribe(recordRecent);
|
|
293
|
+
return () => {
|
|
294
|
+
active = false;
|
|
295
|
+
unsubscribe();
|
|
296
|
+
};
|
|
297
|
+
},
|
|
298
|
+
withRouter: (next) => defineNav({ ...options, router: next }),
|
|
299
|
+
};
|
|
300
|
+
return nav;
|
|
301
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { RouterKind } from "../core/constants.js";
|
|
2
|
+
export type RouterLocation = {
|
|
3
|
+
pathname: string;
|
|
4
|
+
search: string;
|
|
5
|
+
hash: string;
|
|
6
|
+
};
|
|
7
|
+
export type NavigateOptions = {
|
|
8
|
+
replace?: boolean | undefined;
|
|
9
|
+
};
|
|
10
|
+
export interface RouterAdapter {
|
|
11
|
+
kind: RouterKind;
|
|
12
|
+
/** The current location; the same object until it changes, so it can feed `useSyncExternalStore`. */
|
|
13
|
+
location(): RouterLocation;
|
|
14
|
+
subscribe(listener: () => void): () => void;
|
|
15
|
+
/** `to` is a path with an optional query: `/customers/7?tab=orders`. */
|
|
16
|
+
navigate(to: string, options?: NavigateOptions): void;
|
|
17
|
+
href(to: string): string;
|
|
18
|
+
/** Asks before leaving while `when()` holds (unsaved changes); `ask` resolves to true to leave. */
|
|
19
|
+
block?(when: () => boolean, ask: () => Promise<boolean>): () => void;
|
|
20
|
+
}
|
|
21
|
+
export declare function splitPath(path: string): RouterLocation;
|
|
22
|
+
export declare function joinPath(location: {
|
|
23
|
+
pathname: string;
|
|
24
|
+
search: string;
|
|
25
|
+
}): string;
|
|
26
|
+
type BrowserWindow = {
|
|
27
|
+
location: Location;
|
|
28
|
+
history: History;
|
|
29
|
+
addEventListener: Window["addEventListener"];
|
|
30
|
+
removeEventListener: Window["removeEventListener"];
|
|
31
|
+
};
|
|
32
|
+
export type HashRouterOptions = {
|
|
33
|
+
/** What comes before the path in the fragment. Default `"#"`, so `#/customers/7`. */
|
|
34
|
+
prefix?: string;
|
|
35
|
+
window?: BrowserWindow;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Routes in the fragment (`/#/customers/7`): no server configuration, reload and links work, but the server never
|
|
39
|
+
* sees the route (no server rendering) and it clashes with OAuth's implicit flow.
|
|
40
|
+
*/
|
|
41
|
+
export declare function hashRouter(options?: HashRouterOptions): RouterAdapter;
|
|
42
|
+
export type HistoryRouterOptions = {
|
|
43
|
+
base?: string;
|
|
44
|
+
window?: BrowserWindow;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Clean URLs with `pushState` and no library. The server must answer every path with `index.html` (an SPA rewrite),
|
|
48
|
+
* or a reload of `/customers/7` is a 404.
|
|
49
|
+
*/
|
|
50
|
+
export declare function historyRouter(options?: HistoryRouterOptions): RouterAdapter;
|
|
51
|
+
export type MemoryRouter = RouterAdapter & {
|
|
52
|
+
readonly entries: readonly string[];
|
|
53
|
+
};
|
|
54
|
+
export declare function memoryRouter(initial?: string): MemoryRouter;
|
|
55
|
+
export type TanstackRouterLike = {
|
|
56
|
+
history: {
|
|
57
|
+
location: {
|
|
58
|
+
pathname: string;
|
|
59
|
+
search: string;
|
|
60
|
+
hash: string;
|
|
61
|
+
};
|
|
62
|
+
subscribe(listener: () => void): () => void;
|
|
63
|
+
push(path: string): void;
|
|
64
|
+
replace(path: string): void;
|
|
65
|
+
createHref(path: string): string;
|
|
66
|
+
block?(blocker: {
|
|
67
|
+
blockerFn: () => boolean | Promise<boolean>;
|
|
68
|
+
enableBeforeUnload?: boolean | (() => boolean);
|
|
69
|
+
}): () => void;
|
|
70
|
+
};
|
|
71
|
+
navigate?(options: {
|
|
72
|
+
href: string;
|
|
73
|
+
replace?: boolean;
|
|
74
|
+
}): Promise<void> | void;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* TanStack Router as a `RouterAdapter`. Pass the router, or a function that returns it when the router is created
|
|
78
|
+
* after the navigation manifest (its route tree comes from `nav.routes()`).
|
|
79
|
+
*/
|
|
80
|
+
export declare function tanstackRouter(router: TanstackRouterLike | (() => TanstackRouterLike)): RouterAdapter;
|
|
81
|
+
export {};
|