Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

1.7 KB
Raw Blame
import { updateGuideDescription } from '@mimik/core/guides/service';
import { useCallback, useState } from 'react';
import { ai } from '../../env';
import { guideDescriptionErrorMessage } from '../lib/guide-description-error';

export interface GuideDescription {
  text: string;
  set: (next: string) => void;
  generating: boolean;
  error: string | null;
  hasApiKey: boolean;
  setHasApiKey: (has: boolean) => void;
  clearError: () => void;
  commit: (next: string) => void;
  generate: () => Promise<void>;
}

export function useGuideDescription(guideId: string, onWritten: (next: string) => void): GuideDescription {
  const [text, setText] = useState('');
  const [generating, setGenerating] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [hasApiKey, setHasApiKey] = useState(false);

  const commit = useCallback(
    (next: string) => {
      setText(next);
      void updateGuideDescription(guideId, next);
      onWritten(next);
    },
    [guideId, onWritten],
  );

  const generate = useCallback(async () => {
    setGenerating(true);
    setError(null);
    try {
      const result = await ai.describeGuide(guideId);
      if (result.error) {
        setError(guideDescriptionErrorMessage(result.error));
        return;
      }
      if (!result.description) return;
      setText(result.description);
      onWritten(result.description);
    } catch {
      setError(guideDescriptionErrorMessage('generation-failed'));
    } finally {
      setGenerating(false);
    }
  }, [guideId, onWritten]);

  return {
    text,
    set: setText,
    generating,
    error,
    clearError: () => setError(null),
    hasApiKey,
    setHasApiKey,
    commit,
    generate,
  };
}