@jpmorganchase/mosaic-store 0.1.0-beta.10

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 (101) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +29 -0
  3. package/README.md +9 -0
  4. package/dist/chunk-5L7EI47O.js +0 -0
  5. package/dist/chunk-62CYUXGR.js +1 -0
  6. package/dist/chunk-ASPK4M5R.js +0 -0
  7. package/dist/chunk-BHMVW2ZZ.js +0 -0
  8. package/dist/chunk-BZYGOU7Y.js +0 -0
  9. package/dist/chunk-C6AHZ6P3.js +1 -0
  10. package/dist/chunk-GJU2RNS6.js +1 -0
  11. package/dist/chunk-HUOJOLKA.js +1 -0
  12. package/dist/chunk-IJYHRP3M.js +1 -0
  13. package/dist/chunk-JHTRJDHH.js +1 -0
  14. package/dist/chunk-MBLLQB7A.js +0 -0
  15. package/dist/chunk-N4CAMXVV.js +1 -0
  16. package/dist/chunk-QPSCMC27.js +1 -0
  17. package/dist/chunk-SEY6KBKT.js +1 -0
  18. package/dist/chunk-TESIWQVD.js +1 -0
  19. package/dist/chunk-UGD4MMRB.js +0 -0
  20. package/dist/chunk-UK5NIPTI.js +0 -0
  21. package/dist/chunk-VXYECVIB.js +0 -0
  22. package/dist/chunk-ZJYDN5YH.js +0 -0
  23. package/dist/chunk-ZMWHBFUG.js +1 -0
  24. package/dist/chunk-ZYJGHEAC.js +0 -0
  25. package/dist/index.d.ts +11 -0
  26. package/dist/index.js +1 -0
  27. package/dist/store.d.ts +31 -0
  28. package/dist/store.js +1 -0
  29. package/dist/types/appHeader.d.ts +70 -0
  30. package/dist/types/appHeader.js +1 -0
  31. package/dist/types/breadcrumbs.d.ts +20 -0
  32. package/dist/types/breadcrumbs.js +1 -0
  33. package/dist/types/footer.d.ts +16 -0
  34. package/dist/types/footer.js +1 -0
  35. package/dist/types/helpLinks.d.ts +13 -0
  36. package/dist/types/helpLinks.js +1 -0
  37. package/dist/types/index.d.ts +10 -0
  38. package/dist/types/index.js +1 -0
  39. package/dist/types/layout.d.ts +7 -0
  40. package/dist/types/layout.js +1 -0
  41. package/dist/types/meta.d.ts +12 -0
  42. package/dist/types/meta.js +1 -0
  43. package/dist/types/route.d.ts +7 -0
  44. package/dist/types/route.js +1 -0
  45. package/dist/types/sharedConfig.d.ts +14 -0
  46. package/dist/types/sharedConfig.js +1 -0
  47. package/dist/types/sidebar.d.ts +22 -0
  48. package/dist/types/sidebar.js +1 -0
  49. package/dist/types/tableOfContents.d.ts +18 -0
  50. package/dist/types/tableOfContents.js +1 -0
  51. package/dist/useAppHeader.d.ts +9 -0
  52. package/dist/useAppHeader.js +1 -0
  53. package/dist/useBreadcrumbs.d.ts +5 -0
  54. package/dist/useBreadcrumbs.js +1 -0
  55. package/dist/useFooter.d.ts +4 -0
  56. package/dist/useFooter.js +1 -0
  57. package/dist/useLayout.d.ts +4 -0
  58. package/dist/useLayout.js +1 -0
  59. package/dist/useMeta.d.ts +5 -0
  60. package/dist/useMeta.js +1 -0
  61. package/dist/useNextPrev.d.ts +9 -0
  62. package/dist/useNextPrev.js +1 -0
  63. package/dist/useRoute.d.ts +2 -0
  64. package/dist/useRoute.js +1 -0
  65. package/dist/useSidebar.d.ts +5 -0
  66. package/dist/useSidebar.js +1 -0
  67. package/dist/useTableOfContents.d.ts +3 -0
  68. package/dist/useTableOfContents.js +1 -0
  69. package/package.json +32 -0
  70. package/src/__tests__/store.test.ts +126 -0
  71. package/src/__tests__/test-utils/utils.tsx +26 -0
  72. package/src/__tests__/useAppHeader.test.ts +182 -0
  73. package/src/__tests__/useBreadcrumbs.test.ts +42 -0
  74. package/src/__tests__/useFooter.test.ts +64 -0
  75. package/src/__tests__/useLayout.test.ts +24 -0
  76. package/src/__tests__/useMeta.test.ts +35 -0
  77. package/src/__tests__/useRoute.test.ts +24 -0
  78. package/src/__tests__/useTableOfContents.test.ts +31 -0
  79. package/src/index.ts +11 -0
  80. package/src/store.ts +105 -0
  81. package/src/types/appHeader.ts +76 -0
  82. package/src/types/breadcrumbs.ts +21 -0
  83. package/src/types/footer.ts +17 -0
  84. package/src/types/helpLinks.ts +13 -0
  85. package/src/types/index.ts +10 -0
  86. package/src/types/layout.ts +7 -0
  87. package/src/types/meta.ts +13 -0
  88. package/src/types/route.ts +7 -0
  89. package/src/types/sharedConfig.ts +16 -0
  90. package/src/types/sidebar.ts +22 -0
  91. package/src/types/tableOfContents.ts +19 -0
  92. package/src/useAppHeader.ts +64 -0
  93. package/src/useBreadcrumbs.ts +11 -0
  94. package/src/useFooter.ts +10 -0
  95. package/src/useLayout.ts +9 -0
  96. package/src/useMeta.ts +17 -0
  97. package/src/useNextPrev.ts +44 -0
  98. package/src/useRoute.ts +9 -0
  99. package/src/useSidebar.ts +18 -0
  100. package/src/useTableOfContents.ts +8 -0
  101. package/tsconfig.json +8 -0
