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.

1.8 KB
Raw Blame
import { createContext, useContext } from 'react';
import type { IssueOpenMode } from '@/lib/api/endpoints/userPreferences';
import type { CustomField } from '@/lib/api/endpoints/customFields';
import type { ProjectDetail } from '@/lib/api/endpoints/projects';
import type { View } from '@/lib/api/endpoints/views';
import type { NewIssueDefaults } from '@/utils/project';
import type { useViewEditor } from '@/hooks/useViewEditor';

// What the Shell layout provides to its child pages (the work items view and the
// settings pages) through React context. The Shell owns the project data, the
// view editor and the project-level overlays; children read them here instead of
// re-querying.
export type ShellContext = {
  project: ProjectDetail | null;
  filteredProject: ProjectDetail | null;
  views: View[];
  editor: ReturnType<typeof useViewEditor>;
  customFields: CustomField[];
  // Opens an issue. Without `mode` the account's issueOpenMode preference decides
  // between the side panel and the issue page; pass it to force one of them.
  onOpenIssue: (id: number, mode?: IssueOpenMode) => void;
  onAddIssue: (defaults: NewIssueDefaults) => void;
  // Opens the chat panel on a new conversation with the agent.
  onChatWithAgent: (agentId: number) => void;
};

export const ShellCtx = createContext<ShellContext | null>(null);

// The Shell's onOpenIssue on its own. ShellCtx changes on every Shell render, while
// this changes only with the project and the open-mode preference, so a component
// that every card mounts reads this one.
export const IssueOpenerCtx = createContext<ShellContext['onOpenIssue'] | null>(null);

export function useShell(): ShellContext {
  const ctx = useContext(ShellCtx);
  if (!ctx) throw new Error('useShell must be used within the project Shell');
  return ctx;
}