{"version":3,"file":"useFirstPageLoadRetry.mjs","names":[],"sources":["../../../../../src/components/Loading/hooks/useFirstPageLoadRetry.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { useAriaLiveAnnouncer } from '../../Accessibility/useAriaLiveAnnouncer';\nimport { useStableCallback } from '../../../utils/useStableCallback';\n\ntype RetriedPaginator = { items: unknown[] | undefined };\n\nexport type UseFirstPageLoadRetryParams = {\n  /** The error of the failed first page, while there is one (see `getFirstPageLoadError`). */\n  firstPageLoadError: Error | undefined;\n  /** Announced when a retry loads the first page. */\n  loadedMessage: string;\n  /** The paginator the retry loads the first page into. */\n  paginator: RetriedPaginator;\n  /** Queries the first page again. */\n  reload: () => Promise<unknown>;\n};\n\n/**\n * Retries a paginated list's failed first page. While the retry runs, the failure it retries stays\n * the error to show, so the component showing it stays mounted and its focused retry button keeps\n * focus, also when the retry fails and its error replaces the old one. A retry that loads the first\n * page is announced.\n *\n * @returns `error`, the error to show (`undefined` once the first page loads), `isRetrying`, and\n *   `retry`, which starts a retry unless one is running or no first-page error is shown.\n */\nexport const useFirstPageLoadRetry = ({\n  firstPageLoadError,\n  loadedMessage,\n  paginator,\n  reload,\n}: UseFirstPageLoadRetryParams) => {\n  const announce = useAriaLiveAnnouncer();\n  // The paginator is kept with the error, so a list given another paginator shows that one's state.\n  const [retried, setRetried] = useState<{ error: Error; paginator: RetriedPaginator }>();\n  const retriedError = retried?.paginator === paginator ? retried.error : undefined;\n  // A second click before the busy state renders must not start a second retry of that paginator.\n  const retryingPaginatorRef = useRef<RetriedPaginator>(undefined);\n  // A retry that settles after the list unmounted updates and announces nothing, and one that\n  // settles after the list moved to another paginator announces nothing either.\n  const isMountedRef = useRef(false);\n  const paginatorRef = useRef(paginator);\n  paginatorRef.current = paginator;\n\n  useEffect(() => {\n    isMountedRef.current = true;\n    return () => {\n      isMountedRef.current = false;\n    };\n  }, []);\n\n  const retry = useStableCallback(() => {\n    if (retryingPaginatorRef.current === paginator || !firstPageLoadError) return;\n    const retriedPaginator = paginator;\n    retryingPaginatorRef.current = retriedPaginator;\n    setRetried({ error: firstPageLoadError, paginator: retriedPaginator });\n    // With `throwErrors` set, the reload rejects too; the error is in the paginator state either way.\n    reload()\n      .catch(() => undefined)\n      .finally(() => {\n        if (retryingPaginatorRef.current === retriedPaginator) {\n          retryingPaginatorRef.current = undefined;\n        }\n        if (!isMountedRef.current) return;\n        setRetried((current) =>\n          current?.paginator === retriedPaginator ? undefined : current,\n        );\n        const showsRetriedPaginator = paginatorRef.current === retriedPaginator;\n        if (showsRetriedPaginator && retriedPaginator.items !== undefined) {\n          announce(loadedMessage);\n        }\n      });\n  });\n\n  return {\n    error: retriedError ?? firstPageLoadError,\n    isRetrying: !!retriedError,\n    retry,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;AA2BA,IAAa,yBAAyB,EACpC,oBACA,eACA,WACA,aACiC;CACjC,MAAM,WAAW,qBAAqB;CAEtC,MAAM,CAAC,SAAS,cAAc,SAAwD;CACtF,MAAM,eAAe,SAAS,cAAc,YAAY,QAAQ,QAAQ,KAAA;CAExE,MAAM,uBAAuB,OAAyB,KAAA,CAAS;CAG/D,MAAM,eAAe,OAAO,KAAK;CACjC,MAAM,eAAe,OAAO,SAAS;CACrC,aAAa,UAAU;CAEvB,gBAAgB;EACd,aAAa,UAAU;EACvB,aAAa;GACX,aAAa,UAAU;EACzB;CACF,GAAG,CAAC,CAAC;CAEL,MAAM,QAAQ,wBAAwB;EACpC,IAAI,qBAAqB,YAAY,aAAa,CAAC,oBAAoB;EACvE,MAAM,mBAAmB;EACzB,qBAAqB,UAAU;EAC/B,WAAW;GAAE,OAAO;GAAoB,WAAW;EAAiB,CAAC;EAErE,OAAO,CAAC,CACL,YAAY,KAAA,CAAS,CAAC,CACtB,cAAc;GACb,IAAI,qBAAqB,YAAY,kBACnC,qBAAqB,UAAU,KAAA;GAEjC,IAAI,CAAC,aAAa,SAAS;GAC3B,YAAY,YACV,SAAS,cAAc,mBAAmB,KAAA,IAAY,OACxD;GAEA,IAD8B,aAAa,YAAY,oBAC1B,iBAAiB,UAAU,KAAA,GACtD,SAAS,aAAa;EAE1B,CAAC;CACL,CAAC;CAED,OAAO;EACL,OAAO,gBAAgB;EACvB,YAAY,CAAC,CAAC;EACd;CACF;AACF"}