{"version":3,"file":"useIsMenuStale.mjs","names":["useState","useUser","useIsTenantSwitching","useIsMenuStale","menu","loading","scope","$","_c","user","isTenantSwitching","requestKey","currentTenantId","t0","Symbol","for","undefined","key","stamp","setStamp","settled"],"sources":["../../../src/utilities/menus/useIsMenuStale.ts"],"sourcesContent":["import { useState } from 'react';\nimport { useUser } from '../auth/useUser';\nimport { useIsTenantSwitching } from '../tenant/useIsTenantSwitching';\n\n/**\n * True while the menu in hand cannot be trusted to describe the tenant we are\n * on. Hold the `/` home redirect (and anything else that navigates off menu\n * data) while it is true.\n *\n * Two lags stack on a tenant switch:\n *\n * 1. `user.currentTenantId` still names the old tenant (see\n *    useIsTenantSwitching), so the menu request has not even been re-issued —\n *    the old menu sits there with `loading: false`.\n * 2. Once it flips, axios-hooks keeps the previous `data` and only starts the\n *    request from an effect, leaving one render with the new tenant id and the\n *    old menu, still `loading: false`.\n *\n * Either window is a render, which is all `<Redirect>` needs to send the user\n * to the previous tenant's home page.\n *\n * @param menu The menu data as returned by the fetch (undefined until it lands).\n * @param loading The fetch's loading flag.\n * @param scope Extra request identity beyond the tenant — pass the verticalId\n * when the menu request is vertical-scoped, so a vertical change is treated as\n * a new request the same way a tenant change is.\n */\nexport const useIsMenuStale = (\n  menu: unknown,\n  loading: boolean,\n  scope?: string | number | null\n): boolean => {\n  const user = useUser();\n  const isTenantSwitching = useIsTenantSwitching();\n  const requestKey = `${user?.currentTenantId ?? ''}|${scope ?? ''}`;\n\n  // Which request the menu in hand came back from. A response is the only thing\n  // that changes `menu`'s identity, so that is when we stamp it.\n  const [stamp, setStamp] = useState<{ menu: unknown; key: string }>({\n    menu: undefined,\n    key: ''\n  });\n  const settled = !loading && menu !== undefined && menu !== null;\n\n  // Stamped during render, not in an effect, so a response and the verdict on\n  // it land together — an effect would leave one render claiming the fresh menu\n  // is stale, and the `/` route would flash a loader after every menu load.\n  if (settled && menu !== stamp.menu) {\n    setStamp({ menu, key: requestKey });\n  }\n\n  if (isTenantSwitching || !settled) return true;\n\n  return stamp.key !== requestKey;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,MAAaG,kBAAiBC,MAAAC,SAAAC,UAAA;CAAA,MAAAC,IAAAC,EAAA,EAAA;CAK5B,MAAAC,OAAaR,SAAS;CACtB,MAAAS,oBAA0BR,sBAAsB;CAChD,MAAAS,aAAmB,GAAGF,MAAIG,mBAAJ,GAA2B,GAAIN,SAAA;CAAc,IAAAO;AAAA,KAAAN,EAAA,OAAAO,OAAAC,IAAA,4BAAA,EAAA;AAIAF,OAAA;GAAAT,MAC3DY,KAAAA;GAASC,KACV;GACN;AAAAV,IAAA,KAAAM;OAAAA,MAAAN,EAAA;CAHD,MAAA,CAAAW,OAAAC,YAA0BnB,SAAyCa,GAGjE;CACF,MAAAO,UAAgB,CAACf,WAAWD,SAASY,KAAAA,KAAaZ,SAAS;AAK3D,KAAIgB,WAAWhB,SAASc,MAAKd,KAC3Be,UAAS;EAAAf;EAAAa,KAAaN;EAAY,CAAC;AAGrC,KAAID,qBAAA,CAAsBU,QAAO,QAAS;AAAK,QAExCF,MAAKD,QAASN"}