Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.6 KB
Raw Blame
"use client";

import { useEffect } from "react";
import { useNavigationStore } from "@multica/core/navigation";
import { useAuthStore } from "@multica/core/auth";
import {
  paths,
  resolvePostAuthDestination,
  useCurrentWorkspace,
  useHasOnboarded,
} from "@multica/core/paths";
import { useWorkspaceList } from "@multica/core/workspace";
import { useIsGuest } from "@multica/core/permissions";
import { useRecentIssuesStore } from "@multica/core/issues/stores";
import { useRecentVisitsStore } from "@multica/core/command-bar";
import { useNavigation } from "../navigation";

/**
 * Auth + workspace gate for the dashboard.
 *
 * Redirect logic:
 *  - Auth still loading → wait
 *  - Not logged in → /login
 *  - Logged in but workspace list not yet loaded → wait (don't bounce prematurely)
 *  - Logged in but URL slug doesn't resolve to any workspace →
 *    `resolvePostAuthDestination(list, hasOnboarded)` (workspace-presence first;
 *    see paths/resolve.ts for the full table)
 *
 * This guard only redirects when the URL slug doesn't resolve. Onboarding
 * itself marks the user onboarded before navigating into a workspace, so
 * entering the dashboard no longer depends on a follow-up Helper modal.
 *
 * (Older comment claimed this state was physically impossible because
 * CreateWorkspace and AcceptInvitation atomically marked onboarded.
 * CreateWorkspace no longer marks; AcceptInvitation still does — invitees
 * skip the modal entirely.)
 *
 * We read the workspace list query state directly (rather than relying on
 * useCurrentWorkspace's null return) so we can distinguish "list loading"
 * from "slug not found". Otherwise users could see a transient redirect
 * before their workspace list arrives.
 */
// The workspace sections a guest (MUL-9420) may open; every other section is
// about agents or workspace administration, which the server refuses it.
const GUEST_SECTIONS = new Set(["inbox", "my-issues", "issues", "projects", "settings"]);

export function useDashboardGuard() {
  const { pathname, replace } = useNavigation();
  const user = useAuthStore((s) => s.user);
  const isLoading = useAuthStore((s) => s.isLoading);
  const workspace = useCurrentWorkspace();
  const hasOnboarded = useHasOnboarded();
  const { workspaces, ready: workspaceListReady } = useWorkspaceList({
    enabled: !!user,
  });

  useEffect(() => {
    if (isLoading) return;
    if (!user) {
      replace(paths.login());
      return;
    }
    if (!workspaceListReady) return;
    if (!workspace) {
      replace(resolvePostAuthDestination(workspaces, hasOnboarded));
    }
  }, [user, isLoading, workspaceListReady, workspace, workspaces, hasOnboarded, replace]);

  const guest = useIsGuest(workspace?.id);
  const section = pathname.split("/")[2] ?? "";
  const guestBlocked = guest && !!workspace && !GUEST_SECTIONS.has(section);
  useEffect(() => {
    if (guestBlocked && workspace) replace(paths.workspace(workspace.slug).inbox());
  }, [guestBlocked, workspace, replace]);

  useEffect(() => {
    useNavigationStore.getState().onPathChange(pathname);
  }, [pathname]);

  // Drop recent-issues buckets for workspaces the user no longer belongs to.
  // Runs once the workspace list resolves, and again whenever membership
  // changes (workspace deleted, user kicked, user left).
  useEffect(() => {
    if (!workspaceListReady) return;
    const activeWsIds = workspaces.map((w) => w.id);
    useRecentIssuesStore.getState().pruneWorkspaces(activeWsIds);
    useRecentVisitsStore.getState().pruneWorkspaces(activeWsIds);
  }, [workspaceListReady, workspaces]);

  return { user, isLoading, workspace, guestBlocked };
}