useOverviewBranchTarget.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use client";
  2. import React from "react";
  3. import { usePathname } from "next/navigation";
  4. import { useAuth } from "@/components/auth/authContext";
  5. import { getBranches } from "@/lib/frontend/apiClient";
  6. import {
  7. canManageUsers as canManageUsersRole,
  8. isAdminLike as isAdminLikeRole,
  9. } from "@/lib/frontend/auth/roles";
  10. import { isValidBranchParam } from "@/lib/frontend/params";
  11. import { branchPath, searchPath } from "@/lib/frontend/routes";
  12. import {
  13. readRouteBranchFromPathname,
  14. safeReadLocalStorageBranch,
  15. } from "@/lib/frontend/quickNav/branchSwitch";
  16. import { resolveOverviewBranchTarget } from "@/lib/frontend/overview/homeBranchTarget";
  17. const STORAGE_KEY_LAST_BRANCH = "rhl_last_branch";
  18. const BRANCH_LIST_STATE = Object.freeze({
  19. IDLE: "idle",
  20. LOADING: "loading",
  21. READY: "ready",
  22. ERROR: "error",
  23. });
  24. export function useOverviewBranchTarget() {
  25. const pathname = usePathname() || "/";
  26. const { status, user } = useAuth();
  27. const isAuthenticated = status === "authenticated" && user;
  28. const isAdminLike = isAuthenticated && isAdminLikeRole(user.role);
  29. const canManageUsers = isAuthenticated && canManageUsersRole(user.role);
  30. const routeBranch = React.useMemo(
  31. () => readRouteBranchFromPathname(pathname),
  32. [pathname],
  33. );
  34. const [storedBranch, setStoredBranch] = React.useState(null);
  35. const [branchList, setBranchList] = React.useState({
  36. status: BRANCH_LIST_STATE.IDLE,
  37. branches: null,
  38. });
  39. React.useEffect(() => {
  40. if (!isAuthenticated || !isAdminLike) {
  41. setStoredBranch(null);
  42. return;
  43. }
  44. setStoredBranch(safeReadLocalStorageBranch(STORAGE_KEY_LAST_BRANCH));
  45. }, [isAuthenticated, isAdminLike, user?.userId]);
  46. const localResolution = React.useMemo(() => {
  47. return resolveOverviewBranchTarget({
  48. role: user?.role,
  49. userBranchId: user?.branchId,
  50. routeBranch,
  51. storedBranch,
  52. });
  53. }, [user?.role, user?.branchId, routeBranch, storedBranch]);
  54. React.useEffect(() => {
  55. if (
  56. !isAuthenticated ||
  57. !isAdminLike ||
  58. !localResolution.shouldFetchBranches
  59. ) {
  60. setBranchList({ status: BRANCH_LIST_STATE.IDLE, branches: null });
  61. return;
  62. }
  63. let cancelled = false;
  64. setBranchList({ status: BRANCH_LIST_STATE.LOADING, branches: null });
  65. (async () => {
  66. try {
  67. const res = await getBranches();
  68. if (cancelled) return;
  69. const branches = Array.isArray(res?.branches) ? res.branches : [];
  70. setBranchList({ status: BRANCH_LIST_STATE.READY, branches });
  71. } catch (err) {
  72. if (cancelled) return;
  73. console.error("[useOverviewBranchTarget] getBranches failed:", err);
  74. setBranchList({ status: BRANCH_LIST_STATE.ERROR, branches: null });
  75. }
  76. })();
  77. return () => {
  78. cancelled = true;
  79. };
  80. }, [
  81. isAuthenticated,
  82. isAdminLike,
  83. localResolution.shouldFetchBranches,
  84. user?.userId,
  85. ]);
  86. const availableBranches =
  87. branchList.status === BRANCH_LIST_STATE.READY ? branchList.branches : null;
  88. const resolvedBranchTarget = React.useMemo(() => {
  89. return resolveOverviewBranchTarget({
  90. role: user?.role,
  91. userBranchId: user?.branchId,
  92. routeBranch,
  93. storedBranch,
  94. availableBranches,
  95. });
  96. }, [
  97. user?.role,
  98. user?.branchId,
  99. routeBranch,
  100. storedBranch,
  101. availableBranches,
  102. ]);
  103. const targetBranch = isValidBranchParam(resolvedBranchTarget.branch)
  104. ? resolvedBranchTarget.branch
  105. : null;
  106. const explorerHref = targetBranch ? branchPath(targetBranch) : null;
  107. const searchHref = targetBranch ? searchPath(targetBranch) : null;
  108. const disabledHint = "Bitte zuerst eine gültige Niederlassung wählen.";
  109. return {
  110. isAuthenticated,
  111. canManageUsers,
  112. explorerHref,
  113. searchHref,
  114. disabledHint,
  115. };
  116. }