Skip to content

Commit

Permalink
refactor usePrimaryEntityLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
tcaiger committed Aug 21, 2024
1 parent bf0f5d2 commit 966dafa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
3 changes: 1 addition & 2 deletions packages/datatrak-web/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

export { errorToast, successToast } from './toast';
export { useIsMobileMediaQuery as useIsMobile } from './useIsMobileMediaQuery';
export { useFromLocation } from './useFromLocation';
export * from './date';
export * from './detectDevice';
export { gaEvent } from './ga';
Expand All @@ -14,4 +13,4 @@ export {
getTaskFilterSetting,
removeTaskFilterSetting,
} from './taskFilterSettings';
export { usePrimaryEntityLocation } from './usePrimaryEntityLocation';
export { useFromLocation, usePrimaryEntityLocation } from './useLocationState';
18 changes: 0 additions & 18 deletions packages/datatrak-web/src/utils/useFromLocation.ts

This file was deleted.

25 changes: 25 additions & 0 deletions packages/datatrak-web/src/utils/useLocationState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/
import { useLocation } from 'react-router-dom';

function hasProperty<T extends string>(state: unknown, property: T): state is Record<T, string> {
if (state !== null && typeof state === 'object' && property in state) {
return typeof (state as Record<T, unknown>)[property] === 'string';
}
return false;
}

function useLocationState(property: 'primaryEntityCode' | 'from'): string | undefined {
const location = useLocation();
return hasProperty(location.state, property) ? location.state[property] : undefined;
}

export function usePrimaryEntityLocation() {
return useLocationState('primaryEntityCode');
}

export function useFromLocation() {
return useLocationState('from');
}
18 changes: 0 additions & 18 deletions packages/datatrak-web/src/utils/usePrimaryEntityLocation.ts

This file was deleted.

0 comments on commit 966dafa

Please sign in to comment.