import React, { useEffect, useState } from 'react'; import { ChevronDown, ChevronRight, Folder, Link2 } from 'lucide-react'; import { Backlink } from '../../lib/types'; import { api } from '../../lib/api'; interface BacklinksSectionProps { docId: string; title: string; onOpen: (docId: string, blockId?: string) => void; } const COLLAPSED_KEY = 'mindsift:backlinks_collapsed'; // Shows the [[Wiki-Link]] parts of a block in the accent color, the rest as plain text const Snippet: React.FC<{ text: string }> = ({ text }) => ( <> {text.split(/(\[\[[^\]]*\]\])/g).map((part, i) => part.startsWith('[[') && part.endsWith(']]') ? ( {part} ) : ( {part} ) )} ); // "Verlinkt auf diese Notiz" at the end of a note. Hidden while nothing links here; reloads shortly // after the title changes, since links are matched by title. export const BacklinksSection: React.FC = ({ docId, title, onOpen }) => { const [links, setLinks] = useState([]); const [collapsed, setCollapsed] = useState(() => { try { return localStorage.getItem(COLLAPSED_KEY) === 'true'; } catch { return false; } }); useEffect(() => { let cancelled = false; const timer = setTimeout(() => { api .getBacklinks(docId) .then((res) => !cancelled && setLinks(res)) .catch(() => !cancelled && setLinks([])); }, 400); return () => { cancelled = true; clearTimeout(timer); }; }, [docId, title]); if (links.length === 0) return null; const toggle = () => { setCollapsed((prev) => { try { localStorage.setItem(COLLAPSED_KEY, String(!prev)); } catch {} return !prev; }); }; return (
{!collapsed && (
    {links.map((l) => (
    • {l.blocks.map((b) => (
    • ))}
    {!!l.more &&

    … und {l.more} weitere Stellen

    }
  • ))}
)}
); };