Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

1.5 KB
Raw Blame
import { useLingui } from '@lingui/react/macro';
import { useEffect, useSyncExternalStore } from 'react';
import { toast } from 'sonner';
import { hasNonGitHubRemote, remoteTakesStoredToken } from '@/components/SyncStatusBadge';
import type { GitSyncStatus } from '@/hooks/use-git-sync-status';
import { type AuthPromptStep, authPromptStore } from '@/lib/auth-prompt-store';
import { openAccountSettings } from '@/lib/use-settings-route';

export function useSyncAuthPrompt(
  remote: GitSyncStatus['remote'] | undefined,
  onPrompt: (step: AuthPromptStep) => void,
): void {
  const { t } = useLingui();
  const pending = useSyncExternalStore(
    authPromptStore.subscribe,
    authPromptStore.getSnapshot,
    () => null,
  );
  const nonGitHub = hasNonGitHubRemote(remote ?? null);
  const takesToken = remoteTakesStoredToken(remote);
  useEffect(() => {
    if (pending === null) return;
    authPromptStore.clear();
    if (pending === 'identity' || !nonGitHub) {
      onPrompt(pending);
      return;
    }
    if (!takesToken) {
      toast.info(
        t`Git couldn't authenticate with this host. Check the SSH key or saved credentials git uses for it.`,
      );
      return;
    }
    toast.info(
      t`OpenKnowledge has no credential stored for this host. Add a token in Settings, or let git use the credentials it already has.`,
      { action: { label: t`Add token`, onClick: () => openAccountSettings() } },
    );
  }, [pending, nonGitHub, takesToken, onPrompt, t]);
}