|
| 1 | +import { App, Modal, Notice } from "obsidian"; |
| 2 | +import { createRoot, Root } from "react-dom/client"; |
| 3 | +import { StrictMode, useState, useEffect, useRef } from "react"; |
| 4 | +import { DiscourseNode } from "~/types"; |
| 5 | +import type DiscourseGraphPlugin from "~/index"; |
| 6 | + |
| 7 | +type CreateNodeFormProps = { |
| 8 | + nodeTypes: DiscourseNode[]; |
| 9 | + plugin: DiscourseGraphPlugin; |
| 10 | + onNodeCreate: (nodeType: DiscourseNode, title: string) => Promise<void>; |
| 11 | + onCancel: () => void; |
| 12 | +}; |
| 13 | + |
| 14 | +export function CreateNodeForm({ |
| 15 | + nodeTypes, |
| 16 | + plugin, |
| 17 | + onNodeCreate, |
| 18 | + onCancel, |
| 19 | +}: CreateNodeFormProps) { |
| 20 | + const [title, setTitle] = useState(""); |
| 21 | + const [selectedNodeType, setSelectedNodeType] = |
| 22 | + useState<DiscourseNode | null>(null); |
| 23 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 24 | + const titleInputRef = useRef<HTMLInputElement>(null); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + // Focus the title input when component mounts |
| 28 | + setTimeout(() => { |
| 29 | + titleInputRef.current?.focus(); |
| 30 | + }, 50); |
| 31 | + }, []); |
| 32 | + |
| 33 | + const isFormValid = title.trim() && selectedNodeType; |
| 34 | + |
| 35 | + const handleKeyDown = (e: React.KeyboardEvent) => { |
| 36 | + if (e.key === "Enter" && !e.shiftKey) { |
| 37 | + e.preventDefault(); |
| 38 | + handleConfirm(); |
| 39 | + } else if (e.key === "Escape") { |
| 40 | + e.preventDefault(); |
| 41 | + onCancel(); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + const handleNodeTypeChange = (e: React.ChangeEvent<HTMLSelectElement>) => { |
| 46 | + const selectedId = e.target.value; |
| 47 | + setSelectedNodeType(nodeTypes.find((nt) => nt.id === selectedId) || null); |
| 48 | + }; |
| 49 | + |
| 50 | + const handleConfirm = async () => { |
| 51 | + if (!isFormValid || isSubmitting) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const trimmedTitle = title.trim(); |
| 56 | + if (!trimmedTitle) { |
| 57 | + new Notice("Please enter a title", 3000); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (!selectedNodeType) { |
| 62 | + new Notice("Please select a node type", 3000); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + setIsSubmitting(true); |
| 68 | + await onNodeCreate(selectedNodeType, trimmedTitle); |
| 69 | + onCancel(); // Close the modal on success |
| 70 | + } catch (error) { |
| 71 | + console.error("Error creating node:", error); |
| 72 | + new Notice( |
| 73 | + `Error creating node: ${error instanceof Error ? error.message : String(error)}`, |
| 74 | + 5000, |
| 75 | + ); |
| 76 | + } finally { |
| 77 | + setIsSubmitting(false); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <div> |
| 83 | + <h2>Create Discourse Node</h2> |
| 84 | + |
| 85 | + <div className="setting-item"> |
| 86 | + <div className="setting-item-name">Title</div> |
| 87 | + <div className="setting-item-control"> |
| 88 | + <input |
| 89 | + ref={titleInputRef} |
| 90 | + type="text" |
| 91 | + placeholder="Title" |
| 92 | + value={title} |
| 93 | + onChange={(e) => setTitle(e.target.value)} |
| 94 | + onKeyDown={handleKeyDown} |
| 95 | + disabled={isSubmitting} |
| 96 | + className="resize-vertical font-inherit border-background-modifier-border bg-background-primary text-text-normal max-h-[6em] min-h-[2.5em] w-full overflow-y-auto rounded-md border p-2" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="setting-item"> |
| 102 | + <div className="setting-item-name">Type</div> |
| 103 | + <div className="setting-item-control"> |
| 104 | + <select |
| 105 | + value={selectedNodeType?.id || ""} |
| 106 | + onChange={handleNodeTypeChange} |
| 107 | + disabled={isSubmitting} |
| 108 | + className="w-full" |
| 109 | + > |
| 110 | + <option value="">Select node type</option> |
| 111 | + {nodeTypes.map((nodeType) => ( |
| 112 | + <option key={nodeType.id} value={nodeType.id}> |
| 113 | + {nodeType.name} |
| 114 | + </option> |
| 115 | + ))} |
| 116 | + </select> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + |
| 120 | + <div |
| 121 | + className="modal-button-container" |
| 122 | + style={{ |
| 123 | + display: "flex", |
| 124 | + justifyContent: "flex-end", |
| 125 | + gap: "8px", |
| 126 | + marginTop: "20px", |
| 127 | + }} |
| 128 | + > |
| 129 | + <button |
| 130 | + type="button" |
| 131 | + className="mod-normal" |
| 132 | + onClick={onCancel} |
| 133 | + disabled={isSubmitting} |
| 134 | + > |
| 135 | + Cancel |
| 136 | + </button> |
| 137 | + <button |
| 138 | + type="button" |
| 139 | + className="mod-cta" |
| 140 | + onClick={handleConfirm} |
| 141 | + disabled={!isFormValid || isSubmitting} |
| 142 | + > |
| 143 | + {isSubmitting ? "Creating..." : "Confirm"} |
| 144 | + </button> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +type CreateNodeModalProps = { |
| 151 | + nodeTypes: DiscourseNode[]; |
| 152 | + plugin: DiscourseGraphPlugin; |
| 153 | + onNodeCreate: (nodeType: DiscourseNode, title: string) => Promise<void>; |
| 154 | +}; |
| 155 | + |
| 156 | +export class CreateNodeModal extends Modal { |
| 157 | + private nodeTypes: DiscourseNode[]; |
| 158 | + private plugin: DiscourseGraphPlugin; |
| 159 | + private onNodeCreate: ( |
| 160 | + nodeType: DiscourseNode, |
| 161 | + title: string, |
| 162 | + ) => Promise<void>; |
| 163 | + private root: Root | null = null; |
| 164 | + |
| 165 | + constructor(app: App, props: CreateNodeModalProps) { |
| 166 | + super(app); |
| 167 | + this.nodeTypes = props.nodeTypes; |
| 168 | + this.plugin = props.plugin; |
| 169 | + this.onNodeCreate = props.onNodeCreate; |
| 170 | + } |
| 171 | + |
| 172 | + onOpen() { |
| 173 | + const { contentEl } = this; |
| 174 | + contentEl.empty(); |
| 175 | + |
| 176 | + this.root = createRoot(contentEl); |
| 177 | + this.root.render( |
| 178 | + <StrictMode> |
| 179 | + <CreateNodeForm |
| 180 | + nodeTypes={this.nodeTypes} |
| 181 | + plugin={this.plugin} |
| 182 | + onNodeCreate={this.onNodeCreate} |
| 183 | + onCancel={() => this.close()} |
| 184 | + /> |
| 185 | + </StrictMode>, |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + onClose() { |
| 190 | + if (this.root) { |
| 191 | + this.root.unmount(); |
| 192 | + this.root = null; |
| 193 | + } |
| 194 | + const { contentEl } = this; |
| 195 | + contentEl.empty(); |
| 196 | + } |
| 197 | +} |
0 commit comments