import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from "react"; import { useQuery } from "@tanstack/react-query"; import { buildRoutineMentionHref, buildSkillMentionHref } from "@paperclipai/shared"; import { companySkillsApi } from "../api/companySkills"; import { routinesApi } from "../api/routines"; import { useCompany } from "./CompanyContext"; import { queryKeys } from "../lib/queryKeys"; export interface SkillCommandOption { id: string; kind: "skill"; skillId: string; key: string; name: string; slug: string; description: string | null; href: string; aliases: string[]; } export interface RoutineCommandOption { id: string; kind: "routine"; routineId: string; name: string; status: string; href: string; aliases: string[]; } export interface ActionCommandOption { id: string; kind: "action"; command: string; name: string; description: string; aliases: string[]; disabled?: boolean; disabledReason?: string | null; } export type SlashCommandOption = SkillCommandOption | RoutineCommandOption | ActionCommandOption; interface EditorAutocompleteContextValue { slashCommands: SlashCommandOption[]; /** Starts the skills and routines reads. Editors call it when they mount. */ requestSlashCommands: () => void; } const EditorAutocompleteContext = createContext({ slashCommands: [], requestSlashCommands: () => {}, }); /** * Slash-command options for Markdown editors. The company skills and routines * lists are large, and most pages have no editor, so the provider reads them * only after the first editor mounts. Once read they stay current through the * live-update invalidations for skills and routines. */ export function EditorAutocompleteProvider({ children }: { children: ReactNode }) { const { selectedCompanyId } = useCompany(); const [requested, setRequested] = useState(false); const requestSlashCommands = useCallback(() => setRequested(true), []); const { data: companySkills = [] } = useQuery({ queryKey: selectedCompanyId ? queryKeys.companySkills.list(selectedCompanyId) : ["company-skills", "__none__"], queryFn: () => companySkillsApi.list(selectedCompanyId!), enabled: requested && Boolean(selectedCompanyId), }); const { data: routines = [] } = useQuery({ queryKey: selectedCompanyId ? queryKeys.routines.list(selectedCompanyId) : ["routines", "__none__", "__all-projects__"], queryFn: () => routinesApi.list(selectedCompanyId!), enabled: requested && Boolean(selectedCompanyId), }); const value = useMemo(() => ({ slashCommands: [ ...companySkills.map((skill) => ({ id: `skill:${skill.id}`, kind: "skill" as const, skillId: skill.id, key: skill.key, name: skill.name, slug: skill.slug, description: skill.description ?? null, href: buildSkillMentionHref(skill.id, skill.slug), aliases: [skill.slug, skill.name, skill.key], })), ...routines .filter((routine) => routine.status !== "archived") .sort((left, right) => left.title.localeCompare(right.title)) .map((routine) => ({ id: `routine:${routine.id}`, kind: "routine" as const, routineId: routine.id, name: routine.title, status: routine.status, href: buildRoutineMentionHref(routine.id), aliases: [`routine:${routine.title}`, routine.title, routine.id], })), ], requestSlashCommands, }), [companySkills, requestSlashCommands, routines]); return ( {children} ); } /** Returns the slash commands and starts their reads on first use. */ export function useEditorAutocomplete() { const value = useContext(EditorAutocompleteContext); const { requestSlashCommands } = value; useEffect(() => { requestSlashCommands(); }, [requestSlashCommands]); return value; }