@@ -0,0 +1,76 @@
1
+ /**
2
+ * [[`MenuItemType`]] defines the type of App header Menu items
3
+ */
4
+ export enum MenuItemType {
5
+ MENU = 'menu',
6
+ LINK = 'link'
7
+ }
8
+
9
+ /**
10
+ * [[`Menu`]] defines App Header menus
11
+ */
12
+ export type Menu = MenuLinksItem | MenuLinkItem;
13
+
14
+ /**
15
+ * [[`MenuLinksItem`]] defines a menu of links
16
+ */
17
+ export interface MenuLinksItem {
18
+ /** Collection of link options */
19
+ links: MenuLinkItem[];
20
+ /** Title of MenuLinksItem */
21
+ title: string;
22
+ /** Type of MenuLinksItem */
23
+ type: MenuItemType.MENU;
24
+ }
25
+
26
+ /**
27
+ * [[`MenuLinksItem`]] defines a menu of links and includes a type
28
+ */
29
+ export interface AppHeaderMenuLinksItem {
30
+ /** Collection of link options */
31
+ links: MenuLinkItem[];
32
+ /** Title of Tab */
33
+ title: string;
34
+ /** Type of Tab */
35
+ type: MenuItemType.MENU;
36
+ }
37
+
38
+ /**
39
+ * [[`MenuLinkItem`]] define a menu link
40
+ */
41
+ export interface MenuLinkItem {
42
+ /** URL linked by Tab */
43
+ link: string;
44
+ /** Title of Tab */
45
+ title?: string;
46
+ /** Type of MenuLinkItem */
47
+ type: MenuItemType.LINK;
48
+ }
49
+
50
+ /**
51
+ * [[`MenuLinkItem`]] define a menu link and include a type
52
+ */
53
+ export interface AppHeaderMenuLinkItem {
54
+ /** URL linked by Tab */
55
+ link: string;
56
+ /** Title of Tab */
57
+ title?: string;
58
+ /** Type of Tab */
59
+ type: MenuItemType.LINK;
60
+ }
61
+
62
+ /**
63
+ * [[`AppHeaderSlice`]] specifies the contents of the AppHeader
64
+ */
65
+ export type AppHeaderSlice = {
66
+ /** App logo/title */
67
+ title: string;
68
+ /** Link to the home page */
69
+ homeLink: string;
70
+ /** Logo URL */
71
+ logo?: string;
72
+ /** App Header menu */
73
+ menu: Menu[];
74
+ /** Search namespace, suffix used on env variable `process.env.SEARCH_ENDPOINT` to filter search results to a specific namespace. */
75
+ searchNamespace?: string;
76
+ };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * [[`Breadcrumb`]] defines the breadcrumb for a page
3
+ */
4
+ export type Breadcrumb = {
5
+ /** The associated route/path of the breadcrumb */
6
+ path: string;
7
+ /** Whether the breadcrumb is hidden */
8
+ hidden: boolean;
9
+ /** UID */
10
+ id: string;
11
+ /** Breadcrumb label */
12
+ label: string;
13
+ };
14
+
15
+ /**
16
+ * [[`BreadcrumbsSlice`]] is the associated [[`Breadcrumb`]] items
17
+ */
18
+ export type BreadcrumbsSlice = {
19
+ /** Path described in breadcrumbs */
20
+ breadcrumbs: Breadcrumb[];
21
+ };
@@ -0,0 +1,17 @@
1
+ import { HelpLinks } from './helpLinks';
2
+
3
+ /**
4
+ * [[`FooterSlice`]] specifies the footer props
5
+ */
6
+ export type FooterSlice = {
7
+ /** Footer description/message */
8
+ description: string;
9
+ /** Footer title */
10
+ title: string;
11
+ /** Sign-up URL */
12
+ href: string;
13
+ /** Support channels */
14
+ helpLinks: HelpLinks;
15
+ /** Sign-up label */
16
+ label: string;
17
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * [[`HelpLinks`]] specifies the support channels
3
+ */
4
+ export type HelpLinks = {
5
+ /** StackOverflow link label */
6
+ stackoverflowLabel: string;
7
+ /** StackOverflow URL for product support */
8
+ stackoverflowUrl: string;
9
+ /** Symphony link label */
10
+ symphonyLabel: string;
11
+ /** Symphony URL for product support */
12
+ symphonyUrl: string;
13
+ };
@@ -0,0 +1,10 @@
1
+ export * from './appHeader';
2
+ export * from './breadcrumbs';
3
+ export * from './footer';
4
+ export * from './helpLinks';
5
+ export * from './layout';
6
+ export * from './meta';
7
+ export * from './route';
8
+ export * from './sharedConfig';
9
+ export * from './sidebar';
10
+ export * from './tableOfContents';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * [[`LayoutSlice`]] specifies the page's anatomy
3
+ */
4
+ export type LayoutSlice = {
5
+ /** Layout name that will define the page's anatomy */
6
+ layout?: string;
7
+ };
@@ -0,0 +1,13 @@
1
+ import type { Breadcrumb } from './breadcrumbs';
2
+
3
+ /**
4
+ * [[`MetaSlice`]] specifies the page's head metadata
5
+ */
6
+ export type MetaSlice = {
7
+ /** Page description */
8
+ description?: string;
9
+ /** Page breadcrumbs */
10
+ breadcrumbs?: Breadcrumb[];
11
+ /** Page title */
12
+ title?: string;
13
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * [[`RouteSlice`]] specifies the page's route
3
+ */
4
+ export type RouteSlice = {
5
+ /** Current page route */
6
+ route?: string;
7
+ };
@@ -0,0 +1,16 @@
1
+ import type { AppHeaderSlice } from './appHeader';
2
+ import type { FooterSlice } from './footer';
3
+ import type { SidebarSlice } from './sidebar';
4
+
5
+ export type SharedConfig = {
6
+ /** Footer props */
7
+ footer?: FooterSlice;
8
+ /** Header props */
9
+ header?: AppHeaderSlice;
10
+ /** Sidebar props */
11
+ sidebar?: SidebarSlice;
12
+ };
13
+
14
+ export type SharedConfigSlice = {
15
+ sharedConfig?: SharedConfig;
16
+ };
@@ -0,0 +1,22 @@
1
+ export type SidebarItem = {
2
+ /** The id of the item */
3
+ id: string;
4
+ /** The name of the item */
5
+ name: string;
6
+ /** Sidebar data */
7
+ data: {
8
+ /** The link of the item */
9
+ link: string;
10
+ };
11
+ /** Child nodes */
12
+ childNodes: SidebarItem[];
13
+ /** Whether the item is hidden */
14
+ hidden: boolean;
15
+ };
16
+ /**
17
+ * [[`SidebarSlice`]] specifies sidebar content
18
+ */
19
+ export type SidebarSlice = {
20
+ /** Sidebar items */
21
+ sidebarData: SidebarItem[];
22
+ };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * [[`TableOfContentsItem`]] defines a table of contents item
3
+ */
4
+ export type TableOfContentsItem = {
5
+ /** The depth of the heading */
6
+ level: number;
7
+ /** The id attribute of the heading */
8
+ id: string;
9
+ /** The heading text */
10
+ text: string;
11
+ };
12
+
13
+ /**
14
+ * [[`TableOfContentsSlice`]] defines the table of contents for a page
15
+ */
16
+ export type TableOfContentsSlice = {
17
+ /** Path described in breadcrumbs */
18
+ tableOfContents: TableOfContentsItem[];
19
+ };
@@ -0,0 +1,64 @@
1
+ import { useStore } from './store';
2
+ import { MenuItemType } from './types';
3
+ import type {
4
+ AppHeaderSlice,
5
+ AppHeaderMenuLinkItem,
6
+ AppHeaderMenuLinksItem,
7
+ MenuLinkItem,
8
+ MenuLinksItem
9
+ } from './types';
10
+
11
+ function isMenu(menu: MenuLinkItem | MenuLinksItem): menu is MenuLinksItem {
12
+ return (menu as MenuLinksItem).links !== undefined;
13
+ }
14
+ function isMenuLink(menu: MenuLinkItem | MenuLinksItem): menu is MenuLinkItem {
15
+ return (menu as MenuLinkItem).link !== undefined;
16
+ }
17
+
18
+ const parseSearchEndpoint = (searchNamespace: string): string | undefined => {
19
+ if (!searchNamespace) return process.env.SEARCH_ENDPOINT;
20
+ const endpoint = process.env[`SEARCH_ENDPOINT_${searchNamespace}`];
21
+ return endpoint || '';
22
+ };
23
+
24
+ export type AppHeaderMenu = Array<AppHeaderMenuLinksItem | AppHeaderMenuLinkItem>;
25
+
26
+ export interface AppHeader extends Omit<AppHeaderSlice, 'searchNamespace | menu'> {
27
+ HeaderControlsProps?: {
28
+ searchEndpoint: string;
29
+ };
30
+ menu: AppHeaderMenu;
31
+ }
32
+
33
+ export function useAppHeader(): AppHeader | undefined {
34
+ const appHeader = useStore(state => state.sharedConfig?.header);
35
+ if (!appHeader) {
36
+ return undefined;
37
+ }
38
+ let additionalProps = {};
39
+ if (appHeader.searchNamespace) {
40
+ additionalProps = {
41
+ HeaderControlsProps: {
42
+ searchEndpoint: parseSearchEndpoint(appHeader.searchNamespace)
43
+ }
44
+ };
45
+ }
46
+ const typedMenu: AppHeaderMenu = appHeader.menu.reduce<AppHeaderMenu>((result, menu) => {
47
+ if (isMenu(menu)) {
48
+ const linksItem: AppHeaderMenuLinksItem = { ...menu, type: MenuItemType.MENU };
49
+ return [...result, linksItem];
50
+ }
51
+ if (isMenuLink(menu)) {
52
+ const linkItem: AppHeaderMenuLinkItem = { ...menu, type: MenuItemType.LINK };
53
+ return [...result, linkItem];
54
+ }
55
+ console.error('Unknown menu type passed to useAppHeader, ignoring', menu);
56
+ return result;
57
+ }, []);
58
+
59
+ return {
60
+ ...additionalProps,
61
+ ...appHeader,
62
+ menu: typedMenu
63
+ };
64
+ }
@@ -0,0 +1,11 @@
1
+ import { useStore } from './store';
2
+
3
+ export type { Breadcrumb } from './types';
4
+
5
+ export function useBreadcrumbs(minCrumbs = 1) {
6
+ const breadcrumbs = useStore(state => state.breadcrumbs);
7
+ return {
8
+ breadcrumbs: breadcrumbs || [],
9
+ enabled: breadcrumbs?.length > minCrumbs
10
+ };
11
+ }
@@ -0,0 +1,10 @@
1
+ import { useStore } from './store';
2
+ import type { FooterSlice } from './types';
3
+
4
+ type Footer = FooterSlice | undefined;
5
+
6
+ export function useFooter(): Footer {
7
+ const footer = useStore(state => state.sharedConfig?.footer);
8
+
9
+ return footer;
10
+ }
@@ -0,0 +1,9 @@
1
+ import { useStore } from './store';
2
+ import type { LayoutSlice } from './types';
3
+
4
+ type Layout = LayoutSlice;
5
+
6
+ export function useLayout(): Layout {
7
+ const layout = useStore(state => state.layout);
8
+ return { layout };
9
+ }
package/src/useMeta.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { useStore } from './store';
2
+ import type { MetaSlice } from './types';
3
+
4
+ export type Meta = {
5
+ meta: MetaSlice;
6
+ };
7
+
8
+ export function useMeta(): Meta {
9
+ const meta = useStore(state => ({
10
+ description: state.description,
11
+ breadcrumbs: state.breadcrumbs,
12
+ title: state.title
13
+ }));
14
+ return {
15
+ meta
16
+ };
17
+ }
@@ -0,0 +1,44 @@
1
+ import { useStore } from './store';
2
+ import { useRoute } from './useRoute';
3
+ import type { SidebarItem } from './types/sidebar';
4
+
5
+ type NextPrevItem = { link: string; text: string };
6
+
7
+ export function useNextPrev() {
8
+ const sidebarData = useStore(state => state.sidebarData) || [];
9
+ const { route } = useRoute();
10
+ const { next, prev } = findNextPrevForRoute(sidebarData, route || '');
11
+
12
+ return {
13
+ next,
14
+ prev
15
+ };
16
+ }
17
+
18
+ const flatten = (nodes: SidebarItem[]) => {
19
+ let flattenedNodes: NextPrevItem[] = [];
20
+ nodes.forEach(node => {
21
+ flattenedNodes.push({ link: node.data.link, text: node.name });
22
+ if (node.childNodes) {
23
+ flattenedNodes = flattenedNodes.concat(flatten(node.childNodes));
24
+ }
25
+ });
26
+ return flattenedNodes;
27
+ };
28
+
29
+ const nextExists = (index: number, length: number) => index >= 0 && index < length - 1;
30
+
31
+ const prevExists = (index: number, length: number) => index > 0 && index < length;
32
+
33
+ const findNextPrevNodes = (nodes: NextPrevItem[], route: string) => {
34
+ const index = nodes.map(e => e.link).indexOf(route);
35
+ const { length } = nodes;
36
+ const next = nextExists(index, length) ? nodes[index + 1] : null;
37
+ const prev = prevExists(index, length) ? nodes[index - 1] : null;
38
+ return { next, prev };
39
+ };
40
+
41
+ const findNextPrevForRoute = (nodes: SidebarItem[], route: string) => {
42
+ const flattenedNodes = flatten(nodes);
43
+ return findNextPrevNodes(flattenedNodes, route);
44
+ };
@@ -0,0 +1,9 @@
1
+ import { useStore } from './store';
2
+ import type { RouteSlice } from './types';
3
+
4
+ export function useRoute(): RouteSlice {
5
+ const route = useStore(state => state.route);
6
+ return {
7
+ route
8
+ };
9
+ }
@@ -0,0 +1,18 @@
1
+ import { useStore } from './store';
2
+ import { useBreadcrumbs } from './useBreadcrumbs';
3
+ import { useRoute } from './useRoute';
4
+ import type { Breadcrumb } from './types';
5
+
6
+ export function useSidebar() {
7
+ const sidebarData = useStore(state => state.sidebarData) || [];
8
+ const { route } = useRoute();
9
+ const { breadcrumbs } = useBreadcrumbs();
10
+ return {
11
+ sidebarData,
12
+ selectedNodeId: route,
13
+ expandedNodeIds: getIds(breadcrumbs)
14
+ };
15
+ }
16
+ function getIds(breadcrumbs: Breadcrumb[]) {
17
+ return new Set(breadcrumbs.map(({ id }) => id.substr(0, id.lastIndexOf('.'))));
18
+ }
@@ -0,0 +1,8 @@
1
+ import { useStore } from './store';
2
+
3
+ export function useTableOfContents() {
4
+ const tableOfContents = useStore(state => state.tableOfContents) || [];
5
+ return {
6
+ tableOfContents
7
+ };
8
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.bundle.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist"
6
+ },
7
+ "include": ["src"]
8
+ }