Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

3.2 KB
Raw Blame
'use client';

import { useEffect, useState, type ReactNode } from 'react';
import { useTranslations } from 'next-intl';
import { usePathname } from 'next/navigation';
import { useSession } from '@/lib/auth-client';
import { GOD_SECTIONS } from '@/utils/godSections';
import { useGodSectionText } from '@/hooks/useSectionLabels';
import { titlePath, useDocumentTitle } from '@/hooks/useDocumentTitle';
import { SidebarInset, SidebarProvider, SidebarTrigger } from '@/components/ui/sidebar';
import { Separator } from '@/components/ui/separator';
import { ThemeToggle } from '@/components/theme-toggle';
import { LocaleToggle } from '@/components/locale-toggle';
import SectionPageSkeleton from '@/components/common/skeleton/SectionPageSkeleton';
import UserMenu from '@/components/layout/UserMenu';
import GodSidebar from '@/components/layout/GodSidebar';
import { ShellNotice } from '@/cloud';

// The shell for god mode: the instance settings sidebar and a slim header, with no
// project loaded. It is the counterpart of Shell for the project routes, but far
// smaller — nothing here needs the board, overlays, or project permissions.
//
// Access is by the global role: only the instance owner ("god") sees the pages. The
// API enforces the same on every /god route, so this check is about what to render,
// not about security.
export default function GodShell({
  defaultSidebarOpen,
  children,
}: {
  defaultSidebarOpen: boolean;
  children: ReactNode;
}) {
  const t = useTranslations('nav');
  const { data: session, isPending } = useSession();
  // The session can already be in the store on hydration while the server rendered
  // without it, so the role is only read after mount.
  const [mounted, setMounted] = useState(false);
  useEffect(() => setMounted(true), []);
  const isGod = mounted && session?.user.role === 'god';
  const sessionSettled = mounted && !isPending;
  const god = useGodSectionText();
  const slug = usePathname().split('/')[2];
  const section = GOD_SECTIONS.find((entry) => entry.slug === slug);
  useDocumentTitle(titlePath(t('godMode'), section && god.section(section.slug).label));

  return (
    <SidebarProvider defaultOpen={defaultSidebarOpen} className="h-svh overflow-hidden">
      <GodSidebar />
      <SidebarInset className="min-w-0">
        <header className="flex h-12 shrink-0 items-center gap-2 border-b px-2 sm:px-4">
          <SidebarTrigger />
          <Separator orientation="vertical" className="mr-1 h-4" />
          <div className="min-w-0 truncate text-sm font-medium">{t('godMode')}</div>
          <div className="ml-auto flex items-center gap-2">
            <LocaleToggle />
            <ThemeToggle />
            <UserMenu />
          </div>
        </header>
        <ShellNotice />

        <div className="relative flex min-h-0 flex-1 flex-col overflow-hidden">
          {isGod && children}
          {!isGod && sessionSettled && (
            <div className="flex h-full items-center justify-center px-4 text-center text-sm text-muted-foreground">
              {t('godModeOwnerOnly')}
            </div>
          )}
          {!isGod && !sessionSettled && <SectionPageSkeleton />}
        </div>
      </SidebarInset>
    </SidebarProvider>
  );
}