Skip to content

Commit

Permalink
(feat) Allow registering workspace groups via routes.json (openmrs#1256)
Browse files Browse the repository at this point in the history
* Registering workspace groups similar to registration of individual workspaces

* Update docs

* Update tests

* Using the workspace name instead of workspace group name

* Allow workspace to define groups it can be a part of

* Review changes
  • Loading branch information
vasharma05 authored and Twiineenock committed Jan 14, 2025
1 parent 16bd7df commit cb30789
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 65 deletions.
84 changes: 81 additions & 3 deletions packages/framework/esm-extensions/src/workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ReactNode } from 'react';
import { type LifeCycles } from 'single-spa';
import { type WorkspaceWindowState } from '@openmrs/esm-globals';
import { type WorkspaceGroupDefinition, type WorkspaceWindowState } from '@openmrs/esm-globals';
import { type ExtensionRegistration, getExtensionRegistration } from '.';
import { createGlobalStore } from '@openmrs/esm-state';
import { translateFrom } from '@openmrs/esm-translations';
Expand All @@ -17,9 +17,13 @@ export interface WorkspaceRegistration {
preferredWindowSize: WorkspaceWindowState;
load: () => Promise<{ default?: LifeCycles } & LifeCycles>;
moduleName: string;
groups?: Array<string>;
groups: Array<string>;
}

export type WorkspaceGroupRegistration = WorkspaceGroupDefinition & {
members: Array<string>;
};

interface WorkspaceRegistrationStore {
workspaces: Record<string, WorkspaceRegistration>;
}
Expand All @@ -28,6 +32,14 @@ const workspaceRegistrationStore = createGlobalStore<WorkspaceRegistrationStore>
workspaces: {},
});

interface WorkspaceGroupRegistrationStore {
workspaceGroups: Record<string, { name: string; members: Array<string> }>;
}

const workspaceGroupStore = createGlobalStore<WorkspaceGroupRegistrationStore>('workspaceGroups', {
workspaceGroups: {},
});

/** See [[WorkspaceDefinition]] for more information about these properties */
export interface RegisterWorkspaceOptions {
name: string;
Expand Down Expand Up @@ -64,6 +76,23 @@ export function registerWorkspace(workspace: RegisterWorkspaceOptions) {
}));
}

/**
* Tells the workspace system about a workspace group. This is used by the app shell
* to register workspace groups defined in the `routes.json` file.
* @internal
*/
export function registerWorkspaceGroup(workspaceGroup: WorkspaceGroupRegistration) {
workspaceGroupStore.setState((state) => ({
workspaceGroups: {
...state.workspaceGroups,
[workspaceGroup.name]: {
name: workspaceGroup.name,
members: workspaceGroup.members,
},
},
}));
}

const workspaceExtensionWarningsIssued = new Set();
/**
* This exists for compatibility with the old way of registering
Expand Down Expand Up @@ -94,14 +123,29 @@ export function getWorkspaceRegistration(name: string): WorkspaceRegistration {
canHide: workspaceExtension.meta?.canHide ?? false,
canMaximize: workspaceExtension.meta?.canMaximize ?? false,
width: workspaceExtension.meta?.width ?? 'narrow',
groups: workspaceExtension?.meta?.groups ?? [],
groups: workspaceExtension.meta?.groups ?? [],
};
} else {
throw new Error(`No workspace named '${name}' has been registered.`);
}
}
}

/**
* This provides the workspace group registration and is also compatibile with the
* old way of registering workspace groups (as extensions), but isn't recommended.
*
* @param name of the workspace
*/
export function getWorkspaceGroupRegistration(name: string): WorkspaceGroupRegistration {
const registeredWorkspaces = workspaceGroupStore.getState().workspaceGroups;
if (registeredWorkspaces[name]) {
return registeredWorkspaces[name];
} else {
throw new Error(`No workspace group named '${name}' has been registered.`);
}
}

function getTitleFromExtension(ext: ExtensionRegistration) {
const title = ext?.meta?.title;
if (typeof title === 'string') {
Expand All @@ -111,3 +155,37 @@ function getTitleFromExtension(ext: ExtensionRegistration) {
}
return ext.name;
}

function createNewWorkspaceGroupInfo(groupName: string): WorkspaceGroupRegistration {
return {
name: groupName,
members: [],
};
}

export function attachWorkspaceToGroup(workspaceName: string, groupName: string) {
workspaceGroupStore.setState((state) => {
const group = state.workspaceGroups[groupName];
if (group) {
return {
workspaceGroups: {
...state.workspaceGroups,
[groupName]: {
...group,
members: [...group.members, workspaceName],
},
},
};
} else {
return {
workspaceGroups: {
...state.workspaceGroups,
[groupName]: {
...createNewWorkspaceGroupInfo(groupName),
members: [workspaceName],
},
},
};
}
});
}
14 changes: 7 additions & 7 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ ___

#### Defined in

[packages/framework/esm-globals/src/types.ts:409](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L409)
[packages/framework/esm-globals/src/types.ts:422](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L422)

___

Expand All @@ -623,7 +623,7 @@ Basically, this is the same as the app routes, with each routes definition keyed

#### Defined in

[packages/framework/esm-globals/src/types.ts:400](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L400)
[packages/framework/esm-globals/src/types.ts:413](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L413)

___

Expand Down Expand Up @@ -8231,7 +8231,7 @@ Function to close an opened workspace

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:425](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L425)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:433](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L433)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:289](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L289)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:297](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L297)

___

Expand Down Expand Up @@ -8313,7 +8313,7 @@ launchWorkspaceGroup("myGroup", {

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:205](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L205)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:211](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L211)

___

Expand All @@ -8339,7 +8339,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:382](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L382)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:390](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L390)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:536](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L536)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:544](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L544)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Defaults to true except when opening a new workspace of the same group.

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:33](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L33)
[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 @@ -42,7 +42,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:23](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L23)

## Workspace Methods

Expand All @@ -62,4 +62,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:30](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L30)
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:45](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L45)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:50](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L50)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:55](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L55)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:60](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L60)

___

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:50](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L50)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:55](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L55)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:70](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L70)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:75](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L75)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ An explanation of what the flag does, which will be displayed in the Implementer

#### Defined in

[packages/framework/esm-globals/src/types.ts:363](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L363)
[packages/framework/esm-globals/src/types.ts:374](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L374)

___

Expand All @@ -34,7 +34,7 @@ A code-friendly name for the flag, which will be used to reference it in code

#### Defined in

[packages/framework/esm-globals/src/types.ts:359](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L359)
[packages/framework/esm-globals/src/types.ts:370](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L370)

___

Expand All @@ -46,4 +46,4 @@ A human-friendly name which will be displayed in the Implementer Tools

#### Defined in

[packages/framework/esm-globals/src/types.ts:361](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L361)
[packages/framework/esm-globals/src/types.ts:372](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-globals/src/types.ts#L372)
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ___

### groups

`Optional` **groups**: `string`[]
**groups**: `string`[]

#### Inherited from

Expand Down Expand Up @@ -188,7 +188,7 @@ ___

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:109](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L109)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:115](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L115)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:110](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L110)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:116](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L116)

## Methods

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:45](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L45)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:50](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L50)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:55](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L55)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:60](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L60)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:50](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L50)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:55](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L55)

___

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

#### Defined in

[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:70](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L70)
[packages/framework/esm-styleguide/src/workspaces/workspaces.ts:75](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-styleguide/src/workspaces/workspaces.ts#L75)
Loading

0 comments on commit cb30789

Please sign in to comment.