Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

3.7 KB
Raw Blame
import { EDITOR_LABELS } from '@inkeep/open-knowledge-core/constants/editors';
import { plural, t } from '@lingui/core/macro';
import { toast as sonnerToast } from 'sonner';
import type { OkDesktopBridge } from '@/lib/desktop-bridge-types';
import { relativeToProject } from '@/lib/project-paths';

const TOAST_DURATION_MS = 4000;
const STICKY_TOAST_DURATION_MS = 24 * 60 * 60 * 1000;

export function installOnboardingToastListener(opts: {
  bridge: OkDesktopBridge | undefined;
}): (() => void) | undefined {
  const bridge = opts.bridge;
  if (!bridge) return undefined;
  if (!bridge.onboarding) return undefined;
  return bridge.onboarding.onToast((payload) => {
    if (payload.kind === 'ancestor-promote') {
      const ancestorPath = payload.ancestorPath;
      sonnerToast.success(t`Opened existing OpenKnowledge project at ${ancestorPath}`, {
        duration: TOAST_DURATION_MS,
      });
      return;
    }
    if (payload.kind === 'startup-reclaim') {
      const labelOf = (id: string) => EDITOR_LABELS[id as keyof typeof EDITOR_LABELS] ?? id;
      const parts: string[] = [];
      const details: string[] = [];
      if (payload.mcp.status === 'repaired') {
        const names = payload.mcp.editors.map(labelOf).join(', ');
        parts.push(t`repaired MCP integration for ${names}`);
      } else if (payload.mcp.status === 'failed') {
        const { failures: failed, repaired = [] } = payload.mcp;
        const names = repaired.map(labelOf).join(', ');
        if (names.length > 0) parts.push(t`repaired MCP integration for ${names}`);
        const failures = failed.map(({ editor }) => labelOf(editor)).join(', ');
        parts.push(
          failures.length > 0
            ? t`MCP auto-repair failed for ${failures}`
            : t`MCP auto-repair failed`,
        );
        for (const { editor, reason } of failed) {
          if (reason) details.push(`${labelOf(editor)}: ${reason}`);
        }
      }
      if (payload.path.status === 'installed') parts.push(payload.path.summary);
      if (payload.path.status === 'failed') {
        const summary = payload.path.summary;
        parts.push(t`PATH install failed: ${summary}`);
      }
      const message = parts.length > 0 ? parts.join('; ') : t`OpenKnowledge integrations checked.`;
      const hasFailure = payload.mcp.status === 'failed' || payload.path.status === 'failed';
      const pathTouched = payload.path.status !== 'none';
      sonnerToast[hasFailure ? 'error' : 'success'](message, {
        duration: hasFailure || pathTouched ? STICKY_TOAST_DURATION_MS : TOAST_DURATION_MS,
        position: 'bottom-left',
        ...(details.length > 0 ? { description: details.join('\n') } : {}),
      });
      return;
    }
    if (payload.kind === 'sharing-refused-tracked') {
      const trackedCount = payload.tracked.length;
      sonnerToast.error(
        t`Config sharing unchanged: ${plural(trackedCount, {
          one: '# OK file',
          other: '# OK files',
        })} tracked upstream — see message below.`,
        {
          duration: STICKY_TOAST_DURATION_MS,
          description: payload.remediation,
        },
      );
      return;
    }
    if (payload.kind === 'sharing-no-git') {
      sonnerToast.warning(
        t`Local-only requested but no git repository was created. Switch later via Settings → Sync & sharing once the project is in a git repo.`,
        { duration: TOAST_DURATION_MS },
      );
      return;
    }
    const subPath = relativeToProject(payload.gitRoot, payload.pickedPath) ?? payload.pickedPath;
    const gitRoot = payload.gitRoot;
    sonnerToast.success(
      t`Initialized OpenKnowledge at ${gitRoot} — opened parent of ${subPath} because it contains a .git folder`,
      { duration: TOAST_DURATION_MS },
    );
  });
}