@rebasepro/app 0.10.0 → 0.10.1-canary.0a881d4

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.
Files changed (128) hide show
  1. package/dist/auth/types.d.ts +2 -1
  2. package/dist/collections/collection_view_config.d.ts +32 -0
  3. package/dist/collections/entity_image_preview.d.ts +2 -0
  4. package/dist/collections/filter-operator-resolution.d.ts +32 -0
  5. package/dist/collections/index.d.ts +8 -0
  6. package/dist/collections/navigation_from_path.d.ts +34 -0
  7. package/dist/collections/navigation_utils.d.ts +20 -0
  8. package/dist/collections/parent_references_from_path.d.ts +7 -0
  9. package/dist/collections/property_presentation.d.ts +20 -0
  10. package/dist/collections/title-property.d.ts +43 -0
  11. package/dist/components/Debug/UIReferenceView.d.ts +10 -3
  12. package/dist/components/LoginView/LoginView.d.ts +1 -1
  13. package/dist/components/RebaseAuth.d.ts +1 -1
  14. package/dist/components/common/types.d.ts +1 -1
  15. package/dist/components/common/useColumnsIds.d.ts +5 -4
  16. package/dist/components/common/useDataTableController.d.ts +3 -3
  17. package/dist/contexts/AnalyticsContext.d.ts +1 -1
  18. package/dist/contexts/AuthControllerContext.d.ts +1 -1
  19. package/dist/contexts/CollectionScopeContext.d.ts +4 -4
  20. package/dist/contexts/ComponentOverrideContext.d.ts +1 -1
  21. package/dist/contexts/CustomizationControllerContext.d.ts +1 -1
  22. package/dist/contexts/DialogsProvider.d.ts +1 -1
  23. package/dist/contexts/UserConfigurationPersistenceContext.d.ts +1 -1
  24. package/dist/core/PluginLifecycleManager.d.ts +1 -1
  25. package/dist/core/PluginProviderStack.d.ts +1 -1
  26. package/dist/core/RebaseProps.d.ts +2 -1
  27. package/dist/hooks/data/delete.d.ts +4 -4
  28. package/dist/hooks/data/save.d.ts +5 -5
  29. package/dist/hooks/data/useCollection.d.ts +2 -2
  30. package/dist/hooks/data/useFetch.d.ts +2 -2
  31. package/dist/hooks/data/useRelationSelector.d.ts +2 -2
  32. package/dist/hooks/useAnalyticsController.d.ts +1 -1
  33. package/dist/hooks/useAuthController.d.ts +2 -1
  34. package/dist/hooks/useAuthSubscription.d.ts +2 -1
  35. package/dist/hooks/useBuildLocalConfigurationPersistence.d.ts +1 -1
  36. package/dist/hooks/useComponentOverride.d.ts +1 -1
  37. package/dist/hooks/useCustomizationController.d.ts +1 -1
  38. package/dist/hooks/useDialogsController.d.ts +1 -1
  39. package/dist/hooks/usePermissions.d.ts +5 -5
  40. package/dist/hooks/useRebaseContext.d.ts +2 -1
  41. package/dist/hooks/useRebaseRegistry.d.ts +1 -1
  42. package/dist/hooks/useSlot.d.ts +1 -1
  43. package/dist/hooks/useSnackbarController.d.ts +1 -1
  44. package/dist/hooks/useStudioBridge.d.ts +2 -1
  45. package/dist/hooks/useUserConfigurationPersistence.d.ts +1 -1
  46. package/dist/i18n/RebaseI18nProvider.d.ts +1 -1
  47. package/dist/index.d.ts +1 -0
  48. package/dist/index.es.js +799 -121
  49. package/dist/index.es.js.map +1 -1
  50. package/dist/locales/de.d.ts +1 -1
  51. package/dist/locales/en.d.ts +1 -1
  52. package/dist/locales/es.d.ts +1 -1
  53. package/dist/locales/fr.d.ts +1 -1
  54. package/dist/locales/hi.d.ts +1 -1
  55. package/dist/locales/it.d.ts +1 -1
  56. package/dist/locales/pt.d.ts +1 -1
  57. package/dist/util/previews.d.ts +10 -5
  58. package/dist/vitePlugin.js +1 -1
  59. package/package.json +9 -7
  60. package/src/auth/types.ts +2 -1
  61. package/src/collections/collection_view_config.ts +57 -0
  62. package/src/collections/entity_image_preview.ts +48 -0
  63. package/src/collections/filter-operator-resolution.ts +81 -0
  64. package/src/collections/index.ts +8 -0
  65. package/src/collections/navigation_from_path.ts +123 -0
  66. package/src/collections/navigation_utils.ts +171 -0
  67. package/src/collections/parent_references_from_path.ts +59 -0
  68. package/src/collections/property_presentation.ts +224 -0
  69. package/src/collections/title-property.ts +264 -0
  70. package/src/components/Debug/UIReferenceView.tsx +31 -27
  71. package/src/components/Debug/collection-views/CardViewDemo.tsx +4 -2
  72. package/src/components/Debug/collection-views/CollectionTableDemo.tsx +2 -2
  73. package/src/components/Debug/collection-views/KanbanBoardDemo.tsx +1 -2
  74. package/src/components/Debug/crm-dashboard/CalendarWidget.tsx +5 -7
  75. package/src/components/Debug/crm-dashboard/CrmDashboardDemo.tsx +14 -20
  76. package/src/components/Debug/crm-dashboard/DashboardMetrics.tsx +7 -9
  77. package/src/components/Debug/crm-dashboard/PipelineOverview.tsx +8 -10
  78. package/src/components/Debug/crm-dashboard/RecentActivity.tsx +25 -27
  79. package/src/components/Debug/crm-dashboard/TaskFilters.tsx +7 -9
  80. package/src/components/Debug/crm-dashboard/TaskTable.tsx +8 -10
  81. package/src/components/LoginView/LoginView.tsx +55 -4
  82. package/src/components/RebaseAuth.tsx +1 -1
  83. package/src/components/common/types.tsx +1 -1
  84. package/src/components/common/useColumnsIds.tsx +24 -23
  85. package/src/components/common/useDataTableController.tsx +4 -4
  86. package/src/contexts/AnalyticsContext.tsx +1 -1
  87. package/src/contexts/AuthControllerContext.tsx +1 -1
  88. package/src/contexts/CollectionScopeContext.tsx +6 -5
  89. package/src/contexts/ComponentOverrideContext.tsx +1 -1
  90. package/src/contexts/CustomizationControllerContext.tsx +1 -1
  91. package/src/contexts/DialogsProvider.tsx +1 -1
  92. package/src/contexts/UserConfigurationPersistenceContext.tsx +1 -1
  93. package/src/core/PluginLifecycleManager.tsx +2 -1
  94. package/src/core/PluginProviderStack.tsx +1 -1
  95. package/src/core/Rebase.tsx +4 -3
  96. package/src/core/RebaseProps.tsx +2 -1
  97. package/src/core/RebaseRouter.tsx +1 -1
  98. package/src/hooks/data/delete.ts +5 -4
  99. package/src/hooks/data/save.ts +6 -5
  100. package/src/hooks/data/useCollection.tsx +3 -2
  101. package/src/hooks/data/useFetch.tsx +4 -3
  102. package/src/hooks/data/useRelationSelector.tsx +3 -2
  103. package/src/hooks/useAnalyticsController.tsx +1 -1
  104. package/src/hooks/useAuthController.tsx +2 -1
  105. package/src/hooks/useAuthSubscription.ts +2 -1
  106. package/src/hooks/useBuildLocalConfigurationPersistence.tsx +1 -1
  107. package/src/hooks/useComponentOverride.tsx +1 -1
  108. package/src/hooks/useCustomizationController.tsx +1 -1
  109. package/src/hooks/useDialogsController.tsx +1 -1
  110. package/src/hooks/usePermissions.ts +6 -5
  111. package/src/hooks/useRebaseContext.tsx +2 -1
  112. package/src/hooks/useRebaseRegistry.tsx +1 -1
  113. package/src/hooks/useResolvedComponent.tsx +44 -11
  114. package/src/hooks/useSlot.tsx +1 -1
  115. package/src/hooks/useSnackbarController.tsx +1 -1
  116. package/src/hooks/useStudioBridge.tsx +4 -2
  117. package/src/hooks/useUserConfigurationPersistence.tsx +1 -1
  118. package/src/i18n/RebaseI18nProvider.tsx +1 -1
  119. package/src/index.ts +1 -0
  120. package/src/locales/de.ts +1 -1
  121. package/src/locales/en.ts +1 -1
  122. package/src/locales/es.ts +1 -1
  123. package/src/locales/fr.ts +1 -1
  124. package/src/locales/hi.ts +1 -1
  125. package/src/locales/it.ts +1 -1
  126. package/src/locales/pt.ts +1 -1
  127. package/src/util/previews.ts +16 -34
  128. package/src/vitePlugin.ts +5 -1
