Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type DiscourseGraphPlugin from "../index";
import { BulkImportCandidate, BulkImportPattern } from "~/types";
import { QueryEngine } from "~/services/QueryEngine";
import { TFile } from "obsidian";
import { getNodeTypeById } from "~/utils/utils";

type BulkImportModalProps = {
plugin: DiscourseGraphPlugin;
Expand Down Expand Up @@ -216,9 +217,7 @@ const BulkImportContent = ({ plugin, onClose }: BulkImportModalProps) => {
<div className="mb-6 h-80 overflow-y-auto rounded border p-4">
<div className="flex flex-col gap-4">
{patterns.map((pattern, index) => {
const nodeType = plugin.settings.nodeTypes.find(
(n) => n.id === pattern.nodeTypeId,
);
const nodeType = getNodeTypeById(plugin, pattern.nodeTypeId);
return (
<div key={pattern.nodeTypeId} className="rounded border p-3">
<div
Expand Down
5 changes: 2 additions & 3 deletions apps/obsidian/src/components/DiscourseContextView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getDiscourseNodeFormatExpression } from "~/utils/getDiscourseNodeFormat
import { RelationshipSection } from "~/components/RelationshipSection";
import { VIEW_TYPE_DISCOURSE_CONTEXT } from "~/types";
import { PluginProvider, usePlugin } from "~/components/PluginContext";
import { getNodeTypeById } from "~/utils/utils";

type DiscourseContextProps = {
activeFile: TFile | null;
Expand Down Expand Up @@ -39,9 +40,7 @@ const DiscourseContext = ({ activeFile }: DiscourseContextProps) => {
return <div>Not a discourse node (no nodeTypeId)</div>;
}

const nodeType = plugin.settings.nodeTypes.find(
(type) => type.id === frontmatter.nodeTypeId,
);
const nodeType = getNodeTypeById(plugin, frontmatter.nodeTypeId);

if (!nodeType) {
return <div>Unknown node type: {frontmatter.nodeTypeId}</div>;
Expand Down
8 changes: 2 additions & 6 deletions apps/obsidian/src/components/RelationshipSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SearchBar from "./SearchBar";
import { DiscourseNode } from "~/types";
import DropdownSelect from "./DropdownSelect";
import { usePlugin } from "./PluginContext";
import { getNodeTypeById } from "~/utils/utils";

type RelationTypeOption = {
id: string;
Expand Down Expand Up @@ -60,12 +61,7 @@ const AddRelationship = ({ activeFile }: RelationshipSectionProps) => {
);

const compatibleNodeTypes = compatibleNodeTypeIds
.map((id) => {
const nodeType = plugin.settings.nodeTypes.find(
(type) => type.id === id,
);
return nodeType;
})
.map((id) => getNodeTypeById(plugin, id))
.filter(Boolean) as DiscourseNode[];

setCompatibleNodeTypes(compatibleNodeTypes);
Expand Down
21 changes: 7 additions & 14 deletions apps/obsidian/src/components/RelationshipSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { useState } from "react";
import {
DiscourseRelation,
DiscourseNode,
DiscourseRelationType,
} from "~/types";
import { DiscourseRelation, DiscourseRelationType } from "~/types";
import { Notice } from "obsidian";
import { usePlugin } from "./PluginContext";
import { ConfirmationModal } from "./ConfirmationModal";
import { getNodeTypeById } from "~/utils/utils";

const RelationshipSettings = () => {
const plugin = usePlugin();
Expand All @@ -15,10 +12,6 @@ const RelationshipSettings = () => {
>(() => plugin.settings.discourseRelations ?? []);
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);

const findNodeById = (id: string): DiscourseNode | undefined => {
return plugin.settings.nodeTypes.find((node) => node.id === id);
};

const findRelationTypeById = (
id: string,
): DiscourseRelationType | undefined => {
Expand Down Expand Up @@ -72,8 +65,8 @@ const RelationshipSettings = () => {
relation.destinationId &&
relation.relationshipTypeId
) {
const sourceNode = findNodeById(relation.sourceId);
const targetNode = findNodeById(relation.destinationId);
const sourceNode = getNodeTypeById(plugin, relation.sourceId);
const targetNode = getNodeTypeById(plugin, relation.destinationId);
const relationType = findRelationTypeById(relation.relationshipTypeId);

if (sourceNode && targetNode && relationType) {
Expand Down Expand Up @@ -202,7 +195,7 @@ const RelationshipSettings = () => {
<div className="text-normal mt-2 p-2">
<div className="flex items-center justify-between">
<div className="flex-1">
{findNodeById(relation.sourceId)?.name ||
{getNodeTypeById(plugin, relation.sourceId)?.name ||
"Unknown Node"}
</div>

Expand All @@ -224,8 +217,8 @@ const RelationshipSettings = () => {
</div>

<div className="flex-1 text-right">
{findNodeById(relation.destinationId)?.name ||
"Unknown Node"}
{getNodeTypeById(plugin, relation.destinationId)
?.name || "Unknown Node"}
</div>
</div>
</div>
Expand Down
Loading