Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) O3-2724: Fix workspace URL detection #994

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/framework/esm-styleguide/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ module.exports = {
'^@carbon/charts': 'identity-obj-proxy',
'@openmrs/esm-react-utils': '@openmrs/esm-react-utils/mock',
'@openmrs/esm-translations': '@openmrs/esm-translations/mock',
'single-spa': 'single-spa',
'^lodash-es/(.*)$': 'lodash/$1',
dexie: require.resolve('dexie'),
'lodash-es': 'lodash',
dexie: 'dexie',
},
collectCoverageFrom: [
'**/src/**/*.component.tsx',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, ComposedModal, ModalBody, ModalFooter, ModalHeader } from '@carbon/react';
import styles from './workspace-notification.module.scss';
import { navigate } from '@openmrs/esm-navigation';
import { reportError } from '@openmrs/esm-error-handling';
import {
Expand All @@ -14,6 +13,9 @@ import {
resetWorkspaceStore,
useWorkspaces,
} from '../workspaces';
import { escapeRegExp } from 'lodash-es';
import { type SingleSpaCustomEventDetail } from 'single-spa';
import styles from './workspace-notification.module.scss';

export interface WorkspaceNotificationProps {
contextKey: string;
Expand All @@ -26,8 +28,8 @@ export function WorkspaceNotification({ contextKey }: WorkspaceNotificationProps
useEffect(() => {
// When the component initially mounts, check that it has been provided a valid context key.
// I can't think of a reason the component would mount with a valid context key but not matching the URL.
const regex = new RegExp(`\/${contextKey}(\/|$)`);
const isValidContextKey = regex.test(window.location.href);
const regex = new RegExp(`\/${escapeRegExp(contextKey)}(\/|$)`);
const isValidContextKey = regex.test(window.location.pathname);
if (!isValidContextKey) {
reportError(
`WorkspaceOverlay or WorkspaceWindow has provided an invalid context key: "${contextKey}". The context key must be part of the URL path, with no initial or trailing slash.`,
Expand All @@ -45,14 +47,15 @@ export function WorkspaceNotification({ contextKey }: WorkspaceNotificationProps
// If we navigate away from the current context, we need to prompt if there are
// unsaved changes.
useEffect(() => {
const handleRouting = (event) => {
const handleRouting = (event: Event & { detail: SingleSpaCustomEventDetail }) => {
const {
detail: { cancelNavigation, newUrl },
} = event as { detail: { cancelNavigation: () => void; newUrl: string } };
} = event;

// Check if the new URL matches the current context.
const regex = new RegExp(`\/${contextKey}(\/|$)`);
const isSameContextUrl = regex.test(newUrl);
const regex = new RegExp(`\/${escapeRegExp(contextKey)}(\/|$)`);
const url = new URL(newUrl);
const isSameContextUrl = regex.test(url.pathname);
const canCloseAllWorkspaces = getWorkspaceStore()
.getState()
.openWorkspaces.every(({ name }) => {
Expand All @@ -62,12 +65,9 @@ export function WorkspaceNotification({ contextKey }: WorkspaceNotificationProps

if (!isSameContextUrl) {
if (!canCloseAllWorkspaces) {
cancelNavigation();
cancelNavigation?.();
const navigateToNewUrl = () => {
function getUrlWithoutPrefix(url: string) {
return url.split(window['getOpenmrsSpaBase']())?.[1];
}
navigate({ to: `\${openmrsSpaBase}/${getUrlWithoutPrefix(newUrl)}` });
navigate({ to: newUrl });
};

closeAllWorkspaces(navigateToNewUrl);
Expand Down
Loading