@@ -1,2 +1,2 @@
1
- import { RebaseTranslations } from "@rebasepro/types";
1
+ import { RebaseTranslations } from "@rebasepro/admin-types";
2
2
  export declare const de: RebaseTranslations;
@@ -1,4 +1,4 @@
1
- import { RebaseTranslations } from "@rebasepro/types";
1
+ import { RebaseTranslations } from "@rebasepro/admin-types";
2
2
  /**
3
3
  * English baseline translations for @rebasepro/app.
4
4
  *
@@ -1,4 +1,4 @@
1
- import { RebaseTranslations } from "@rebasepro/types";
1
+ import { RebaseTranslations } from "@rebasepro/admin-types";
2
2
  /**
3
3
  * Spanish baseline translations for @rebasepro/app.
4
4
  *
@@ -1,2 +1,2 @@
1
- import { RebaseTranslations } from "@rebasepro/types";
1
+ import { RebaseTranslations } from "@rebasepro/admin-types";
2
2
  export declare const fr: RebaseTranslations;
@@ -1,2 +1,2 @@
1
- import { RebaseTranslations } from "@rebasepro/types";
1
+ import { RebaseTranslations } from "@rebasepro/admin-types";
2
2
  export declare const hi: RebaseTranslations;
@@ -1,2 +1,2 @@
1
- import { RebaseTranslations } from "@rebasepro/types";
1
+ import { RebaseTranslations } from "@rebasepro/admin-types";
2
2
  export declare const it: RebaseTranslations;
@@ -1,4 +1,4 @@
1
- import { RebaseTranslations } from "@rebasepro/types";
1
+ import { RebaseTranslations } from "@rebasepro/admin-types";
2
2
  /**
3
3
  * Portuguese (pt) translations for @rebasepro/app.
4
4
  *
@@ -1,6 +1,6 @@
1
- import type { CollectionConfig, PropertyConfig } from "@rebasepro/types";
2
- import { AuthController } from "@rebasepro/types";
3
- export declare function getEntityPreviewKeys(authController: AuthController, targetCollection: CollectionConfig<any>, fields: Record<string, PropertyConfig>, previewProperties?: string[], limit?: number): string[];
1
+ import type { PropertyConfig, AdminCollection } from "@rebasepro/admin-types";
2
+ import { AuthController } from "@rebasepro/admin-types";
3
+ export declare function getEntityPreviewKeys(authController: AuthController, targetCollection: AdminCollection<any>, fields: Record<string, PropertyConfig>, previewProperties?: string[], limit?: number): string[];
4
4
  /**
5
5
  * The `include` params that eager-load a collection's relations in the same
6
6
  * request as its rows, so previews never fetch once per relation cell.
@@ -9,5 +9,10 @@ export declare function getEntityPreviewKeys(authController: AuthController, tar
9
9
  * relation data unconditionally and ignores it. Passing it either way keeps a
10
10
  * realtime-less deployment rendering the same cells as a realtime one.
11
11
  */
