Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions apps/obsidian/src/components/RelationshipSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ const CurrentRelationships = ({ activeFile }: RelationshipSectionProps) => {
};
}, [activeFile, plugin]);

const loadCurrentRelationships = async () => {
const loadCurrentRelationships = useCallback(async () => {
const fileCache = plugin.app.metadataCache.getFileCache(activeFile);
if (!fileCache?.frontmatter) return;

Expand Down Expand Up @@ -423,7 +423,72 @@ const CurrentRelationships = ({ activeFile }: RelationshipSectionProps) => {
}

setGroupedRelationships(Array.from(tempRelationships.values()));
};
}, [activeFile, plugin]);

const deleteRelationship = useCallback(
async (linkedFile: TFile, relationTypeId: string) => {
const relationType = plugin.settings.relationTypes.find(
(r) => r.id === relationTypeId,
);
if (!relationType) return;

try {
const removeLinkFromFrontmatter = async (
file: TFile,
targetFileName: string,
relationTypeId: string,
) => {
await plugin.app.fileManager.processFrontMatter(file, (fm) => {
const existingLinks = Array.isArray(fm[relationTypeId])
? fm[relationTypeId]
: [fm[relationTypeId]].filter(Boolean);

const linkToRemove = `[[${targetFileName}]]`;
const filteredLinks = existingLinks.filter(
(link) => link !== linkToRemove,
);

if (filteredLinks.length === 0) {
delete fm[relationTypeId];
} else {
fm[relationTypeId] = filteredLinks;
}
});
};

// Remove link from active file
await removeLinkFromFrontmatter(
activeFile,
linkedFile.name,
relationTypeId,
);

// Remove reverse link from linked file
await removeLinkFromFrontmatter(
linkedFile,
activeFile.name,
relationTypeId,
);

new Notice(
`Successfully removed ${relationType.label} with ${linkedFile.basename}`,
);

loadCurrentRelationships();
} catch (error) {
console.error("Failed to delete relationship:", error);
new Notice(
`Failed to delete relationship: ${error instanceof Error ? error.message : "Unknown error"}`,
);
}
},
[
activeFile,
plugin.app.fileManager,
plugin.settings.relationTypes,
loadCurrentRelationships,
],
);

if (groupedRelationships.length === 0) return null;

Expand All @@ -445,10 +510,10 @@ const CurrentRelationships = ({ activeFile }: RelationshipSectionProps) => {

<ul className="m-0 ml-6 list-none p-0">
{group.linkedFiles.map((file) => (
<li key={file.path} className="mt-1">
<li key={file.path} className="mt-1 flex items-center gap-2">
<a
href="#"
className="text-accent-text"
className="text-accent-text flex-1"
onClick={(e) => {
e.preventDefault();
plugin.app.workspace.openLinkText(
Expand All @@ -459,6 +524,16 @@ const CurrentRelationships = ({ activeFile }: RelationshipSectionProps) => {
>
{file.basename}
</a>
<button
className="!text-muted hover:!text-error cursor-pointer border-0 !bg-transparent p-1 text-sm"
onClick={(e) => {
e.preventDefault();
deleteRelationship(file, group.relationTypeOptions.id);
}}
title="Delete relationship"
>
×
</button>
</li>
))}
</ul>
Expand Down
4 changes: 2 additions & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
"inputs": ["$TURBO_DEFAULT$", ".env*"]
},
"roam#dev": {
"with": ["@repo/database#dev"]
"dependsOn": ["@repo/database#dev"]
},
"website#dev": {
"with": ["@repo/database#dev"]
"dependsOn": ["@repo/database#dev"]
},
"genenv": {
"env": ["SUPABASE_USE_DB"],
Expand Down