import { LinkIcon, MilestoneIcon } from "lucide-react"; import { observer } from "mobx-react-lite"; import { useEffect, useState } from "react"; import { cn } from "@/lib/utils"; import { memoStore } from "@/store"; import { Memo, MemoRelation, MemoRelation_Type } from "@/types/proto/api/v1/memo_service"; import { useTranslate } from "@/utils/i18n"; import MetadataCard from "./MetadataCard"; import RelationCard from "./RelationCard"; import { BaseMetadataProps } from "./types"; interface RelationListProps extends BaseMetadataProps { relations: MemoRelation[]; currentMemoName?: string; onRelationsChange?: (relations: MemoRelation[]) => void; parentPage?: string; } /** * Unified RelationList component for both editor and view modes * * Editor mode: * - Shows only outgoing relations (referencing) * - Badge-style display with remove buttons * - Compact inline layout * * View mode: * - Shows bidirectional relations in tabbed card * - "Referencing" tab: Memos this memo links to * - "Referenced by" tab: Memos that link to this memo * - Navigable links with memo IDs */ const RelationList = observer(({ relations, currentMemoName, mode, onRelationsChange, parentPage, className }: RelationListProps) => { const t = useTranslate(); const [referencingMemos, setReferencingMemos] = useState([]); const [selectedTab, setSelectedTab] = useState<"referencing" | "referenced">("referencing"); // Get referencing and referenced relations const referencingRelations = relations.filter( (relation) => relation.type === MemoRelation_Type.REFERENCE && (mode === "edit" || relation.memo?.name === currentMemoName) && relation.relatedMemo?.name !== currentMemoName, ); const referencedRelations = relations.filter( (relation) => relation.type === MemoRelation_Type.REFERENCE && relation.memo?.name !== currentMemoName && relation.relatedMemo?.name === currentMemoName, ); // Fetch full memo details for editor mode useEffect(() => { if (mode === "edit") { (async () => { if (referencingRelations.length > 0) { const requests = referencingRelations.map(async (relation) => { return await memoStore.getOrFetchMemoByName(relation.relatedMemo!.name, { skipStore: true }); }); const list = await Promise.all(requests); setReferencingMemos(list); } else { setReferencingMemos([]); } })(); } }, [mode, relations]); const handleDeleteRelation = (memoName: string) => { if (onRelationsChange) { onRelationsChange(relations.filter((relation) => relation.relatedMemo?.name !== memoName)); } }; // Editor mode: Simple badge list if (mode === "edit") { if (referencingMemos.length === 0) { return null; } return (
{referencingMemos.map((memo) => ( handleDeleteRelation(memo.name)} /> ))}
); } // View mode: Tabbed card with bidirectional relations if (referencingRelations.length === 0 && referencedRelations.length === 0) { return null; } // Auto-select tab based on which has content const activeTab = referencingRelations.length === 0 ? "referenced" : selectedTab; return ( {/* Tabs */}
{referencingRelations.length > 0 && ( )} {referencedRelations.length > 0 && ( )}
{/* Referencing List */} {activeTab === "referencing" && referencingRelations.length > 0 && (
{referencingRelations.map((relation) => ( ))}
)} {/* Referenced List */} {activeTab === "referenced" && referencedRelations.length > 0 && (
{referencedRelations.map((relation) => ( ))}
)}
); }); export default RelationList;