@firecms/core 3.0.0-canary.5 → 3.0.0-canary.51

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 (218) hide show
  1. package/README.md +2 -2
  2. package/dist/components/ClearFilterSortButton.d.ts +5 -0
  3. package/dist/components/EntityCollectionTable/EntityCollectionRowActions.d.ts +11 -11
  4. package/dist/components/EntityCollectionTable/EntityCollectionTable.d.ts +2 -2
  5. package/dist/components/EntityCollectionTable/EntityCollectionTableProps.d.ts +5 -3
  6. package/dist/components/EntityCollectionTable/PropertyTableCell.d.ts +3 -2
  7. package/dist/components/EntityCollectionTable/column_utils.d.ts +1 -2
  8. package/dist/components/EntityCollectionTable/internal/CollectionTableToolbar.d.ts +1 -4
  9. package/dist/components/EntityCollectionTable/internal/popup_field/PopupFormField.d.ts +1 -1
  10. package/dist/components/EntityCollectionView/EntityCollectionView.d.ts +12 -3
  11. package/dist/components/EntityCollectionView/EntityCollectionViewStartActions.d.ts +11 -0
  12. package/dist/components/EntityCollectionView/useSelectionController.d.ts +2 -0
  13. package/dist/components/EntityPreview.d.ts +25 -7
  14. package/dist/components/EntityView.d.ts +11 -0
  15. package/dist/components/FieldCaption.d.ts +5 -0
  16. package/dist/components/FireCMSAppBar.d.ts +3 -2
  17. package/dist/components/HomePage/NavigationCard.d.ts +8 -0
  18. package/dist/components/HomePage/{NavigationCollectionCard.d.ts → NavigationCardBinding.d.ts} +2 -2
  19. package/dist/components/HomePage/SmallNavigationCard.d.ts +6 -0
  20. package/dist/components/HomePage/index.d.ts +3 -1
  21. package/dist/components/SelectableTable/SelectableTable.d.ts +1 -1
  22. package/dist/components/VirtualTable/VirtualTableProps.d.ts +1 -1
  23. package/dist/components/common/types.d.ts +4 -6
  24. package/dist/components/common/useDataSourceEntityCollectionTableController.d.ts +3 -0
  25. package/dist/components/index.d.ts +4 -2
  26. package/dist/contexts/AuthControllerContext.d.ts +1 -1
  27. package/dist/core/Drawer.d.ts +5 -12
  28. package/dist/core/DrawerNavigationItem.d.ts +9 -0
  29. package/dist/core/{EntityView.d.ts → EntityEditView.d.ts} +2 -2
  30. package/dist/core/NavigationRoutes.d.ts +1 -1
  31. package/dist/core/Scaffold.d.ts +7 -10
  32. package/dist/core/index.d.ts +3 -4
  33. package/dist/form/EntityForm.d.ts +1 -1
  34. package/dist/form/components/ErrorFocus.d.ts +1 -1
  35. package/dist/form/components/StorageItemPreview.d.ts +3 -2
  36. package/dist/form/components/StorageUploadProgress.d.ts +1 -1
  37. package/dist/form/field_bindings/KeyValueFieldBinding.d.ts +1 -1
  38. package/dist/form/field_bindings/MapFieldBinding.d.ts +1 -1
  39. package/dist/form/field_bindings/StorageUploadFieldBinding.d.ts +4 -3
  40. package/dist/form/field_bindings/TextFieldBinding.d.ts +2 -2
  41. package/dist/form/validation.d.ts +1 -1
  42. package/dist/hooks/data/delete.d.ts +2 -2
  43. package/dist/hooks/data/save.d.ts +2 -3
  44. package/dist/hooks/data/useDataSource.d.ts +2 -2
  45. package/dist/hooks/data/useEntityFetch.d.ts +3 -3
  46. package/dist/hooks/index.d.ts +2 -0
  47. package/dist/hooks/useBuildNavigationController.d.ts +6 -4
  48. package/dist/hooks/useProjectLog.d.ts +6 -2
  49. package/dist/hooks/useStorageSource.d.ts +2 -2
  50. package/dist/hooks/useValidateAuthenticator.d.ts +21 -0
  51. package/dist/index.es.js +9644 -9122
  52. package/dist/index.es.js.map +1 -1
  53. package/dist/index.umd.js +5 -5
  54. package/dist/index.umd.js.map +1 -1
  55. package/dist/internal/useBuildDataSource.d.ts +1 -12
  56. package/dist/preview/PropertyPreview.d.ts +1 -1
  57. package/dist/preview/PropertyPreviewProps.d.ts +1 -4
  58. package/dist/preview/components/BooleanPreview.d.ts +5 -1
  59. package/dist/preview/components/EnumValuesChip.d.ts +1 -1
  60. package/dist/preview/components/ReferencePreview.d.ts +1 -7
  61. package/dist/types/analytics.d.ts +1 -1
  62. package/dist/types/auth.d.ts +37 -1
  63. package/dist/types/collections.d.ts +29 -5
  64. package/dist/types/datasource.d.ts +3 -6
  65. package/dist/types/entities.d.ts +5 -1
  66. package/dist/types/entity_actions.d.ts +14 -0
  67. package/dist/types/entity_callbacks.d.ts +2 -2
  68. package/dist/types/entity_overrides.d.ts +6 -0
  69. package/dist/types/index.d.ts +2 -1
  70. package/dist/types/navigation.d.ts +14 -13
  71. package/dist/types/permissions.d.ts +5 -1
  72. package/dist/types/plugins.d.ts +20 -20
  73. package/dist/types/properties.d.ts +4 -4
  74. package/dist/types/property_config.d.ts +2 -2
  75. package/dist/types/roles.d.ts +31 -0
  76. package/dist/types/storage.d.ts +11 -3
  77. package/dist/types/user.d.ts +5 -0
  78. package/dist/util/collections.d.ts +9 -1
  79. package/dist/util/entities.d.ts +1 -1
  80. package/dist/util/icon_synonyms.d.ts +2 -4
  81. package/dist/util/icons.d.ts +8 -2
  82. package/dist/util/navigation_utils.d.ts +2 -2
  83. package/dist/util/permissions.d.ts +4 -4
  84. package/dist/util/references.d.ts +4 -2
  85. package/dist/util/resolutions.d.ts +9 -13
  86. package/dist/util/useTraceUpdate.d.ts +1 -0
  87. package/package.json +139 -119
  88. package/src/components/ClearFilterSortButton.tsx +41 -0
  89. package/src/components/DeleteEntityDialog.tsx +4 -4
  90. package/src/components/EntityCollectionTable/EntityCollectionRowActions.tsx +2 -2
  91. package/src/components/EntityCollectionTable/EntityCollectionTable.tsx +275 -278
  92. package/src/components/EntityCollectionTable/EntityCollectionTableProps.tsx +9 -5
  93. package/src/components/EntityCollectionTable/PropertyTableCell.tsx +44 -44
  94. package/src/components/EntityCollectionTable/column_utils.tsx +3 -3
  95. package/src/components/EntityCollectionTable/fields/TableReferenceField.tsx +9 -16
  96. package/src/components/EntityCollectionTable/fields/TableStorageUpload.tsx +3 -3
  97. package/src/components/EntityCollectionTable/internal/CollectionTableToolbar.tsx +27 -32
  98. package/src/components/EntityCollectionTable/internal/EntityTableCell.tsx +11 -6
  99. package/src/components/EntityCollectionTable/internal/default_entity_actions.tsx +9 -5
  100. package/src/components/EntityCollectionTable/internal/popup_field/PopupFormField.tsx +2 -4
  101. package/src/components/EntityCollectionView/EntityCollectionView.tsx +560 -554
  102. package/src/components/EntityCollectionView/EntityCollectionViewActions.tsx +5 -6
  103. package/src/components/EntityCollectionView/EntityCollectionViewStartActions.tsx +68 -0
  104. package/src/components/EntityCollectionView/useSelectionController.tsx +30 -0
  105. package/src/components/EntityPreview.tsx +207 -70
  106. package/src/components/EntityView.tsx +84 -0
  107. package/src/components/FieldCaption.tsx +14 -0
  108. package/src/components/FireCMSAppBar.tsx +33 -11
  109. package/src/components/HomePage/DefaultHomePage.tsx +15 -11
  110. package/src/components/HomePage/NavigationCard.tsx +69 -0
  111. package/src/components/HomePage/NavigationCardBinding.tsx +116 -0
  112. package/src/components/HomePage/SmallNavigationCard.tsx +45 -0
  113. package/src/components/HomePage/index.tsx +3 -1
  114. package/src/components/PropertyIdCopyTooltipContent.tsx +2 -3
  115. package/src/components/ReferenceTable/ReferenceSelectionTable.tsx +4 -4
  116. package/src/components/ReferenceWidget.tsx +5 -5
  117. package/src/components/SearchIconsView.tsx +4 -4
  118. package/src/components/SelectableTable/SelectableTable.tsx +1 -1
  119. package/src/components/SelectableTable/filters/BooleanFilterField.tsx +2 -3
  120. package/src/components/SelectableTable/filters/DateTimeFilterField.tsx +23 -8
  121. package/src/components/SelectableTable/filters/ReferenceFilterField.tsx +35 -24
  122. package/src/components/SelectableTable/filters/StringNumberFilterField.tsx +35 -15
  123. package/src/components/VirtualTable/VirtualTable.tsx +28 -20
  124. package/src/components/VirtualTable/VirtualTableProps.tsx +1 -1
  125. package/src/components/VirtualTable/fields/VirtualTableDateField.tsx +1 -1
  126. package/src/components/common/types.tsx +4 -6
  127. package/src/components/common/useDataSourceEntityCollectionTableController.tsx +12 -1
  128. package/src/components/index.tsx +4 -2
  129. package/src/contexts/AuthControllerContext.tsx +1 -1
  130. package/src/core/Drawer.tsx +78 -103
  131. package/src/core/DrawerNavigationItem.tsx +62 -0
  132. package/src/core/{EntityView.tsx → EntityEditView.tsx} +21 -40
  133. package/src/core/EntitySidePanel.tsx +2 -2
  134. package/src/core/FireCMS.tsx +22 -7
  135. package/src/core/NavigationRoutes.tsx +11 -4
  136. package/src/core/Scaffold.tsx +76 -61
  137. package/src/core/field_configs.tsx +1 -2
  138. package/src/core/index.tsx +3 -4
  139. package/src/form/EntityForm.tsx +42 -22
  140. package/src/form/PropertyFieldBinding.tsx +0 -2
  141. package/src/form/components/StorageItemPreview.tsx +5 -3
  142. package/src/form/components/StorageUploadProgress.tsx +7 -6
  143. package/src/form/field_bindings/ArrayOfReferencesFieldBinding.tsx +8 -12
  144. package/src/form/field_bindings/DateTimeFieldBinding.tsx +1 -1
  145. package/src/form/field_bindings/KeyValueFieldBinding.tsx +15 -15
  146. package/src/form/field_bindings/MapFieldBinding.tsx +15 -15
  147. package/src/form/field_bindings/ReadOnlyFieldBinding.tsx +1 -1
  148. package/src/form/field_bindings/ReferenceFieldBinding.tsx +1 -0
  149. package/src/form/field_bindings/StorageUploadFieldBinding.tsx +14 -5
  150. package/src/form/field_bindings/TextFieldBinding.tsx +7 -5
  151. package/src/form/validation.ts +3 -4
  152. package/src/hooks/data/delete.ts +3 -3
  153. package/src/hooks/data/save.ts +2 -2
  154. package/src/hooks/data/useCollectionFetch.tsx +1 -1
  155. package/src/hooks/data/useDataSource.tsx +8 -3
  156. package/src/hooks/data/useEntityFetch.tsx +4 -4
  157. package/src/hooks/index.tsx +3 -0
  158. package/src/hooks/useBuildLocalConfigurationPersistence.tsx +9 -10
  159. package/src/hooks/useBuildModeController.tsx +11 -5
  160. package/src/hooks/useBuildNavigationController.tsx +199 -81
  161. package/src/hooks/useProjectLog.tsx +17 -7
  162. package/src/hooks/useReferenceDialog.tsx +2 -2
  163. package/src/hooks/useStorageSource.tsx +7 -2
  164. package/src/hooks/useValidateAuthenticator.tsx +115 -0
  165. package/src/internal/useBuildDataSource.ts +42 -44
  166. package/src/internal/useBuildSideEntityController.tsx +86 -20
  167. package/src/preview/PropertyPreview.tsx +3 -14
  168. package/src/preview/PropertyPreviewProps.tsx +1 -11
  169. package/src/preview/components/BooleanPreview.tsx +19 -4
  170. package/src/preview/components/EnumValuesChip.tsx +1 -1
  171. package/src/preview/components/ReferencePreview.tsx +55 -147
  172. package/src/preview/property_previews/ArrayOfMapsPreview.tsx +0 -1
  173. package/src/preview/property_previews/ArrayOfReferencesPreview.tsx +0 -1
  174. package/src/preview/property_previews/ArrayOfStorageComponentsPreview.tsx +0 -1
  175. package/src/preview/property_previews/ArrayOfStringsPreview.tsx +0 -1
  176. package/src/preview/property_previews/ArrayOneOfPreview.tsx +0 -1
  177. package/src/preview/property_previews/ArrayPropertyPreview.tsx +0 -1
  178. package/src/preview/property_previews/StringPropertyPreview.tsx +8 -7
  179. package/src/types/analytics.ts +1 -0
  180. package/src/types/auth.tsx +50 -1
  181. package/src/types/collections.ts +33 -5
  182. package/src/types/datasource.ts +8 -5
  183. package/src/types/entities.ts +9 -1
  184. package/src/types/entity_actions.tsx +17 -0
  185. package/src/types/entity_callbacks.ts +2 -2
  186. package/src/types/entity_overrides.tsx +7 -0
  187. package/src/types/firecms.tsx +0 -1
  188. package/src/types/index.ts +2 -1
  189. package/src/types/navigation.ts +17 -16
  190. package/src/types/permissions.ts +6 -1
  191. package/src/types/plugins.tsx +26 -28
  192. package/src/types/properties.ts +8 -6
  193. package/src/types/property_config.tsx +2 -2
  194. package/src/types/roles.ts +41 -0
  195. package/src/types/side_entity_controller.tsx +1 -0
  196. package/src/types/storage.ts +12 -3
  197. package/src/types/user.ts +7 -0
  198. package/src/util/collections.ts +22 -0
  199. package/src/util/entities.ts +1 -1
  200. package/src/util/icon_list.ts +2 -2
  201. package/src/util/icon_synonyms.ts +4 -6
  202. package/src/util/icons.tsx +11 -3
  203. package/src/util/navigation_utils.ts +6 -6
  204. package/src/util/objects.ts +0 -14
  205. package/src/util/permissions.ts +11 -8
  206. package/src/util/references.ts +36 -5
  207. package/src/util/resolutions.ts +6 -24
  208. package/src/util/strings.ts +2 -2
  209. package/src/util/useTraceUpdate.tsx +2 -1
  210. package/dist/core/SideEntityView.d.ts +0 -7
  211. package/dist/internal/useBuildCustomizationController.d.ts +0 -2
  212. package/dist/internal/useLocaleConfig.d.ts +0 -1
  213. package/dist/types/appcheck.d.ts +0 -26
  214. package/src/components/HomePage/NavigationCollectionCard.tsx +0 -146
  215. package/src/core/SideEntityView.tsx +0 -38
  216. package/src/internal/useBuildCustomizationController.tsx +0 -5
  217. package/src/internal/useLocaleConfig.tsx +0 -18
  218. package/src/types/appcheck.ts +0 -29
