|
@@ -44,13 +44,15 @@ export default function QuickNav() {
|
|
|
const router = useRouter();
|
|
const router = useRouter();
|
|
|
const pathname = usePathname() || "/";
|
|
const pathname = usePathname() || "/";
|
|
|
|
|
|
|
|
- const { status, user } = useAuth();
|
|
|
|
|
|
|
+ const { status, user, retry } = useAuth();
|
|
|
|
|
|
|
|
const isAuthenticated = status === "authenticated" && user;
|
|
const isAuthenticated = status === "authenticated" && user;
|
|
|
const isAdminDev =
|
|
const isAdminDev =
|
|
|
isAuthenticated && (user.role === "admin" || user.role === "dev");
|
|
isAuthenticated && (user.role === "admin" || user.role === "dev");
|
|
|
const isBranchUser = isAuthenticated && user.role === "branch";
|
|
const isBranchUser = isAuthenticated && user.role === "branch";
|
|
|
|
|
|
|
|
|
|
+ const canRevalidate = typeof retry === "function";
|
|
|
|
|
+
|
|
|
const [selectedBranch, setSelectedBranch] = React.useState(null);
|
|
const [selectedBranch, setSelectedBranch] = React.useState(null);
|
|
|
|
|
|
|
|
const [branchList, setBranchList] = React.useState({
|
|
const [branchList, setBranchList] = React.useState({
|
|
@@ -68,20 +70,17 @@ export default function QuickNav() {
|
|
|
React.useEffect(() => {
|
|
React.useEffect(() => {
|
|
|
if (!isAuthenticated) return;
|
|
if (!isAuthenticated) return;
|
|
|
|
|
|
|
|
- // Branch users: selection is fixed to their own branch.
|
|
|
|
|
if (isBranchUser) {
|
|
if (isBranchUser) {
|
|
|
const own = user.branchId;
|
|
const own = user.branchId;
|
|
|
setSelectedBranch(own && isValidBranchParam(own) ? own : null);
|
|
setSelectedBranch(own && isValidBranchParam(own) ? own : null);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Admin/dev: prefer current route branch, fallback to last-used localStorage.
|
|
|
|
|
const fromRoute = readRouteBranchFromPathname(pathname);
|
|
const fromRoute = readRouteBranchFromPathname(pathname);
|
|
|
const fromStorage = safeReadLocalStorageBranch(STORAGE_KEY_LAST_BRANCH);
|
|
const fromStorage = safeReadLocalStorageBranch(STORAGE_KEY_LAST_BRANCH);
|
|
|
|
|
|
|
|
const initial = fromRoute || fromStorage || null;
|
|
const initial = fromRoute || fromStorage || null;
|
|
|
|
|
|
|
|
- // Avoid unnecessary state updates.
|
|
|
|
|
if (initial && initial !== selectedBranch) {
|
|
if (initial && initial !== selectedBranch) {
|
|
|
setSelectedBranch(initial);
|
|
setSelectedBranch(initial);
|
|
|
safeWriteLocalStorageBranch(STORAGE_KEY_LAST_BRANCH, initial);
|
|
safeWriteLocalStorageBranch(STORAGE_KEY_LAST_BRANCH, initial);
|
|
@@ -89,8 +88,6 @@ export default function QuickNav() {
|
|
|
}, [isAuthenticated, isBranchUser, user?.branchId, pathname, selectedBranch]);
|
|
}, [isAuthenticated, isBranchUser, user?.branchId, pathname, selectedBranch]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
React.useEffect(() => {
|
|
|
- // Fetch the branch list once for admin/dev users (or when the user changes),
|
|
|
|
|
- // not on every selectedBranch change.
|
|
|
|
|
if (!isAdminDev) return;
|
|
if (!isAdminDev) return;
|
|
|
|
|
|
|
|
let cancelled = false;
|
|
let cancelled = false;
|
|
@@ -118,8 +115,6 @@ export default function QuickNav() {
|
|
|
}, [isAdminDev, user?.userId]);
|
|
}, [isAdminDev, user?.userId]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
React.useEffect(() => {
|
|
|
- // After we have the branch list, ensure selectedBranch is valid and known.
|
|
|
|
|
- // This effect does NOT trigger any refetches (only local state).
|
|
|
|
|
if (!isAdminDev) return;
|
|
if (!isAdminDev) return;
|
|
|
if (branchList.status !== BRANCH_LIST_STATE.READY) return;
|
|
if (branchList.status !== BRANCH_LIST_STATE.READY) return;
|
|
|
|
|
|
|
@@ -152,9 +147,6 @@ export default function QuickNav() {
|
|
|
function navigateToBranchKeepingContext(nextBranch) {
|
|
function navigateToBranchKeepingContext(nextBranch) {
|
|
|
if (!isValidBranchParam(nextBranch)) return;
|
|
if (!isValidBranchParam(nextBranch)) return;
|
|
|
|
|
|
|
|
- // IMPORTANT:
|
|
|
|
|
- // Avoid useSearchParams() here to prevent build-time prerender failures on static routes.
|
|
|
|
|
- // We only need the current query string at click-time (client-only), so window is fine.
|
|
|
|
|
const currentPathname =
|
|
const currentPathname =
|
|
|
typeof window !== "undefined"
|
|
typeof window !== "undefined"
|
|
|
? window.location.pathname || pathname
|
|
? window.location.pathname || pathname
|
|
@@ -171,7 +163,10 @@ export default function QuickNav() {
|
|
|
|
|
|
|
|
if (!nextUrl) return;
|
|
if (!nextUrl) return;
|
|
|
|
|
|
|
|
- // Client navigation: keeps providers mounted and avoids hard reload flicker.
|
|
|
|
|
|
|
+ // Optional but desired (RHL-032):
|
|
|
|
|
+ // Trigger a session revalidation without causing content flicker.
|
|
|
|
|
+ if (canRevalidate) retry();
|
|
|
|
|
+
|
|
|
router.push(nextUrl);
|
|
router.push(nextUrl);
|
|
|
}
|
|
}
|
|
|
|
|
|