Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.4 KB
Raw Blame
import { useEffect, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import apiClient from "@editor/services/apiClient";
import { useAuth } from "@editor/auth/UseSession";
import {
  readCachedOtherApp,
  writeCachedOtherApp,
} from "@editor/services/navFooterCache";
import { qk } from "@editor/query/keys";
import type { PortalAccessState } from "@editor-core/hooks/usePortalAccess";

export type { PortalAccessState };

async function fetchPortalAccess(): Promise<boolean> {
  const res = await apiClient.get<{ user?: { portalAccess?: boolean } }>(
    "/api/v1/auth/me",
  );
  return res.data.user?.portalAccess === true;
}

/**
 * Whether the current user can open the processor (admin portal), straight
 * from the backend (`/api/v1/auth/me` → `portalAccess`) — the same signal the
 * processor's own SaasPortalGate uses. Components that must mirror processor
 * access (the sidebar's editor⇄processor switcher and its footer row) ask here.
 *
 * The editor's Supabase auth context can't *answer* this — it never fetches
 * /me — so it is used only to identify who is asking. That identity is the
 * cache key, which is what keeps the answer per-user: the SPA can swap users
 * without a reload (Supabase fires SIGNED_OUT/SIGNED_IN in place; only the
 * settings Logout button hard-navigates), and a keyed cache addresses each
 * identity separately rather than holding one answer that would have to be
 * invalidated on the swap — the bug class this hook once had.
 *
 * Cached through the app query client, so leaving the editor for the processor
 * and coming back resolves from cache: the switcher is there on first paint
 * instead of appearing a request later. Guests skip the request entirely.
 */
export function usePortalAccessState(): PortalAccessState {
  const { user, loading, isAnonymous } = useAuth();
  const userId = user?.id ?? null;
  const canQuery = !loading && userId !== null && !isAnonymous;
  // The query cache is per-tree and per-load, so it can't help a cold start or
  // the hop into the processor, which mounts its own client. Seed from the last
  // answer this browser saw so the switcher and the footer's "Open ..." row are
  // there at first paint. Marked ancient so it still revalidates immediately.
  const [seed] = useState(readCachedOtherApp);

  const { data, isSuccess, isFetched, isFetching } = useQuery({
    queryKey: qk.portalAccess(userId),
    queryFn: fetchPortalAccess,
    // Signed out: nothing to ask, and any previous answer is void.
    enabled: canQuery,
    // Backend unreachable or guest (401) means no access now; a later refetch
    // asks again rather than trusting the failure.
    retry: false,
    initialData: seed,
    initialDataUpdatedAt: 0,
  });

  useEffect(() => {
    // Only a real answer is recorded — a failed probe is not one, so the next
    // mount trusts the last backend response rather than a network blip.
    if (canQuery && isSuccess && data !== undefined) writeCachedOtherApp(data);
  }, [canQuery, isSuccess, data]);

  // A signed-out user has nothing to look up, so that answer is settled at
  // once. Otherwise the seeded value is a guess until the probe comes back -
  // callers that hide UI on "no access" must not act on the guess.
  return {
    granted: canQuery && data === true,
    settled: !loading && (!canQuery || (isFetched && !isFetching)),
  };
}

export function usePortalAccess(): boolean {
  return usePortalAccessState().granted;
}