12
- export declare function getRelationIncludeParams(collection: CollectionConfig<any>): string[] | undefined;
13
- export declare function getEntityTitlePropertyKey<M extends Record<string, any>>(collection: CollectionConfig<M>, propertyConfigs: Record<string, PropertyConfig>): string | undefined;
12
+ export declare function getRelationIncludeParams(collection: AdminCollection<any>): string[] | undefined;
13
+ /**
14
+ * The property that fills the title slot for a collection. Ranking lives in
15
+ * `@rebasepro/common`, shared with the admin package so both agree on what an
16
+ * entity is called.
17
+ */
18
+ export declare function getEntityTitlePropertyKey<M extends Record<string, any>>(collection: AdminCollection<M>, propertyConfigs: Record<string, PropertyConfig>): string | undefined;
@@ -1,7 +1,7 @@
1
1
  import path from "path";
2
2
  import ts from "typescript";
3
3
  import MagicString from "magic-string";
4
- const LAZY_COMPONENT_KEYS = /* @__PURE__ */ new Set(["Field", "Preview", "Builder"]);
4
+ const LAZY_COMPONENT_KEYS = /* @__PURE__ */ new Set(["Field", "Preview", "Builder", "Filter"]);
5
5
  function walkAST(node, visitor) {
6
6
  visitor(node);
7
7
  node.forEachChild((child) => walkAST(child, visitor));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebasepro/app",
3
3
  "type": "module",
4
- "version": "0.10.0",
4
+ "version": "0.10.1-canary.0a881d4",
5
5
  "description": "Rebase core — framework-agnostic runtime for data-driven admin panels",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -52,11 +52,12 @@
52
52
  "magic-string": "^0.30.0",
53
53
  "notistack": "^3.0.2",
54
54
  "react-i18next": "^17.0.8",
55
- "@rebasepro/common": "0.10.0",
56
- "@rebasepro/types": "0.10.0",
57
- "@rebasepro/forms": "0.10.0",
58
- "@rebasepro/ui": "0.10.0",
59
- "@rebasepro/utils": "0.10.0"
55
+ "@rebasepro/admin-types": "0.10.1-canary.0a881d4",
56
+ "@rebasepro/forms": "0.10.1-canary.0a881d4",
57
+ "@rebasepro/types": "0.10.1-canary.0a881d4",
58
+ "@rebasepro/ui": "0.10.1-canary.0a881d4",
59
+ "@rebasepro/utils": "0.10.1-canary.0a881d4",
60
+ "@rebasepro/common": "0.10.1-canary.0a881d4"
60
61
  },
