|
| 1 | +import { nextApiRoot } from "@repo/utils/execContext"; |
| 2 | +import { App, Modal, Notice, requestUrl, setIcon } from "obsidian"; |
| 3 | +import { createRoot, Root } from "react-dom/client"; |
| 4 | +import { StrictMode, useState, useRef, useEffect } from "react"; |
| 5 | +import type DiscourseGraphPlugin from "~/index"; |
| 6 | + |
| 7 | +const FEEDBACK_ENDPOINT = `${nextApiRoot()}/feedback`; |
| 8 | + |
| 9 | +const FEEDBACK_TYPES = ["feedback", "bugReport", "featureRequest"] as const; |
| 10 | + |
| 11 | +type FeedbackType = (typeof FEEDBACK_TYPES)[number]; |
| 12 | + |
| 13 | +const FEEDBACK_TYPE_LABELS: Record<FeedbackType, string> = { |
| 14 | + feedback: "Feedback", |
| 15 | + bugReport: "Bug report", |
| 16 | + featureRequest: "Feature request", |
| 17 | +}; |
| 18 | + |
| 19 | +const isFeedbackType = (value: string): value is FeedbackType => |
| 20 | + (FEEDBACK_TYPES as readonly string[]).includes(value); |
| 21 | + |
| 22 | +const readFileAsBase64 = (file: File): Promise<string> => |
| 23 | + new Promise((resolve, reject) => { |
| 24 | + const reader = new FileReader(); |
| 25 | + reader.onload = () => { |
| 26 | + const base64 = (reader.result as string).split(",")[1]; |
| 27 | + if (!base64) { |
| 28 | + reject(new Error("Failed to read file as base64")); |
| 29 | + return; |
| 30 | + } |
| 31 | + resolve(base64); |
| 32 | + }; |
| 33 | + reader.onerror = reject; |
| 34 | + reader.readAsDataURL(file); |
| 35 | + }); |
| 36 | + |
| 37 | +const fieldClass = |
| 38 | + "w-full bg-modifier-form-field border border-modifier-border rounded text-normal px-3 py-2 text-sm box-border"; |
| 39 | + |
| 40 | +const selectClass = `${fieldClass} appearance-none h-9 leading-none`; |
| 41 | + |
| 42 | +const accentButtonClass = |
| 43 | + "flex items-center justify-center gap-1.5 bg-accent text-on-accent rounded py-2.5 px-3 text-sm font-medium border-none cursor-pointer hover:opacity-90"; |
| 44 | + |
| 45 | +const isValidEmail = (value: string) => |
| 46 | + /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value); |
| 47 | + |
| 48 | +type FeedbackContentProps = { |
| 49 | + plugin: DiscourseGraphPlugin; |
| 50 | + onClose: () => void; |
| 51 | +}; |
| 52 | + |
| 53 | +const FeedbackContent = ({ plugin, onClose }: FeedbackContentProps) => { |
| 54 | + const [email, setEmail] = useState(""); |
| 55 | + const [title, setTitle] = useState(""); |
| 56 | + const [description, setDescription] = useState(""); |
| 57 | + const [feedbackType, setFeedbackType] = useState<FeedbackType>("bugReport"); |
| 58 | + const [screenshot, setScreenshot] = useState<File | null>(null); |
| 59 | + const [previewUrl, setPreviewUrl] = useState<string | null>(null); |
| 60 | + const [emailError, setEmailError] = useState<string | null>(null); |
| 61 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 62 | + const fileInputRef = useRef<HTMLInputElement>(null); |
| 63 | + |
| 64 | + const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 65 | + const value = e.target.value; |
| 66 | + setEmail(value); |
| 67 | + if (emailError && (!value.trim() || isValidEmail(value.trim()))) { |
| 68 | + setEmailError(null); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + const handleEmailBlur = () => { |
| 73 | + const trimmed = email.trim(); |
| 74 | + if (trimmed && !isValidEmail(trimmed)) { |
| 75 | + setEmailError("Please enter a valid email address."); |
| 76 | + } else { |
| 77 | + setEmailError(null); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + if (!screenshot) { |
| 83 | + setPreviewUrl(null); |
| 84 | + return; |
| 85 | + } |
| 86 | + const url = URL.createObjectURL(screenshot); |
| 87 | + setPreviewUrl(url); |
| 88 | + return () => URL.revokeObjectURL(url); |
| 89 | + }, [screenshot]); |
| 90 | + |
| 91 | + const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 92 | + setScreenshot(e.target.files?.[0] ?? null); |
| 93 | + }; |
| 94 | + |
| 95 | + const removeScreenshot = () => { |
| 96 | + setScreenshot(null); |
| 97 | + if (fileInputRef.current) fileInputRef.current.value = ""; |
| 98 | + }; |
| 99 | + |
| 100 | + const isSubmittable = |
| 101 | + title.trim().length > 0 && |
| 102 | + email.trim().length > 0 && |
| 103 | + isValidEmail(email.trim()) && |
| 104 | + !emailError; |
| 105 | + |
| 106 | + const handleSubmit = async () => { |
| 107 | + if (!isSubmittable) return; |
| 108 | + |
| 109 | + setIsSubmitting(true); |
| 110 | + try { |
| 111 | + const payload: { |
| 112 | + email?: string; |
| 113 | + title: string; |
| 114 | + description?: string; |
| 115 | + type: FeedbackType; |
| 116 | + screenshot?: { data: string; mimeType: string; name: string }; |
| 117 | + pluginVersion: string; |
| 118 | + } = { |
| 119 | + email: email.trim() || undefined, |
| 120 | + title: title.trim(), |
| 121 | + description: description.trim() || undefined, |
| 122 | + type: feedbackType, |
| 123 | + pluginVersion: plugin.manifest.version, |
| 124 | + }; |
| 125 | + |
| 126 | + if (screenshot) { |
| 127 | + payload.screenshot = { |
| 128 | + data: await readFileAsBase64(screenshot), |
| 129 | + mimeType: screenshot.type, |
| 130 | + name: screenshot.name, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + const response = await requestUrl({ |
| 135 | + url: FEEDBACK_ENDPOINT, |
| 136 | + method: "POST", |
| 137 | + headers: { "Content-Type": "application/json" }, |
| 138 | + body: JSON.stringify(payload), |
| 139 | + }); |
| 140 | + |
| 141 | + if (response.status >= 200 && response.status < 300) { |
| 142 | + new Notice("Feedback submitted — thank you!"); |
| 143 | + onClose(); |
| 144 | + } else { |
| 145 | + new Notice("Failed to submit feedback. Please try again."); |
| 146 | + } |
| 147 | + } catch (err) { |
| 148 | + console.error("[FeedbackModal] request failed:", err); |
| 149 | + new Notice("Failed to submit feedback. Please check your connection."); |
| 150 | + } finally { |
| 151 | + setIsSubmitting(false); |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + return ( |
| 156 | + <div className="flex flex-col gap-3 pb-2"> |
| 157 | + {/* Screenshot button */} |
| 158 | + <input |
| 159 | + ref={fileInputRef} |
| 160 | + type="file" |
| 161 | + accept="image/*" |
| 162 | + className="hidden" |
| 163 | + onChange={handleFileChange} |
| 164 | + /> |
| 165 | + <button |
| 166 | + onClick={() => fileInputRef.current?.click()} |
| 167 | + className={`${accentButtonClass} w-full`} |
| 168 | + > |
| 169 | + <span |
| 170 | + ref={(el) => { |
| 171 | + if (el) setIcon(el, "image"); |
| 172 | + }} |
| 173 | + className="inline-flex items-center" |
| 174 | + aria-hidden |
| 175 | + /> |
| 176 | + Take screenshot |
| 177 | + </button> |
| 178 | + |
| 179 | + {/* Screenshot preview */} |
| 180 | + {screenshot && previewUrl && ( |
| 181 | + <div className="bg-modifier-form-field border-modifier-border flex items-center gap-2 rounded border p-2"> |
| 182 | + <img |
| 183 | + src={previewUrl} |
| 184 | + alt="Screenshot preview" |
| 185 | + className="h-12 w-12 flex-shrink-0 rounded object-cover" |
| 186 | + /> |
| 187 | + <span className="text-muted flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-xs"> |
| 188 | + {screenshot.name} |
| 189 | + </span> |
| 190 | + <button |
| 191 | + onClick={removeScreenshot} |
| 192 | + aria-label="Remove screenshot" |
| 193 | + className="text-muted flex-shrink-0 cursor-pointer border-none bg-transparent p-0.5 hover:opacity-70" |
| 194 | + > |
| 195 | + <span |
| 196 | + ref={(el) => { |
| 197 | + if (el) setIcon(el, "x"); |
| 198 | + }} |
| 199 | + className="inline-flex items-center" |
| 200 | + aria-hidden |
| 201 | + /> |
| 202 | + </button> |
| 203 | + </div> |
| 204 | + )} |
| 205 | + |
| 206 | + {/* Email */} |
| 207 | + <div className="flex flex-col gap-1"> |
| 208 | + <label className="text-normal text-xs font-medium"> |
| 209 | + Email <span className="text-error">*</span> |
| 210 | + </label> |
| 211 | + <input |
| 212 | + type="email" |
| 213 | + value={email} |
| 214 | + onChange={handleEmailChange} |
| 215 | + onBlur={handleEmailBlur} |
| 216 | + placeholder="your@email.com" |
| 217 | + className={`${fieldClass} ${emailError ? "border-error" : ""}`} |
| 218 | + /> |
| 219 | + {emailError && <span className="text-error text-xs">{emailError}</span>} |
| 220 | + </div> |
| 221 | + |
| 222 | + {/* Title */} |
| 223 | + <div className="flex flex-col gap-1"> |
| 224 | + <label className="text-normal text-xs font-medium"> |
| 225 | + Title <span className="text-error">*</span> |
| 226 | + </label> |
| 227 | + <input |
| 228 | + type="text" |
| 229 | + value={title} |
| 230 | + onChange={(e) => setTitle(e.target.value)} |
| 231 | + placeholder="Add a title" |
| 232 | + className={fieldClass} |
| 233 | + /> |
| 234 | + </div> |
| 235 | + |
| 236 | + {/* Description */} |
| 237 | + <div className="flex flex-col gap-1"> |
| 238 | + <label className="text-normal text-xs font-medium"> |
| 239 | + Description <span className="text-muted font-normal">(optional)</span> |
| 240 | + </label> |
| 241 | + <textarea |
| 242 | + value={description} |
| 243 | + onChange={(e) => setDescription(e.target.value)} |
| 244 | + placeholder="Add a description" |
| 245 | + rows={4} |
| 246 | + className={`${fieldClass} resize-y`} |
| 247 | + /> |
| 248 | + </div> |
| 249 | + |
| 250 | + {/* Feedback type */} |
| 251 | + <select |
| 252 | + value={feedbackType} |
| 253 | + onChange={(e) => { |
| 254 | + if (isFeedbackType(e.target.value)) { |
| 255 | + setFeedbackType(e.target.value); |
| 256 | + } |
| 257 | + }} |
| 258 | + className={selectClass} |
| 259 | + > |
| 260 | + {FEEDBACK_TYPES.map((type) => ( |
| 261 | + <option key={type} value={type}> |
| 262 | + {FEEDBACK_TYPE_LABELS[type]} |
| 263 | + </option> |
| 264 | + ))} |
| 265 | + </select> |
| 266 | + |
| 267 | + {/* Submit */} |
| 268 | + <div className="flex justify-end"> |
| 269 | + <button |
| 270 | + onClick={() => void handleSubmit()} |
| 271 | + disabled={isSubmitting || !isSubmittable} |
| 272 | + className={`${accentButtonClass} px-5 disabled:cursor-not-allowed disabled:opacity-40`} |
| 273 | + > |
| 274 | + {isSubmitting ? ( |
| 275 | + "Submitting…" |
| 276 | + ) : ( |
| 277 | + <> |
| 278 | + Submit |
| 279 | + <span |
| 280 | + ref={(el) => { |
| 281 | + if (el) setIcon(el, "send"); |
| 282 | + }} |
| 283 | + className="inline-flex items-center" |
| 284 | + aria-hidden |
| 285 | + /> |
| 286 | + </> |
| 287 | + )} |
| 288 | + </button> |
| 289 | + </div> |
| 290 | + </div> |
| 291 | + ); |
| 292 | +}; |
| 293 | + |
| 294 | +export class FeedbackModal extends Modal { |
| 295 | + private plugin: DiscourseGraphPlugin; |
| 296 | + private root: Root | null = null; |
| 297 | + |
| 298 | + constructor(app: App, plugin: DiscourseGraphPlugin) { |
| 299 | + super(app); |
| 300 | + this.plugin = plugin; |
| 301 | + } |
| 302 | + |
| 303 | + onOpen(): void { |
| 304 | + this.titleEl.setText("Discourse Graphs feedback"); |
| 305 | + const { contentEl } = this; |
| 306 | + contentEl.empty(); |
| 307 | + this.root = createRoot(contentEl); |
| 308 | + this.root.render( |
| 309 | + <StrictMode> |
| 310 | + <FeedbackContent plugin={this.plugin} onClose={() => this.close()} /> |
| 311 | + </StrictMode>, |
| 312 | + ); |
| 313 | + } |
| 314 | + |
| 315 | + onClose(): void { |
| 316 | + if (this.root) { |
| 317 | + this.root.unmount(); |
| 318 | + this.root = null; |
| 319 | + } |
| 320 | + } |
| 321 | +} |
0 commit comments