Skip to content

Commit

Permalink
(feat) O3-3244 & O3-3250: Allow data sharing between workspaces with …
Browse files Browse the repository at this point in the history
…same sidebar family name (#1094)
  • Loading branch information
vasharma05 authored Jul 30, 2024
1 parent 4d3f1b0 commit 9811845
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 26 deletions.
8 changes: 4 additions & 4 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -7031,7 +7031,7 @@ Function to close an opened workspace

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:282](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L282)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:288](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L288)

___

Expand Down Expand Up @@ -7082,7 +7082,7 @@ prop named `workspaceTitle` will override the title of the workspace.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:165](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L165)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:166](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L166)

___

Expand All @@ -7108,7 +7108,7 @@ Use this function to navigate to a new page and launch a workspace on that page.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:245](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L245)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:251](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L251)

___

Expand All @@ -7122,4 +7122,4 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:384](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L384)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:402](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L402)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ even if the `testFcn` passed to `promptBeforeClosing` returns `true`.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:18](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L18)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:19](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L19)

## Workspace Methods

Expand All @@ -45,4 +45,4 @@ void

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:25](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L25)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:26](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L26)
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ closed, given the user forcefully closes the workspace.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:37](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L37)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:38](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L38)

___

Expand All @@ -66,7 +66,7 @@ will directly close the workspace without any prompt

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:47](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L47)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:48](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L48)

___

Expand All @@ -89,7 +89,7 @@ this workspace is closed; e.g. if there is unsaved data.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:42](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L42)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:43](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L43)

___

Expand Down Expand Up @@ -117,4 +117,4 @@ title needs to be set dynamically.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:62](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L62)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:63](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L63)
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:97](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L97)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:98](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L98)

## Methods

Expand Down Expand Up @@ -232,7 +232,7 @@ closed, given the user forcefully closes the workspace.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:37](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L37)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:38](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L38)

___

Expand All @@ -259,7 +259,7 @@ will directly close the workspace without any prompt

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:47](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L47)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:48](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L48)

___

Expand Down Expand Up @@ -304,7 +304,7 @@ this workspace is closed; e.g. if there is unsaved data.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:42](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L42)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:43](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L43)

___

Expand Down Expand Up @@ -336,4 +336,4 @@ title needs to be set dynamically.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:62](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L62)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:63](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L63)
10 changes: 5 additions & 5 deletions packages/framework/esm-framework/docs/interfaces/Prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:81](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L81)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:82](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L82)

___

Expand All @@ -35,7 +35,7 @@ Defaults to "Cancel"

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:86](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L86)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:87](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L87)

___

Expand All @@ -47,7 +47,7 @@ Defaults to "Confirm"

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:83](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L83)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:84](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L84)

___

Expand All @@ -57,7 +57,7 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:80](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L80)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:81](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L81)

## Methods

Expand All @@ -71,4 +71,4 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:84](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L84)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:85](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L85)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:378](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L378)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:396](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L396)

___

Expand All @@ -29,7 +29,7 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:379](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L379)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:397](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L397)

___

Expand All @@ -39,7 +39,7 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:380](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L380)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:398](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L398)

___

Expand All @@ -49,4 +49,4 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:381](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L381)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:399](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L399)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InlineLoading } from '@carbon/react';
import { getCoreTranslation } from '@openmrs/esm-translations';
import styles from './workspace.module.scss';
import { type OpenWorkspace } from '../workspaces';
import { useWorkspaceFamilyStore } from '../workspace-sidebar-store/useWorkspaceFamilyStore';

interface WorkspaceRendererProps {
workspace: OpenWorkspace;
Expand All @@ -13,6 +14,7 @@ interface WorkspaceRendererProps {

export function WorkspaceRenderer({ workspace, additionalPropsFromPage }: WorkspaceRendererProps) {
const [lifecycle, setLifecycle] = useState<ParcelConfig | undefined>();
const workspaceFamilyState = useWorkspaceFamilyStore(workspace.sidebarFamily);

useEffect(() => {
let active = true;
Expand All @@ -34,6 +36,7 @@ export function WorkspaceRenderer({ workspace, additionalPropsFromPage }: Worksp
promptBeforeClosing: workspace.promptBeforeClosing,
setTitle: workspace.setTitle,
...additionalPropsFromPage,
...workspaceFamilyState,
...workspace.additionalProps,
},
[workspace, additionalPropsFromPage],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useEffect, useMemo, useState } from 'react';
import { getWorkspaceFamilyStore } from '../workspaces';
import type { StoreApi } from 'zustand/vanilla';

/**
* This hook is used to interact with the store of a workspace family.
* A workspace family is defined as a group of workspaces that share the same sidebarFamilyName.
*
* In case a workspace doesn't have a sidebarFamilyName, it will be considered as a standalone workspace, and hence this hook will return an empty object and updateFunction as an empty function.
*
* @internal
*
* @param {string} sidebarFamilyName The sidebarFamilyName of the workspace used when registering the workspace in the module's routes.json file.
*/
export function useWorkspaceFamilyStore(sidebarFamilyName: string) {
const [storeState, setStoreState] = useState<object>({});
const [currentSidebarFamilyName, setCurrentSidebarFamilyName] = useState(sidebarFamilyName);

useEffect(() => {
if (currentSidebarFamilyName !== sidebarFamilyName) {
setCurrentSidebarFamilyName(sidebarFamilyName);
}
}, [sidebarFamilyName]);

useEffect(() => {
const store = getWorkspaceFamilyStore(currentSidebarFamilyName);
let unsubscribe: () => void;
if (store) {
setStoreState(store.getState());
unsubscribe = store.subscribe(setStoreState);
}
return () => {
if (store) {
unsubscribe?.();
}
};
}, [currentSidebarFamilyName]);

return storeState;
}
Loading

0 comments on commit 9811845

Please sign in to comment.