Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

4.6 KB
Raw Blame
import {
  EDITOR_LABELS,
  EDITOR_PROJECT_SKILL_ROOT,
  type EditorId,
  RESERVED_PROJECT_SKILL_NAME,
  STABLE_EDITOR_PROJECT_CONFIG_PATH,
} from '@inkeep/open-knowledge-core/constants/editors';
import { i18n } from '@lingui/core';
import { Trans, useLingui } from '@lingui/react/macro';
import { useId } from 'react';
import { RowDisclosure } from '@/components/RowDisclosure';
import { Checkbox } from '@/components/ui/checkbox';
import { Label } from '@/components/ui/label';
import { formatToolList } from '@/lib/tool-list-format';
import { cn } from '@/lib/utils';

interface ProjectAiToolsFieldProps {
  detectedEditors: readonly EditorId[] | null;
  checked: boolean;
  onCheckedChange: (next: boolean) => void;
  disabled: boolean;
  testIdPrefix: string;
  itemTestIdPrefix: string;
}

export function ProjectAiToolsField({
  detectedEditors,
  checked,
  onCheckedChange,
  disabled,
  testIdPrefix,
  itemTestIdPrefix,
}: ProjectAiToolsFieldProps) {
  const { t } = useLingui();
  const checkboxId = useId();

  const status =
    detectedEditors === null
      ? { kind: 'probing' as const, message: t`Checking which AI tools you have` }
      : detectedEditors.length === 0
        ? {
            kind: 'none' as const,
            message: t`No AI tools detected yet. Once you install one, connect it from Settings > This project.`,
          }
        : { kind: 'ready' as const, message: '', editors: detectedEditors };

  const statusRegion = (
    <p
      aria-live="polite"
      className={cn(
        status.kind === 'ready'
          ? 'sr-only'
          : 'rounded-md border border-border px-3 py-2.5 text-1sm text-muted-foreground',
      )}
      data-status={status.kind}
      data-testid={`${testIdPrefix}-status`}
    >
      {status.message}
    </p>
  );

  if (status.kind !== 'ready') return statusRegion;

  const toolList = formatToolList(
    status.editors.map((id) => EDITOR_LABELS[id]),
    i18n.locale,
  );

  return (
    <>
      {statusRegion}
      <div className="relative overflow-hidden rounded-lg border border-border bg-card/50 px-4 py-3 hover:bg-accent">
        <Label
          htmlFor={checkboxId}
          className="flex min-w-0 cursor-pointer flex-col items-start gap-1 font-normal"
        >
          <span className="flex w-full items-center gap-2.5">
            <Checkbox
              id={checkboxId}
              checked={checked}
              onCheckedChange={() => onCheckedChange(!checked)}
              disabled={disabled}
              data-testid={`${testIdPrefix}-checkbox`}
            />
            <span
              className="flex min-w-0 flex-1 items-center gap-1.5 pe-28 text-sm font-medium text-foreground"
              data-testid={`${testIdPrefix}-title`}
            >
              <Trans comment="Checkbox that wires the OpenKnowledge MCP into every detected AI tool for the project">
                Connect your AI tools to this project
              </Trans>
            </span>
          </span>
          <span
            className="text-1sm leading-normal ps-6.5 text-muted-foreground"
            data-testid={`${testIdPrefix}-summary`}
          >
            <Trans comment="Subtext under the project AI-tools checkbox">
              Adds an OpenKnowledge MCP entry and the project skill to{' '}
              <span className="font-medium text-foreground">{toolList}</span>, so your agents can
              read and edit your files.
            </Trans>
          </span>
        </Label>
        <RowDisclosure title={t`Adds these files`} testId={`${testIdPrefix}-details-toggle`}>
          <ul className="flex flex-col gap-2" data-testid={`${testIdPrefix}-details`}>
            {status.editors.map((id) => {
              const configPath = STABLE_EDITOR_PROJECT_CONFIG_PATH[id];
              const skillRoot = EDITOR_PROJECT_SKILL_ROOT[id];
              return (
                <li
                  key={id}
                  className="flex min-w-0 flex-col"
                  data-testid={`${itemTestIdPrefix}-${id}`}
                >
                  <span className="font-medium">{EDITOR_LABELS[id]}</span>
                  {configPath !== null && (
                    <span className="wrap-break-word opacity-75">
                      <code className="break-all">{configPath}</code>
                    </span>
                  )}
                  {skillRoot !== null && (
                    <span className="wrap-break-word opacity-75">
                      <code className="break-all">{`${skillRoot}/${RESERVED_PROJECT_SKILL_NAME}/`}</code>
                    </span>
                  )}
                </li>
              );
            })}
          </ul>
        </RowDisclosure>
      </div>
    </>
  );
}