@vtex/faststore-plugin-buyer-portal 2.0.49 → 2.0.50
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.
- package/package.json +1 -1
- package/src/features/accounting-fields/hooks/useListAccountingFields.ts +2 -1
- package/src/features/addresses/components/AddressLine/AddressLine.tsx +3 -6
- package/src/features/contracts/components/ContractListItem/ContractListItem.tsx +0 -5
- package/src/features/contracts/components/index.ts +0 -2
- package/src/features/contracts/hooks/useSwitchContract.ts +13 -5
- package/src/features/contracts/layouts/ContractListingLayout/contract-listing-layout.scss +0 -1
- package/src/features/org-units/components/CreateOrgUnitDrawer/CreateOrgUnitDrawer.tsx +4 -2
- package/src/features/org-units/components/DeleteOrgUnitDrawer/DeleteOrgUnitDrawer.tsx +6 -2
- package/src/features/org-units/components/OrgUnitBreadcrumb/OrgUnitBreadcrumbLink.tsx +3 -4
- package/src/features/org-units/components/OrgUnitDetailsNavbar/OrgUnitDetailsNavbar.tsx +82 -3
- package/src/features/org-units/components/OrgUnitHeaderNav/OrgUnitHeaderNav.tsx +13 -2
- package/src/features/org-units/components/OrgUnitsHierarchyTree/OrgUnitsHierarchyTree.tsx +18 -10
- package/src/features/org-units/components/UpdateOrgUnitDrawer/UpdateOrgUnitDrawer.tsx +4 -2
- package/src/features/org-units/hooks/useOrgUnitByUser.ts +2 -0
- package/src/features/shared/components/BasicCard/BasicCard.tsx +6 -3
- package/src/features/shared/components/BasicCard/BasicCardLine.tsx +3 -5
- package/src/features/shared/components/BuyerPortalProvider/BuyerPortalProvider.tsx +2 -0
- package/src/features/shared/components/HeaderInside/HeaderInside.tsx +2 -5
- package/src/features/shared/components/InternalTopbar/InternalTopbar.tsx +3 -4
- package/src/features/shared/components/PageLoadingIndicator/PageLoadingIndicator.tsx +129 -0
- package/src/features/shared/components/PageLoadingIndicator/page-loading-indicator.scss +49 -0
- package/src/features/shared/components/Table/TableRow/TableRow.tsx +2 -5
- package/src/features/shared/components/VerticalNav/VerticalNavLink.tsx +11 -10
- package/src/features/shared/hooks/index.ts +2 -0
- package/src/features/shared/hooks/useDisplayedOrgUnitName.ts +12 -0
- package/src/features/shared/hooks/useDrawerProps.ts +17 -4
- package/src/features/shared/hooks/usePageItems.ts +7 -1
- package/src/features/shared/hooks/usePageLoading.ts +11 -0
- package/src/features/shared/hooks/useQuery.ts +62 -20
- package/src/features/shared/layouts/GlobalLayout/global-layout.scss +1 -0
- package/src/features/shared/layouts/LoadingTabsLayout/LoadingTabsLayout.tsx +5 -2
- package/src/features/shared/layouts/SumaPageLayout/SumaSidebarDrawer.tsx +20 -2
- package/src/features/shared/layouts/SumaPageLayout/suma-sidebar-drawer.scss +14 -0
- package/src/features/shared/utils/__tests__/getRouteKey.test.ts +29 -0
- package/src/features/shared/utils/constants.ts +1 -1
- package/src/features/shared/utils/getRouteKey.ts +4 -0
- package/src/features/shared/utils/pageLoading/__tests__/keptSidebarDrawer.test.ts +170 -0
- package/src/features/shared/utils/pageLoading/__tests__/navigationLevel.test.ts +107 -0
- package/src/features/shared/utils/pageLoading/__tests__/orgUnitNames.test.ts +60 -0
- package/src/features/shared/utils/pageLoading/__tests__/pageLoadingStore.test.ts +266 -0
- package/src/features/shared/utils/pageLoading/keptSidebarDrawer.ts +133 -0
- package/src/features/shared/utils/pageLoading/navigationLevel.ts +89 -0
- package/src/features/shared/utils/pageLoading/orgUnitNames.ts +49 -0
- package/src/features/shared/utils/pageLoading/pageLoadingStore.ts +277 -0
- package/src/features/shared/utils/withProviders.tsx +5 -1
- package/src/features/shared/utils/withRouteKey.ts +23 -0
- package/src/features/contracts/components/ContractSwitchOverlay/ContractSwitchOverlay.tsx +0 -47
- package/src/features/contracts/components/ContractSwitchOverlay/contract-switch-overlay.scss +0 -7
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { getRouteKey } from "../getRouteKey";
|
|
2
|
+
|
|
3
|
+
import { removePathPrefix } from "./navigationLevel";
|
|
4
|
+
|
|
5
|
+
import type { PageLoadingState } from "./pageLoadingStore";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The header mounts again on every page, so a sidebar drawer the buyer opened
|
|
9
|
+
* while a page was loading would close when that page shows. The drawer is
|
|
10
|
+
* kept from when the buyer opens it until they close it or a navigation
|
|
11
|
+
* starts (a link, the browser's back button), which closes it.
|
|
12
|
+
*/
|
|
13
|
+
let isKept = false;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A full load (e.g. opening a contract) replaces the document and the module
|
|
17
|
+
* state with it, so a drawer opened during one is also kept in the session,
|
|
18
|
+
* for the next document only.
|
|
19
|
+
*/
|
|
20
|
+
export const KEPT_SIDEBAR_DRAWER_STORAGE_KEY = "fs-bp-kept-sidebar-drawer";
|
|
21
|
+
|
|
22
|
+
/** A kept drawer older than this isn't restored. */
|
|
23
|
+
export const KEPT_SIDEBAR_DRAWER_MAX_AGE = 60_000;
|
|
24
|
+
|
|
25
|
+
type KeptAcrossLoad = { path: string; keptAt: number };
|
|
26
|
+
|
|
27
|
+
type NavigationState = Pick<
|
|
28
|
+
PageLoadingState,
|
|
29
|
+
"phase" | "targetPath" | "fullLoad"
|
|
30
|
+
>;
|
|
31
|
+
|
|
32
|
+
const getStorage = () => {
|
|
33
|
+
try {
|
|
34
|
+
return typeof window === "undefined" ? null : window.sessionStorage;
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Forgets the drawer kept for the next document, keeping it on this one. */
|
|
41
|
+
export const forgetSidebarDrawerKeptAcrossLoad = () => {
|
|
42
|
+
try {
|
|
43
|
+
getStorage()?.removeItem(KEPT_SIDEBAR_DRAWER_STORAGE_KEY);
|
|
44
|
+
} catch {
|
|
45
|
+
// Without storage, the drawer just isn't restored.
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Keeps the drawer the buyer opened. During a full load, it's also kept for
|
|
51
|
+
* the page that load opens.
|
|
52
|
+
*/
|
|
53
|
+
export const keepSidebarDrawer = (
|
|
54
|
+
navigation?: NavigationState,
|
|
55
|
+
now = Date.now()
|
|
56
|
+
) => {
|
|
57
|
+
isKept = true;
|
|
58
|
+
|
|
59
|
+
if (
|
|
60
|
+
navigation?.phase !== "navigating" ||
|
|
61
|
+
!navigation.fullLoad ||
|
|
62
|
+
!navigation.targetPath
|
|
63
|
+
) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const kept: KeptAcrossLoad = {
|
|
68
|
+
path: getRouteKey(removePathPrefix(navigation.targetPath)),
|
|
69
|
+
keptAt: now,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
getStorage()?.setItem(
|
|
74
|
+
KEPT_SIDEBAR_DRAWER_STORAGE_KEY,
|
|
75
|
+
JSON.stringify(kept)
|
|
76
|
+
);
|
|
77
|
+
} catch {
|
|
78
|
+
// Without storage, the drawer just isn't restored.
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const releaseSidebarDrawer = () => {
|
|
83
|
+
isKept = false;
|
|
84
|
+
forgetSidebarDrawerKeptAcrossLoad();
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const isSidebarDrawerKept = () => isKept;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Whether the full load that opened `currentPath` kept the drawer. It's read
|
|
91
|
+
* once: reloading the page doesn't restore it.
|
|
92
|
+
*/
|
|
93
|
+
export const takeSidebarDrawerKeptAcrossLoad = (
|
|
94
|
+
currentPath: string,
|
|
95
|
+
now = Date.now()
|
|
96
|
+
) => {
|
|
97
|
+
let kept: Partial<KeptAcrossLoad> | null = null;
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
const value = getStorage()?.getItem(KEPT_SIDEBAR_DRAWER_STORAGE_KEY);
|
|
101
|
+
|
|
102
|
+
kept = value ? JSON.parse(value) : null;
|
|
103
|
+
} catch {
|
|
104
|
+
kept = null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
forgetSidebarDrawerKeptAcrossLoad();
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
typeof kept?.keptAt === "number" &&
|
|
111
|
+
now - kept.keptAt >= 0 &&
|
|
112
|
+
now - kept.keptAt <= KEPT_SIDEBAR_DRAWER_MAX_AGE &&
|
|
113
|
+
kept.path === getRouteKey(removePathPrefix(currentPath))
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** Whether the store went from `previous` to a new navigation in `next`. */
|
|
118
|
+
export const isNavigationStart = (
|
|
119
|
+
previous: Pick<NavigationState, "phase" | "targetPath">,
|
|
120
|
+
next: Pick<NavigationState, "phase" | "targetPath">
|
|
121
|
+
) =>
|
|
122
|
+
next.phase === "navigating" &&
|
|
123
|
+
(previous.phase !== "navigating" || previous.targetPath !== next.targetPath);
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Whether a full load ended without leaving the page (it failed, or the page
|
|
127
|
+
* came back from the back-forward cache), so its kept drawer is forgotten.
|
|
128
|
+
*/
|
|
129
|
+
export const isFullLoadEnd = (
|
|
130
|
+
previous: NavigationState,
|
|
131
|
+
next: Pick<NavigationState, "phase">
|
|
132
|
+
) =>
|
|
133
|
+
previous.phase === "navigating" && previous.fullLoad && next.phase === "idle";
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { getRouteKey } from "../getRouteKey";
|
|
2
|
+
|
|
3
|
+
const BASE_SEGMENT = "organization-account";
|
|
4
|
+
const BASE_PATH = `/pvt/${BASE_SEGMENT}`;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Pages of the contract menu (level 1). Every other page belongs to the unit
|
|
8
|
+
* menu (level 0).
|
|
9
|
+
*/
|
|
10
|
+
const CONTRACT_LEVEL_PAGES = new Set([
|
|
11
|
+
"profile",
|
|
12
|
+
"addresses",
|
|
13
|
+
"address",
|
|
14
|
+
"recipients",
|
|
15
|
+
"payment-groups",
|
|
16
|
+
"credit-cards",
|
|
17
|
+
"product-assortment",
|
|
18
|
+
"accounting-field",
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
export type NavigationLevel = 0 | 1;
|
|
22
|
+
|
|
23
|
+
export type NavigationLocation = {
|
|
24
|
+
level: NavigationLevel;
|
|
25
|
+
page: string;
|
|
26
|
+
orgUnitId?: string;
|
|
27
|
+
contractId?: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The area hidden while a page loads: the content column when the sidebar
|
|
32
|
+
* stays the same, everything below the header otherwise.
|
|
33
|
+
*/
|
|
34
|
+
export type LoadingRegion = "content" | "body";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The path as the router's `asPath` has it. The URL of `routeChangeStart`
|
|
38
|
+
* also has the store's base path and locale prefix (e.g. `/it-IT`).
|
|
39
|
+
*/
|
|
40
|
+
export const removePathPrefix = (path: string) => {
|
|
41
|
+
const index = path.indexOf(BASE_PATH);
|
|
42
|
+
const queryIndex = path.search(/[?#]/);
|
|
43
|
+
const isInPathname = queryIndex === -1 || index < queryIndex;
|
|
44
|
+
|
|
45
|
+
return index > 0 && isInPathname ? path.slice(index) : path;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const getNavigationLocation = (
|
|
49
|
+
path: string
|
|
50
|
+
): NavigationLocation | null => {
|
|
51
|
+
const segments = getRouteKey(path).split("/").filter(Boolean);
|
|
52
|
+
const baseIndex = segments.indexOf(BASE_SEGMENT);
|
|
53
|
+
|
|
54
|
+
if (baseIndex === -1) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const [page = "", orgUnitId, contractId] = segments.slice(baseIndex + 1);
|
|
59
|
+
const isContractLevel = CONTRACT_LEVEL_PAGES.has(page);
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
level: isContractLevel ? 1 : 0,
|
|
63
|
+
page,
|
|
64
|
+
orgUnitId,
|
|
65
|
+
contractId: isContractLevel ? contractId : undefined,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const getLoadingRegion = (
|
|
70
|
+
fromPath: string,
|
|
71
|
+
toPath: string
|
|
72
|
+
): LoadingRegion => {
|
|
73
|
+
const from = getNavigationLocation(fromPath);
|
|
74
|
+
const to = getNavigationLocation(toPath);
|
|
75
|
+
|
|
76
|
+
if (!from || !to) {
|
|
77
|
+
return "body";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (from.level !== to.level || from.orgUnitId !== to.orgUnitId) {
|
|
81
|
+
return "body";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (from.level === 1 && from.contractId !== to.contractId) {
|
|
85
|
+
return "body";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return "content";
|
|
89
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getNavigationLocation } from "./navigationLevel";
|
|
2
|
+
|
|
3
|
+
import type { PageLoadingState } from "./pageLoadingStore";
|
|
4
|
+
|
|
5
|
+
type OrgUnitName = { id: string; name: string };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Names of the units the buyer can navigate to from the current page, so the
|
|
9
|
+
* header shows the next unit's name as soon as its navigation starts, before
|
|
10
|
+
* the next page's loader returns it.
|
|
11
|
+
*/
|
|
12
|
+
const names = new Map<string, string>();
|
|
13
|
+
|
|
14
|
+
export const rememberOrgUnitNames = (units: OrgUnitName[]) => {
|
|
15
|
+
units.forEach(({ id, name }) => {
|
|
16
|
+
if (id && name) names.set(id, name);
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** Remembers every unit of a unit's path (`ids` starts with a "/"). */
|
|
21
|
+
export const rememberOrgUnitPath = (path?: { ids: string; names: string }) => {
|
|
22
|
+
if (!path) return;
|
|
23
|
+
|
|
24
|
+
const ids = path.ids.split("/").slice(1);
|
|
25
|
+
const pathNames = path.names.split("/");
|
|
26
|
+
|
|
27
|
+
rememberOrgUnitNames(
|
|
28
|
+
ids.map((id, index) => ({ id, name: pathNames[index] }))
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const forgetOrgUnitNames = () => names.clear();
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The unit name to show: the destination unit's while navigating to another
|
|
36
|
+
* unit whose name is known, the current unit's otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export const getDisplayedOrgUnitName = (
|
|
39
|
+
orgUnit: OrgUnitName,
|
|
40
|
+
{ phase, targetPath }: Pick<PageLoadingState, "phase" | "targetPath">
|
|
41
|
+
) => {
|
|
42
|
+
if (phase !== "navigating" || !targetPath) return orgUnit.name;
|
|
43
|
+
|
|
44
|
+
const targetId = getNavigationLocation(targetPath)?.orgUnitId;
|
|
45
|
+
|
|
46
|
+
if (!targetId || targetId === orgUnit.id) return orgUnit.name;
|
|
47
|
+
|
|
48
|
+
return names.get(targetId) ?? orgUnit.name;
|
|
49
|
+
};
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { getRouteKey } from "../getRouteKey";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getLoadingRegion,
|
|
5
|
+
removePathPrefix,
|
|
6
|
+
type LoadingRegion,
|
|
7
|
+
} from "./navigationLevel";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* - `navigating`: the next page's loader is running; the current page stays
|
|
11
|
+
* mounted with its loading region hidden.
|
|
12
|
+
* - `mounting`: the next page is mounted and fetching its data.
|
|
13
|
+
* - `idle`: the page is shown.
|
|
14
|
+
*/
|
|
15
|
+
export type PageLoadingPhase = "idle" | "navigating" | "mounting";
|
|
16
|
+
|
|
17
|
+
export type PageLoadingState = {
|
|
18
|
+
phase: PageLoadingPhase;
|
|
19
|
+
region: LoadingRegion;
|
|
20
|
+
/** The destination URL while navigating. */
|
|
21
|
+
targetPath: string | null;
|
|
22
|
+
/** Whether the navigation loads a new document (see `startFullLoad`). */
|
|
23
|
+
fullLoad: boolean;
|
|
24
|
+
/** `performance.now()` when the loading started. */
|
|
25
|
+
startedAt: number | null;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the page has the shell (header and sidebar) this store hides
|
|
28
|
+
* regions of. Pages without it keep the legacy loading layout.
|
|
29
|
+
*/
|
|
30
|
+
handled: boolean;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type RouterEvent =
|
|
34
|
+
| "routeChangeStart"
|
|
35
|
+
| "routeChangeComplete"
|
|
36
|
+
| "routeChangeError";
|
|
37
|
+
|
|
38
|
+
/** The part of Next.js' `Router.events` the store listens to. */
|
|
39
|
+
export type RouterEvents = {
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
on(event: RouterEvent, handler: (...args: any[]) => void): void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type PageLoadingEnvironment = {
|
|
45
|
+
now: () => number;
|
|
46
|
+
setTimeout: (callback: () => void, delay: number) => unknown;
|
|
47
|
+
clearTimeout: (timer: unknown) => void;
|
|
48
|
+
/** Whether the current document renders the shell's header. */
|
|
49
|
+
hasShell: () => boolean;
|
|
50
|
+
/** The sidebar variant on screen, `null` when there is none. */
|
|
51
|
+
getSidebarVariant: () => string | null;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Delay after the last pending query ends before revealing the page. */
|
|
55
|
+
export const SETTLE_DELAY = 50;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The page is revealed after this delay, even with queries still pending.
|
|
59
|
+
* It starts once the new page mounts; while the loader runs, the 10s CSS
|
|
60
|
+
* fallback in `page-loading-indicator.scss` shows the region instead.
|
|
61
|
+
*/
|
|
62
|
+
export const REVEAL_TIMEOUT = 8000;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A hard load renders the page already hidden, so the server's markup and
|
|
66
|
+
* the first client render agree, and the page shows once its data is ready.
|
|
67
|
+
*/
|
|
68
|
+
export const INITIAL_PAGE_LOADING_STATE: PageLoadingState = {
|
|
69
|
+
phase: "mounting",
|
|
70
|
+
region: "body",
|
|
71
|
+
targetPath: null,
|
|
72
|
+
fullLoad: false,
|
|
73
|
+
startedAt: null,
|
|
74
|
+
handled: true,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const createPageLoadingStore = (env: PageLoadingEnvironment) => {
|
|
78
|
+
let state = INITIAL_PAGE_LOADING_STATE;
|
|
79
|
+
let pending = 0;
|
|
80
|
+
// Bumped on each navigation, so queries of a previous one don't count.
|
|
81
|
+
let generation = 0;
|
|
82
|
+
let startVariant: string | null = null;
|
|
83
|
+
let settleTimer: unknown = null;
|
|
84
|
+
let revealTimer: unknown = null;
|
|
85
|
+
let initialized = false;
|
|
86
|
+
let attached = false;
|
|
87
|
+
const listeners = new Set<() => void>();
|
|
88
|
+
|
|
89
|
+
const set = (patch: Partial<PageLoadingState>) => {
|
|
90
|
+
state = { ...state, ...patch };
|
|
91
|
+
listeners.forEach((listener) => listener());
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const clearTimers = () => {
|
|
95
|
+
if (settleTimer !== null) env.clearTimeout(settleTimer);
|
|
96
|
+
if (revealTimer !== null) env.clearTimeout(revealTimer);
|
|
97
|
+
settleTimer = null;
|
|
98
|
+
revealTimer = null;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const reveal = () => {
|
|
102
|
+
clearTimers();
|
|
103
|
+
pending = 0;
|
|
104
|
+
generation += 1;
|
|
105
|
+
set({ phase: "idle", targetPath: null, fullLoad: false });
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const scheduleSettle = () => {
|
|
109
|
+
if (settleTimer !== null) env.clearTimeout(settleTimer);
|
|
110
|
+
|
|
111
|
+
settleTimer = env.setTimeout(() => {
|
|
112
|
+
settleTimer = null;
|
|
113
|
+
|
|
114
|
+
if (state.phase === "mounting" && pending === 0) {
|
|
115
|
+
reveal();
|
|
116
|
+
}
|
|
117
|
+
}, SETTLE_DELAY);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const enterMounting = () => {
|
|
121
|
+
clearTimers();
|
|
122
|
+
set({ phase: "mounting", startedAt: state.startedAt ?? env.now() });
|
|
123
|
+
revealTimer = env.setTimeout(reveal, REVEAL_TIMEOUT);
|
|
124
|
+
scheduleSettle();
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A navigation to a page with another sidebar (e.g. a page outside the
|
|
129
|
+
* level map) hides everything below the header once it mounts.
|
|
130
|
+
*/
|
|
131
|
+
const syncRegion = () => {
|
|
132
|
+
if (state.phase === "idle" || state.region === "body") return;
|
|
133
|
+
|
|
134
|
+
if (env.getSidebarVariant() !== startVariant) {
|
|
135
|
+
set({ region: "body" });
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const navigate = (url: string, region: LoadingRegion, fullLoad = false) => {
|
|
140
|
+
clearTimers();
|
|
141
|
+
pending = 0;
|
|
142
|
+
generation += 1;
|
|
143
|
+
startVariant = env.getSidebarVariant();
|
|
144
|
+
|
|
145
|
+
set({
|
|
146
|
+
phase: "navigating",
|
|
147
|
+
region,
|
|
148
|
+
targetPath: url,
|
|
149
|
+
fullLoad,
|
|
150
|
+
// A click while loading keeps the bar's progress.
|
|
151
|
+
startedAt:
|
|
152
|
+
state.phase === "idle" ? env.now() : state.startedAt ?? env.now(),
|
|
153
|
+
handled: env.hasShell(),
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const start = (url: string, currentPath: string) => {
|
|
158
|
+
const path = removePathPrefix(url);
|
|
159
|
+
|
|
160
|
+
if (getRouteKey(path) === getRouteKey(currentPath)) return;
|
|
161
|
+
|
|
162
|
+
navigate(path, getLoadingRegion(currentPath, path));
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* A navigation the router doesn't run (e.g. the full page load of a
|
|
167
|
+
* contract switch): everything below the header stays hidden until the
|
|
168
|
+
* next document replaces it, which starts hidden too.
|
|
169
|
+
*/
|
|
170
|
+
const startFullLoad = (url: string) => navigate(url, "body", true);
|
|
171
|
+
|
|
172
|
+
const complete = () => {
|
|
173
|
+
if (state.phase !== "navigating") return;
|
|
174
|
+
|
|
175
|
+
syncRegion();
|
|
176
|
+
enterMounting();
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const fail = (error?: { cancelled?: boolean }) => {
|
|
180
|
+
// A cancelled navigation is followed by the one that cancelled it.
|
|
181
|
+
if (error?.cancelled) return;
|
|
182
|
+
if (state.phase === "navigating") reveal();
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
getSnapshot: () => state,
|
|
187
|
+
getServerSnapshot: () => INITIAL_PAGE_LOADING_STATE,
|
|
188
|
+
|
|
189
|
+
subscribe: (listener: () => void) => {
|
|
190
|
+
listeners.add(listener);
|
|
191
|
+
|
|
192
|
+
return () => {
|
|
193
|
+
listeners.delete(listener);
|
|
194
|
+
};
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
/** Starts the hard load's reveal. Runs once, on the client. */
|
|
198
|
+
init: () => {
|
|
199
|
+
if (initialized) return;
|
|
200
|
+
initialized = true;
|
|
201
|
+
|
|
202
|
+
if (state.phase === "mounting") {
|
|
203
|
+
set({ startedAt: 0 });
|
|
204
|
+
enterMounting();
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
/** Listens to the router. Runs once: the router outlives the pages. */
|
|
209
|
+
attach: (events: RouterEvents, getCurrentPath: () => string) => {
|
|
210
|
+
if (attached) return;
|
|
211
|
+
attached = true;
|
|
212
|
+
|
|
213
|
+
events.on(
|
|
214
|
+
"routeChangeStart",
|
|
215
|
+
(url: string, options?: { shallow?: boolean }) => {
|
|
216
|
+
if (options?.shallow) return;
|
|
217
|
+
start(url, getCurrentPath());
|
|
218
|
+
}
|
|
219
|
+
);
|
|
220
|
+
events.on("routeChangeComplete", complete);
|
|
221
|
+
events.on("routeChangeError", (error: { cancelled?: boolean }) =>
|
|
222
|
+
fail(error)
|
|
223
|
+
);
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Registers a request the page needs before it shows. Returns the
|
|
228
|
+
* function to call when it ends.
|
|
229
|
+
*/
|
|
230
|
+
track: () => {
|
|
231
|
+
if (state.phase === "idle") return () => {};
|
|
232
|
+
|
|
233
|
+
const trackedGeneration = generation;
|
|
234
|
+
let ended = false;
|
|
235
|
+
pending += 1;
|
|
236
|
+
|
|
237
|
+
return () => {
|
|
238
|
+
if (ended || trackedGeneration !== generation) return;
|
|
239
|
+
ended = true;
|
|
240
|
+
pending -= 1;
|
|
241
|
+
|
|
242
|
+
if (pending === 0 && state.phase === "mounting") {
|
|
243
|
+
scheduleSettle();
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
syncRegion,
|
|
249
|
+
start,
|
|
250
|
+
startFullLoad,
|
|
251
|
+
complete,
|
|
252
|
+
fail,
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
export type PageLoadingStore = ReturnType<typeof createPageLoadingStore>;
|
|
257
|
+
|
|
258
|
+
export const SHELL_HEADER_SELECTOR =
|
|
259
|
+
"header[data-fs-bp-org-unit-details-navbar]";
|
|
260
|
+
const SIDEBAR_SELECTOR = "[data-fs-bp-suma-sidebar-variant]";
|
|
261
|
+
|
|
262
|
+
/** The element each loading region hides. */
|
|
263
|
+
export const LOADING_REGION_SELECTORS: Record<LoadingRegion, string> = {
|
|
264
|
+
content: "[data-fs-bp-contract-listing-section]",
|
|
265
|
+
body: "[data-fs-bp-contract-listing-body]",
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export const pageLoadingStore = createPageLoadingStore({
|
|
269
|
+
now: () => performance.now(),
|
|
270
|
+
setTimeout: (callback, delay) => window.setTimeout(callback, delay),
|
|
271
|
+
clearTimeout: (timer) => window.clearTimeout(timer as number),
|
|
272
|
+
hasShell: () => document.querySelector(SHELL_HEADER_SELECTOR) !== null,
|
|
273
|
+
getSidebarVariant: () =>
|
|
274
|
+
document
|
|
275
|
+
.querySelector(SIDEBAR_SELECTOR)
|
|
276
|
+
?.getAttribute("data-fs-bp-suma-sidebar-variant") ?? null,
|
|
277
|
+
});
|
|
@@ -4,6 +4,7 @@ import { withLocalization } from "../localization/withLocalization";
|
|
|
4
4
|
|
|
5
5
|
import { withAuth } from "./withAuth";
|
|
6
6
|
import { withBuyerPortal } from "./withBuyerPortal";
|
|
7
|
+
import { withRouteKey } from "./withRouteKey";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Type for HOCs that can be applied in sequence
|
|
@@ -15,13 +16,16 @@ type ProviderHOC = (Component: ComponentType<any>) => ComponentType<any>;
|
|
|
15
16
|
/**
|
|
16
17
|
* Default project providers applied to all pages.
|
|
17
18
|
* Order: right to left (like pipe/compose).
|
|
18
|
-
* Resulting tree: withAuth › withLocalization › withBuyerPortal › Component
|
|
19
|
+
* Resulting tree: withRouteKey › withAuth › withLocalization › withBuyerPortal › Component
|
|
19
20
|
*
|
|
20
21
|
* withLocalization is placed before withBuyerPortal so it:
|
|
21
22
|
* 1. Receives `context` (withBuyerPortal strips it before rendering its child).
|
|
22
23
|
* 2. Wraps BuyerPortalProvider (and its LoadingTabsLayout), enabling localization in loading states.
|
|
24
|
+
*
|
|
25
|
+
* withRouteKey is outermost so a path change remounts the whole page tree.
|
|
23
26
|
*/
|
|
24
27
|
const projectProviders: ProviderHOC[] = [
|
|
28
|
+
withRouteKey,
|
|
25
29
|
withAuth,
|
|
26
30
|
withLocalization,
|
|
27
31
|
withBuyerPortal,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ComponentType, createElement } from "react";
|
|
2
|
+
|
|
3
|
+
import { useRouter } from "next/router";
|
|
4
|
+
|
|
5
|
+
import { getRouteKey } from "./getRouteKey";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Remounts the page when its path changes.
|
|
9
|
+
*
|
|
10
|
+
* Client-side navigations between two URLs of the same Next.js page (e.g. the
|
|
11
|
+
* users of one unit, then of another) keep the page mounted, so state
|
|
12
|
+
* initialized from the loader data would go stale. Query-only changes
|
|
13
|
+
* (pagination, search, filters) keep the page mounted, as before.
|
|
14
|
+
*/
|
|
15
|
+
export const withRouteKey = <T extends Record<string, unknown>>(
|
|
16
|
+
Component: ComponentType<T>
|
|
17
|
+
) => {
|
|
18
|
+
return function WrappedWithRouteKey(props: T) {
|
|
19
|
+
const { asPath } = useRouter();
|
|
20
|
+
|
|
21
|
+
return createElement(Component, { ...props, key: getRouteKey(asPath) });
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { useBuyerPortal } from "../../../shared/hooks";
|
|
2
|
-
import { ContractTabsLayout } from "../../../shared/layouts/ContractTabsLayout/ContractTabsLayout";
|
|
3
|
-
import { LoadingTabsLayoutContent } from "../../../shared/layouts/LoadingTabsLayout/LoadingTabsLayout";
|
|
4
|
-
import { useLocalization } from "../../../shared/localization/LocalizationContext";
|
|
5
|
-
|
|
6
|
-
export type ContractSwitchOverlayProps = {
|
|
7
|
-
orgUnitId: string;
|
|
8
|
-
contractId: string;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Full-screen skeleton shown while a contract switch is in flight.
|
|
13
|
-
*
|
|
14
|
-
* A switch performs a hard navigation (`window.location.assign`) to the
|
|
15
|
-
* contract's Profile page, so Next's router-loading skeleton never fires. This
|
|
16
|
-
* overlay renders the same destination skeleton other screens use
|
|
17
|
-
* (`ContractTabsLayout` in loading mode), keeping the loading experience
|
|
18
|
-
* consistent until the new contract context loads and tears down the page.
|
|
19
|
-
*/
|
|
20
|
-
export const ContractSwitchOverlay = ({
|
|
21
|
-
orgUnitId,
|
|
22
|
-
contractId,
|
|
23
|
-
}: ContractSwitchOverlayProps) => {
|
|
24
|
-
const { t } = useLocalization();
|
|
25
|
-
const { featureFlags } = useBuyerPortal();
|
|
26
|
-
|
|
27
|
-
return (
|
|
28
|
-
<div data-fs-bp-contract-switch-overlay role="alert" aria-busy="true">
|
|
29
|
-
<ContractTabsLayout
|
|
30
|
-
orgUnitName=""
|
|
31
|
-
orgUnitId={orgUnitId}
|
|
32
|
-
contractName=""
|
|
33
|
-
contractId={contractId}
|
|
34
|
-
pageName={t("layouts.tabs.contract")}
|
|
35
|
-
loading
|
|
36
|
-
>
|
|
37
|
-
<LoadingTabsLayoutContent
|
|
38
|
-
title={t(
|
|
39
|
-
featureFlags?.suma
|
|
40
|
-
? "layouts.navigation.basicInformation"
|
|
41
|
-
: "layouts.titles.profile"
|
|
42
|
-
)}
|
|
43
|
-
/>
|
|
44
|
-
</ContractTabsLayout>
|
|
45
|
-
</div>
|
|
46
|
-
);
|
|
47
|
-
};
|