61
62
  "peerDependencies": {
62
63
  "react": ">=19.0.0",
@@ -124,7 +125,8 @@
124
125
  ],
125
126
  "moduleNameMapper": {
126
127
  "\\.(css|less)$": "<rootDir>/test/__mocks__/styleMock.js",
127
- "^@rebasepro/([a-z0-9-]+)$": "<rootDir>/../$1/src/index.ts"
128
+ "^@rebasepro/([a-z0-9-]+)$": "<rootDir>/../$1/src/index.ts",
129
+ "^@rebasepro/admin-types$": "<rootDir>/../admin-types/src/index.ts"
128
130
  }
129
131
  },
130
132
  "scripts": {
package/src/auth/types.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { AuthController, User, AuthTokens, DeviceSession, RebaseSession, AuthChangeEvent } from "@rebasepro/types";
1
+ import { User, AuthTokens, DeviceSession, RebaseSession, AuthChangeEvent } from "@rebasepro/types";
2
+ import { AuthController } from "@rebasepro/admin-types";
2
3
  import type { AuthConfigResponse } from "./api";
3
4
 
4
5
  /** @deprecated Use `User` from `@rebasepro/types` instead. */
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Collection helpers that only the admin panel needs.
3
+ *
4
+ * These lived in `@rebasepro/common`, which is on the backend's dependency path,
5
+ * and they read presentation fields — `defaultSelectedView`, `localChangesBackup`
6
+ * — that now live under a collection's `admin` block. A server has no use for
7
+ * either, and `resolveDefaultSelectedView` was the *only* thing making
8
+ * `@rebasepro/common` name an admin view-model type
9
+ * (`DefaultSelectedViewBuilder`), which was the last UI symbol any core package
10
+ * imported.
11
+ */
12
+ import type { CollectionConfig } from "@rebasepro/types";
13
+ import { type AdminCollection, type DefaultSelectedViewBuilder, type DefaultSelectedViewParams, resolveAdminCollection } from "@rebasepro/admin-types";
14
+ import { getSubcollections } from "@rebasepro/common";
15
+
16
+ export function resolveDefaultSelectedView(
17
+ defaultSelectedView: string | DefaultSelectedViewBuilder | undefined,
18
+ params: DefaultSelectedViewParams
19
+ ) {
20
+ if (!defaultSelectedView) {
21
+ return undefined;
22
+ } else if (typeof defaultSelectedView === "string") {
23
+ return defaultSelectedView;
24
+ } else {
25
+ return defaultSelectedView(params);
26
+ }
27
+ }
28
+
29
+ export function getLocalChangesBackup(collection: AdminCollection) {
30
+ if (!collection.localChangesBackup) {
31
+ return "manual_apply";
32
+ }
33
+
34
+ return collection.localChangesBackup;
35
+ }
36
+
37
+ /**
38
+ * Returns the primary keys for a entity collection by inspecting the properties
39
+ * and finding any properties with `isId`.
40
+ * Fallbacks to `["id"]` if no properties are marked as `isId: true`.
41
+ * @param collection
42
+ */
43
+ /**
44
+ * A collection's child collections, as the panel's view model.
45
+ *
46
+ * `getSubcollections` in `@rebasepro/common` returns the contract type, so the
47
+ * children of a resolved collection arrived un-flattened and reading
48
+ * `child.icon` or `child.hideFromNavigation` failed. Resolving is not recursive
49
+ * on purpose — children are fetched lazily through a thunk
50
+ * (`childCollections`/`subcollections`), so walking the whole tree eagerly to
51
+ * flatten it would defeat that.
52
+ */
53
+ export function getAdminSubcollections(
54
+ collection: AdminCollection | CollectionConfig
55
+ ): AdminCollection[] {
56
+ return (getSubcollections(collection as CollectionConfig) ?? []).map(resolveAdminCollection);
57
+ }
@@ -0,0 +1,48 @@
1
+ import { CollectionConfig } from "@rebasepro/types";
2
+
3
+ export function getEntityImagePreviewPropertyKey<M extends Record<string, unknown>>(collection: CollectionConfig<M>): string | undefined {
4
+
5
+ // find first storage property of type image
6
+ for (const key in collection.properties) {
7
+ const property = collection.properties[key];
8
+ if (property.type === "string" && property.storage?.acceptedFiles?.includes("image/*")) {
9
+ return key;
10
+ }
11
+ }
12
+ // alternatively, look for the first array of images
13
+ for (const key in collection.properties) {
14
+ const property = collection.properties[key];
15
+ if (property.type === "array" && !Array.isArray(property.of) && property.of?.type === "string" && property.of.storage?.acceptedFiles?.includes("image/*")) {
16
+ return key;
17
+ }
18
+ }
19
+ // also check for URL properties with image preview type
20
+ for (const key in collection.properties) {
21
+ const property = collection.properties[key];
22
+ if (property.type === "string" && property.admin?.urlPreview === "image") {
23
+ return key;
24
+ }
25
+ }
26
+ // and arrays of URL properties with image preview type
27
+ for (const key in collection.properties) {
28
+ const property = collection.properties[key];
29
+ if (property.type === "array" && property.of && !Array.isArray(property.of) && property.of.type === "string" && property.of.admin?.urlPreview === "image") {
30
+ return key;
31
+ }
32
+ }
33
+ // fallback: any storage property without explicit acceptedFiles (e.g. a generic "picture" field)
34
+ for (const key in collection.properties) {
35
+ const property = collection.properties[key];
36
+ if (property.type === "string" && property.storage && !property.storage.acceptedFiles) {
37
+ return key;
38
+ }
39
+ }
40
+ // fallback: any array of storage properties without explicit acceptedFiles
41
+ for (const key in collection.properties) {
42
+ const property = collection.properties[key];
43
+ if (property.type === "array" && !Array.isArray(property.of) && property.of?.type === "string" && property.of.storage && !property.of.storage.acceptedFiles) {
44
+ return key;
45
+ }
46
+ }
47
+ return undefined;
48
+ }
@@ -0,0 +1,81 @@
1
+ import {
2
+ ALL_WHERE_FILTER_OPS,
3
+ DataType,
4
+ getDataSourceCapabilities,
5
+ Property,
6
+ WhereFilterOp
7
+ } from "@rebasepro/types";
8
+
9
+ /**
10
+ * Default operators offered per property type, before engine capabilities and
11
+ * per-property narrowing are applied. These mirror what the built-in filter
12
+ * fields can render.
13
+ */
14
+ const COMPARISON_OPS: readonly WhereFilterOp[] = ["==", "!=", ">", ">=", "<", "<="];
15
+ const NULL_CHECK_OPS: readonly WhereFilterOp[] = ["is-null", "is-not-null"];
16
+ const MEMBERSHIP_OPS: readonly WhereFilterOp[] = ["in", "not-in"];
17
+ const PATTERN_OPS: readonly WhereFilterOp[] = ["like", "ilike", "not-like", "not-ilike"];
18
+
19
+ const DEFAULT_OPS_BY_TYPE: Partial<Record<DataType, readonly WhereFilterOp[]>> = {
20
+ string: [...COMPARISON_OPS, ...MEMBERSHIP_OPS, ...PATTERN_OPS, ...NULL_CHECK_OPS],
21
+ number: [...COMPARISON_OPS, ...MEMBERSHIP_OPS, ...NULL_CHECK_OPS],
22
+ date: [...COMPARISON_OPS, ...NULL_CHECK_OPS],
23
+ boolean: ["==", "!=", ...NULL_CHECK_OPS],
24
+ reference: ["==", "!=", ...MEMBERSHIP_OPS, ...NULL_CHECK_OPS],
25
+ relation: ["==", "!=", ...MEMBERSHIP_OPS, ...NULL_CHECK_OPS]
26
+ // geopoint, map, vector, binary, array (as a container): not filterable
27
+ // through the generic filter UI.
28
+ };
29
+
30
+ /** Operators offered when the property is an *array of* a filterable type. */
31
+ const ARRAY_OPS: readonly WhereFilterOp[] = ["array-contains", "array-contains-any"];
32
+
33
+ export interface ResolveFilterOperatorsParams {
34
+ /**
35
+ * The property to filter on. For array properties, pass the **item**
36
+ * property (`property.of`) together with `isArray: true` — the same
37
+ * convention the filter field dispatchers use.
38
+ */
39
+ property: Property;
40
+ /** True when filtering an array of `property`. */
41
+ isArray?: boolean;
42
+ /**
43
+ * The engine backing the collection (`collection.engine`, e.g.
44
+ * `"postgres"`, `"firestore"`). Falls back to the default engine's
45
+ * capabilities when omitted.
46
+ */
47
+ engine?: string;
48
+ }
49
+
50
+ /**
51
+ * Resolve which filter operators the UI should offer for a property.
52
+ *
53
+ * The result is the **intersection** of three sets:
54
+ * 1. what the engine can execute — {@link DataSourceCapabilities.filterOperators}
55
+ * (e.g. Firestore cannot run the LIKE family);
56
+ * 2. what makes sense for the property type (e.g. no `>` on booleans);
57
+ * 3. the developer's optional narrowing — `property.admin.filterOperators`.
58
+ *
59
+ * Returns an empty array when the property is not filterable (either by
60
+ * type, or because the developer disabled it with `filterOperators: []`).
61
+ *
62
+ * @group Models
63
+ */
64
+ export function resolveFilterOperators({
65
+ property,
66
+ isArray,
67
+ engine
68
+ }: ResolveFilterOperatorsParams): WhereFilterOp[] {
69
+ const typeDefaults: readonly WhereFilterOp[] = isArray
70
+ ? ARRAY_OPS
71
+ : DEFAULT_OPS_BY_TYPE[property.type] ?? [];
72
+ if (typeDefaults.length === 0) return [];
73
+
74
+ const engineOps = new Set(getDataSourceCapabilities(engine).filterOperators ?? ALL_WHERE_FILTER_OPS);
75
+
76
+ const narrowing = property.admin?.filterOperators;
77
+ const narrowingSet = narrowing !== undefined ? new Set(narrowing) : undefined;
78
+
79
+ return typeDefaults.filter(op =>
80
+ engineOps.has(op) && (narrowingSet === undefined || narrowingSet.has(op)));
81
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./collection_view_config";
2
+ export * from "./entity_image_preview";
3
+ export * from "./filter-operator-resolution";
4
+ export * from "./navigation_from_path";
5
+ export * from "./navigation_utils";
6
+ export * from "./parent_references_from_path";
7
+ export * from "./property_presentation";
8
+ export * from "./title-property";
@@ -0,0 +1,123 @@
1
+
2
+ type EntityCustomView<M extends Record<string, unknown> = Record<string, unknown>> = { key: string; [key: string]: unknown };
3
+ import { getCollectionPathsCombinations, removeInitialAndTrailingSlashes } from "./navigation_utils";
4
+ import { getSubcollections } from "@rebasepro/common";
5
+ import type { AdminCollection } from "@rebasepro/admin-types";
6
+
7
+ export type NavigationViewInternal<M extends Record<string, unknown> = Record<string, unknown>> =
8
+ | NavigationViewEntityInternal<M>
9
+ | NavigationViewCollectionInternal<M>
10
+ | NavigationViewEntityCustomInternal<M>;
11
+
12
+ export interface NavigationViewEntityInternal<M extends Record<string, unknown>> {
13
+ type: "entity";
14
+ entityId: string | number;
15
+ slug: string;
16
+ path: string;
17
+ parentCollection: AdminCollection<M>;
18
+ }
19
+
20
+ export interface NavigationViewCollectionInternal<M extends Record<string, unknown>> {
21
+ type: "collection";
22
+ id: string;
23
+ slug: string;
24
+ path: string;
25
+ collection: AdminCollection<M>;
26
+ }
27
+
28
+ export interface NavigationViewEntityCustomInternal<M extends Record<string, unknown>> {
29
+ type: "custom_view";
30
+ slug: string;
31
+ path: string;
32
+ entityId: string | number;
33
+ view: EntityCustomView<M>;
34
+ }
35
+
36
+ export function getNavigationEntriesFromPath(props: {
37
+ path: string,
38
+ collections: AdminCollection[] | undefined,
39
+ currentFullPath?: string,
40
+ contextEntityViews?: EntityCustomView[]
41
+ }): NavigationViewInternal[] {
42
+
43
+ const {
44
+ path,
45
+ collections = [],
46
+ currentFullPath
47
+ } = props;
48
+
49
+ const subpaths = removeInitialAndTrailingSlashes(path).split("/");
50
+ const subpathCombinations = getCollectionPathsCombinations(subpaths);
51
+
52
+ const result: NavigationViewInternal[] = [];
53
+ for (let i = 0; i < subpathCombinations.length; i++) {
54
+ const subpathCombination = subpathCombinations[i];
55
+
56
+ const collection = collections && collections.find((entry) => entry.slug === subpathCombination);
57
+
58
+ if (collection) {
59
+ const collectionPath = currentFullPath && currentFullPath.length > 0
60
+ ? (currentFullPath + "/" + collection.slug)
61
+ : collection.slug;
62
+ result.push({
63
+ type: "collection",
64
+ id: collection.slug,
65
+ slug: collectionPath,
66
+ path: collectionPath,
67
+ collection
68
+ });
69
+ const restOfThePath = removeInitialAndTrailingSlashes(removeInitialAndTrailingSlashes(path).replace(subpathCombination, ""));
70
+ const nextSegments = restOfThePath.length > 0 ? restOfThePath.split("/") : [];
71
+ if (nextSegments.length > 0) {
72
+ const entityId = nextSegments[0];
73
+ const path = collectionPath + "/" + entityId;
74
+ result.push({
75
+ type: "entity",
76
+ entityId,
77
+ slug: collectionPath,
78
+ path,
79
+ parentCollection: collection
80
+ });
81
+ if (nextSegments.length > 1) {
82
+ const newPath = nextSegments.slice(1).join("/");
83
+ if (!collection) {
84
+ throw Error("collection not found resolving path: " + collection);
85
+ }
86
+ const entityViews = collection.entityViews;
87
+ const customView = entityViews && entityViews
88
+ .map((entry) => resolveEntityView(entry, props.contextEntityViews))
89
+ .filter((v): v is EntityCustomView => v != null)
90
+ .find((entry) => entry.key === newPath);
91
+ const subcollections = getSubcollections(collection);
92
+ if (customView) {
93
+ result.push({
94
+ type: "custom_view",
95
+ slug: collectionPath,
96
+ entityId: entityId,
97
+ path: path + "/" + customView.key,
98
+ view: customView
99
+ });
100
+ } else if (subcollections) {
101
+ result.push(...getNavigationEntriesFromPath({
102
+ path: newPath,
103
+ collections: subcollections,
104
+ currentFullPath: path,
105
+ contextEntityViews: props.contextEntityViews
106
+ }));
107
+ }
108
+ }
109
+ }
110
+ break;
111
+ }
112
+
113
+ }
114
+ return result;
115
+ }
116
+
117
+ function resolveEntityView(entityView: string | EntityCustomView, contextEntityViews?: EntityCustomView[]): EntityCustomView | undefined {
118
+ if (typeof entityView === "string") {
119
+ return contextEntityViews?.find((entry) => entry.key === entityView);
120
+ } else {
121
+ return entityView;
122
+ }
123
+ }
@@ -0,0 +1,171 @@
1
+
2
+
3
+ import { getSubcollections } from "@rebasepro/common";
4
+ import type { AdminCollection } from "@rebasepro/admin-types";
5
+
6
+ export function removeInitialAndTrailingSlashes(s: string): string {
7
+ return removeInitialSlash(removeTrailingSlash(s));
8
+ }
9
+
10
+ export function removeInitialSlash(s: string) {
11
+ if (s.startsWith("/"))
12
+ return s.slice(1);
13
+ else return s;
14
+ }
15
+
16
+ export function removeTrailingSlash(s: string) {
17
+ if (s.endsWith("/"))
18
+ return s.slice(0, -1);
19
+ else return s;
20
+ }
21
+
22
+ export function addInitialSlash(s: string) {
23
+ if (s.startsWith("/"))
24
+ return s;
25
+ else return `/${s}`;
26
+ }
27
+
28
+ export function getLastSegment(path: string) {
29
+ const cleanPath = removeInitialAndTrailingSlashes(path);
30
+ if (cleanPath.includes("/")) {
31
+ const segments = cleanPath.split("/");
32
+ return segments[segments.length - 1];
33
+ }
34
+ return cleanPath;
35
+ }
36
+
37
+ export function resolveCollectionPathIds(path: string, allCollections: AdminCollection[]): string {
38
+ let remainingPath = removeInitialAndTrailingSlashes(path);
39
+ if (!remainingPath) {
40
+ return "";
41
+ }
42
+
43
+ let currentCollections: AdminCollection[] | undefined = allCollections;
44
+ const resolvedPathParts: string[] = [];
45
+
46
+ while (remainingPath.length > 0) {
47
+ if (!currentCollections || currentCollections.length === 0) {
48
+ // We have remaining path segments but no more collections to match against
49
+ console.warn(`resolveCollectionPathIds: Path structure implies subcollections, but none found before segment starting with "${remainingPath}" in original path "${path}". Appending remaining original path.`);
50
+ resolvedPathParts.push(remainingPath);
51
+ remainingPath = ""; // Stop processing
52
+ break;
53
+ }
54
+
55
+ let foundMatch = false;
56
+ // Sort potential matches by length descending to prioritize longer matches (e.g., "a/b" over "a")
57
+ const potentialMatches: { col: AdminCollection; match: string; }[] = currentCollections
58
+ .flatMap(col => [{
59
+ col,
60
+ match: col.slug
61
+ }])
62
+ .filter(p => p.match && remainingPath.startsWith(p.match))
63
+ .sort((a, b) => b.match.length - a.match.length);
64
+
65
+ if (potentialMatches.length > 0) {
66
+ const {
67
+ col: foundCollection,
68
+ match: matchString
69
+ } = potentialMatches[0];
70
+
71
+ resolvedPathParts.push(foundCollection.slug); // Use the defined path
72
+ remainingPath = removeInitialSlash(remainingPath.substring(matchString.length));
73
+
74
+ // Check if we are at the end of the path
75
+ if (remainingPath.length === 0) {
76
+ foundMatch = true;
77
+ break; // Path ends with a collection segment
78
+ }
79
+
80
+ // The next segment must be a entity ID
81
+ const idSeparatorIndex = remainingPath.indexOf("/");
82
+ let entityId: string | number;
83
+ if (idSeparatorIndex > -1) {
84
+ entityId = remainingPath.substring(0, idSeparatorIndex);
85
+ remainingPath = remainingPath.substring(idSeparatorIndex + 1);
86
+ } else {
87
+ // This should not happen if the original path is valid (odd segments)
88
+ // but handle it defensively: assume the rest is the ID
89
+ entityId = remainingPath;
90
+ remainingPath = "";
91
+ console.warn(`resolveCollectionPathIds: Path seems to end with a entity ID "${entityId}" instead of a collection segment in original path "${path}". This might indicate an invalid input path.`);
92
+ // Even if it ends here, we still need to push the ID
93
+ }
94
+
95
+ resolvedPathParts.push(entityId); // Append entity ID
96
+ currentCollections = getSubcollections(foundCollection); // Move to subcollections
97
+ foundMatch = true;
98
+
99
+ if (!currentCollections && remainingPath.length > 0) {
100
+ // Warn if the path continues but no subcollections were defined
101
+ console.warn(`resolveCollectionPathIds: Path continues after entity ID "${entityId}", but no subcollections are defined for the preceding collection "${foundCollection.slug}" in path "${path}". Appending remaining original path.`);
102
+ resolvedPathParts.push(remainingPath); // Append the rest
103
+ remainingPath = ""; // Stop processing
104
+ break;
105
+ }
106
+
107
+ }
108
+
109
+ if (!foundMatch) {
110
+ // Collection definition not found for the start of the remaining path
111
+ console.warn(`resolveCollectionPathIds: Collection definition not found for segment starting with "${remainingPath}" in original path "${path}". Appending remaining original path.`);
112
+ resolvedPathParts.push(remainingPath); // Append the rest
113
+ remainingPath = ""; // Stop processing
114
+ break;
115
+ }
116
+ }
117
+
118
+ return resolvedPathParts.join("/");
119
+ }
120
+
121
+ /**
122
+ * Find the corresponding view at any depth for a given path.
123
+ * Note that path or segments of the paths can be collection aliases.
124
+ * @param slugOrPath
125
+ * @param collections
126
+ */
127
+ export function getCollectionBySlugWithin(slugOrPath: string, collections: AdminCollection[]): AdminCollection | undefined {
128
+
129
+ const subpaths = removeInitialAndTrailingSlashes(slugOrPath).split("/");
130
+ if (subpaths.length % 2 === 0) {
131
+ throw Error(`getCollectionBySlug: Collection paths must have an odd number of segments: ${slugOrPath}`);
132
+ }
133
+
134
+ const subpathCombinations = getCollectionPathsCombinations(subpaths);
135
+ let result: AdminCollection | undefined;
136
+ for (let i = 0; i < subpathCombinations.length; i++) {
137
+ const subpathCombination = subpathCombinations[i];
138
+ const navigationEntry = collections && collections
139
+ .sort((a, b) => (a.slug ?? "").localeCompare(b.slug ?? ""))
140
+ .find((entry) => entry.slug === subpathCombination);
141
+
142
+ if (navigationEntry) {
143
+
144
+ if (subpathCombination === slugOrPath) {
145
+ result = navigationEntry;
146
+ } else if (getSubcollections(navigationEntry).length > 0) {
147
+ const newPath = slugOrPath.replace(subpathCombination, "").split("/").slice(2).join("/");
148
+ if (newPath.length > 0)
149
+ result = getCollectionBySlugWithin(newPath, getSubcollections(navigationEntry));
150
+ }
151
+ }
152
+ if (result) break;
153
+ }
154
+ return result;
155
+ }
156
+
157
+ /**
158
+ * Get the subcollection combinations from a path:
159
+ * "sites/es/locales" => ["sites/es/locales", "sites"]
160
+ * @param subpaths
161
+ */
162
+ export function getCollectionPathsCombinations(subpaths: string[]): string[] {
163
+ const entries = subpaths.length > 0 && subpaths.length % 2 === 0 ? subpaths.splice(0, subpaths.length - 1) : subpaths;
164
+
165
+ const length = entries.length;
166
+ const result: string[] = [];
167
+ for (let i = length; i > 0; i = i - 2) {
168
+ result.push(entries.slice(0, i).join("/"));
169
+ }
170
+ return result;
171
+ }