@@ -12,10 +12,10 @@ import {
12
12
  /**
13
13
  * @group Hooks and utilities
14
14
  */
15
- export type DeleteEntityWithCallbacksProps<M extends Record<string, any>> =
15
+ export type DeleteEntityWithCallbacksProps<M extends Record<string, any>, UserType extends User = User> =
16
16
  DeleteEntityProps<M>
17
17
  & {
18
- callbacks?: EntityCallbacks<M>;
18
+ callbacks?: EntityCallbacks<M, UserType>;
19
19
  onDeleteSuccess?: (entity: Entity<M>) => void;
20
20
  onDeleteFailure?: (entity: Entity<M>, e: Error) => void;
21
21
  onPreDeleteHookError?: (entity: Entity<M>, e: Error) => void;
@@ -62,7 +62,7 @@ export async function deleteEntityWithCallbacks<M extends Record<string, any>, U
62
62
 
63
63
  console.debug("Deleting entity", entity.path, entity.id);
64
64
 
65
- const entityDeleteProps: EntityOnDeleteProps<M, UserType> = {
65
+ const entityDeleteProps: EntityOnDeleteProps<M, any> = {
66
66
  entity,
67
67
  collection,
68
68
  entityId: entity.id,
@@ -17,7 +17,6 @@ import { resolveCollection } from "../../util";
17
17
  export type SaveEntityWithCallbacksProps<M extends Record<string, any>> =
18
18
  SaveEntityProps<M> &
19
19
  {
20
- callbacks?: EntityCallbacks<M>;
21
20
  onSaveSuccess?: (updatedEntity: Entity<M>) => void,
22
21
  onSaveFailure?: (e: Error) => void,
23
22
  onPreSaveHookError?: (e: Error) => void,
@@ -64,7 +63,7 @@ export async function saveEntityWithCallbacks<M extends Record<string, any>, Use
64
63
  onPreSaveHookError,
65
64
  onSaveSuccessHookError
66
65
  }: SaveEntityWithCallbacksProps<M> & {
67
- collection: EntityCollection<M>,
66
+ collection: EntityCollection<M, UserType>,
68
67
  dataSource: DataSource,
69
68
  context: FireCMSContext<UserType>,
70
69
  }
@@ -145,6 +144,7 @@ export async function saveEntityWithCallbacks<M extends Record<string, any>, Use
145
144
  onSaveSuccess(entity);
146
145
  })
147
146
  .catch((e) => {
147
+ console.error("!!!", e);
148
148
  if (callbacks?.onSaveFailure) {
149
149
 
150
150
  const resolvedCollection = resolveCollection<M>({
@@ -70,7 +70,7 @@ export function useCollectionFetch<M extends Record<string, any>, UserType exten
70
70
  searchString
71
71
  }: CollectionFetchProps<M>): CollectionFetchResult<M> {
72
72
 
73
- const dataSource = useDataSource();
73
+ const dataSource = useDataSource(collection);
74
74
  const navigationController = useNavigationController();
75
75
 
76
76
  const path = navigationController.resolveAliasesFrom(inputPath);
@@ -1,9 +1,14 @@
1
- import { useContext } from "react";
2
- import { DataSource } from "../../types";
1
+ import { useContext, useMemo } from "react";
2
+ import { DataSource, EntityCollection } from "../../types";
3
3
  import { DataSourceContext } from "../../contexts/DataSourceContext";
4
4
 
5
5
  /**
6
6
  * Use this hook to get the datasource being used
7
7
  * @group Hooks and utilities
8
8
  */
9
- export const useDataSource = (): DataSource => useContext(DataSourceContext);
9
+ export const useDataSource = (collection?: EntityCollection<any, any>): DataSource => {
10
+ const defaultDataSource = useContext(DataSourceContext);
11
+ if (collection?.overrides?.dataSource)
12
+ return collection?.overrides.dataSource;
13
+ return defaultDataSource;
14
+ };
@@ -7,10 +7,10 @@ import { useFireCMSContext } from "../useFireCMSContext";
7
7
  /**
8
8
  * @group Hooks and utilities
9
9
  */
10
- export interface EntityFetchProps<M extends Record<string, any>> {
10
+ export interface EntityFetchProps<M extends Record<string, any>, UserType extends User = User> {
11
11
  path: string;
12
12
  entityId?: string;
13
- collection: EntityCollection<M>;
13
+ collection: EntityCollection<M, UserType>;
14
14
  useCache?: boolean;
15
15
  }
16
16
 
@@ -41,9 +41,9 @@ export function useEntityFetch<M extends Record<string, any>, UserType extends U
41
41
  entityId,
42
42
  collection,
43
43
  useCache = false
44
- }: EntityFetchProps<M>): EntityFetchResult<M> {
44
+ }: EntityFetchProps<M, UserType>): EntityFetchResult<M> {
45
45
 
46
- const dataSource = useDataSource();
46
+ const dataSource = useDataSource(collection);
47
47
  const navigationController = useNavigationController();
48
48
 
49
49
  const path = navigationController.resolveAliasesFrom(inputPath);
@@ -10,6 +10,7 @@ export * from "./useResolvedNavigationFrom";
10
10
 
11
11
  export * from "./useStorageSource";
12
12
  export * from "./useAuthController";
13
+ export * from "./useDialogsController";
13
14
  export * from "./useSideDialogsController";
14
15
  export * from "./useSideEntityController";
15
16
  export * from "./useFireCMSContext";
@@ -25,3 +26,5 @@ export * from "./useBuildNavigationController";
25
26
 
26
27
  export * from "./useBuildLocalConfigurationPersistence";
27
28
  export * from "./useBuildModeController";
29
+
30
+ export * from "./useValidateAuthenticator";
@@ -1,10 +1,10 @@
1
- import { useCallback, useEffect, useState } from "react";
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
2
  import { PartialEntityCollection, UserConfigurationPersistence } from "../types";
3
3
  import { mergeDeep, stripCollectionPath } from "../util";
4
4
 
5
5
  export function useBuildLocalConfigurationPersistence(): UserConfigurationPersistence {
6
6
 
7
- const [configCache, setConfigCache] = useState<Record<string, PartialEntityCollection>>({});
7
+ const configCache = useRef<Record<string, PartialEntityCollection>>({});
8
8
 
9
9
  const getCollectionFromStorage = useCallback((storageKey: string) => {
10
10
  const item = localStorage.getItem(storageKey);
@@ -13,20 +13,19 @@ export function useBuildLocalConfigurationPersistence(): UserConfigurationPersis
13
13
 
14
14
  const getCollectionConfig = useCallback(<M extends Record<string, any>>(path: string): PartialEntityCollection<M> => {
15
15
  const storageKey = `collection_config::${stripCollectionPath(path)}`;
16
- if (configCache[storageKey]) {
17
- return configCache[storageKey] as PartialEntityCollection<M>;
16
+ if (configCache.current[storageKey]) {
17
+ return configCache.current[storageKey] as PartialEntityCollection<M>;
18
18
  }
19
19
  return getCollectionFromStorage(storageKey);
20
- }, [configCache, getCollectionFromStorage]);
20
+ }, [getCollectionFromStorage]);
21
21
 
22
22
  const onCollectionModified = useCallback(<M extends Record<string, any>>(path: string, data: PartialEntityCollection<M>) => {
23
23
  const storageKey = `collection_config::${stripCollectionPath(path)}`;
24
24
  localStorage.setItem(storageKey, JSON.stringify(data));
25
- setConfigCache((currentCache) => {
26
- const cachedConfig = currentCache[storageKey];
27
- const newConfig = mergeDeep(cachedConfig ?? getCollectionFromStorage(path), data);
28
- return (mergeDeep(currentCache, newConfig));
29
- });
25
+ const currentCache = configCache.current;
26
+ const cachedConfig = currentCache[storageKey];
27
+ const newConfig = mergeDeep(cachedConfig ?? getCollectionFromStorage(path), data);
28
+ configCache.current = mergeDeep(currentCache, newConfig);
30
29
  }, [getCollectionFromStorage]);
31
30
 
32
31
  const [recentlyVisitedPaths, _setRecentlyVisitedPaths] = useState<string[]>([]);
@@ -8,8 +8,13 @@ import { ModeController } from "./index";
8
8
  */
9
9
  export function useBuildModeController(): ModeController {
10
10
 
11
- const prefersDarkModeQuery = typeof window !== "undefined" &&
12
- window.matchMedia("(prefers-color-scheme: dark)");
11
+ const prefersDarkModeQuery = useCallback((): boolean => {
12
+ if (typeof window === "undefined")
13
+ return false;
14
+ const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
15
+ return mediaQueryList.matches;
16
+ }, []);
17
+
13
18
  const prefersDarkModeStorage: boolean | null = localStorage.getItem("prefers-dark-mode") != null ? localStorage.getItem("prefers-dark-mode") === "true" : null;
14
19
  const prefersDarkMode = prefersDarkModeStorage ?? prefersDarkModeQuery;
15
20
  const [mode, setMode] = useState<"light" | "dark">(prefersDarkMode ? "dark" : "light");
@@ -23,7 +28,7 @@ export function useBuildModeController(): ModeController {
23
28
  const setDarkMode = useCallback(() => {
24
29
  setMode("dark");
25
30
  setDocumentMode("dark");
26
- }, [prefersDarkModeQuery]);
31
+ }, []);
27
32
 
28
33
  const setLightMode = useCallback(() => {
29
34
  setMode("light");
@@ -37,14 +42,15 @@ export function useBuildModeController(): ModeController {
37
42
 
38
43
  const toggleMode = useCallback(() => {
39
44
 
45
+ const prefersDarkModeQueryResult = prefersDarkModeQuery();
40
46
  if (mode === "light") {
41
- if (!prefersDarkModeQuery)
47
+ if (!prefersDarkModeQueryResult)
42
48
  localStorage.setItem("prefers-dark-mode", "true");
43
49
  else
44
50
  localStorage.removeItem("prefers-dark-mode");
45
51
  setDarkMode();
46
52
  } else {
47
- if (prefersDarkModeQuery)
53
+ if (prefersDarkModeQueryResult)
48
54
  localStorage.setItem("prefers-dark-mode", "false");
49
55
  else
50
56
  localStorage.removeItem("prefers-dark-mode");
@@ -1,5 +1,4 @@
1
1
  import { useCallback, useEffect, useRef, useState } from "react";
2
- import { useLocation } from "react-router-dom";
3
2
  import equal from "react-fast-compare"
4
3
 
5
4
  import {
@@ -11,12 +10,14 @@ import {
11
10
  EntityCollectionsBuilder,
12
11
  EntityReference,
13
12
  NavigationController,
13
+ PermissionsBuilder,
14
14
  TopNavigationEntry,
15
15
  TopNavigationResult,
16
16
  User,
17
17
  UserConfigurationPersistence
18
18
  } from "../types";
19
19
  import {
20
+ applyPermissionsFunctionIfEmpty,
20
21
  getCollectionByPathOrId,
21
22
  mergeDeep,
22
23
  removeInitialAndTrailingSlashes,
@@ -28,12 +29,15 @@ import { getParentReferencesFromPath } from "../util/parent_references_from_path
28
29
  const DEFAULT_BASE_PATH = "/";
29
30
  const DEFAULT_COLLECTION_PATH = "/c";
30
31
 
31
- type BuildNavigationContextProps<EC extends EntityCollection, UserType extends User> = {
32
+ export type BuildNavigationContextProps<EC extends EntityCollection, UserType extends User> = {
32
33
  basePath?: string,
33
34
  baseCollectionPath?: string,
34
35
  authController: AuthController<UserType>;
35
36
  collections?: EC[] | EntityCollectionsBuilder<EC>;
37
+ collectionPermissions?: PermissionsBuilder;
36
38
  views?: CMSView[] | CMSViewsBuilder;
39
+ adminViews?: CMSView[] | CMSViewsBuilder;
40
+ viewsOrder?: string[];
37
41
  userConfigPersistence?: UserConfigurationPersistence;
38
42
  dataSourceDelegate: DataSourceDelegate;
39
43
  /**
@@ -46,22 +50,25 @@ type BuildNavigationContextProps<EC extends EntityCollection, UserType extends U
46
50
  injectCollections?: (collections: EntityCollection[]) => EntityCollection[];
47
51
  };
48
52
 
49
- export function useBuildNavigationController<EC extends EntityCollection, UserType extends User>({
50
- basePath = DEFAULT_BASE_PATH,
51
- baseCollectionPath = DEFAULT_COLLECTION_PATH,
52
- authController,
53
- collections: collectionsProp,
54
- views: baseViews,
55
- userConfigPersistence,
56
- dataSourceDelegate,
57
- injectCollections
58
- }: BuildNavigationContextProps<EC, UserType>): NavigationController {
59
-
60
- const location = useLocation();
61
-
62
- const collectionsRef = useRef<EntityCollection[] | null>();
63
- const [collections, setCollections] = useState<EntityCollection[] | undefined>();
64
- const [views, setViews] = useState<CMSView[] | undefined>();
53
+ export function useBuildNavigationController<EC extends EntityCollection, UserType extends User>(props: BuildNavigationContextProps<EC, UserType>): NavigationController {
54
+ const {
55
+ basePath = DEFAULT_BASE_PATH,
56
+ baseCollectionPath = DEFAULT_COLLECTION_PATH,
57
+ authController,
58
+ collections: collectionsProp,
59
+ collectionPermissions,
60
+ views: viewsProp,
61
+ adminViews: adminViewsProp,
62
+ viewsOrder,
63
+ userConfigPersistence,
64
+ dataSourceDelegate,
65
+ injectCollections
66
+ } = props;
67
+
68
+ const collectionsRef = useRef<EntityCollection[] | undefined>();
69
+ const viewsRef = useRef<CMSView[] | undefined>();
70
+ const adminViewsRef = useRef<CMSView[] | undefined>();
71
+
65
72
  const [initialised, setInitialised] = useState<boolean>(false);
66
73
 
67
74
  const [topLevelNavigation, setTopLevelNavigation] = useState<TopNavigationResult | undefined>(undefined);
@@ -81,18 +88,18 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
81
88
  const buildUrlCollectionPath = useCallback((path: string): string => `${removeInitialAndTrailingSlashes(baseCollectionPath)}/${encodePath(path)}`,
82
89
  [baseCollectionPath]);
83
90
 
84
- const computeTopNavigation = useCallback((collections: EntityCollection[], views: CMSView[]): TopNavigationResult => {
85
- const navigationEntries: TopNavigationEntry[] = [
91
+ const computeTopNavigation = useCallback((collections: EntityCollection[], views: CMSView[], adminViews: CMSView[], viewsOrder?: string[]): TopNavigationResult => {
92
+ let navigationEntries: TopNavigationEntry[] = [
86
93
  ...(collections ?? []).map(collection => (!collection.hideFromNavigation
87
- ? {
94
+ ? ({
88
95
  url: buildUrlCollectionPath(collection.id ?? collection.path),
89
96
  type: "collection",
90
97
  name: collection.name.trim(),
91
98
  path: collection.id ?? collection.path,
92
99
  collection,
93
100
  description: collection.description?.trim(),
94
- group: collection.group?.trim()
95
- }
101
+ group: getGroup(collection)
102
+ } satisfies TopNavigationEntry)
96
103
  : undefined))
97
104
  .filter(Boolean) as TopNavigationEntry[],
98
105
  ...(views ?? []).map(view =>
@@ -101,18 +108,74 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
101
108
  url: buildCMSUrlPath(Array.isArray(view.path) ? view.path[0] : view.path),
102
109
  name: view.name.trim(),
103
110
  type: "view",
111
+ path: view.path,
112
+ view,
113
+ description: view.description?.trim(),
114
+ group: getGroup(view)
115
+ } satisfies TopNavigationEntry)
116
+ : undefined)
117
+ .filter(Boolean) as TopNavigationEntry[],
118
+ ...(adminViews ?? []).map(view =>
119
+ !view.hideFromNavigation
120
+ ? ({
121
+ url: buildCMSUrlPath(Array.isArray(view.path) ? view.path[0] : view.path),
122
+ name: view.name.trim(),
123
+ type: "admin",
124
+ path: view.path,
104
125
  view,
105
126
  description: view.description?.trim(),
106
- group: view.group?.trim()
107
- })
127
+ group: "Admin"
128
+ } satisfies TopNavigationEntry)
108
129
  : undefined)
109
130
  .filter(Boolean) as TopNavigationEntry[]
110
131
  ];
111
132
 
133
+ // Sort by group, entries with group "Admin" will go last, and second to last will be the group "Views"
134
+ navigationEntries = navigationEntries.sort((a, b) => {
135
+ if (a.group !== "Views" && a.group !== "Admin" && (b.group === "Views" || b.group === "Admin")) {
136
+ return -1;
137
+ }
138
+ if (b.group !== "Views" && b.group !== "Admin" && (a.group === "Views" || a.group === "Admin")) {
139
+ return 1;
140
+ }
141
+ if (a.group === "Admin" && b.group !== "Admin") {
142
+ return 1;
143
+ }
144
+ if (a.group !== "Admin" && b.group === "Admin") {
145
+ return -1;
146
+ }
147
+ if (a.group === "Views" && b.group !== "Views") {
148
+ return -1;
149
+ }
150
+ if (a.group !== "Views" && b.group === "Views") {
151
+ return 1;
152
+ }
153
+ return 0;
154
+
155
+ });
156
+
157
+ if (viewsOrder) {
158
+ navigationEntries = navigationEntries.sort((a, b) => {
159
+ const aIndex = viewsOrder.indexOf(a.path);
160
+ const bIndex = viewsOrder.indexOf(b.path);
161
+ if (aIndex === -1 && bIndex === -1) {
162
+ return 0;
163
+ }
164
+ if (aIndex === -1) {
165
+ return 1;
166
+ }
167
+ if (bIndex === -1) {
168
+ return -1;
169
+ }
170
+ return aIndex - bIndex;
171
+ });
172
+ }
173
+
112
174
  const groups: string[] = Object.values(navigationEntries)
113
175
  .map(e => e.group)
114
176
  .filter(Boolean)
115
177
  .filter((value, index, array) => array.indexOf(value) === index) as string[];
178
+
116
179
  return {
117
180
  navigationEntries,
118
181
  groups
@@ -124,17 +187,34 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
124
187
  if (authController.initialLoading)
125
188
  return;
126
189
 
190
+ console.debug("Refreshing navigation");
191
+
127
192
  try {
128
- const [resolvedCollections = [], resolvedViews = []] = await Promise.all([
129
- resolveCollections(collectionsProp, authController, dataSourceDelegate, injectCollections),
130
- resolveCMSViews(baseViews, authController, dataSourceDelegate)
193
+
194
+ const [resolvedCollections = [], resolvedViews, resolvedAdminViews = []] = await Promise.all([
195
+ resolveCollections(collectionsProp, collectionPermissions, authController, dataSourceDelegate, injectCollections),
196
+ resolveCMSViews(viewsProp, authController, dataSourceDelegate),
197
+ resolveCMSViews(adminViewsProp, authController, dataSourceDelegate)
131
198
  ]
132
199
  );
133
- if (!equal(collectionsRef.current, resolvedCollections) || !equal(views, resolvedViews) || !equal(topLevelNavigation, computeTopNavigation(resolvedCollections, resolvedViews))) {
200
+
201
+ let shouldUpdateTopLevelNav = false;
202
+ if (!areCollectionListsEqual(collectionsRef.current ?? [], resolvedCollections)) {
134
203
  collectionsRef.current = resolvedCollections;
135
- setCollections(resolvedCollections);
136
- setViews(resolvedViews);
137
- setTopLevelNavigation(computeTopNavigation(resolvedCollections ?? [], resolvedViews));
204
+ shouldUpdateTopLevelNav = true;
205
+ }
206
+ if (!equal(viewsRef.current, resolvedViews)) {
207
+ viewsRef.current = resolvedViews;
208
+ shouldUpdateTopLevelNav = true;
209
+ }
210
+ if (!equal(adminViewsRef.current, resolvedAdminViews)) {
211
+ adminViewsRef.current = resolvedAdminViews;
212
+ shouldUpdateTopLevelNav = true;
213
+ }
214
+
215
+ const computedTopLevelNav = computeTopNavigation(resolvedCollections, resolvedViews, resolvedAdminViews, viewsOrder);
216
+ if (shouldUpdateTopLevelNav && !equal(topLevelNavigation, computedTopLevelNav)) {
217
+ setTopLevelNavigation(computedTopLevelNav);
138
218
  }
139
219
  } catch (e) {
140
220
  console.error(e);
@@ -143,25 +223,34 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
143
223
 
144
224
  setNavigationLoading(false);
145
225
  setInitialised(true);
146
- }, [collectionsProp, authController.user, authController.initialLoading, baseViews, computeTopNavigation, injectCollections]);
226
+
227
+ }, [
228
+ collectionsProp,
229
+ collectionPermissions,
230
+ authController.user,
231
+ authController.initialLoading,
232
+ viewsProp,
233
+ adminViewsProp,
234
+ computeTopNavigation,
235
+ injectCollections
236
+ ]);
147
237
 
148
238
  useEffect(() => {
149
239
  refreshNavigation();
150
240
  }, [refreshNavigation]);
151
241
 
152
- const getCollection = useCallback(<EC extends EntityCollection>(
242
+ const getCollection = useCallback((
153
243
  idOrPath: string,
154
244
  entityId?: string,
155
245
  includeUserOverride = false
156
246
  ): EC | undefined => {
157
-
247
+ const collections = collectionsRef.current;
158
248
  if (!collections)
159
249
  return undefined;
160
250
 
161
251
  const baseCollection = getCollectionByPathOrId(removeInitialAndTrailingSlashes(idOrPath), collections);
162
252
 
163
253
  const userOverride = includeUserOverride ? userConfigPersistence?.getCollectionConfig(idOrPath) : undefined;
164
-
165
254
  const overriddenCollection = baseCollection ? mergeDeep(baseCollection, userOverride) : undefined;
166
255
 
167
256
  let result: Partial<EntityCollection> | undefined = overriddenCollection;
@@ -182,16 +271,14 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
182
271
 
183
272
  return { ...overriddenCollection, ...result } as EC;
184
273
 
185
- }, [
186
- basePath,
187
- baseCollectionPath,
188
- collections,
189
- ]);
274
+ }, [userConfigPersistence]);
190
275
 
191
276
  const getCollectionFromPaths = useCallback(<EC extends EntityCollection>(pathSegments: string[]): EC | undefined => {
192
- let currentCollections = collections;
193
- if (!currentCollections)
194
- throw Error("Collections have not been initialised yet");
277
+
278
+ const collections = collectionsRef.current;
279
+ if (collections === undefined)
280
+ throw Error("getCollectionFromPaths: Collections have not been initialised yet");
281
+ let currentCollections: EntityCollection[] | undefined = [...(collections ?? [])];
195
282
 
196
283
  for (let i = 0; i < pathSegments.length; i++) {
197
284
  const pathSegment = pathSegments[i];
@@ -205,12 +292,14 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
205
292
 
206
293
  return undefined;
207
294
 
208
- }, [collections]);
295
+ }, []);
209
296
 
210
297
  const getCollectionFromIds = useCallback(<EC extends EntityCollection>(ids: string[]): EC | undefined => {
211
- let currentCollections = collections;
212
- if (!currentCollections)
213
- throw Error("Collections have not been initialised yet");
298
+
299
+ const collections = collectionsRef.current;
300
+ if (collections === undefined)
301
+ throw Error("getCollectionFromIds: Collections have not been initialised yet");
302
+ let currentCollections: EntityCollection[] | undefined = [...(collections ?? [])];
214
303
 
215
304
  for (let i = 0; i < ids.length; i++) {
216
305
  const id = ids[i];
@@ -224,7 +313,7 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
224
313
 
225
314
  return undefined;
226
315
 
227
- }, [collections]);
316
+ }, []);
228
317
 
229
318
  const isUrlCollectionPath = useCallback(
230
319
  (path: string): boolean => removeInitialAndTrailingSlashes(path + "/").startsWith(removeInitialAndTrailingSlashes(fullCollectionPath) + "/"),
@@ -246,25 +335,17 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
246
335
  []);
247
336
 
248
337
  const resolveAliasesFrom = useCallback((path: string): string => {
249
- if (!collections)
250
- throw Error("Collections have not been initialised yet");
338
+ const collections = collectionsRef.current ?? [];
251
339
  return resolveCollectionPathIds(path, collections);
252
- }, [collections]);
253
-
254
- const state = location.state as any;
255
- /**
256
- * The location can be overridden if `base_location` is set in the
257
- * state field of the current location. This can happen if you open
258
- * a side entity, like `products`, from a different one, like `users`
259
- */
260
- const baseLocation = state && state.base_location ? state.base_location : location;
340
+ }, []);
261
341
 
262
342
  const getAllParentReferencesForPath = useCallback((path: string): EntityReference[] => {
343
+ const collections = collectionsRef.current ?? [];
263
344
  return getParentReferencesFromPath({
264
345
  path,
265
346
  collections
266
347
  });
267
- }, [collections]);
348
+ }, []);
268
349
 
269
350
  const getParentCollectionIds = useCallback((path: string): string[] => {
270
351
 
@@ -283,23 +364,24 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
283
364
  }, [getAllParentReferencesForPath])
284
365
 
285
366
  const convertIdsToPaths = useCallback((ids: string[]): string[] => {
286
- let currentCollections = collections;
287
- const paths: string[] = [];
288
- for (let i = 0; i < ids.length; i++) {
289
- const id = ids[i];
290
- const collection: EntityCollection | undefined = currentCollections!.find(c => c.id === id);
291
- if (!collection)
292
- throw Error(`Collection with id ${id} not found`);
293
- paths.push(collection.path);
294
- currentCollections = collection.subcollections;
295
- }
296
- return paths;
367
+ const collections = collectionsRef.current;
368
+ let currentCollections = collections;
369
+ const paths: string[] = [];
370
+ for (let i = 0; i < ids.length; i++) {
371
+ const id = ids[i];
372
+ const collection: EntityCollection | undefined = currentCollections!.find(c => c.id === id);
373
+ if (!collection)
374
+ throw Error(`Collection with id ${id} not found`);
375
+ paths.push(collection.path);
376
+ currentCollections = collection.subcollections;
297
377
  }
298
- , [getCollectionFromIds]);
378
+ return paths;
379
+ }, [getCollectionFromIds]);
299
380
 
300
381
  return {
301
- collections,
302
- views,
382
+ collections: collectionsRef.current,
383
+ views: viewsRef.current,
384
+ adminViews: adminViewsRef.current,
303
385
  loading: !initialised || navigationLoading,
304
386
  navigationLoadingError,
305
387
  homeUrl,
@@ -316,7 +398,6 @@ export function useBuildNavigationController<EC extends EntityCollection, UserTy
316
398
  buildCMSUrlPath,
317
399
  resolveAliasesFrom,
318
400
  topLevelNavigation,
319
- baseLocation,
320
401
  refreshNavigation,
321
402
  getParentReferencesFromPath: getAllParentReferencesForPath,
322
403
  getParentCollectionIds,
@@ -341,8 +422,8 @@ function filterOutNotAllowedCollections(resolvedCollections: EntityCollection[],
341
422
  return resolvedCollections
342
423
  .filter((c) => {
343
424
  if (!c.permissions) return true;
344
- const resolvedPermissions = resolvePermissions(c, authController, [c.path], null,)
345
- return resolvedPermissions.read !== false;
425
+ const resolvedPermissions = resolvePermissions(c, authController, c.path, null)
426
+ return resolvedPermissions?.read !== false;
346
427
  })
347
428
  .map((c) => {
348
429
  if (!c.subcollections) return c;
@@ -354,9 +435,10 @@ function filterOutNotAllowedCollections(resolvedCollections: EntityCollection[],
354
435
  }
355
436
 
356
437
  async function resolveCollections(collections: undefined | EntityCollection[] | EntityCollectionsBuilder<any>,
438
+ collectionPermissions: PermissionsBuilder | undefined,
357
439
  authController: AuthController,
358
440
  dataSource: DataSourceDelegate,
359
- injectCollections?: (collections: EntityCollection[]) => EntityCollection[]) {
441
+ injectCollections?: (collections: EntityCollection[]) => EntityCollection[]): Promise<EntityCollection[]> {
360
442
  let resolvedCollections: EntityCollection[] = [];
361
443
  if (typeof collections === "function") {
362
444
  resolvedCollections = await collections({
@@ -368,12 +450,14 @@ async function resolveCollections(collections: undefined | EntityCollection[] |
368
450
  resolvedCollections = collections;
369
451
  }
370
452
 
371
- resolvedCollections = filterOutNotAllowedCollections(resolvedCollections, authController);
372
-
373
453
  if (injectCollections) {
374
454
  resolvedCollections = injectCollections(resolvedCollections ?? []);
375
455
  }
376
456
 
457
+ resolvedCollections = applyPermissionsFunctionIfEmpty(resolvedCollections, collectionPermissions);
458
+
459
+ resolvedCollections = filterOutNotAllowedCollections(resolvedCollections, authController);
460
+
377
461
  return resolvedCollections;
378
462
  }
379
463
 
@@ -390,3 +474,37 @@ async function resolveCMSViews(baseViews: CMSView[] | CMSViewsBuilder | undefine
390
474
  }
391
475
  return resolvedViews;
392
476
  }
477
+
478
+ function getGroup(collectionOrView: EntityCollection<any, any> | CMSView) {
479
+ const trimmed = collectionOrView.group?.trim();
480
+ if (!trimmed || trimmed === "") {
481
+ return "Views";
482
+ }
483
+ return trimmed ?? "Views";
484
+ }
485
+
486
+ function areCollectionListsEqual(a: EntityCollection[], b: EntityCollection[]) {
487
+ if (a.length !== b.length) {
488
+ return false;
489
+ }
490
+ const aCopy = [...a];
491
+ const bCopy = [...b];
492
+ const aSorted = aCopy.sort((x, y) => x.id.localeCompare(y.id));
493
+ const bSorted = bCopy.sort((x, y) => x.id.localeCompare(y.id));
494
+ return aSorted.every((value, index) => areCollectionsEqual(value, bSorted[index]));
495
+ }
496
+
497
+ function areCollectionsEqual(a: EntityCollection, b: EntityCollection) {
498
+ const {
499
+ subcollections: subcollectionsA,
500
+ ...restA
501
+ } = a;
502
+ const {
503
+ subcollections: subcollectionsB,
504
+ ...restB
505
+ } = b;
506
+ if (!areCollectionListsEqual(subcollectionsA ?? [], subcollectionsB ?? [])) {
507
+ return false;
508
+ }
509
+ return equal(restA, restB);
510
+ }