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.

5.3 KB
Raw Blame
'use client';

import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { useTranslations } from 'next-intl';
import { CloudOff, SquareKanban, Users } from 'lucide-react';
import { NoTeamStart } from '@/cloud';
import { useSession } from '@/lib/auth-client';
import { useProjectsQuery } from '@/services/projects.service';
import { useTeamsQuery } from '@/services/teams.service';
import { useWorkspacesQuery } from '@/services/workspaces.service';
import { useAccountPreferencesQuery } from '@/services/preferences.service';
import { startPagePath, projectPath } from '@/utils/paths';
import NewProjectModal from '@/components/layout/NewProjectModal';
import NewTeamModal from '@/features/teams/components/NewTeamModal';
import ProjectSwitcherHiddenState from '@/components/layout/ProjectSwitcherHiddenState';
import StartEmpty from '@/components/layout/StartEmpty';
import InviteStart from '@/features/invite/components/InviteStart';

// The index route: reopen the last-used project if it still exists, otherwise the
// first visible favorite or project, on the user's preferred start page. Waits for the
// project list and, when the client has a session, the preferences before deciding so
// it does not flash the wrong destination, and says so when a list it needs cannot be
// read. With no projects at all, an account that owns or manages a team is offered to
// create the first one there. An account that may create a team in a workspace, its own
// first, is offered to; one with no team and nowhere to create one gets `NoTeamStart`,
// and a team member is told who adds them to a project.
export default function Home() {
  const t = useTranslations('shell');
  const tCommon = useTranslations('common');
  const router = useRouter();
  const { data: teams, isError: teamsFailed } = useTeamsQuery();
  const { data: workspaces, isError: workspacesFailed } = useWorkspacesQuery();
  const { data: projects, isError: projectsFailed } = useProjectsQuery();
  const { data: session, isPending: sessionPending } = useSession();
  const { data: prefs, isPending: prefsPending } = useAccountPreferencesQuery();
  // The preferences are read only once the client has the session. Without one (its
  // request failed or came back empty) they never load, so the default start page is used.
  const prefsSettled = session ? !prefsPending : !sessionPending;
  const [creating, setCreating] = useState(false);

  useEffect(() => {
    if (projects == null || projects.length === 0 || !prefsSettled) return;
    const visible = projects.filter((project) => !project.isHidden);
    if (visible.length === 0) return;
    const last = visible.find((p) => p.id === prefs?.lastProjectId);
    const target = last?.ref ?? visible.find((p) => p.isFavorite)?.ref ?? visible[0]?.ref;
    if (target) router.replace(startPagePath(target, prefs?.startPage ?? 'work-items'));
  }, [projects, prefs, prefsSettled, router]);

  if (projects && projects.length > 0 && projects.every((project) => project.isHidden)) {
    return <ProjectSwitcherHiddenState projects={projects} />;
  }

  const managedTeam = teams?.find((one) => one.role !== 'member');
  const teamWorkspace =
    workspaces?.find((one) => one.role === 'owner') ?? workspaces?.find((one) => one.canCreateTeam);

  if (teams && workspaces && !managedTeam && projects?.length === 0) {
    if (teams.length === 0 && !teamWorkspace)
      return (
        <InviteStart>
          <NoTeamStart />
        </InviteStart>
      );
    return (
      <InviteStart>
        <StartEmpty
          icon={<Users />}
          title={teams.length === 0 ? t('noTeamsTitle') : t('noProjectsTitle')}
          hint={
            teams.length === 0
              ? t('noTeamsHint')
              : t(teamWorkspace ? 'noProjectAccessOwnerHint' : 'noProjectAccessHint')
          }
          action={teamWorkspace && t('createTeam')}
          onAction={() => setCreating(true)}
        >
          {creating && teamWorkspace && (
            <NewTeamModal workspaceId={teamWorkspace.id} onClose={() => setCreating(false)} />
          )}
        </StartEmpty>
      </InviteStart>
    );
  }

  // No projects yet: the Shell (which owns the New project modal) never mounts
  // without a project, so the empty state offers project creation directly.
  if (managedTeam && projects?.length === 0) {
    return (
      <InviteStart>
        <StartEmpty
          icon={<SquareKanban />}
          title={t('noProjectsTitle')}
          hint={t('noProjectsHint')}
          action={t('createProject')}
          onAction={() => setCreating(true)}
        >
          {creating && (
            <NewProjectModal
              teamId={managedTeam.id}
              onClose={() => setCreating(false)}
              onCreated={(key) => {
                setCreating(false);
                router.push(projectPath(key));
              }}
            />
          )}
        </StartEmpty>
      </InviteStart>
    );
  }

  const loadFailed = projects
    ? projects.length === 0 && (teamsFailed || workspacesFailed)
    : projectsFailed;
  if (loadFailed) {
    return (
      <StartEmpty icon={<CloudOff />} title={t('loadFailedTitle')} hint={t('loadFailedHint')} />
    );
  }

  return (
    <div className="flex h-svh items-center justify-center bg-background text-sm text-muted-foreground">
      {tCommon('loading')}
    </div>
  );
}