import React, { useMemo, useState } from 'react'; import { GitMerge, Search } from 'lucide-react'; import { FormSheet } from '../../components/FormSheet'; import { api } from '../../lib/api'; import { Document, MergeResult } from '../../lib/types'; interface MergeDialogProps { docs: Document[]; sourceId: string; /** Known after a drop in the sidebar; otherwise the user picks it here */ targetId?: string | null; onClose: () => void; onMerged: (result: MergeResult) => void; } const canWrite = (d: Document) => d.isOwner || d.permission === 'owner' || d.permission === 'write'; // Merging a note into another: pick the target (when not dropped onto one), confirm, merge. The source // is appended to the target under a heading with its title and goes to the trash. export const MergeDialog: React.FC = ({ docs, sourceId, targetId, onClose, onMerged }) => { const [target, setTarget] = useState(targetId || null); const [query, setQuery] = useState(''); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const source = docs.find((d) => d.id === sourceId); const targetDoc = docs.find((d) => d.id === target); const candidates = useMemo(() => { const q = query.trim().toLowerCase(); return docs .filter((d) => d.id !== sourceId && !d.isArchived && canWrite(d)) .filter((d) => !q || (d.title || '').toLowerCase().includes(q)) .slice(0, 50); }, [docs, sourceId, query]); const submit = async (e: React.FormEvent) => { e.preventDefault(); if (!target) return; setBusy(true); setError(null); try { onMerged(await api.mergeDocuments(target, sourceId)); } catch (err: any) { setError(err?.message || 'Zusammenführen fehlgeschlagen'); setBusy(false); } }; const title = (d?: Document) => `„${d?.title || 'Unbenannte Notiz'}“`; return ( } onClose={onClose} onSubmit={submit} submitLabel={target ? 'Zusammenführen' : 'Ziel wählen'} busy={busy || !target} > {targetDoc ? (

Notiz {title(source)} in {title(targetDoc)} zusammenführen?

Die Punkte von {title(source)} werden als eigener Abschnitt an {title(targetDoc)} angehängt.{' '} {title(source)} wandert danach in den Papierkorb; „Rückgängig“ macht beides wieder rückgängig.

{!targetId && ( )}
) : (

In welche Notiz soll {title(source)} eingefügt werden?

setQuery(e.target.value)} placeholder="Notiz suchen…" className="w-full h-11 sm:h-9 pl-8 pr-2 input-field text-sm" />
    {candidates.map((d) => (
  • ))} {candidates.length === 0 &&
  • Keine passende Notiz
  • }
)} {error &&

{error}

}
); };