@jfdevelops/react-layout 0.24.0 → 0.24.2
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/dist/create-config/create-resource-links.cjs +59 -13
- package/dist/create-config/create-resource-links.cjs.map +1 -1
- package/dist/create-config/create-resource-links.d.cts +63 -4
- package/dist/create-config/create-resource-links.d.cts.map +1 -1
- package/dist/create-config/create-resource-links.d.mts +63 -4
- package/dist/create-config/create-resource-links.d.mts.map +1 -1
- package/dist/create-config/create-resource-links.mjs +59 -13
- package/dist/create-config/create-resource-links.mjs.map +1 -1
- package/dist/create-config/index.d.cts +1 -1
- package/dist/create-config/index.d.mts +1 -1
- package/package.json +1 -1
- package/skills/build-react-layouts/references/routing-and-navigation.md +105 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_layout_configuration_error = require('../errors/layout-configuration-error.cjs');
|
|
2
|
+
const require_resource = require('../resource.cjs');
|
|
2
3
|
|
|
3
4
|
//#region src/create-config/create-resource-links.ts
|
|
4
5
|
function resolveResourceLinkHref(value, resource, fallback) {
|
|
@@ -55,6 +56,21 @@ function createResourceLinkGroupId(group, index) {
|
|
|
55
56
|
if (typeof group.id === "string" && group.id.length > 0) return group.id;
|
|
56
57
|
return `group-${index}-${typeof group.label === "string" ? group.label : ""}-${Object.keys(group.links).join(",")}`;
|
|
57
58
|
}
|
|
59
|
+
function readResourceValue(resource) {
|
|
60
|
+
return typeof resource === "string" ? resource : resource.value;
|
|
61
|
+
}
|
|
62
|
+
function createResourceSelection(definitions) {
|
|
63
|
+
const selection = (() => definitions);
|
|
64
|
+
selection.isResource = require_resource.createIsValidResourceFn(definitions);
|
|
65
|
+
return selection;
|
|
66
|
+
}
|
|
67
|
+
function createRenderResourcesAccessor(resources, current) {
|
|
68
|
+
const accessor = Object.assign(() => resources, { current });
|
|
69
|
+
accessor.isResource = require_resource.createIsValidResourceFn(resources);
|
|
70
|
+
accessor.omit = ((...keys) => createResourceSelection(resources.filter((resource) => !keys.includes(readResourceValue(resource)))));
|
|
71
|
+
accessor.pick = ((...keys) => createResourceSelection(resources.filter((resource) => keys.includes(readResourceValue(resource)))));
|
|
72
|
+
return accessor;
|
|
73
|
+
}
|
|
58
74
|
function createResourceMapFn(resources) {
|
|
59
75
|
const helperNames = new Set(["get", "isValidResource"]);
|
|
60
76
|
const resourceKeys = new Set(resources.map((resource) => typeof resource === "string" ? resource : resource.value));
|
|
@@ -77,8 +93,9 @@ function createResourceMapFn(resources) {
|
|
|
77
93
|
return map;
|
|
78
94
|
};
|
|
79
95
|
}
|
|
80
|
-
function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
96
|
+
function createResourceLinksWithGroups(resources, createResourceMap, groups) {
|
|
81
97
|
if (!Array.isArray(groups)) throw new require_layout_configuration_error.LayoutConfigurationError("[createResourceLinks.withGroups]: \"groups\" must be an array.");
|
|
98
|
+
if (groups.length === 0) throw new require_layout_configuration_error.LayoutConfigurationError("[createResourceLinks.withGroups]: \"groups\" must include at least one group.");
|
|
82
99
|
const createdGroups = groups.map((group, index) => {
|
|
83
100
|
if (!group) throw new require_layout_configuration_error.LayoutConfigurationError(`[createResourceLinks.withGroups]: group at index ${index} is required.`);
|
|
84
101
|
if (typeof group !== "object") throw new require_layout_configuration_error.LayoutConfigurationError(`[createResourceLinks.withGroups]: group at index ${index} must be an object. Received ${typeof group}`);
|
|
@@ -95,10 +112,9 @@ function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
|
95
112
|
};
|
|
96
113
|
});
|
|
97
114
|
function resolveGroups(resource) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
return createdGroups.map((group) => ({
|
|
115
|
+
if (resource === void 0) throw new require_layout_configuration_error.LayoutConfigurationError("[createResourceLinks.withGroups]: \"resource\" is required.");
|
|
116
|
+
const currentResources = createRenderResourcesAccessor(resources, resource);
|
|
117
|
+
const resolvedGroups = createdGroups.map((group) => ({
|
|
102
118
|
...group,
|
|
103
119
|
icon: typeof group.icon === "function" ? group.icon(currentResources) : group.icon,
|
|
104
120
|
links: group.links.map((link) => ({
|
|
@@ -106,6 +122,10 @@ function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
|
106
122
|
icon: typeof link.icon === "function" ? link.icon(currentResources) : link.icon
|
|
107
123
|
}))
|
|
108
124
|
}));
|
|
125
|
+
Object.defineProperty(resolvedGroups, "findResource", { value(requestedResource) {
|
|
126
|
+
return resolvedGroups.find((group) => group.links.some((link) => link.resource === requestedResource));
|
|
127
|
+
} });
|
|
128
|
+
return resolvedGroups;
|
|
109
129
|
}
|
|
110
130
|
const callableGroups = new Proxy(resolveGroups, {
|
|
111
131
|
get(target, property, receiver) {
|
|
@@ -137,23 +157,49 @@ function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
|
137
157
|
} });
|
|
138
158
|
return callableGroups;
|
|
139
159
|
}
|
|
140
|
-
function
|
|
141
|
-
|
|
142
|
-
return function
|
|
143
|
-
return
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
160
|
+
function createDefineIcon() {
|
|
161
|
+
function defineIcon(render) {
|
|
162
|
+
return function createIcon(Icon) {
|
|
163
|
+
return function renderIcon(resource) {
|
|
164
|
+
return render({
|
|
165
|
+
resource,
|
|
166
|
+
Icon
|
|
167
|
+
});
|
|
168
|
+
};
|
|
147
169
|
};
|
|
170
|
+
}
|
|
171
|
+
defineIcon.input = function defineIconInput(defineInput) {
|
|
172
|
+
const resourceMarker = {};
|
|
173
|
+
const componentMarker = {};
|
|
174
|
+
defineInput({
|
|
175
|
+
resource() {
|
|
176
|
+
return resourceMarker;
|
|
177
|
+
},
|
|
178
|
+
icon() {
|
|
179
|
+
return componentMarker;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return { render(render) {
|
|
183
|
+
return function createIcon(input) {
|
|
184
|
+
return function renderIcon(resource) {
|
|
185
|
+
return render({
|
|
186
|
+
resource,
|
|
187
|
+
input
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
} };
|
|
148
192
|
};
|
|
193
|
+
return defineIcon;
|
|
149
194
|
}
|
|
150
195
|
function createResourceLinksFn(_resources) {
|
|
151
196
|
const createResourceMap = createResourceMapFn(_resources);
|
|
197
|
+
const defineIcon = createDefineIcon();
|
|
152
198
|
function createResourceLinks(config) {
|
|
153
199
|
return createResourceLinksFromConfig(config);
|
|
154
200
|
}
|
|
155
201
|
function withGroups(groups) {
|
|
156
|
-
return createResourceLinksWithGroups(createResourceMap, groups);
|
|
202
|
+
return createResourceLinksWithGroups(_resources, createResourceMap, groups);
|
|
157
203
|
}
|
|
158
204
|
return Object.assign(createResourceLinks, {
|
|
159
205
|
createResourceMap,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-resource-links.cjs","names":["LayoutConfigurationError"],"sources":["../../src/create-config/create-resource-links.ts"],"sourcesContent":["import type { ElementType, ReactNode } from 'react';\nimport {\n type LayoutResourceKey,\n type ResourceDefinition,\n} from '../resource';\nimport { LayoutConfigurationError } from '../errors';\nimport type { LayoutRenderResourcesAccessor } from './define-layout';\n\nexport type ResourceAnchorLinkFn<Resource extends string> = (\n resource: Resource,\n) => string;\n\nexport type ResourceLinkHref<Resource extends string> =\n | string\n | ResourceAnchorLinkFn<Resource>;\n\nexport type ResourceLinkConfigResource<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = LayoutResourceKey<Resources> | (string & {});\n\nexport type ResourceIconFn<Resource = any> = (resource: Resource) => ReactNode;\n\nexport type ResourceIcon<Resource = any> = ReactNode | ResourceIconFn<Resource>;\n\nexport interface DefineResourceIconFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <Icon extends ElementType = ElementType>(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n Icon: Icon;\n }) => ReactNode,\n ): (Icon: Icon) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;\n}\nexport interface CreateResourceLinkOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n> {\n /**\n * The label of the link. This is the text that will be displayed in the link.\n */\n label: string;\n /**\n * An optional icon to render when the link is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the link. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The href of the link. By default, the href will be `\"/\"`. While you **don't** need to include a hash,\n * if you do, it will override the `hash` property (if provided).\n */\n href?: ResourceLinkHref<Resource>;\n /**\n * The hash of the link. By default, the hash will be the resource name. If you need to customize it, you can provide\n * a string or a function that returns a string.\n *\n * @remarks A `\"#\"` is not required. The generated href will include it automatically.\n */\n hash?: ResourceLinkHref<Resource>;\n}\n\nexport type CreateResourceLinkConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = {\n [Key in Resource]?: CreateResourceLinkOptions<\n Resources,\n Key\n >;\n};\n\nexport type InferHashFromResourceLinkHref<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Hash extends string\n ? Hash\n : Hash extends ResourceAnchorLinkFn<Resource>\n ? ReturnType<Hash>\n : never;\n\nexport type CreatedResourceHref = {\n /**\n * The full generated href with the `hash` if provided.\n */\n full: string;\n /**\n * The given href. This is the same value as the `href` property of the config.\n */\n given: string;\n};\n\nexport type CreatedResourceLinkBase<Resource extends string> = {\n activeIcon?: ReactNode;\n href: CreatedResourceHref;\n label: string;\n resource: Resource;\n icon: ResourceIcon;\n};\nexport type CreateResourceLinkWithHash<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Omit<CreatedResourceLinkBase<Resource>, 'href'> & {\n href: CreatedResourceHref & {\n hash: InferHashFromResourceLinkHref<Resource, Hash>;\n };\n};\n\nexport type CreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Config,\n> = {\n [Resource in keyof Config]-?: Resource extends ResourceLinkConfigResource<Resources>\n ? Config[Resource] extends {\n hash: infer hash extends ResourceLinkHref<Resource>;\n }\n ? CreateResourceLinkWithHash<Resource, hash>\n : CreatedResourceLinkBase<Resource>\n : never;\n}[keyof Config];\n\nexport interface CreateResourceLinksFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n config: CreateResourceLinkConfig<Resources, Resource>,\n ): Array<\n CreatedResourceLink<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n >\n >;\n createResourceMap: CreateResourceMapFn<Resources>;\n defineIcon: DefineResourceIconFn<Resources>;\n withGroups: CreateResourceLinksWithGroupsFn<Resources>;\n}\n\nexport interface CreateResourceLinkGroupOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Links extends object =\n CreateResourceLinkConfig<Resources>,\n> {\n /**\n * An optional, caller-provided identity for the group. Provide this when you\n * combine groups from more than one `withGroups()` call into one list (e.g.\n * a module-owned nav section alongside a feature-owned one) — without it,\n * the id is derived from the group's `label` and its link resources, which\n * is enough to keep groups from a single call apart but can't distinguish\n * two independently-defined groups that happen to look identical.\n */\n id?: string;\n /**\n * The label of the group. This is the text that will be displayed for the group.\n */\n label?: string;\n /**\n * An optional icon to render when the group is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the group. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The resource links in this group.\n */\n links: Links;\n}\n\nexport type CreateResourceLinkGroupInput<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = CreateResourceLinkGroupOptions<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n>;\n\nexport type AnyCreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n [Resource in LayoutResourceKey<Resources>]:\n | CreatedResourceLinkBase<Resource>\n | CreateResourceLinkWithHash<Resource, string>;\n}[LayoutResourceKey<Resources>];\n\nexport type CreateResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n activeIcon?: ReactNode;\n id: string;\n label: string | null;\n icon: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n links: Array<AnyCreatedResourceLink<Resources>>;\n};\n\nexport type ResolvedResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = Omit<CreateResourceLinksWithGroups<Resources>, 'icon' | 'links'> & {\n icon: ReactNode;\n links: Array<\n Omit<AnyCreatedResourceLink<Resources>, 'icon'> & { icon: ReactNode }\n >;\n};\n\nexport type CreateResourceMap<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Value,\n> = Partial<Record<LayoutResourceKey<Resources>, Value>>;\n\nexport type ValidateResource<\n Resource extends string,\n Map extends object,\n> = Resource extends Extract<keyof Map, string> ? Resource : never;\n\nexport type CreatedResourceMap<Map extends object> = Map & {\n isValidResource(value: unknown): value is Extract<keyof Map, string>;\n get<const Resource extends string>(\n resource: ValidateResource<Resource, Map>,\n ): Map[typeof resource];\n};\n\nexport interface CreateResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <const Map extends CreateResourceMap<Resources, unknown>>(\n map: Map,\n ): CreatedResourceMap<Map>;\n}\n\nexport interface CreateResourceLinksWithGroupsFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n (groups: readonly []): CreateResourceLinksWithGroupsResult<Resources, never>;\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n ): CreateResourceLinksWithGroupsResult<Resources, Resource>;\n}\n\nexport type CreateResourceLinksWithGroupsResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = Array<CreateResourceLinksWithGroups<Resources>> & {\n (\n resource?: LayoutRenderResourcesAccessor<Resources>,\n ): Array<ResolvedResourceLinksWithGroups<Resources>>;\n createResourceMap: CreateResourceMapFn<Resources>;\n findResource<\n const RequestedResource extends LayoutResourceKey<Resources> | Resource,\n >(\n resource: RequestedResource,\n ): FindResourceGroupResult<Resources, Resource, RequestedResource>;\n};\n\nexport type FindResourceGroupResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n IncludedResource extends string,\n RequestedResource extends string,\n> = RequestedResource extends unknown\n ? RequestedResource extends IncludedResource\n ? CreateResourceLinksWithGroups<Resources>\n : undefined\n : never;\n\nfunction resolveResourceLinkHref<Resource extends string>(\n value: ResourceLinkHref<Resource> | undefined,\n resource: Resource,\n fallback: string,\n) {\n return typeof value === 'function' ? value(resource) : (value ?? fallback);\n}\n\nfunction normalizeResourceLinkHash(hash: string) {\n return hash.replace(/^#+/, '');\n}\n\nfunction createFullResourceLinkHref(href: string, hash: string) {\n if (href.includes('#')) {\n return href;\n }\n\n return `${href}#${normalizeResourceLinkHash(hash)}`;\n}\n\nfunction createResourceLinksFromConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(config: CreateResourceLinkConfig<Resources, Resource>) {\n return Object.entries(config).map(([resource, config]) => {\n if (!config) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" must be an object for the ${resource} resource. Received ${typeof config}`,\n );\n }\n\n if (!('label' in config)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config.label !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" must be a string for the ${resource} resource. Received ${typeof config.label}`,\n );\n }\n\n const linkConfig = config as CreateResourceLinkOptions<\n Resources,\n typeof resource\n >;\n\n if (\n 'href' in linkConfig &&\n linkConfig.href !== undefined &&\n typeof linkConfig.href !== 'string' &&\n typeof linkConfig.href !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"href\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.href}`,\n );\n }\n\n if (\n 'hash' in linkConfig &&\n linkConfig.hash !== undefined &&\n typeof linkConfig.hash !== 'string' &&\n typeof linkConfig.hash !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"hash\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.hash}`,\n );\n }\n\n const givenHref = resolveResourceLinkHref(linkConfig.href, resource, '/');\n const givenHash = resolveResourceLinkHref(\n linkConfig.hash,\n resource,\n resource,\n );\n const href = {\n given: givenHref,\n full: createFullResourceLinkHref(givenHref, givenHash),\n ...(linkConfig.hash !== undefined && !givenHref.includes('#')\n ? { hash: normalizeResourceLinkHash(givenHash) }\n : {}),\n };\n\n return {\n ...(linkConfig.activeIcon !== undefined\n ? { activeIcon: linkConfig.activeIcon }\n : {}),\n href,\n label: config.label,\n resource,\n icon: 'icon' in config ? config.icon : null,\n };\n });\n}\n\n/**\n * Deterministic, not random: a random id (the previous implementation used\n * `crypto.randomUUID()` / `Math.random()`) would make `withGroups()` unsafe to\n * call from module scope — some runtimes (e.g. Cloudflare Workers) disallow\n * generating random values outside a request handler, and config arrays like\n * this are routinely defined as top-level constants.\n *\n * Falls back to the group's own shape — its `label` and its link resource\n * keys — rather than just the array index, so groups from *separate*\n * `withGroups()` calls that get concatenated into one list (a module-owned nav\n * section next to a feature-owned one, say) don't collide just because they're\n * both first in their own call. Two groups that are genuinely identical in\n * shape will still collide; pass an explicit `id` on the group to rule that\n * out entirely.\n */\nfunction createResourceLinkGroupId(\n group: { id?: unknown; label?: unknown; links: object },\n index: number,\n) {\n if (typeof group.id === 'string' && group.id.length > 0) {\n return group.id;\n }\n\n const label = typeof group.label === 'string' ? group.label : '';\n const linkKeys = Object.keys(group.links).join(',');\n return `group-${index}-${label}-${linkKeys}`;\n}\n\nfunction createResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(resources: Resources): CreateResourceMapFn<Resources> {\n const helperNames = new Set(['get', 'isValidResource']);\n const resourceKeys = new Set(\n resources.map((resource) =>\n typeof resource === 'string' ? resource : resource.value,\n ),\n );\n\n return function createResourceMap<\n const Map extends CreateResourceMap<Resources, unknown>,\n >(map: Map) {\n const configuredResources = Object.keys(map);\n\n for (const resource of configuredResources) {\n if (!resourceKeys.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: unknown resource \"${resource}\".`,\n );\n }\n\n if (helperNames.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: resource \"${resource}\" conflicts with a map helper.`,\n );\n }\n }\n\n const configuredResourceKeys = new Set(configuredResources);\n const resourceValues = new Map(\n configuredResources.map((resource) => [\n resource,\n (map as Record<string, unknown>)[resource],\n ]),\n );\n\n Object.defineProperties(map, {\n get: {\n value(resource: string) {\n return resourceValues.get(resource);\n },\n },\n isValidResource: {\n value(value: unknown): value is Extract<keyof Map, string> {\n return (\n typeof value === 'string' && configuredResourceKeys.has(value)\n );\n },\n },\n });\n\n return map as CreatedResourceMap<Map>;\n };\n}\n\nfunction createResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(\n createResourceMap: CreateResourceMapFn<Resources>,\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n): CreateResourceLinksWithGroupsResult<Resources, Resource> {\n if (!Array.isArray(groups)) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"groups\" must be an array.',\n );\n }\n\n const createdGroups = groups.map((group, index) => {\n if (!group) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} is required.`,\n );\n }\n\n if (typeof group !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} must be an object. Received ${typeof group}`,\n );\n }\n\n if (\n 'label' in group &&\n group.label !== undefined &&\n typeof group.label !== 'string'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"label\" must be a string for group at index ${index}. Received ${typeof group.label}`,\n );\n }\n\n if ('id' in group && group.id !== undefined && typeof group.id !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"id\" must be a string for group at index ${index}. Received ${typeof group.id}`,\n );\n }\n\n if (!('links' in group)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" is required for group at index ${index}.`,\n );\n }\n\n if (\n typeof group.links !== 'object' ||\n group.links === null ||\n Array.isArray(group.links)\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" must be an object for group at index ${index}. Received ${Array.isArray(group.links) ? 'array' : typeof group.links}`,\n );\n }\n\n return {\n ...(group.activeIcon !== undefined\n ? { activeIcon: group.activeIcon }\n : {}),\n id: createResourceLinkGroupId(group, index),\n label: 'label' in group ? (group.label ?? null) : null,\n icon: 'icon' in group ? group.icon : null,\n links: createResourceLinksFromConfig(group.links),\n };\n }) as Array<CreateResourceLinksWithGroups<Resources>>;\n\n function resolveGroups(\n resource?: LayoutRenderResourcesAccessor<Resources>,\n ) {\n const hasFunctionalIcon = createdGroups.some(\n (group) =>\n typeof group.icon === 'function' ||\n group.links.some((link) => typeof link.icon === 'function'),\n );\n\n if (resource === undefined && hasFunctionalIcon) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"resource\" is required when resolving functional icons.',\n );\n }\n\n const currentResources = resource as LayoutRenderResourcesAccessor<Resources>;\n\n return createdGroups.map((group) => ({\n ...group,\n icon:\n typeof group.icon === 'function'\n ? group.icon(currentResources)\n : group.icon,\n links: group.links.map((link) => ({\n ...link,\n icon:\n typeof link.icon === 'function'\n ? link.icon(currentResources)\n : link.icon,\n })),\n }));\n }\n\n const callableGroups = new Proxy(resolveGroups, {\n get(target, property, receiver) {\n if (property === 'createResourceMap') {\n return createResourceMap;\n }\n\n if (property === Symbol.iterator) {\n return createdGroups[Symbol.iterator].bind(createdGroups);\n }\n\n if (property === 'toJSON') {\n return () => createdGroups;\n }\n\n if (property in createdGroups) {\n const value = Reflect.get(createdGroups, property);\n return typeof value === 'function' ? value.bind(createdGroups) : value;\n }\n\n return Reflect.get(target, property, receiver);\n },\n getOwnPropertyDescriptor(target, property) {\n return (\n Reflect.getOwnPropertyDescriptor(createdGroups, property) ??\n Reflect.getOwnPropertyDescriptor(target, property)\n );\n },\n getPrototypeOf() {\n return Array.prototype;\n },\n ownKeys(target) {\n return [\n ...new Set([\n ...Reflect.ownKeys(target),\n ...Reflect.ownKeys(createdGroups),\n ]),\n ];\n },\n set(target, property, value, receiver) {\n if (property in createdGroups || /^\\d+$/.test(String(property))) {\n return Reflect.set(createdGroups, property, value);\n }\n\n return Reflect.set(target, property, value, receiver);\n },\n });\n Object.defineProperty(createdGroups, 'findResource', {\n value(resource: ResourceLinkConfigResource<Resources>) {\n return createdGroups.find((group) =>\n group.links.some((link) => link.resource === resource),\n );\n },\n });\n\n return callableGroups as CreateResourceLinksWithGroupsResult<\n Resources,\n Resource\n >;\n}\n\nfunction defineIcon<Icon extends ElementType = ElementType, Resource = any>(\n render: (options: { resource: Resource; Icon: Icon }) => ReactNode,\n) {\n return function createIcon(Icon: Icon): ResourceIconFn<Resource> {\n return function renderIcon(resource) {\n return render({ resource, Icon });\n };\n };\n}\n\nexport function createResourceLinksFn<\n const Resources extends ReadonlyArray<ResourceDefinition>,\n>(_resources: Resources): CreateResourceLinksFn<Resources> {\n const createResourceMap = createResourceMapFn(_resources);\n\n function createResourceLinks<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(config: CreateResourceLinkConfig<Resources, Resource>) {\n return createResourceLinksFromConfig(config);\n }\n\n function withGroups<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n ) {\n return createResourceLinksWithGroups(createResourceMap, groups);\n }\n\n return Object.assign(createResourceLinks, {\n createResourceMap,\n defineIcon,\n withGroups,\n }) as CreateResourceLinksFn<Resources>;\n}\n"],"mappings":";;;AA2QA,SAAS,wBACP,OACA,UACA,UACA;CACA,OAAO,OAAO,UAAU,aAAa,MAAM,QAAQ,IAAK,SAAS;AACnE;AAEA,SAAS,0BAA0B,MAAc;CAC/C,OAAO,KAAK,QAAQ,OAAO,EAAE;AAC/B;AAEA,SAAS,2BAA2B,MAAc,MAAc;CAC9D,IAAI,KAAK,SAAS,GAAG,GACnB,OAAO;CAGT,OAAO,GAAG,KAAK,GAAG,0BAA0B,IAAI;AAClD;AAEA,SAAS,8BAGP,QAAuD;CACvD,OAAO,OAAO,QAAQ,MAAM,EAAE,KAAK,CAAC,UAAU,YAAY;EACxD,IAAI,CAAC,QACH,MAAM,IAAIA,4DACR,uDAAuD,SAAS,WAClE;EAGF,IAAI,OAAO,WAAW,UACpB,MAAM,IAAIA,4DACR,6DAA6D,SAAS,sBAAsB,OAAO,QACrG;EAGF,IAAI,EAAE,WAAW,SACf,MAAM,IAAIA,4DACR,sDAAsD,SAAS,WACjE;EAGF,IAAI,OAAO,OAAO,UAAU,UAC1B,MAAM,IAAIA,4DACR,2DAA2D,SAAS,sBAAsB,OAAO,OAAO,OAC1G;EAGF,MAAM,aAAa;EAKnB,IACE,UAAU,cACV,WAAW,SAAS,UACpB,OAAO,WAAW,SAAS,YAC3B,OAAO,WAAW,SAAS,YAE3B,MAAM,IAAIA,4DACR,sEAAsE,SAAS,sBAAsB,OAAO,WAAW,MACzH;EAGF,IACE,UAAU,cACV,WAAW,SAAS,UACpB,OAAO,WAAW,SAAS,YAC3B,OAAO,WAAW,SAAS,YAE3B,MAAM,IAAIA,4DACR,sEAAsE,SAAS,sBAAsB,OAAO,WAAW,MACzH;EAGF,MAAM,YAAY,wBAAwB,WAAW,MAAM,UAAU,GAAG;EACxE,MAAM,YAAY,wBAChB,WAAW,MACX,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D,OAAO,QAAQ,IAAI,QAAQ,UAAU,OAAO,QAAQ;EACtD;CACF,CAAC;CACD,OAAO,eAAe,eAAe,gBAAgB,EACnD,MAAM,UAAiD;EACrD,OAAO,cAAc,MAAM,UACzB,MAAM,MAAM,MAAM,SAAS,KAAK,aAAa,QAAQ,CACvD;CACF,EACF,CAAC;CAED,OAAO;AAIT;AAEA,SAAS,WACP,QACA;CACA,OAAO,SAAS,WAAW,MAAsC;EAC/D,OAAO,SAAS,WAAW,UAAU;GACnC,OAAO,OAAO;IAAE;IAAU;GAAK,CAAC;EAClC;CACF;AACF;AAEA,SAAgB,sBAEd,YAAyD;CACzD,MAAM,oBAAoB,oBAAoB,UAAU;CAExD,SAAS,oBAEP,QAAuD;EACvD,OAAO,8BAA8B,MAAM;CAC7C;CAEA,SAAS,WAGP,QACA;EACA,OAAO,8BAA8B,mBAAmB,MAAM;CAChE;CAEA,OAAO,OAAO,OAAO,qBAAqB;EACxC;EACA;EACA;CACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"create-resource-links.cjs","names":["LayoutConfigurationError","createIsValidResourceFn"],"sources":["../../src/create-config/create-resource-links.ts"],"sourcesContent":["import type { ElementType, ReactNode } from 'react';\nimport type { NonEmptyReadonlyArray } from '@jfdevelops/react-layout-validator';\nimport {\n createIsValidResourceFn,\n type LayoutResourceKey,\n type ResourceDefinition,\n} from '../resource';\nimport { LayoutConfigurationError } from '../errors';\nimport type {\n LayoutRenderResourcesAccessor,\n ResourceSelection,\n} from './define-layout';\n\nexport type ResourceAnchorLinkFn<Resource extends string> = (\n resource: Resource,\n) => string;\n\nexport type ResourceLinkHref<Resource extends string> =\n | string\n | ResourceAnchorLinkFn<Resource>;\n\nexport type ResourceLinkConfigResource<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = LayoutResourceKey<Resources> | (string & {});\n\nexport type ResourceIconFn<Resource = any> = (resource: Resource) => ReactNode;\n\nexport type ResourceIcon<Resource = any> = ReactNode | ResourceIconFn<Resource>;\n\ndeclare const resourceIconInputMarker: unique symbol;\ndeclare const resourceIconComponentInputMarker: unique symbol;\n\ntype ResourceIconInputMarker<Value> = {\n readonly [resourceIconInputMarker]: Value;\n};\n\ntype ResourceIconComponentInputMarker<Icon extends ElementType> = {\n readonly [resourceIconComponentInputMarker]: Icon;\n};\n\nexport type ResolveResourceIconInput<Definition> =\n Definition extends ResourceIconComponentInputMarker<infer Icon>\n ? Icon\n : Definition extends ResourceIconInputMarker<infer Value>\n ? Value\n : Definition extends ReadonlyArray<unknown>\n ? { [Index in keyof Definition]: ResolveResourceIconInput<Definition[Index]> }\n : Definition extends object\n ? { [Key in keyof Definition]: ResolveResourceIconInput<Definition[Key]> }\n : Definition;\n\nexport interface DefineResourceIconInputHelpers<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n resource(): ResourceIconInputMarker<LayoutResourceKey<Resources>>;\n icon<Icon extends ElementType = ElementType>(): ResourceIconComponentInputMarker<Icon>;\n}\n\nexport interface DefineResourceIconInputResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Definition,\n> {\n render(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n input: ResolveResourceIconInput<Definition>;\n }) => ReactNode,\n ): (\n input: ResolveResourceIconInput<Definition>,\n ) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;\n}\n\nexport interface DefineResourceIconFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <Icon extends ElementType = ElementType>(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n Icon: Icon;\n }) => ReactNode,\n ): (Icon: Icon) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;\n input<Definition>(\n defineInput: (\n helpers: DefineResourceIconInputHelpers<Resources>,\n ) => Definition,\n ): DefineResourceIconInputResult<Resources, Definition>;\n}\nexport interface CreateResourceLinkOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n> {\n /**\n * The label of the link. This is the text that will be displayed in the link.\n */\n label: string;\n /**\n * An optional icon to render when the link is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the link. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The href of the link. By default, the href will be `\"/\"`. While you **don't** need to include a hash,\n * if you do, it will override the `hash` property (if provided).\n */\n href?: ResourceLinkHref<Resource>;\n /**\n * The hash of the link. By default, the hash will be the resource name. If you need to customize it, you can provide\n * a string or a function that returns a string.\n *\n * @remarks A `\"#\"` is not required. The generated href will include it automatically.\n */\n hash?: ResourceLinkHref<Resource>;\n}\n\nexport type CreateResourceLinkConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = {\n [Key in Resource]?: CreateResourceLinkOptions<\n Resources,\n Key\n >;\n};\n\nexport type InferHashFromResourceLinkHref<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Hash extends string\n ? Hash\n : Hash extends ResourceAnchorLinkFn<Resource>\n ? ReturnType<Hash>\n : never;\n\nexport type CreatedResourceHref = {\n /**\n * The full generated href with the `hash` if provided.\n */\n full: string;\n /**\n * The given href. This is the same value as the `href` property of the config.\n */\n given: string;\n};\n\nexport type CreatedResourceLinkBase<Resource extends string> = {\n activeIcon?: ReactNode;\n href: CreatedResourceHref;\n label: string;\n resource: Resource;\n icon: ResourceIcon;\n};\nexport type CreateResourceLinkWithHash<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Omit<CreatedResourceLinkBase<Resource>, 'href'> & {\n href: CreatedResourceHref & {\n hash: InferHashFromResourceLinkHref<Resource, Hash>;\n };\n};\n\nexport type CreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Config,\n> = {\n [Resource in keyof Config]-?: Resource extends ResourceLinkConfigResource<Resources>\n ? Config[Resource] extends {\n hash: infer hash extends ResourceLinkHref<Resource>;\n }\n ? CreateResourceLinkWithHash<Resource, hash>\n : CreatedResourceLinkBase<Resource>\n : never;\n}[keyof Config];\n\nexport interface CreateResourceLinksFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n config: CreateResourceLinkConfig<Resources, Resource>,\n ): Array<\n CreatedResourceLink<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n >\n >;\n createResourceMap: CreateResourceMapFn<Resources>;\n defineIcon: DefineResourceIconFn<Resources>;\n withGroups: CreateResourceLinksWithGroupsFn<Resources>;\n}\n\nexport interface CreateResourceLinkGroupOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Links extends object =\n CreateResourceLinkConfig<Resources>,\n> {\n /**\n * An optional, caller-provided identity for the group. Provide this when you\n * combine groups from more than one `withGroups()` call into one list (e.g.\n * a module-owned nav section alongside a feature-owned one) — without it,\n * the id is derived from the group's `label` and its link resources, which\n * is enough to keep groups from a single call apart but can't distinguish\n * two independently-defined groups that happen to look identical.\n */\n id?: string;\n /**\n * The label of the group. This is the text that will be displayed for the group.\n */\n label?: string;\n /**\n * An optional icon to render when the group is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the group. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The resource links in this group.\n */\n links: Links;\n}\n\nexport type CreateResourceLinkGroupInput<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = CreateResourceLinkGroupOptions<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n>;\n\nexport type AnyCreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n [Resource in LayoutResourceKey<Resources>]:\n | CreatedResourceLinkBase<Resource>\n | CreateResourceLinkWithHash<Resource, string>;\n}[LayoutResourceKey<Resources>];\n\nexport type CreateResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n activeIcon?: ReactNode;\n id: string;\n label: string | null;\n icon: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n links: Array<AnyCreatedResourceLink<Resources>>;\n};\n\nexport type ResolvedResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = Omit<CreateResourceLinksWithGroups<Resources>, 'icon' | 'links'> & {\n icon: ReactNode;\n links: Array<\n Omit<AnyCreatedResourceLink<Resources>, 'icon'> & { icon: ReactNode }\n >;\n};\n\nexport type CreateResourceMap<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Value,\n> = Partial<Record<LayoutResourceKey<Resources>, Value>>;\n\nexport type ValidateResource<\n Resource extends string,\n Map extends object,\n> = Resource extends Extract<keyof Map, string> ? Resource : never;\n\nexport type CreatedResourceMap<Map extends object> = Map & {\n isValidResource(value: unknown): value is Extract<keyof Map, string>;\n get<const Resource extends string>(\n resource: ValidateResource<Resource, Map>,\n ): Map[typeof resource];\n};\n\nexport interface CreateResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n /**\n * Creates a resource map for this layout.\n *\n * @param map Values keyed by this layout's resources. An unknown resource,\n * or a key that collides with a map helper, is rejected.\n */\n <const Map extends CreateResourceMap<Resources, unknown>>(\n map: Map,\n ): CreatedResourceMap<Map>;\n}\n\nexport interface CreateResourceLinksWithGroupsFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n /**\n * Groups resource links into sections.\n *\n * @param groups The sections to create. At least one is required. The link\n * keys in those sections are the resources `findResource` can resolve.\n */\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n groups: NonEmptyReadonlyArray<\n CreateResourceLinkGroupInput<Resources, Resource>\n >,\n ): CreateResourceLinksWithGroupsResult<Resources, Resource>;\n}\n\nexport type CreateResourceLinksWithGroupsResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = Array<CreateResourceLinksWithGroups<Resources>> & {\n /**\n * Resolves each group's icon and its link icons for the active resource.\n *\n * Functional icons receive the resources accessor for `resource`. Read the\n * groups value directly when you do not need to resolve icons.\n *\n * @param resource The active resource key.\n */\n (\n resource: LayoutResourceKey<Resources>,\n ): ResolvedResourceLinksWithGroupsResult<Resources, Resource>;\n /**\n * Creates a resource map for this layout. Same helper as\n * `createResourceLinks.createResourceMap`.\n *\n * @param map Values keyed by this layout's resources. An unknown resource,\n * or a key that collides with a map helper, is rejected.\n */\n createResourceMap: CreateResourceMapFn<Resources>;\n /**\n * Returns the group whose links include `resource`.\n *\n * The result is `undefined` when that resource was not configured in any\n * group. A resource outside this layout is rejected by the type.\n *\n * @param resource The resource whose group should be returned.\n */\n findResource<\n const RequestedResource extends LayoutResourceKey<Resources> | Resource,\n >(\n resource: RequestedResource,\n ): FindResourceGroupResult<Resources, Resource, RequestedResource>;\n};\n\nexport type ResolvedResourceLinksWithGroupsResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = Array<ResolvedResourceLinksWithGroups<Resources>> & {\n /** Returns the resolved group whose links include `resource`. */\n findResource<\n const RequestedResource extends LayoutResourceKey<Resources> | Resource,\n >(\n resource: RequestedResource,\n ): FindResolvedResourceGroupResult<Resources, Resource, RequestedResource>;\n};\n\nexport type FindResourceGroupResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n IncludedResource extends string,\n RequestedResource extends string,\n> = RequestedResource extends unknown\n ? RequestedResource extends IncludedResource\n ? CreateResourceLinksWithGroups<Resources>\n : undefined\n : never;\n\nexport type FindResolvedResourceGroupResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n IncludedResource extends string,\n RequestedResource extends string,\n> = RequestedResource extends unknown\n ? RequestedResource extends IncludedResource\n ? ResolvedResourceLinksWithGroups<Resources>\n : undefined\n : never;\n\nfunction resolveResourceLinkHref<Resource extends string>(\n value: ResourceLinkHref<Resource> | undefined,\n resource: Resource,\n fallback: string,\n) {\n return typeof value === 'function' ? value(resource) : (value ?? fallback);\n}\n\nfunction normalizeResourceLinkHash(hash: string) {\n return hash.replace(/^#+/, '');\n}\n\nfunction createFullResourceLinkHref(href: string, hash: string) {\n if (href.includes('#')) {\n return href;\n }\n\n return `${href}#${normalizeResourceLinkHash(hash)}`;\n}\n\nfunction createResourceLinksFromConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(config: CreateResourceLinkConfig<Resources, Resource>) {\n return Object.entries(config).map(([resource, config]) => {\n if (!config) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" must be an object for the ${resource} resource. Received ${typeof config}`,\n );\n }\n\n if (!('label' in config)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config.label !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" must be a string for the ${resource} resource. Received ${typeof config.label}`,\n );\n }\n\n const linkConfig = config as CreateResourceLinkOptions<\n Resources,\n typeof resource\n >;\n\n if (\n 'href' in linkConfig &&\n linkConfig.href !== undefined &&\n typeof linkConfig.href !== 'string' &&\n typeof linkConfig.href !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"href\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.href}`,\n );\n }\n\n if (\n 'hash' in linkConfig &&\n linkConfig.hash !== undefined &&\n typeof linkConfig.hash !== 'string' &&\n typeof linkConfig.hash !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"hash\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.hash}`,\n );\n }\n\n const givenHref = resolveResourceLinkHref(linkConfig.href, resource, '/');\n const givenHash = resolveResourceLinkHref(\n linkConfig.hash,\n resource,\n resource,\n );\n const href = {\n given: givenHref,\n full: createFullResourceLinkHref(givenHref, givenHash),\n ...(linkConfig.hash !== undefined && !givenHref.includes('#')\n ? { hash: normalizeResourceLinkHash(givenHash) }\n : {}),\n };\n\n return {\n ...(linkConfig.activeIcon !== undefined\n ? { activeIcon: linkConfig.activeIcon }\n : {}),\n href,\n label: config.label,\n resource,\n icon: 'icon' in config ? config.icon : null,\n };\n });\n}\n\n/**\n * Deterministic, not random: a random id (the previous implementation used\n * `crypto.randomUUID()` / `Math.random()`) would make `withGroups()` unsafe to\n * call from module scope — some runtimes (e.g. Cloudflare Workers) disallow\n * generating random values outside a request handler, and config arrays like\n * this are routinely defined as top-level constants.\n *\n * Falls back to the group's own shape — its `label` and its link resource\n * keys — rather than just the array index, so groups from *separate*\n * `withGroups()` calls that get concatenated into one list (a module-owned nav\n * section next to a feature-owned one, say) don't collide just because they're\n * both first in their own call. Two groups that are genuinely identical in\n * shape will still collide; pass an explicit `id` on the group to rule that\n * out entirely.\n */\nfunction createResourceLinkGroupId(\n group: { id?: unknown; label?: unknown; links: object },\n index: number,\n) {\n if (typeof group.id === 'string' && group.id.length > 0) {\n return group.id;\n }\n\n const label = typeof group.label === 'string' ? group.label : '';\n const linkKeys = Object.keys(group.links).join(',');\n return `group-${index}-${label}-${linkKeys}`;\n}\n\nfunction readResourceValue(resource: ResourceDefinition) {\n return typeof resource === 'string' ? resource : resource.value;\n}\n\nfunction createResourceSelection(\n definitions: ReadonlyArray<ResourceDefinition>,\n): ResourceSelection<ReadonlyArray<ResourceDefinition>> {\n const selection = (() => definitions) as ResourceSelection<\n ReadonlyArray<ResourceDefinition>\n >;\n\n selection.isResource = createIsValidResourceFn(definitions);\n\n return selection;\n}\n\nfunction createRenderResourcesAccessor<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(resources: Resources, current: LayoutResourceKey<Resources>) {\n const accessor = Object.assign(() => resources, {\n current,\n }) as LayoutRenderResourcesAccessor<Resources>;\n\n accessor.isResource = createIsValidResourceFn(resources);\n accessor.omit = ((...keys: string[]) =>\n createResourceSelection(\n resources.filter((resource) => !keys.includes(readResourceValue(resource))),\n )) as LayoutRenderResourcesAccessor<Resources>['omit'];\n accessor.pick = ((...keys: string[]) =>\n createResourceSelection(\n resources.filter((resource) => keys.includes(readResourceValue(resource))),\n )) as LayoutRenderResourcesAccessor<Resources>['pick'];\n\n return accessor;\n}\n\nfunction createResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(resources: Resources): CreateResourceMapFn<Resources> {\n const helperNames = new Set(['get', 'isValidResource']);\n const resourceKeys = new Set(\n resources.map((resource) =>\n typeof resource === 'string' ? resource : resource.value,\n ),\n );\n\n return function createResourceMap<\n const Map extends CreateResourceMap<Resources, unknown>,\n >(map: Map) {\n const configuredResources = Object.keys(map);\n\n for (const resource of configuredResources) {\n if (!resourceKeys.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: unknown resource \"${resource}\".`,\n );\n }\n\n if (helperNames.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: resource \"${resource}\" conflicts with a map helper.`,\n );\n }\n }\n\n const configuredResourceKeys = new Set(configuredResources);\n const resourceValues = new Map(\n configuredResources.map((resource) => [\n resource,\n (map as Record<string, unknown>)[resource],\n ]),\n );\n\n Object.defineProperties(map, {\n get: {\n value(resource: string) {\n return resourceValues.get(resource);\n },\n },\n isValidResource: {\n value(value: unknown): value is Extract<keyof Map, string> {\n return (\n typeof value === 'string' && configuredResourceKeys.has(value)\n );\n },\n },\n });\n\n return map as CreatedResourceMap<Map>;\n };\n}\n\nfunction createResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(\n resources: Resources,\n createResourceMap: CreateResourceMapFn<Resources>,\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n): CreateResourceLinksWithGroupsResult<Resources, Resource> {\n if (!Array.isArray(groups)) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"groups\" must be an array.',\n );\n }\n\n if (groups.length === 0) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"groups\" must include at least one group.',\n );\n }\n\n const createdGroups = groups.map((group, index) => {\n if (!group) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} is required.`,\n );\n }\n\n if (typeof group !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} must be an object. Received ${typeof group}`,\n );\n }\n\n if (\n 'label' in group &&\n group.label !== undefined &&\n typeof group.label !== 'string'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"label\" must be a string for group at index ${index}. Received ${typeof group.label}`,\n );\n }\n\n if ('id' in group && group.id !== undefined && typeof group.id !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"id\" must be a string for group at index ${index}. Received ${typeof group.id}`,\n );\n }\n\n if (!('links' in group)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" is required for group at index ${index}.`,\n );\n }\n\n if (\n typeof group.links !== 'object' ||\n group.links === null ||\n Array.isArray(group.links)\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" must be an object for group at index ${index}. Received ${Array.isArray(group.links) ? 'array' : typeof group.links}`,\n );\n }\n\n return {\n ...(group.activeIcon !== undefined\n ? { activeIcon: group.activeIcon }\n : {}),\n id: createResourceLinkGroupId(group, index),\n label: 'label' in group ? (group.label ?? null) : null,\n icon: 'icon' in group ? group.icon : null,\n links: createResourceLinksFromConfig(group.links),\n };\n }) as Array<CreateResourceLinksWithGroups<Resources>>;\n\n function resolveGroups(resource?: LayoutResourceKey<Resources>) {\n if (resource === undefined) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"resource\" is required.',\n );\n }\n\n const currentResources = createRenderResourcesAccessor(resources, resource);\n\n const resolvedGroups = createdGroups.map((group) => ({\n ...group,\n icon:\n typeof group.icon === 'function'\n ? group.icon(currentResources)\n : group.icon,\n links: group.links.map((link) => ({\n ...link,\n icon:\n typeof link.icon === 'function'\n ? link.icon(currentResources)\n : link.icon,\n })),\n }));\n\n Object.defineProperty(resolvedGroups, 'findResource', {\n value(requestedResource: ResourceLinkConfigResource<Resources>) {\n return resolvedGroups.find((group) =>\n group.links.some((link) => link.resource === requestedResource),\n );\n },\n });\n\n return resolvedGroups as ResolvedResourceLinksWithGroupsResult<\n Resources,\n Resource\n >;\n }\n\n const callableGroups = new Proxy(resolveGroups, {\n get(target, property, receiver) {\n if (property === 'createResourceMap') {\n return createResourceMap;\n }\n\n if (property === Symbol.iterator) {\n return createdGroups[Symbol.iterator].bind(createdGroups);\n }\n\n if (property === 'toJSON') {\n return () => createdGroups;\n }\n\n if (property in createdGroups) {\n const value = Reflect.get(createdGroups, property);\n return typeof value === 'function' ? value.bind(createdGroups) : value;\n }\n\n return Reflect.get(target, property, receiver);\n },\n getOwnPropertyDescriptor(target, property) {\n return (\n Reflect.getOwnPropertyDescriptor(createdGroups, property) ??\n Reflect.getOwnPropertyDescriptor(target, property)\n );\n },\n getPrototypeOf() {\n return Array.prototype;\n },\n ownKeys(target) {\n return [\n ...new Set([\n ...Reflect.ownKeys(target),\n ...Reflect.ownKeys(createdGroups),\n ]),\n ];\n },\n set(target, property, value, receiver) {\n if (property in createdGroups || /^\\d+$/.test(String(property))) {\n return Reflect.set(createdGroups, property, value);\n }\n\n return Reflect.set(target, property, value, receiver);\n },\n });\n Object.defineProperty(createdGroups, 'findResource', {\n value(resource: ResourceLinkConfigResource<Resources>) {\n return createdGroups.find((group) =>\n group.links.some((link) => link.resource === resource),\n );\n },\n });\n\n return callableGroups as CreateResourceLinksWithGroupsResult<\n Resources,\n Resource\n >;\n}\n\nfunction createDefineIcon<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(): DefineResourceIconFn<Resources> {\n function defineIcon<Icon extends ElementType = ElementType>(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n Icon: Icon;\n }) => ReactNode,\n ) {\n return function createIcon(Icon: Icon) {\n return function renderIcon(\n resource: LayoutRenderResourcesAccessor<Resources>,\n ) {\n return render({ resource, Icon });\n };\n };\n }\n\n defineIcon.input = function defineIconInput<Definition>(\n defineInput: (\n helpers: DefineResourceIconInputHelpers<Resources>,\n ) => Definition,\n ): DefineResourceIconInputResult<Resources, Definition> {\n const resourceMarker = {} as ResourceIconInputMarker<never>;\n const componentMarker = {} as ResourceIconComponentInputMarker<ElementType>;\n\n defineInput({\n resource() {\n return resourceMarker;\n },\n icon<Icon extends ElementType = ElementType>() {\n return componentMarker as ResourceIconComponentInputMarker<Icon>;\n },\n });\n\n return {\n render(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n input: ResolveResourceIconInput<Definition>;\n }) => ReactNode,\n ) {\n return function createIcon(\n input: ResolveResourceIconInput<Definition>,\n ) {\n return function renderIcon(\n resource: LayoutRenderResourcesAccessor<Resources>,\n ) {\n return render({ resource, input });\n };\n };\n },\n };\n };\n\n return defineIcon;\n}\n\nexport function createResourceLinksFn<\n const Resources extends ReadonlyArray<ResourceDefinition>,\n>(_resources: Resources): CreateResourceLinksFn<Resources> {\n const createResourceMap = createResourceMapFn(_resources);\n const defineIcon = createDefineIcon<Resources>();\n\n function createResourceLinks<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(config: CreateResourceLinkConfig<Resources, Resource>) {\n return createResourceLinksFromConfig(config);\n }\n\n function withGroups<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(\n groups: NonEmptyReadonlyArray<\n CreateResourceLinkGroupInput<Resources, Resource>\n >,\n ) {\n return createResourceLinksWithGroups(_resources, createResourceMap, groups);\n }\n\n return Object.assign(createResourceLinks, {\n createResourceMap,\n defineIcon,\n withGroups,\n }) as CreateResourceLinksFn<Resources>;\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EACF,EACF;CACF;CAEA,OAAO;AACT;AAEA,SAAgB,sBAEd,YAAyD;CACzD,MAAM,oBAAoB,oBAAoB,UAAU;CACxD,MAAM,aAAa,iBAA4B;CAE/C,SAAS,oBAEP,QAAuD;EACvD,OAAO,8BAA8B,MAAM;CAC7C;CAEA,SAAS,WAGP,QAGA;EACA,OAAO,8BAA8B,YAAY,mBAAmB,MAAM;CAC5E;CAEA,OAAO,OAAO,OAAO,qBAAqB;EACxC;EACA;EACA;CACF,CAAC;AACH"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LayoutResourceKey, ResourceDefinition } from "../resource.cjs";
|
|
2
2
|
import { LayoutRenderResourcesAccessor } from "./define-layout.cjs";
|
|
3
3
|
import { ElementType, ReactNode } from "react";
|
|
4
|
+
import { NonEmptyReadonlyArray } from "@jfdevelops/react-layout-validator";
|
|
4
5
|
|
|
5
6
|
//#region src/create-config/create-resource-links.d.ts
|
|
6
7
|
type ResourceAnchorLinkFn<Resource extends string> = (resource: Resource) => string;
|
|
@@ -8,11 +9,31 @@ type ResourceLinkHref<Resource extends string> = string | ResourceAnchorLinkFn<R
|
|
|
8
9
|
type ResourceLinkConfigResource<Resources extends ReadonlyArray<ResourceDefinition>> = LayoutResourceKey<Resources> | (string & {});
|
|
9
10
|
type ResourceIconFn<Resource = any> = (resource: Resource) => ReactNode;
|
|
10
11
|
type ResourceIcon<Resource = any> = ReactNode | ResourceIconFn<Resource>;
|
|
12
|
+
declare const resourceIconInputMarker: unique symbol;
|
|
13
|
+
declare const resourceIconComponentInputMarker: unique symbol;
|
|
14
|
+
type ResourceIconInputMarker<Value> = {
|
|
15
|
+
readonly [resourceIconInputMarker]: Value;
|
|
16
|
+
};
|
|
17
|
+
type ResourceIconComponentInputMarker<Icon extends ElementType> = {
|
|
18
|
+
readonly [resourceIconComponentInputMarker]: Icon;
|
|
19
|
+
};
|
|
20
|
+
type ResolveResourceIconInput<Definition> = Definition extends ResourceIconComponentInputMarker<infer Icon> ? Icon : Definition extends ResourceIconInputMarker<infer Value> ? Value : Definition extends ReadonlyArray<unknown> ? { [Index in keyof Definition]: ResolveResourceIconInput<Definition[Index]> } : Definition extends object ? { [Key in keyof Definition]: ResolveResourceIconInput<Definition[Key]> } : Definition;
|
|
21
|
+
interface DefineResourceIconInputHelpers<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
22
|
+
resource(): ResourceIconInputMarker<LayoutResourceKey<Resources>>;
|
|
23
|
+
icon<Icon extends ElementType = ElementType>(): ResourceIconComponentInputMarker<Icon>;
|
|
24
|
+
}
|
|
25
|
+
interface DefineResourceIconInputResult<Resources extends ReadonlyArray<ResourceDefinition>, Definition> {
|
|
26
|
+
render(render: (options: {
|
|
27
|
+
resource: LayoutRenderResourcesAccessor<Resources>;
|
|
28
|
+
input: ResolveResourceIconInput<Definition>;
|
|
29
|
+
}) => ReactNode): (input: ResolveResourceIconInput<Definition>) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;
|
|
30
|
+
}
|
|
11
31
|
interface DefineResourceIconFn<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
12
32
|
<Icon extends ElementType = ElementType>(render: (options: {
|
|
13
33
|
resource: LayoutRenderResourcesAccessor<Resources>;
|
|
14
34
|
Icon: Icon;
|
|
15
35
|
}) => ReactNode): (Icon: Icon) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;
|
|
36
|
+
input<Definition>(defineInput: (helpers: DefineResourceIconInputHelpers<Resources>) => Definition): DefineResourceIconInputResult<Resources, Definition>;
|
|
16
37
|
}
|
|
17
38
|
interface CreateResourceLinkOptions<Resources extends ReadonlyArray<ResourceDefinition>, Resource extends ResourceLinkConfigResource<Resources>> {
|
|
18
39
|
/**
|
|
@@ -124,18 +145,56 @@ type CreatedResourceMap<Map extends object> = Map & {
|
|
|
124
145
|
get<const Resource extends string>(resource: ValidateResource<Resource, Map>): Map[typeof resource];
|
|
125
146
|
};
|
|
126
147
|
interface CreateResourceMapFn<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
148
|
+
/**
|
|
149
|
+
* Creates a resource map for this layout.
|
|
150
|
+
*
|
|
151
|
+
* @param map Values keyed by this layout's resources. An unknown resource,
|
|
152
|
+
* or a key that collides with a map helper, is rejected.
|
|
153
|
+
*/
|
|
127
154
|
<const Map extends CreateResourceMap<Resources, unknown>>(map: Map): CreatedResourceMap<Map>;
|
|
128
155
|
}
|
|
129
156
|
interface CreateResourceLinksWithGroupsFn<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
130
|
-
|
|
131
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Groups resource links into sections.
|
|
159
|
+
*
|
|
160
|
+
* @param groups The sections to create. At least one is required. The link
|
|
161
|
+
* keys in those sections are the resources `findResource` can resolve.
|
|
162
|
+
*/
|
|
163
|
+
<const Resource extends ResourceLinkConfigResource<Resources>>(groups: NonEmptyReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>): CreateResourceLinksWithGroupsResult<Resources, Resource>;
|
|
132
164
|
}
|
|
133
165
|
type CreateResourceLinksWithGroupsResult<Resources extends ReadonlyArray<ResourceDefinition>, Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>> = Array<CreateResourceLinksWithGroups<Resources>> & {
|
|
134
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Resolves each group's icon and its link icons for the active resource.
|
|
168
|
+
*
|
|
169
|
+
* Functional icons receive the resources accessor for `resource`. Read the
|
|
170
|
+
* groups value directly when you do not need to resolve icons.
|
|
171
|
+
*
|
|
172
|
+
* @param resource The active resource key.
|
|
173
|
+
*/
|
|
174
|
+
(resource: LayoutResourceKey<Resources>): ResolvedResourceLinksWithGroupsResult<Resources, Resource>;
|
|
175
|
+
/**
|
|
176
|
+
* Creates a resource map for this layout. Same helper as
|
|
177
|
+
* `createResourceLinks.createResourceMap`.
|
|
178
|
+
*
|
|
179
|
+
* @param map Values keyed by this layout's resources. An unknown resource,
|
|
180
|
+
* or a key that collides with a map helper, is rejected.
|
|
181
|
+
*/
|
|
135
182
|
createResourceMap: CreateResourceMapFn<Resources>;
|
|
183
|
+
/**
|
|
184
|
+
* Returns the group whose links include `resource`.
|
|
185
|
+
*
|
|
186
|
+
* The result is `undefined` when that resource was not configured in any
|
|
187
|
+
* group. A resource outside this layout is rejected by the type.
|
|
188
|
+
*
|
|
189
|
+
* @param resource The resource whose group should be returned.
|
|
190
|
+
*/
|
|
136
191
|
findResource<const RequestedResource extends LayoutResourceKey<Resources> | Resource>(resource: RequestedResource): FindResourceGroupResult<Resources, Resource, RequestedResource>;
|
|
137
192
|
};
|
|
193
|
+
type ResolvedResourceLinksWithGroupsResult<Resources extends ReadonlyArray<ResourceDefinition>, Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>> = Array<ResolvedResourceLinksWithGroups<Resources>> & {
|
|
194
|
+
/** Returns the resolved group whose links include `resource`. */findResource<const RequestedResource extends LayoutResourceKey<Resources> | Resource>(resource: RequestedResource): FindResolvedResourceGroupResult<Resources, Resource, RequestedResource>;
|
|
195
|
+
};
|
|
138
196
|
type FindResourceGroupResult<Resources extends ReadonlyArray<ResourceDefinition>, IncludedResource extends string, RequestedResource extends string> = RequestedResource extends unknown ? RequestedResource extends IncludedResource ? CreateResourceLinksWithGroups<Resources> : undefined : never;
|
|
197
|
+
type FindResolvedResourceGroupResult<Resources extends ReadonlyArray<ResourceDefinition>, IncludedResource extends string, RequestedResource extends string> = RequestedResource extends unknown ? RequestedResource extends IncludedResource ? ResolvedResourceLinksWithGroups<Resources> : undefined : never;
|
|
139
198
|
//#endregion
|
|
140
|
-
export { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolvedResourceLinksWithGroups, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource };
|
|
199
|
+
export { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, DefineResourceIconInputHelpers, DefineResourceIconInputResult, FindResolvedResourceGroupResult, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolveResourceIconInput, ResolvedResourceLinksWithGroups, ResolvedResourceLinksWithGroupsResult, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource };
|
|
141
200
|
//# sourceMappingURL=create-resource-links.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-resource-links.d.cts","names":[],"sources":["../../src/create-config/create-resource-links.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-resource-links.d.cts","names":[],"sources":["../../src/create-config/create-resource-links.ts"],"mappings":";;;;;;KAaY,oBAAA,6BACV,QAAA,EAAU,QAAQ;AAAA,KAGR,gBAAA,qCAER,oBAAoB,CAAC,QAAA;AAAA,KAEb,0BAAA,mBACQ,aAAA,CAAc,kBAAA,KAC9B,iBAAA,CAAkB,SAAA;AAAA,KAEV,cAAA,oBAAkC,QAAA,EAAU,QAAA,KAAa,SAAS;AAAA,KAElE,YAAA,mBAA+B,SAAA,GAAY,cAAA,CAAe,QAAA;AAAA,cAExD,uBAAA;AAAA,cACA,gCAAA;AAAA,KAET,uBAAA;EAAA,UACO,uBAAuB,GAAG,KAAA;AAAA;AAAA,KAGjC,gCAAA,cAA8C,WAAA;EAAA,UACvC,gCAAA,GAAmC,IAAA;AAAA;AA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YAAA,iCACkC,iBAAA,CAAkB,SAAA,IAAa,QAAA,EAE/D,QAAA,EAAU,iBAAA,GACT,+BAAA,CAAgC,SAAA,EAAW,QAAA,EAAU,iBAAA;AAAA;AAAA,KAG9C,uBAAA,mBACQ,aAAA,CAAc,kBAAA,wEAG9B,iBAAA,mBACA,iBAAA,SAA0B,gBAAA,GACxB,6BAAA,CAA8B,SAAA;AAAA,KAIxB,+BAAA,mBACQ,aAAA,CAAc,kBAAA,wEAG9B,iBAAA,mBACA,iBAAA,SAA0B,gBAAA,GACxB,+BAAA,CAAgC,SAAA"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LayoutResourceKey, ResourceDefinition } from "../resource.mjs";
|
|
2
2
|
import { LayoutRenderResourcesAccessor } from "./define-layout.mjs";
|
|
3
3
|
import { ElementType, ReactNode } from "react";
|
|
4
|
+
import { NonEmptyReadonlyArray } from "@jfdevelops/react-layout-validator";
|
|
4
5
|
|
|
5
6
|
//#region src/create-config/create-resource-links.d.ts
|
|
6
7
|
type ResourceAnchorLinkFn<Resource extends string> = (resource: Resource) => string;
|
|
@@ -8,11 +9,31 @@ type ResourceLinkHref<Resource extends string> = string | ResourceAnchorLinkFn<R
|
|
|
8
9
|
type ResourceLinkConfigResource<Resources extends ReadonlyArray<ResourceDefinition>> = LayoutResourceKey<Resources> | (string & {});
|
|
9
10
|
type ResourceIconFn<Resource = any> = (resource: Resource) => ReactNode;
|
|
10
11
|
type ResourceIcon<Resource = any> = ReactNode | ResourceIconFn<Resource>;
|
|
12
|
+
declare const resourceIconInputMarker: unique symbol;
|
|
13
|
+
declare const resourceIconComponentInputMarker: unique symbol;
|
|
14
|
+
type ResourceIconInputMarker<Value> = {
|
|
15
|
+
readonly [resourceIconInputMarker]: Value;
|
|
16
|
+
};
|
|
17
|
+
type ResourceIconComponentInputMarker<Icon extends ElementType> = {
|
|
18
|
+
readonly [resourceIconComponentInputMarker]: Icon;
|
|
19
|
+
};
|
|
20
|
+
type ResolveResourceIconInput<Definition> = Definition extends ResourceIconComponentInputMarker<infer Icon> ? Icon : Definition extends ResourceIconInputMarker<infer Value> ? Value : Definition extends ReadonlyArray<unknown> ? { [Index in keyof Definition]: ResolveResourceIconInput<Definition[Index]> } : Definition extends object ? { [Key in keyof Definition]: ResolveResourceIconInput<Definition[Key]> } : Definition;
|
|
21
|
+
interface DefineResourceIconInputHelpers<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
22
|
+
resource(): ResourceIconInputMarker<LayoutResourceKey<Resources>>;
|
|
23
|
+
icon<Icon extends ElementType = ElementType>(): ResourceIconComponentInputMarker<Icon>;
|
|
24
|
+
}
|
|
25
|
+
interface DefineResourceIconInputResult<Resources extends ReadonlyArray<ResourceDefinition>, Definition> {
|
|
26
|
+
render(render: (options: {
|
|
27
|
+
resource: LayoutRenderResourcesAccessor<Resources>;
|
|
28
|
+
input: ResolveResourceIconInput<Definition>;
|
|
29
|
+
}) => ReactNode): (input: ResolveResourceIconInput<Definition>) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;
|
|
30
|
+
}
|
|
11
31
|
interface DefineResourceIconFn<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
12
32
|
<Icon extends ElementType = ElementType>(render: (options: {
|
|
13
33
|
resource: LayoutRenderResourcesAccessor<Resources>;
|
|
14
34
|
Icon: Icon;
|
|
15
35
|
}) => ReactNode): (Icon: Icon) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;
|
|
36
|
+
input<Definition>(defineInput: (helpers: DefineResourceIconInputHelpers<Resources>) => Definition): DefineResourceIconInputResult<Resources, Definition>;
|
|
16
37
|
}
|
|
17
38
|
interface CreateResourceLinkOptions<Resources extends ReadonlyArray<ResourceDefinition>, Resource extends ResourceLinkConfigResource<Resources>> {
|
|
18
39
|
/**
|
|
@@ -124,18 +145,56 @@ type CreatedResourceMap<Map extends object> = Map & {
|
|
|
124
145
|
get<const Resource extends string>(resource: ValidateResource<Resource, Map>): Map[typeof resource];
|
|
125
146
|
};
|
|
126
147
|
interface CreateResourceMapFn<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
148
|
+
/**
|
|
149
|
+
* Creates a resource map for this layout.
|
|
150
|
+
*
|
|
151
|
+
* @param map Values keyed by this layout's resources. An unknown resource,
|
|
152
|
+
* or a key that collides with a map helper, is rejected.
|
|
153
|
+
*/
|
|
127
154
|
<const Map extends CreateResourceMap<Resources, unknown>>(map: Map): CreatedResourceMap<Map>;
|
|
128
155
|
}
|
|
129
156
|
interface CreateResourceLinksWithGroupsFn<Resources extends ReadonlyArray<ResourceDefinition>> {
|
|
130
|
-
|
|
131
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Groups resource links into sections.
|
|
159
|
+
*
|
|
160
|
+
* @param groups The sections to create. At least one is required. The link
|
|
161
|
+
* keys in those sections are the resources `findResource` can resolve.
|
|
162
|
+
*/
|
|
163
|
+
<const Resource extends ResourceLinkConfigResource<Resources>>(groups: NonEmptyReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>): CreateResourceLinksWithGroupsResult<Resources, Resource>;
|
|
132
164
|
}
|
|
133
165
|
type CreateResourceLinksWithGroupsResult<Resources extends ReadonlyArray<ResourceDefinition>, Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>> = Array<CreateResourceLinksWithGroups<Resources>> & {
|
|
134
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Resolves each group's icon and its link icons for the active resource.
|
|
168
|
+
*
|
|
169
|
+
* Functional icons receive the resources accessor for `resource`. Read the
|
|
170
|
+
* groups value directly when you do not need to resolve icons.
|
|
171
|
+
*
|
|
172
|
+
* @param resource The active resource key.
|
|
173
|
+
*/
|
|
174
|
+
(resource: LayoutResourceKey<Resources>): ResolvedResourceLinksWithGroupsResult<Resources, Resource>;
|
|
175
|
+
/**
|
|
176
|
+
* Creates a resource map for this layout. Same helper as
|
|
177
|
+
* `createResourceLinks.createResourceMap`.
|
|
178
|
+
*
|
|
179
|
+
* @param map Values keyed by this layout's resources. An unknown resource,
|
|
180
|
+
* or a key that collides with a map helper, is rejected.
|
|
181
|
+
*/
|
|
135
182
|
createResourceMap: CreateResourceMapFn<Resources>;
|
|
183
|
+
/**
|
|
184
|
+
* Returns the group whose links include `resource`.
|
|
185
|
+
*
|
|
186
|
+
* The result is `undefined` when that resource was not configured in any
|
|
187
|
+
* group. A resource outside this layout is rejected by the type.
|
|
188
|
+
*
|
|
189
|
+
* @param resource The resource whose group should be returned.
|
|
190
|
+
*/
|
|
136
191
|
findResource<const RequestedResource extends LayoutResourceKey<Resources> | Resource>(resource: RequestedResource): FindResourceGroupResult<Resources, Resource, RequestedResource>;
|
|
137
192
|
};
|
|
193
|
+
type ResolvedResourceLinksWithGroupsResult<Resources extends ReadonlyArray<ResourceDefinition>, Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>> = Array<ResolvedResourceLinksWithGroups<Resources>> & {
|
|
194
|
+
/** Returns the resolved group whose links include `resource`. */findResource<const RequestedResource extends LayoutResourceKey<Resources> | Resource>(resource: RequestedResource): FindResolvedResourceGroupResult<Resources, Resource, RequestedResource>;
|
|
195
|
+
};
|
|
138
196
|
type FindResourceGroupResult<Resources extends ReadonlyArray<ResourceDefinition>, IncludedResource extends string, RequestedResource extends string> = RequestedResource extends unknown ? RequestedResource extends IncludedResource ? CreateResourceLinksWithGroups<Resources> : undefined : never;
|
|
197
|
+
type FindResolvedResourceGroupResult<Resources extends ReadonlyArray<ResourceDefinition>, IncludedResource extends string, RequestedResource extends string> = RequestedResource extends unknown ? RequestedResource extends IncludedResource ? ResolvedResourceLinksWithGroups<Resources> : undefined : never;
|
|
139
198
|
//#endregion
|
|
140
|
-
export { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolvedResourceLinksWithGroups, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource };
|
|
199
|
+
export { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, DefineResourceIconInputHelpers, DefineResourceIconInputResult, FindResolvedResourceGroupResult, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolveResourceIconInput, ResolvedResourceLinksWithGroups, ResolvedResourceLinksWithGroupsResult, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource };
|
|
141
200
|
//# sourceMappingURL=create-resource-links.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-resource-links.d.mts","names":[],"sources":["../../src/create-config/create-resource-links.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-resource-links.d.mts","names":[],"sources":["../../src/create-config/create-resource-links.ts"],"mappings":";;;;;;KAaY,oBAAA,6BACV,QAAA,EAAU,QAAQ;AAAA,KAGR,gBAAA,qCAER,oBAAoB,CAAC,QAAA;AAAA,KAEb,0BAAA,mBACQ,aAAA,CAAc,kBAAA,KAC9B,iBAAA,CAAkB,SAAA;AAAA,KAEV,cAAA,oBAAkC,QAAA,EAAU,QAAA,KAAa,SAAS;AAAA,KAElE,YAAA,mBAA+B,SAAA,GAAY,cAAA,CAAe,QAAA;AAAA,cAExD,uBAAA;AAAA,cACA,gCAAA;AAAA,KAET,uBAAA;EAAA,UACO,uBAAuB,GAAG,KAAA;AAAA;AAAA,KAGjC,gCAAA,cAA8C,W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A,CAAM,+BAAA,CAAgC,SAAA;EA3RhC,iEA6RR,YAAA,iCACkC,iBAAA,CAAkB,SAAA,IAAa,QAAA,EAE/D,QAAA,EAAU,iBAAA,GACT,+BAAA,CAAgC,SAAA,EAAW,QAAA,EAAU,iBAAA;AAAA;AAAA,KAG9C,uBAAA,mBACQ,aAAA,CAAc,kBAAA,wEAG9B,iBAAA,mBACA,iBAAA,SAA0B,gBAAA,GACxB,6BAAA,CAA8B,SAAA;AAAA,KAIxB,+BAAA,mBACQ,aAAA,CAAc,kBAAA,wEAG9B,iBAAA,mBACA,iBAAA,SAA0B,gBAAA,GACxB,+BAAA,CAAgC,SAAA"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LayoutConfigurationError } from "../errors/layout-configuration-error.mjs";
|
|
2
|
+
import { createIsValidResourceFn } from "../resource.mjs";
|
|
2
3
|
|
|
3
4
|
//#region src/create-config/create-resource-links.ts
|
|
4
5
|
function resolveResourceLinkHref(value, resource, fallback) {
|
|
@@ -55,6 +56,21 @@ function createResourceLinkGroupId(group, index) {
|
|
|
55
56
|
if (typeof group.id === "string" && group.id.length > 0) return group.id;
|
|
56
57
|
return `group-${index}-${typeof group.label === "string" ? group.label : ""}-${Object.keys(group.links).join(",")}`;
|
|
57
58
|
}
|
|
59
|
+
function readResourceValue(resource) {
|
|
60
|
+
return typeof resource === "string" ? resource : resource.value;
|
|
61
|
+
}
|
|
62
|
+
function createResourceSelection(definitions) {
|
|
63
|
+
const selection = (() => definitions);
|
|
64
|
+
selection.isResource = createIsValidResourceFn(definitions);
|
|
65
|
+
return selection;
|
|
66
|
+
}
|
|
67
|
+
function createRenderResourcesAccessor(resources, current) {
|
|
68
|
+
const accessor = Object.assign(() => resources, { current });
|
|
69
|
+
accessor.isResource = createIsValidResourceFn(resources);
|
|
70
|
+
accessor.omit = ((...keys) => createResourceSelection(resources.filter((resource) => !keys.includes(readResourceValue(resource)))));
|
|
71
|
+
accessor.pick = ((...keys) => createResourceSelection(resources.filter((resource) => keys.includes(readResourceValue(resource)))));
|
|
72
|
+
return accessor;
|
|
73
|
+
}
|
|
58
74
|
function createResourceMapFn(resources) {
|
|
59
75
|
const helperNames = new Set(["get", "isValidResource"]);
|
|
60
76
|
const resourceKeys = new Set(resources.map((resource) => typeof resource === "string" ? resource : resource.value));
|
|
@@ -77,8 +93,9 @@ function createResourceMapFn(resources) {
|
|
|
77
93
|
return map;
|
|
78
94
|
};
|
|
79
95
|
}
|
|
80
|
-
function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
96
|
+
function createResourceLinksWithGroups(resources, createResourceMap, groups) {
|
|
81
97
|
if (!Array.isArray(groups)) throw new LayoutConfigurationError("[createResourceLinks.withGroups]: \"groups\" must be an array.");
|
|
98
|
+
if (groups.length === 0) throw new LayoutConfigurationError("[createResourceLinks.withGroups]: \"groups\" must include at least one group.");
|
|
82
99
|
const createdGroups = groups.map((group, index) => {
|
|
83
100
|
if (!group) throw new LayoutConfigurationError(`[createResourceLinks.withGroups]: group at index ${index} is required.`);
|
|
84
101
|
if (typeof group !== "object") throw new LayoutConfigurationError(`[createResourceLinks.withGroups]: group at index ${index} must be an object. Received ${typeof group}`);
|
|
@@ -95,10 +112,9 @@ function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
|
95
112
|
};
|
|
96
113
|
});
|
|
97
114
|
function resolveGroups(resource) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
return createdGroups.map((group) => ({
|
|
115
|
+
if (resource === void 0) throw new LayoutConfigurationError("[createResourceLinks.withGroups]: \"resource\" is required.");
|
|
116
|
+
const currentResources = createRenderResourcesAccessor(resources, resource);
|
|
117
|
+
const resolvedGroups = createdGroups.map((group) => ({
|
|
102
118
|
...group,
|
|
103
119
|
icon: typeof group.icon === "function" ? group.icon(currentResources) : group.icon,
|
|
104
120
|
links: group.links.map((link) => ({
|
|
@@ -106,6 +122,10 @@ function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
|
106
122
|
icon: typeof link.icon === "function" ? link.icon(currentResources) : link.icon
|
|
107
123
|
}))
|
|
108
124
|
}));
|
|
125
|
+
Object.defineProperty(resolvedGroups, "findResource", { value(requestedResource) {
|
|
126
|
+
return resolvedGroups.find((group) => group.links.some((link) => link.resource === requestedResource));
|
|
127
|
+
} });
|
|
128
|
+
return resolvedGroups;
|
|
109
129
|
}
|
|
110
130
|
const callableGroups = new Proxy(resolveGroups, {
|
|
111
131
|
get(target, property, receiver) {
|
|
@@ -137,23 +157,49 @@ function createResourceLinksWithGroups(createResourceMap, groups) {
|
|
|
137
157
|
} });
|
|
138
158
|
return callableGroups;
|
|
139
159
|
}
|
|
140
|
-
function
|
|
141
|
-
|
|
142
|
-
return function
|
|
143
|
-
return
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
160
|
+
function createDefineIcon() {
|
|
161
|
+
function defineIcon(render) {
|
|
162
|
+
return function createIcon(Icon) {
|
|
163
|
+
return function renderIcon(resource) {
|
|
164
|
+
return render({
|
|
165
|
+
resource,
|
|
166
|
+
Icon
|
|
167
|
+
});
|
|
168
|
+
};
|
|
147
169
|
};
|
|
170
|
+
}
|
|
171
|
+
defineIcon.input = function defineIconInput(defineInput) {
|
|
172
|
+
const resourceMarker = {};
|
|
173
|
+
const componentMarker = {};
|
|
174
|
+
defineInput({
|
|
175
|
+
resource() {
|
|
176
|
+
return resourceMarker;
|
|
177
|
+
},
|
|
178
|
+
icon() {
|
|
179
|
+
return componentMarker;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return { render(render) {
|
|
183
|
+
return function createIcon(input) {
|
|
184
|
+
return function renderIcon(resource) {
|
|
185
|
+
return render({
|
|
186
|
+
resource,
|
|
187
|
+
input
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
} };
|
|
148
192
|
};
|
|
193
|
+
return defineIcon;
|
|
149
194
|
}
|
|
150
195
|
function createResourceLinksFn(_resources) {
|
|
151
196
|
const createResourceMap = createResourceMapFn(_resources);
|
|
197
|
+
const defineIcon = createDefineIcon();
|
|
152
198
|
function createResourceLinks(config) {
|
|
153
199
|
return createResourceLinksFromConfig(config);
|
|
154
200
|
}
|
|
155
201
|
function withGroups(groups) {
|
|
156
|
-
return createResourceLinksWithGroups(createResourceMap, groups);
|
|
202
|
+
return createResourceLinksWithGroups(_resources, createResourceMap, groups);
|
|
157
203
|
}
|
|
158
204
|
return Object.assign(createResourceLinks, {
|
|
159
205
|
createResourceMap,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-resource-links.mjs","names":[],"sources":["../../src/create-config/create-resource-links.ts"],"sourcesContent":["import type { ElementType, ReactNode } from 'react';\nimport {\n type LayoutResourceKey,\n type ResourceDefinition,\n} from '../resource';\nimport { LayoutConfigurationError } from '../errors';\nimport type { LayoutRenderResourcesAccessor } from './define-layout';\n\nexport type ResourceAnchorLinkFn<Resource extends string> = (\n resource: Resource,\n) => string;\n\nexport type ResourceLinkHref<Resource extends string> =\n | string\n | ResourceAnchorLinkFn<Resource>;\n\nexport type ResourceLinkConfigResource<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = LayoutResourceKey<Resources> | (string & {});\n\nexport type ResourceIconFn<Resource = any> = (resource: Resource) => ReactNode;\n\nexport type ResourceIcon<Resource = any> = ReactNode | ResourceIconFn<Resource>;\n\nexport interface DefineResourceIconFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <Icon extends ElementType = ElementType>(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n Icon: Icon;\n }) => ReactNode,\n ): (Icon: Icon) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;\n}\nexport interface CreateResourceLinkOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n> {\n /**\n * The label of the link. This is the text that will be displayed in the link.\n */\n label: string;\n /**\n * An optional icon to render when the link is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the link. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The href of the link. By default, the href will be `\"/\"`. While you **don't** need to include a hash,\n * if you do, it will override the `hash` property (if provided).\n */\n href?: ResourceLinkHref<Resource>;\n /**\n * The hash of the link. By default, the hash will be the resource name. If you need to customize it, you can provide\n * a string or a function that returns a string.\n *\n * @remarks A `\"#\"` is not required. The generated href will include it automatically.\n */\n hash?: ResourceLinkHref<Resource>;\n}\n\nexport type CreateResourceLinkConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = {\n [Key in Resource]?: CreateResourceLinkOptions<\n Resources,\n Key\n >;\n};\n\nexport type InferHashFromResourceLinkHref<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Hash extends string\n ? Hash\n : Hash extends ResourceAnchorLinkFn<Resource>\n ? ReturnType<Hash>\n : never;\n\nexport type CreatedResourceHref = {\n /**\n * The full generated href with the `hash` if provided.\n */\n full: string;\n /**\n * The given href. This is the same value as the `href` property of the config.\n */\n given: string;\n};\n\nexport type CreatedResourceLinkBase<Resource extends string> = {\n activeIcon?: ReactNode;\n href: CreatedResourceHref;\n label: string;\n resource: Resource;\n icon: ResourceIcon;\n};\nexport type CreateResourceLinkWithHash<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Omit<CreatedResourceLinkBase<Resource>, 'href'> & {\n href: CreatedResourceHref & {\n hash: InferHashFromResourceLinkHref<Resource, Hash>;\n };\n};\n\nexport type CreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Config,\n> = {\n [Resource in keyof Config]-?: Resource extends ResourceLinkConfigResource<Resources>\n ? Config[Resource] extends {\n hash: infer hash extends ResourceLinkHref<Resource>;\n }\n ? CreateResourceLinkWithHash<Resource, hash>\n : CreatedResourceLinkBase<Resource>\n : never;\n}[keyof Config];\n\nexport interface CreateResourceLinksFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n config: CreateResourceLinkConfig<Resources, Resource>,\n ): Array<\n CreatedResourceLink<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n >\n >;\n createResourceMap: CreateResourceMapFn<Resources>;\n defineIcon: DefineResourceIconFn<Resources>;\n withGroups: CreateResourceLinksWithGroupsFn<Resources>;\n}\n\nexport interface CreateResourceLinkGroupOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Links extends object =\n CreateResourceLinkConfig<Resources>,\n> {\n /**\n * An optional, caller-provided identity for the group. Provide this when you\n * combine groups from more than one `withGroups()` call into one list (e.g.\n * a module-owned nav section alongside a feature-owned one) — without it,\n * the id is derived from the group's `label` and its link resources, which\n * is enough to keep groups from a single call apart but can't distinguish\n * two independently-defined groups that happen to look identical.\n */\n id?: string;\n /**\n * The label of the group. This is the text that will be displayed for the group.\n */\n label?: string;\n /**\n * An optional icon to render when the group is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the group. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The resource links in this group.\n */\n links: Links;\n}\n\nexport type CreateResourceLinkGroupInput<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = CreateResourceLinkGroupOptions<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n>;\n\nexport type AnyCreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n [Resource in LayoutResourceKey<Resources>]:\n | CreatedResourceLinkBase<Resource>\n | CreateResourceLinkWithHash<Resource, string>;\n}[LayoutResourceKey<Resources>];\n\nexport type CreateResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n activeIcon?: ReactNode;\n id: string;\n label: string | null;\n icon: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n links: Array<AnyCreatedResourceLink<Resources>>;\n};\n\nexport type ResolvedResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = Omit<CreateResourceLinksWithGroups<Resources>, 'icon' | 'links'> & {\n icon: ReactNode;\n links: Array<\n Omit<AnyCreatedResourceLink<Resources>, 'icon'> & { icon: ReactNode }\n >;\n};\n\nexport type CreateResourceMap<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Value,\n> = Partial<Record<LayoutResourceKey<Resources>, Value>>;\n\nexport type ValidateResource<\n Resource extends string,\n Map extends object,\n> = Resource extends Extract<keyof Map, string> ? Resource : never;\n\nexport type CreatedResourceMap<Map extends object> = Map & {\n isValidResource(value: unknown): value is Extract<keyof Map, string>;\n get<const Resource extends string>(\n resource: ValidateResource<Resource, Map>,\n ): Map[typeof resource];\n};\n\nexport interface CreateResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <const Map extends CreateResourceMap<Resources, unknown>>(\n map: Map,\n ): CreatedResourceMap<Map>;\n}\n\nexport interface CreateResourceLinksWithGroupsFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n (groups: readonly []): CreateResourceLinksWithGroupsResult<Resources, never>;\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n ): CreateResourceLinksWithGroupsResult<Resources, Resource>;\n}\n\nexport type CreateResourceLinksWithGroupsResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = Array<CreateResourceLinksWithGroups<Resources>> & {\n (\n resource?: LayoutRenderResourcesAccessor<Resources>,\n ): Array<ResolvedResourceLinksWithGroups<Resources>>;\n createResourceMap: CreateResourceMapFn<Resources>;\n findResource<\n const RequestedResource extends LayoutResourceKey<Resources> | Resource,\n >(\n resource: RequestedResource,\n ): FindResourceGroupResult<Resources, Resource, RequestedResource>;\n};\n\nexport type FindResourceGroupResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n IncludedResource extends string,\n RequestedResource extends string,\n> = RequestedResource extends unknown\n ? RequestedResource extends IncludedResource\n ? CreateResourceLinksWithGroups<Resources>\n : undefined\n : never;\n\nfunction resolveResourceLinkHref<Resource extends string>(\n value: ResourceLinkHref<Resource> | undefined,\n resource: Resource,\n fallback: string,\n) {\n return typeof value === 'function' ? value(resource) : (value ?? fallback);\n}\n\nfunction normalizeResourceLinkHash(hash: string) {\n return hash.replace(/^#+/, '');\n}\n\nfunction createFullResourceLinkHref(href: string, hash: string) {\n if (href.includes('#')) {\n return href;\n }\n\n return `${href}#${normalizeResourceLinkHash(hash)}`;\n}\n\nfunction createResourceLinksFromConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(config: CreateResourceLinkConfig<Resources, Resource>) {\n return Object.entries(config).map(([resource, config]) => {\n if (!config) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" must be an object for the ${resource} resource. Received ${typeof config}`,\n );\n }\n\n if (!('label' in config)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config.label !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" must be a string for the ${resource} resource. Received ${typeof config.label}`,\n );\n }\n\n const linkConfig = config as CreateResourceLinkOptions<\n Resources,\n typeof resource\n >;\n\n if (\n 'href' in linkConfig &&\n linkConfig.href !== undefined &&\n typeof linkConfig.href !== 'string' &&\n typeof linkConfig.href !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"href\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.href}`,\n );\n }\n\n if (\n 'hash' in linkConfig &&\n linkConfig.hash !== undefined &&\n typeof linkConfig.hash !== 'string' &&\n typeof linkConfig.hash !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"hash\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.hash}`,\n );\n }\n\n const givenHref = resolveResourceLinkHref(linkConfig.href, resource, '/');\n const givenHash = resolveResourceLinkHref(\n linkConfig.hash,\n resource,\n resource,\n );\n const href = {\n given: givenHref,\n full: createFullResourceLinkHref(givenHref, givenHash),\n ...(linkConfig.hash !== undefined && !givenHref.includes('#')\n ? { hash: normalizeResourceLinkHash(givenHash) }\n : {}),\n };\n\n return {\n ...(linkConfig.activeIcon !== undefined\n ? { activeIcon: linkConfig.activeIcon }\n : {}),\n href,\n label: config.label,\n resource,\n icon: 'icon' in config ? config.icon : null,\n };\n });\n}\n\n/**\n * Deterministic, not random: a random id (the previous implementation used\n * `crypto.randomUUID()` / `Math.random()`) would make `withGroups()` unsafe to\n * call from module scope — some runtimes (e.g. Cloudflare Workers) disallow\n * generating random values outside a request handler, and config arrays like\n * this are routinely defined as top-level constants.\n *\n * Falls back to the group's own shape — its `label` and its link resource\n * keys — rather than just the array index, so groups from *separate*\n * `withGroups()` calls that get concatenated into one list (a module-owned nav\n * section next to a feature-owned one, say) don't collide just because they're\n * both first in their own call. Two groups that are genuinely identical in\n * shape will still collide; pass an explicit `id` on the group to rule that\n * out entirely.\n */\nfunction createResourceLinkGroupId(\n group: { id?: unknown; label?: unknown; links: object },\n index: number,\n) {\n if (typeof group.id === 'string' && group.id.length > 0) {\n return group.id;\n }\n\n const label = typeof group.label === 'string' ? group.label : '';\n const linkKeys = Object.keys(group.links).join(',');\n return `group-${index}-${label}-${linkKeys}`;\n}\n\nfunction createResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(resources: Resources): CreateResourceMapFn<Resources> {\n const helperNames = new Set(['get', 'isValidResource']);\n const resourceKeys = new Set(\n resources.map((resource) =>\n typeof resource === 'string' ? resource : resource.value,\n ),\n );\n\n return function createResourceMap<\n const Map extends CreateResourceMap<Resources, unknown>,\n >(map: Map) {\n const configuredResources = Object.keys(map);\n\n for (const resource of configuredResources) {\n if (!resourceKeys.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: unknown resource \"${resource}\".`,\n );\n }\n\n if (helperNames.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: resource \"${resource}\" conflicts with a map helper.`,\n );\n }\n }\n\n const configuredResourceKeys = new Set(configuredResources);\n const resourceValues = new Map(\n configuredResources.map((resource) => [\n resource,\n (map as Record<string, unknown>)[resource],\n ]),\n );\n\n Object.defineProperties(map, {\n get: {\n value(resource: string) {\n return resourceValues.get(resource);\n },\n },\n isValidResource: {\n value(value: unknown): value is Extract<keyof Map, string> {\n return (\n typeof value === 'string' && configuredResourceKeys.has(value)\n );\n },\n },\n });\n\n return map as CreatedResourceMap<Map>;\n };\n}\n\nfunction createResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(\n createResourceMap: CreateResourceMapFn<Resources>,\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n): CreateResourceLinksWithGroupsResult<Resources, Resource> {\n if (!Array.isArray(groups)) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"groups\" must be an array.',\n );\n }\n\n const createdGroups = groups.map((group, index) => {\n if (!group) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} is required.`,\n );\n }\n\n if (typeof group !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} must be an object. Received ${typeof group}`,\n );\n }\n\n if (\n 'label' in group &&\n group.label !== undefined &&\n typeof group.label !== 'string'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"label\" must be a string for group at index ${index}. Received ${typeof group.label}`,\n );\n }\n\n if ('id' in group && group.id !== undefined && typeof group.id !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"id\" must be a string for group at index ${index}. Received ${typeof group.id}`,\n );\n }\n\n if (!('links' in group)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" is required for group at index ${index}.`,\n );\n }\n\n if (\n typeof group.links !== 'object' ||\n group.links === null ||\n Array.isArray(group.links)\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" must be an object for group at index ${index}. Received ${Array.isArray(group.links) ? 'array' : typeof group.links}`,\n );\n }\n\n return {\n ...(group.activeIcon !== undefined\n ? { activeIcon: group.activeIcon }\n : {}),\n id: createResourceLinkGroupId(group, index),\n label: 'label' in group ? (group.label ?? null) : null,\n icon: 'icon' in group ? group.icon : null,\n links: createResourceLinksFromConfig(group.links),\n };\n }) as Array<CreateResourceLinksWithGroups<Resources>>;\n\n function resolveGroups(\n resource?: LayoutRenderResourcesAccessor<Resources>,\n ) {\n const hasFunctionalIcon = createdGroups.some(\n (group) =>\n typeof group.icon === 'function' ||\n group.links.some((link) => typeof link.icon === 'function'),\n );\n\n if (resource === undefined && hasFunctionalIcon) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"resource\" is required when resolving functional icons.',\n );\n }\n\n const currentResources = resource as LayoutRenderResourcesAccessor<Resources>;\n\n return createdGroups.map((group) => ({\n ...group,\n icon:\n typeof group.icon === 'function'\n ? group.icon(currentResources)\n : group.icon,\n links: group.links.map((link) => ({\n ...link,\n icon:\n typeof link.icon === 'function'\n ? link.icon(currentResources)\n : link.icon,\n })),\n }));\n }\n\n const callableGroups = new Proxy(resolveGroups, {\n get(target, property, receiver) {\n if (property === 'createResourceMap') {\n return createResourceMap;\n }\n\n if (property === Symbol.iterator) {\n return createdGroups[Symbol.iterator].bind(createdGroups);\n }\n\n if (property === 'toJSON') {\n return () => createdGroups;\n }\n\n if (property in createdGroups) {\n const value = Reflect.get(createdGroups, property);\n return typeof value === 'function' ? value.bind(createdGroups) : value;\n }\n\n return Reflect.get(target, property, receiver);\n },\n getOwnPropertyDescriptor(target, property) {\n return (\n Reflect.getOwnPropertyDescriptor(createdGroups, property) ??\n Reflect.getOwnPropertyDescriptor(target, property)\n );\n },\n getPrototypeOf() {\n return Array.prototype;\n },\n ownKeys(target) {\n return [\n ...new Set([\n ...Reflect.ownKeys(target),\n ...Reflect.ownKeys(createdGroups),\n ]),\n ];\n },\n set(target, property, value, receiver) {\n if (property in createdGroups || /^\\d+$/.test(String(property))) {\n return Reflect.set(createdGroups, property, value);\n }\n\n return Reflect.set(target, property, value, receiver);\n },\n });\n Object.defineProperty(createdGroups, 'findResource', {\n value(resource: ResourceLinkConfigResource<Resources>) {\n return createdGroups.find((group) =>\n group.links.some((link) => link.resource === resource),\n );\n },\n });\n\n return callableGroups as CreateResourceLinksWithGroupsResult<\n Resources,\n Resource\n >;\n}\n\nfunction defineIcon<Icon extends ElementType = ElementType, Resource = any>(\n render: (options: { resource: Resource; Icon: Icon }) => ReactNode,\n) {\n return function createIcon(Icon: Icon): ResourceIconFn<Resource> {\n return function renderIcon(resource) {\n return render({ resource, Icon });\n };\n };\n}\n\nexport function createResourceLinksFn<\n const Resources extends ReadonlyArray<ResourceDefinition>,\n>(_resources: Resources): CreateResourceLinksFn<Resources> {\n const createResourceMap = createResourceMapFn(_resources);\n\n function createResourceLinks<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(config: CreateResourceLinkConfig<Resources, Resource>) {\n return createResourceLinksFromConfig(config);\n }\n\n function withGroups<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n ) {\n return createResourceLinksWithGroups(createResourceMap, groups);\n }\n\n return Object.assign(createResourceLinks, {\n createResourceMap,\n defineIcon,\n withGroups,\n }) as CreateResourceLinksFn<Resources>;\n}\n"],"mappings":";;;AA2QA,SAAS,wBACP,OACA,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,aAAa,QAAQ,CACvD;CACF,EACF,CAAC;CAED,OAAO;AAIT;AAEA,SAAS,WACP,QACA;CACA,OAAO,SAAS,WAAW,MAAsC;EAC/D,OAAO,SAAS,WAAW,UAAU;GACnC,OAAO,OAAO;IAAE;IAAU;GAAK,CAAC;EAClC;CACF;AACF;AAEA,SAAgB,sBAEd,YAAyD;CACzD,MAAM,oBAAoB,oBAAoB,UAAU;CAExD,SAAS,oBAEP,QAAuD;EACvD,OAAO,8BAA8B,MAAM;CAC7C;CAEA,SAAS,WAGP,QACA;EACA,OAAO,8BAA8B,mBAAmB,MAAM;CAChE;CAEA,OAAO,OAAO,OAAO,qBAAqB;EACxC;EACA;EACA;CACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"create-resource-links.mjs","names":[],"sources":["../../src/create-config/create-resource-links.ts"],"sourcesContent":["import type { ElementType, ReactNode } from 'react';\nimport type { NonEmptyReadonlyArray } from '@jfdevelops/react-layout-validator';\nimport {\n createIsValidResourceFn,\n type LayoutResourceKey,\n type ResourceDefinition,\n} from '../resource';\nimport { LayoutConfigurationError } from '../errors';\nimport type {\n LayoutRenderResourcesAccessor,\n ResourceSelection,\n} from './define-layout';\n\nexport type ResourceAnchorLinkFn<Resource extends string> = (\n resource: Resource,\n) => string;\n\nexport type ResourceLinkHref<Resource extends string> =\n | string\n | ResourceAnchorLinkFn<Resource>;\n\nexport type ResourceLinkConfigResource<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = LayoutResourceKey<Resources> | (string & {});\n\nexport type ResourceIconFn<Resource = any> = (resource: Resource) => ReactNode;\n\nexport type ResourceIcon<Resource = any> = ReactNode | ResourceIconFn<Resource>;\n\ndeclare const resourceIconInputMarker: unique symbol;\ndeclare const resourceIconComponentInputMarker: unique symbol;\n\ntype ResourceIconInputMarker<Value> = {\n readonly [resourceIconInputMarker]: Value;\n};\n\ntype ResourceIconComponentInputMarker<Icon extends ElementType> = {\n readonly [resourceIconComponentInputMarker]: Icon;\n};\n\nexport type ResolveResourceIconInput<Definition> =\n Definition extends ResourceIconComponentInputMarker<infer Icon>\n ? Icon\n : Definition extends ResourceIconInputMarker<infer Value>\n ? Value\n : Definition extends ReadonlyArray<unknown>\n ? { [Index in keyof Definition]: ResolveResourceIconInput<Definition[Index]> }\n : Definition extends object\n ? { [Key in keyof Definition]: ResolveResourceIconInput<Definition[Key]> }\n : Definition;\n\nexport interface DefineResourceIconInputHelpers<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n resource(): ResourceIconInputMarker<LayoutResourceKey<Resources>>;\n icon<Icon extends ElementType = ElementType>(): ResourceIconComponentInputMarker<Icon>;\n}\n\nexport interface DefineResourceIconInputResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Definition,\n> {\n render(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n input: ResolveResourceIconInput<Definition>;\n }) => ReactNode,\n ): (\n input: ResolveResourceIconInput<Definition>,\n ) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;\n}\n\nexport interface DefineResourceIconFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <Icon extends ElementType = ElementType>(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n Icon: Icon;\n }) => ReactNode,\n ): (Icon: Icon) => ResourceIconFn<LayoutRenderResourcesAccessor<Resources>>;\n input<Definition>(\n defineInput: (\n helpers: DefineResourceIconInputHelpers<Resources>,\n ) => Definition,\n ): DefineResourceIconInputResult<Resources, Definition>;\n}\nexport interface CreateResourceLinkOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n> {\n /**\n * The label of the link. This is the text that will be displayed in the link.\n */\n label: string;\n /**\n * An optional icon to render when the link is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the link. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The href of the link. By default, the href will be `\"/\"`. While you **don't** need to include a hash,\n * if you do, it will override the `hash` property (if provided).\n */\n href?: ResourceLinkHref<Resource>;\n /**\n * The hash of the link. By default, the hash will be the resource name. If you need to customize it, you can provide\n * a string or a function that returns a string.\n *\n * @remarks A `\"#\"` is not required. The generated href will include it automatically.\n */\n hash?: ResourceLinkHref<Resource>;\n}\n\nexport type CreateResourceLinkConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = {\n [Key in Resource]?: CreateResourceLinkOptions<\n Resources,\n Key\n >;\n};\n\nexport type InferHashFromResourceLinkHref<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Hash extends string\n ? Hash\n : Hash extends ResourceAnchorLinkFn<Resource>\n ? ReturnType<Hash>\n : never;\n\nexport type CreatedResourceHref = {\n /**\n * The full generated href with the `hash` if provided.\n */\n full: string;\n /**\n * The given href. This is the same value as the `href` property of the config.\n */\n given: string;\n};\n\nexport type CreatedResourceLinkBase<Resource extends string> = {\n activeIcon?: ReactNode;\n href: CreatedResourceHref;\n label: string;\n resource: Resource;\n icon: ResourceIcon;\n};\nexport type CreateResourceLinkWithHash<\n Resource extends string,\n Hash extends ResourceLinkHref<Resource>,\n> = Omit<CreatedResourceLinkBase<Resource>, 'href'> & {\n href: CreatedResourceHref & {\n hash: InferHashFromResourceLinkHref<Resource, Hash>;\n };\n};\n\nexport type CreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Config,\n> = {\n [Resource in keyof Config]-?: Resource extends ResourceLinkConfigResource<Resources>\n ? Config[Resource] extends {\n hash: infer hash extends ResourceLinkHref<Resource>;\n }\n ? CreateResourceLinkWithHash<Resource, hash>\n : CreatedResourceLinkBase<Resource>\n : never;\n}[keyof Config];\n\nexport interface CreateResourceLinksFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n config: CreateResourceLinkConfig<Resources, Resource>,\n ): Array<\n CreatedResourceLink<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n >\n >;\n createResourceMap: CreateResourceMapFn<Resources>;\n defineIcon: DefineResourceIconFn<Resources>;\n withGroups: CreateResourceLinksWithGroupsFn<Resources>;\n}\n\nexport interface CreateResourceLinkGroupOptions<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Links extends object =\n CreateResourceLinkConfig<Resources>,\n> {\n /**\n * An optional, caller-provided identity for the group. Provide this when you\n * combine groups from more than one `withGroups()` call into one list (e.g.\n * a module-owned nav section alongside a feature-owned one) — without it,\n * the id is derived from the group's `label` and its link resources, which\n * is enough to keep groups from a single call apart but can't distinguish\n * two independently-defined groups that happen to look identical.\n */\n id?: string;\n /**\n * The label of the group. This is the text that will be displayed for the group.\n */\n label?: string;\n /**\n * An optional icon to render when the group is active.\n */\n activeIcon?: ReactNode;\n /**\n * An optional icon to register with the group. This is a headless approach, meaning you have full control\n * over the icon's rendering.\n */\n icon?: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n /**\n * The resource links in this group.\n */\n links: Links;\n}\n\nexport type CreateResourceLinkGroupInput<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = CreateResourceLinkGroupOptions<\n Resources,\n CreateResourceLinkConfig<Resources, Resource>\n>;\n\nexport type AnyCreatedResourceLink<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n [Resource in LayoutResourceKey<Resources>]:\n | CreatedResourceLinkBase<Resource>\n | CreateResourceLinkWithHash<Resource, string>;\n}[LayoutResourceKey<Resources>];\n\nexport type CreateResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = {\n activeIcon?: ReactNode;\n id: string;\n label: string | null;\n icon: ResourceIcon<LayoutRenderResourcesAccessor<Resources>>;\n links: Array<AnyCreatedResourceLink<Resources>>;\n};\n\nexport type ResolvedResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> = Omit<CreateResourceLinksWithGroups<Resources>, 'icon' | 'links'> & {\n icon: ReactNode;\n links: Array<\n Omit<AnyCreatedResourceLink<Resources>, 'icon'> & { icon: ReactNode }\n >;\n};\n\nexport type CreateResourceMap<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Value,\n> = Partial<Record<LayoutResourceKey<Resources>, Value>>;\n\nexport type ValidateResource<\n Resource extends string,\n Map extends object,\n> = Resource extends Extract<keyof Map, string> ? Resource : never;\n\nexport type CreatedResourceMap<Map extends object> = Map & {\n isValidResource(value: unknown): value is Extract<keyof Map, string>;\n get<const Resource extends string>(\n resource: ValidateResource<Resource, Map>,\n ): Map[typeof resource];\n};\n\nexport interface CreateResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n /**\n * Creates a resource map for this layout.\n *\n * @param map Values keyed by this layout's resources. An unknown resource,\n * or a key that collides with a map helper, is rejected.\n */\n <const Map extends CreateResourceMap<Resources, unknown>>(\n map: Map,\n ): CreatedResourceMap<Map>;\n}\n\nexport interface CreateResourceLinksWithGroupsFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n> {\n /**\n * Groups resource links into sections.\n *\n * @param groups The sections to create. At least one is required. The link\n * keys in those sections are the resources `findResource` can resolve.\n */\n <const Resource extends ResourceLinkConfigResource<Resources>>(\n groups: NonEmptyReadonlyArray<\n CreateResourceLinkGroupInput<Resources, Resource>\n >,\n ): CreateResourceLinksWithGroupsResult<Resources, Resource>;\n}\n\nexport type CreateResourceLinksWithGroupsResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = Array<CreateResourceLinksWithGroups<Resources>> & {\n /**\n * Resolves each group's icon and its link icons for the active resource.\n *\n * Functional icons receive the resources accessor for `resource`. Read the\n * groups value directly when you do not need to resolve icons.\n *\n * @param resource The active resource key.\n */\n (\n resource: LayoutResourceKey<Resources>,\n ): ResolvedResourceLinksWithGroupsResult<Resources, Resource>;\n /**\n * Creates a resource map for this layout. Same helper as\n * `createResourceLinks.createResourceMap`.\n *\n * @param map Values keyed by this layout's resources. An unknown resource,\n * or a key that collides with a map helper, is rejected.\n */\n createResourceMap: CreateResourceMapFn<Resources>;\n /**\n * Returns the group whose links include `resource`.\n *\n * The result is `undefined` when that resource was not configured in any\n * group. A resource outside this layout is rejected by the type.\n *\n * @param resource The resource whose group should be returned.\n */\n findResource<\n const RequestedResource extends LayoutResourceKey<Resources> | Resource,\n >(\n resource: RequestedResource,\n ): FindResourceGroupResult<Resources, Resource, RequestedResource>;\n};\n\nexport type ResolvedResourceLinksWithGroupsResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources> = ResourceLinkConfigResource<Resources>,\n> = Array<ResolvedResourceLinksWithGroups<Resources>> & {\n /** Returns the resolved group whose links include `resource`. */\n findResource<\n const RequestedResource extends LayoutResourceKey<Resources> | Resource,\n >(\n resource: RequestedResource,\n ): FindResolvedResourceGroupResult<Resources, Resource, RequestedResource>;\n};\n\nexport type FindResourceGroupResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n IncludedResource extends string,\n RequestedResource extends string,\n> = RequestedResource extends unknown\n ? RequestedResource extends IncludedResource\n ? CreateResourceLinksWithGroups<Resources>\n : undefined\n : never;\n\nexport type FindResolvedResourceGroupResult<\n Resources extends ReadonlyArray<ResourceDefinition>,\n IncludedResource extends string,\n RequestedResource extends string,\n> = RequestedResource extends unknown\n ? RequestedResource extends IncludedResource\n ? ResolvedResourceLinksWithGroups<Resources>\n : undefined\n : never;\n\nfunction resolveResourceLinkHref<Resource extends string>(\n value: ResourceLinkHref<Resource> | undefined,\n resource: Resource,\n fallback: string,\n) {\n return typeof value === 'function' ? value(resource) : (value ?? fallback);\n}\n\nfunction normalizeResourceLinkHash(hash: string) {\n return hash.replace(/^#+/, '');\n}\n\nfunction createFullResourceLinkHref(href: string, hash: string) {\n if (href.includes('#')) {\n return href;\n }\n\n return `${href}#${normalizeResourceLinkHash(hash)}`;\n}\n\nfunction createResourceLinksFromConfig<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(config: CreateResourceLinkConfig<Resources, Resource>) {\n return Object.entries(config).map(([resource, config]) => {\n if (!config) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"config\" must be an object for the ${resource} resource. Received ${typeof config}`,\n );\n }\n\n if (!('label' in config)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" is required for the ${resource} resource.`,\n );\n }\n\n if (typeof config.label !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"label\" must be a string for the ${resource} resource. Received ${typeof config.label}`,\n );\n }\n\n const linkConfig = config as CreateResourceLinkOptions<\n Resources,\n typeof resource\n >;\n\n if (\n 'href' in linkConfig &&\n linkConfig.href !== undefined &&\n typeof linkConfig.href !== 'string' &&\n typeof linkConfig.href !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"href\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.href}`,\n );\n }\n\n if (\n 'hash' in linkConfig &&\n linkConfig.hash !== undefined &&\n typeof linkConfig.hash !== 'string' &&\n typeof linkConfig.hash !== 'function'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks]: \"hash\" must be a string or function for the ${resource} resource. Received ${typeof linkConfig.hash}`,\n );\n }\n\n const givenHref = resolveResourceLinkHref(linkConfig.href, resource, '/');\n const givenHash = resolveResourceLinkHref(\n linkConfig.hash,\n resource,\n resource,\n );\n const href = {\n given: givenHref,\n full: createFullResourceLinkHref(givenHref, givenHash),\n ...(linkConfig.hash !== undefined && !givenHref.includes('#')\n ? { hash: normalizeResourceLinkHash(givenHash) }\n : {}),\n };\n\n return {\n ...(linkConfig.activeIcon !== undefined\n ? { activeIcon: linkConfig.activeIcon }\n : {}),\n href,\n label: config.label,\n resource,\n icon: 'icon' in config ? config.icon : null,\n };\n });\n}\n\n/**\n * Deterministic, not random: a random id (the previous implementation used\n * `crypto.randomUUID()` / `Math.random()`) would make `withGroups()` unsafe to\n * call from module scope — some runtimes (e.g. Cloudflare Workers) disallow\n * generating random values outside a request handler, and config arrays like\n * this are routinely defined as top-level constants.\n *\n * Falls back to the group's own shape — its `label` and its link resource\n * keys — rather than just the array index, so groups from *separate*\n * `withGroups()` calls that get concatenated into one list (a module-owned nav\n * section next to a feature-owned one, say) don't collide just because they're\n * both first in their own call. Two groups that are genuinely identical in\n * shape will still collide; pass an explicit `id` on the group to rule that\n * out entirely.\n */\nfunction createResourceLinkGroupId(\n group: { id?: unknown; label?: unknown; links: object },\n index: number,\n) {\n if (typeof group.id === 'string' && group.id.length > 0) {\n return group.id;\n }\n\n const label = typeof group.label === 'string' ? group.label : '';\n const linkKeys = Object.keys(group.links).join(',');\n return `group-${index}-${label}-${linkKeys}`;\n}\n\nfunction readResourceValue(resource: ResourceDefinition) {\n return typeof resource === 'string' ? resource : resource.value;\n}\n\nfunction createResourceSelection(\n definitions: ReadonlyArray<ResourceDefinition>,\n): ResourceSelection<ReadonlyArray<ResourceDefinition>> {\n const selection = (() => definitions) as ResourceSelection<\n ReadonlyArray<ResourceDefinition>\n >;\n\n selection.isResource = createIsValidResourceFn(definitions);\n\n return selection;\n}\n\nfunction createRenderResourcesAccessor<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(resources: Resources, current: LayoutResourceKey<Resources>) {\n const accessor = Object.assign(() => resources, {\n current,\n }) as LayoutRenderResourcesAccessor<Resources>;\n\n accessor.isResource = createIsValidResourceFn(resources);\n accessor.omit = ((...keys: string[]) =>\n createResourceSelection(\n resources.filter((resource) => !keys.includes(readResourceValue(resource))),\n )) as LayoutRenderResourcesAccessor<Resources>['omit'];\n accessor.pick = ((...keys: string[]) =>\n createResourceSelection(\n resources.filter((resource) => keys.includes(readResourceValue(resource))),\n )) as LayoutRenderResourcesAccessor<Resources>['pick'];\n\n return accessor;\n}\n\nfunction createResourceMapFn<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(resources: Resources): CreateResourceMapFn<Resources> {\n const helperNames = new Set(['get', 'isValidResource']);\n const resourceKeys = new Set(\n resources.map((resource) =>\n typeof resource === 'string' ? resource : resource.value,\n ),\n );\n\n return function createResourceMap<\n const Map extends CreateResourceMap<Resources, unknown>,\n >(map: Map) {\n const configuredResources = Object.keys(map);\n\n for (const resource of configuredResources) {\n if (!resourceKeys.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: unknown resource \"${resource}\".`,\n );\n }\n\n if (helperNames.has(resource)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.createResourceMap]: resource \"${resource}\" conflicts with a map helper.`,\n );\n }\n }\n\n const configuredResourceKeys = new Set(configuredResources);\n const resourceValues = new Map(\n configuredResources.map((resource) => [\n resource,\n (map as Record<string, unknown>)[resource],\n ]),\n );\n\n Object.defineProperties(map, {\n get: {\n value(resource: string) {\n return resourceValues.get(resource);\n },\n },\n isValidResource: {\n value(value: unknown): value is Extract<keyof Map, string> {\n return (\n typeof value === 'string' && configuredResourceKeys.has(value)\n );\n },\n },\n });\n\n return map as CreatedResourceMap<Map>;\n };\n}\n\nfunction createResourceLinksWithGroups<\n Resources extends ReadonlyArray<ResourceDefinition>,\n Resource extends ResourceLinkConfigResource<Resources>,\n>(\n resources: Resources,\n createResourceMap: CreateResourceMapFn<Resources>,\n groups: ReadonlyArray<CreateResourceLinkGroupInput<Resources, Resource>>,\n): CreateResourceLinksWithGroupsResult<Resources, Resource> {\n if (!Array.isArray(groups)) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"groups\" must be an array.',\n );\n }\n\n if (groups.length === 0) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"groups\" must include at least one group.',\n );\n }\n\n const createdGroups = groups.map((group, index) => {\n if (!group) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} is required.`,\n );\n }\n\n if (typeof group !== 'object') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: group at index ${index} must be an object. Received ${typeof group}`,\n );\n }\n\n if (\n 'label' in group &&\n group.label !== undefined &&\n typeof group.label !== 'string'\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"label\" must be a string for group at index ${index}. Received ${typeof group.label}`,\n );\n }\n\n if ('id' in group && group.id !== undefined && typeof group.id !== 'string') {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"id\" must be a string for group at index ${index}. Received ${typeof group.id}`,\n );\n }\n\n if (!('links' in group)) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" is required for group at index ${index}.`,\n );\n }\n\n if (\n typeof group.links !== 'object' ||\n group.links === null ||\n Array.isArray(group.links)\n ) {\n throw new LayoutConfigurationError(\n `[createResourceLinks.withGroups]: \"links\" must be an object for group at index ${index}. Received ${Array.isArray(group.links) ? 'array' : typeof group.links}`,\n );\n }\n\n return {\n ...(group.activeIcon !== undefined\n ? { activeIcon: group.activeIcon }\n : {}),\n id: createResourceLinkGroupId(group, index),\n label: 'label' in group ? (group.label ?? null) : null,\n icon: 'icon' in group ? group.icon : null,\n links: createResourceLinksFromConfig(group.links),\n };\n }) as Array<CreateResourceLinksWithGroups<Resources>>;\n\n function resolveGroups(resource?: LayoutResourceKey<Resources>) {\n if (resource === undefined) {\n throw new LayoutConfigurationError(\n '[createResourceLinks.withGroups]: \"resource\" is required.',\n );\n }\n\n const currentResources = createRenderResourcesAccessor(resources, resource);\n\n const resolvedGroups = createdGroups.map((group) => ({\n ...group,\n icon:\n typeof group.icon === 'function'\n ? group.icon(currentResources)\n : group.icon,\n links: group.links.map((link) => ({\n ...link,\n icon:\n typeof link.icon === 'function'\n ? link.icon(currentResources)\n : link.icon,\n })),\n }));\n\n Object.defineProperty(resolvedGroups, 'findResource', {\n value(requestedResource: ResourceLinkConfigResource<Resources>) {\n return resolvedGroups.find((group) =>\n group.links.some((link) => link.resource === requestedResource),\n );\n },\n });\n\n return resolvedGroups as ResolvedResourceLinksWithGroupsResult<\n Resources,\n Resource\n >;\n }\n\n const callableGroups = new Proxy(resolveGroups, {\n get(target, property, receiver) {\n if (property === 'createResourceMap') {\n return createResourceMap;\n }\n\n if (property === Symbol.iterator) {\n return createdGroups[Symbol.iterator].bind(createdGroups);\n }\n\n if (property === 'toJSON') {\n return () => createdGroups;\n }\n\n if (property in createdGroups) {\n const value = Reflect.get(createdGroups, property);\n return typeof value === 'function' ? value.bind(createdGroups) : value;\n }\n\n return Reflect.get(target, property, receiver);\n },\n getOwnPropertyDescriptor(target, property) {\n return (\n Reflect.getOwnPropertyDescriptor(createdGroups, property) ??\n Reflect.getOwnPropertyDescriptor(target, property)\n );\n },\n getPrototypeOf() {\n return Array.prototype;\n },\n ownKeys(target) {\n return [\n ...new Set([\n ...Reflect.ownKeys(target),\n ...Reflect.ownKeys(createdGroups),\n ]),\n ];\n },\n set(target, property, value, receiver) {\n if (property in createdGroups || /^\\d+$/.test(String(property))) {\n return Reflect.set(createdGroups, property, value);\n }\n\n return Reflect.set(target, property, value, receiver);\n },\n });\n Object.defineProperty(createdGroups, 'findResource', {\n value(resource: ResourceLinkConfigResource<Resources>) {\n return createdGroups.find((group) =>\n group.links.some((link) => link.resource === resource),\n );\n },\n });\n\n return callableGroups as CreateResourceLinksWithGroupsResult<\n Resources,\n Resource\n >;\n}\n\nfunction createDefineIcon<\n Resources extends ReadonlyArray<ResourceDefinition>,\n>(): DefineResourceIconFn<Resources> {\n function defineIcon<Icon extends ElementType = ElementType>(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n Icon: Icon;\n }) => ReactNode,\n ) {\n return function createIcon(Icon: Icon) {\n return function renderIcon(\n resource: LayoutRenderResourcesAccessor<Resources>,\n ) {\n return render({ resource, Icon });\n };\n };\n }\n\n defineIcon.input = function defineIconInput<Definition>(\n defineInput: (\n helpers: DefineResourceIconInputHelpers<Resources>,\n ) => Definition,\n ): DefineResourceIconInputResult<Resources, Definition> {\n const resourceMarker = {} as ResourceIconInputMarker<never>;\n const componentMarker = {} as ResourceIconComponentInputMarker<ElementType>;\n\n defineInput({\n resource() {\n return resourceMarker;\n },\n icon<Icon extends ElementType = ElementType>() {\n return componentMarker as ResourceIconComponentInputMarker<Icon>;\n },\n });\n\n return {\n render(\n render: (options: {\n resource: LayoutRenderResourcesAccessor<Resources>;\n input: ResolveResourceIconInput<Definition>;\n }) => ReactNode,\n ) {\n return function createIcon(\n input: ResolveResourceIconInput<Definition>,\n ) {\n return function renderIcon(\n resource: LayoutRenderResourcesAccessor<Resources>,\n ) {\n return render({ resource, input });\n };\n };\n },\n };\n };\n\n return defineIcon;\n}\n\nexport function createResourceLinksFn<\n const Resources extends ReadonlyArray<ResourceDefinition>,\n>(_resources: Resources): CreateResourceLinksFn<Resources> {\n const createResourceMap = createResourceMapFn(_resources);\n const defineIcon = createDefineIcon<Resources>();\n\n function createResourceLinks<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(config: CreateResourceLinkConfig<Resources, Resource>) {\n return createResourceLinksFromConfig(config);\n }\n\n function withGroups<\n const Resource extends ResourceLinkConfigResource<Resources>,\n >(\n groups: NonEmptyReadonlyArray<\n CreateResourceLinkGroupInput<Resources, Resource>\n >,\n ) {\n return createResourceLinksWithGroups(_resources, createResourceMap, groups);\n }\n\n return Object.assign(createResourceLinks, {\n createResourceMap,\n defineIcon,\n withGroups,\n }) as CreateResourceLinksFn<Resources>;\n}\n"],"mappings":";;;;AA0XA,SAAS,wBACP,OACA,UACA,UACA;CACA,OAAO,OAAO,UAAU,aAAa,MAAM,QAAQ,IAAK,SAAS;AACnE;AAEA,SAAS,0BAA0B,MAAc;CAC/C,OAAO,KAAK,QAAQ,OAAO,EAAE;AAC/B;AAEA,SAAS,2BAA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B,YAAY,mBAAmB,MAAM;CAC5E;CAEA,OAAO,OAAO,OAAO,qBAAqB;EACxC;EACA;EACA;CACF,CAAC;AACH"}
|
|
@@ -5,6 +5,6 @@ import { CreateGetComponentForPath, GetComponentForPath, GetComponentForPathApi,
|
|
|
5
5
|
import { ForPaths, ForPathsRenderContext, RenderedPathComponent, RenderedPathComponentHOF, TargetPathBuilder } from "./for-paths/builder.cjs";
|
|
6
6
|
import { IsSubResourceKey, IsSubResourceKeyOptions, IsSubResourceKeyResult } from "./is-sub-resource-key.cjs";
|
|
7
7
|
import { ComponentTypes, CreateResourceConfigFn, CreatedResourceConfig, GetComponent, GetComponentAtBound, GetComponentForResource, GetComponentForResourceOptions, GetComponentOptions, GetComponentOptionsForResource, ResourceConfigPath, ResourceFromGetComponentBound, SubResourceFromGetComponentBound, ValidateForResourceBound } from "./get-component.cjs";
|
|
8
|
-
import { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolvedResourceLinksWithGroups, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource } from "./create-resource-links.cjs";
|
|
8
|
+
import { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, DefineResourceIconInputHelpers, DefineResourceIconInputResult, FindResolvedResourceGroupResult, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolveResourceIconInput, ResolvedResourceLinksWithGroups, ResolvedResourceLinksWithGroupsResult, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource } from "./create-resource-links.cjs";
|
|
9
9
|
import { CreateResourceLayoutFn, CreateResourceLayoutMakeComposableOptions, CreateResourceLayoutWithLayoutOptions, DefineResourceLayout, DefineResourceLayoutFn, DefineResourceLayoutForResources, DefineResourceLayoutForResourcesFactory, DefineResourceLayoutWithLayoutFn, DefineResourcesFn, DefinedResourceLayoutWithShell, LayoutRenderResourcesAccessor, LayoutResourcesAccessor, ResourceLayoutProvider, ResourceLayoutProviderProps, ResourceLayoutShellComponent, ResourceLayoutShellProps, ResourceSelection, ShellLayoutDefinition, ShellRenderContext, defineResourceLayout } from "./define-layout.cjs";
|
|
10
10
|
import { ResourceLayoutShellPublisher } from "./shell-context.cjs";
|
|
@@ -5,6 +5,6 @@ import { CreateGetComponentForPath, GetComponentForPath, GetComponentForPathApi,
|
|
|
5
5
|
import { ForPaths, ForPathsRenderContext, RenderedPathComponent, RenderedPathComponentHOF, TargetPathBuilder } from "./for-paths/builder.mjs";
|
|
6
6
|
import { IsSubResourceKey, IsSubResourceKeyOptions, IsSubResourceKeyResult } from "./is-sub-resource-key.mjs";
|
|
7
7
|
import { ComponentTypes, CreateResourceConfigFn, CreatedResourceConfig, GetComponent, GetComponentAtBound, GetComponentForResource, GetComponentForResourceOptions, GetComponentOptions, GetComponentOptionsForResource, ResourceConfigPath, ResourceFromGetComponentBound, SubResourceFromGetComponentBound, ValidateForResourceBound } from "./get-component.mjs";
|
|
8
|
-
import { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolvedResourceLinksWithGroups, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource } from "./create-resource-links.mjs";
|
|
8
|
+
import { CreateResourceLinkConfig, CreateResourceLinkGroupInput, CreateResourceLinkGroupOptions, CreateResourceLinkOptions, CreateResourceLinksFn, CreateResourceLinksWithGroups, CreateResourceLinksWithGroupsFn, CreateResourceLinksWithGroupsResult, CreateResourceMap, CreateResourceMapFn, CreatedResourceHref, CreatedResourceLink, CreatedResourceLinkBase, CreatedResourceMap, DefineResourceIconFn, DefineResourceIconInputHelpers, DefineResourceIconInputResult, FindResolvedResourceGroupResult, FindResourceGroupResult, InferHashFromResourceLinkHref, ResolveResourceIconInput, ResolvedResourceLinksWithGroups, ResolvedResourceLinksWithGroupsResult, ResourceAnchorLinkFn, ResourceIcon, ResourceIconFn, ResourceLinkHref, ValidateResource } from "./create-resource-links.mjs";
|
|
9
9
|
import { CreateResourceLayoutFn, CreateResourceLayoutMakeComposableOptions, CreateResourceLayoutWithLayoutOptions, DefineResourceLayout, DefineResourceLayoutFn, DefineResourceLayoutForResources, DefineResourceLayoutForResourcesFactory, DefineResourceLayoutWithLayoutFn, DefineResourcesFn, DefinedResourceLayoutWithShell, LayoutRenderResourcesAccessor, LayoutResourcesAccessor, ResourceLayoutProvider, ResourceLayoutProviderProps, ResourceLayoutShellComponent, ResourceLayoutShellProps, ResourceSelection, ShellLayoutDefinition, ShellRenderContext, defineResourceLayout } from "./define-layout.mjs";
|
|
10
10
|
import { ResourceLayoutShellPublisher } from "./shell-context.mjs";
|
package/package.json
CHANGED
|
@@ -47,6 +47,111 @@ does not follow the general pattern.
|
|
|
47
47
|
Render links using the generated `resource` value instead of reconstructing it
|
|
48
48
|
from labels.
|
|
49
49
|
|
|
50
|
+
### Define reusable resource icons
|
|
51
|
+
|
|
52
|
+
The `icon` property accepts either a plain React node or a function. Use a
|
|
53
|
+
plain node when the icon does not depend on the current resource:
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
const navigation = createResourceLinks.withGroups([
|
|
57
|
+
{
|
|
58
|
+
links: {
|
|
59
|
+
dashboard: {
|
|
60
|
+
label: 'Dashboard',
|
|
61
|
+
icon: <DashboardIcon />,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
]);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Use `defineIcon` when multiple link definitions share the same render logic.
|
|
69
|
+
Without custom input, the returned function accepts the icon component
|
|
70
|
+
directly. Its render callback receives the full render-scoped resources
|
|
71
|
+
accessor:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import type { LucideIcon } from 'lucide-react';
|
|
75
|
+
|
|
76
|
+
const defineDashboardIcon = createResourceLinks.defineIcon<LucideIcon>(
|
|
77
|
+
({ resource, Icon }) => (
|
|
78
|
+
<Icon
|
|
79
|
+
fill={resource.current === 'dashboard' ? 'currentColor' : 'none'}
|
|
80
|
+
/>
|
|
81
|
+
),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const navigation = createResourceLinks.withGroups([
|
|
85
|
+
{
|
|
86
|
+
links: {
|
|
87
|
+
dashboard: {
|
|
88
|
+
label: 'Dashboard',
|
|
89
|
+
icon: defineDashboardIcon(DashboardIcon),
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
]);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Use `.input(...).render(...)` when the same renderer needs caller-provided
|
|
97
|
+
values. The `.input` callback declares the input shape: `resource()` represents
|
|
98
|
+
a valid resource key and `icon<IconType>()` represents an icon component. The
|
|
99
|
+
render callback receives the full resources accessor and the inferred input
|
|
100
|
+
object. The icon component is available as
|
|
101
|
+
`input.Icon`; it is intentionally not duplicated as a top-level render value.
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
import type { LucideIcon } from 'lucide-react';
|
|
105
|
+
|
|
106
|
+
const defineNavigationIcon = createResourceLinks.defineIcon
|
|
107
|
+
.input(({ resource, icon }) => ({
|
|
108
|
+
resource: resource(),
|
|
109
|
+
Icon: icon<LucideIcon>(),
|
|
110
|
+
}))
|
|
111
|
+
.render(({ resource, input }) => {
|
|
112
|
+
const Icon = input.Icon;
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<Icon
|
|
116
|
+
fill={
|
|
117
|
+
resource.current === input.resource ? 'currentColor' : 'none'
|
|
118
|
+
}
|
|
119
|
+
/>
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const navigation = createResourceLinks.withGroups([
|
|
124
|
+
{
|
|
125
|
+
links: {
|
|
126
|
+
dashboard: {
|
|
127
|
+
label: 'Dashboard',
|
|
128
|
+
icon: defineNavigationIcon({
|
|
129
|
+
resource: 'dashboard',
|
|
130
|
+
Icon: DashboardIcon,
|
|
131
|
+
}),
|
|
132
|
+
},
|
|
133
|
+
appointments: {
|
|
134
|
+
label: 'Appointments',
|
|
135
|
+
icon: defineNavigationIcon({
|
|
136
|
+
resource: 'appointments',
|
|
137
|
+
Icon: AppointmentsIcon,
|
|
138
|
+
}),
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
]);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Resolve functional icons by calling the grouped collection with the current
|
|
146
|
+
resource key. The collection remains array-like when unresolved, so array
|
|
147
|
+
methods such as `filter` and `map` are still available directly.
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
const resolvedNavigation = navigation(currentResource);
|
|
151
|
+
const currentGroup = resolvedNavigation.findResource(currentResource);
|
|
152
|
+
const visibleGroups = navigation.filter(Boolean);
|
|
153
|
+
```
|
|
154
|
+
|
|
50
155
|
## Create the resource configuration
|
|
51
156
|
|
|
52
157
|
Create a typed configuration that maps each routeable resource to its normal
|