-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1886 Send feedback button in Obsidian #1181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+575
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0eeacb
ENG-1886 Add send feedback button in Obsidian
trangdoan982 eef45ae
Fix lint failures and stop prefilling email with username.
trangdoan982 46fa9d0
add comment
trangdoan982 d3e5a0c
Merge main into eng-1886-send-feedback-button-in-obsidian
trangdoan982 f925795
Address review feedback on feedback endpoint and modal.
trangdoan982 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,321 @@ | ||
| import { nextApiRoot } from "@repo/utils/execContext"; | ||
| import { App, Modal, Notice, requestUrl, setIcon } from "obsidian"; | ||
| import { createRoot, Root } from "react-dom/client"; | ||
| import { StrictMode, useState, useRef, useEffect } from "react"; | ||
| import type DiscourseGraphPlugin from "~/index"; | ||
|
|
||
| const FEEDBACK_ENDPOINT = `${nextApiRoot()}/feedback`; | ||
|
|
||
| const FEEDBACK_TYPES = ["feedback", "bugReport", "featureRequest"] as const; | ||
|
|
||
| type FeedbackType = (typeof FEEDBACK_TYPES)[number]; | ||
|
|
||
| const FEEDBACK_TYPE_LABELS: Record<FeedbackType, string> = { | ||
| feedback: "Feedback", | ||
| bugReport: "Bug report", | ||
| featureRequest: "Feature request", | ||
| }; | ||
|
|
||
| const isFeedbackType = (value: string): value is FeedbackType => | ||
| (FEEDBACK_TYPES as readonly string[]).includes(value); | ||
|
|
||
| const readFileAsBase64 = (file: File): Promise<string> => | ||
| new Promise((resolve, reject) => { | ||
| const reader = new FileReader(); | ||
| reader.onload = () => { | ||
| const base64 = (reader.result as string).split(",")[1]; | ||
| if (!base64) { | ||
| reject(new Error("Failed to read file as base64")); | ||
| return; | ||
| } | ||
| resolve(base64); | ||
| }; | ||
| reader.onerror = reject; | ||
| reader.readAsDataURL(file); | ||
| }); | ||
|
|
||
| const fieldClass = | ||
| "w-full bg-modifier-form-field border border-modifier-border rounded text-normal px-3 py-2 text-sm box-border"; | ||
|
|
||
| const selectClass = `${fieldClass} appearance-none h-9 leading-none`; | ||
|
|
||
| const accentButtonClass = | ||
| "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"; | ||
|
|
||
| const isValidEmail = (value: string) => | ||
| /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value); | ||
|
|
||
| type FeedbackContentProps = { | ||
| plugin: DiscourseGraphPlugin; | ||
| onClose: () => void; | ||
| }; | ||
|
|
||
| const FeedbackContent = ({ plugin, onClose }: FeedbackContentProps) => { | ||
| const [email, setEmail] = useState(""); | ||
| const [title, setTitle] = useState(""); | ||
| const [description, setDescription] = useState(""); | ||
| const [feedbackType, setFeedbackType] = useState<FeedbackType>("bugReport"); | ||
| const [screenshot, setScreenshot] = useState<File | null>(null); | ||
| const [previewUrl, setPreviewUrl] = useState<string | null>(null); | ||
| const [emailError, setEmailError] = useState<string | null>(null); | ||
| const [isSubmitting, setIsSubmitting] = useState(false); | ||
| const fileInputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| const value = e.target.value; | ||
| setEmail(value); | ||
| if (emailError && (!value.trim() || isValidEmail(value.trim()))) { | ||
| setEmailError(null); | ||
| } | ||
| }; | ||
|
|
||
| const handleEmailBlur = () => { | ||
| const trimmed = email.trim(); | ||
| if (trimmed && !isValidEmail(trimmed)) { | ||
| setEmailError("Please enter a valid email address."); | ||
| } else { | ||
| setEmailError(null); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (!screenshot) { | ||
| setPreviewUrl(null); | ||
| return; | ||
| } | ||
| const url = URL.createObjectURL(screenshot); | ||
| setPreviewUrl(url); | ||
| return () => URL.revokeObjectURL(url); | ||
| }, [screenshot]); | ||
|
|
||
| const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| setScreenshot(e.target.files?.[0] ?? null); | ||
| }; | ||
|
|
||
| const removeScreenshot = () => { | ||
| setScreenshot(null); | ||
| if (fileInputRef.current) fileInputRef.current.value = ""; | ||
| }; | ||
|
|
||
| const isSubmittable = | ||
| title.trim().length > 0 && | ||
| email.trim().length > 0 && | ||
| isValidEmail(email.trim()) && | ||
| !emailError; | ||
|
|
||
| const handleSubmit = async () => { | ||
| if (!isSubmittable) return; | ||
|
|
||
| setIsSubmitting(true); | ||
| try { | ||
| const payload: { | ||
| email?: string; | ||
| title: string; | ||
| description?: string; | ||
| type: FeedbackType; | ||
| screenshot?: { data: string; mimeType: string; name: string }; | ||
| pluginVersion: string; | ||
| } = { | ||
| email: email.trim() || undefined, | ||
| title: title.trim(), | ||
| description: description.trim() || undefined, | ||
| type: feedbackType, | ||
| pluginVersion: plugin.manifest.version, | ||
| }; | ||
|
|
||
| if (screenshot) { | ||
| payload.screenshot = { | ||
| data: await readFileAsBase64(screenshot), | ||
| mimeType: screenshot.type, | ||
| name: screenshot.name, | ||
| }; | ||
| } | ||
|
|
||
| const response = await requestUrl({ | ||
| url: FEEDBACK_ENDPOINT, | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify(payload), | ||
| }); | ||
|
|
||
| if (response.status >= 200 && response.status < 300) { | ||
| new Notice("Feedback submitted — thank you!"); | ||
| onClose(); | ||
| } else { | ||
| new Notice("Failed to submit feedback. Please try again."); | ||
| } | ||
| } catch (err) { | ||
| console.error("[FeedbackModal] request failed:", err); | ||
| new Notice("Failed to submit feedback. Please check your connection."); | ||
| } finally { | ||
| setIsSubmitting(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-3 pb-2"> | ||
| {/* Screenshot button */} | ||
| <input | ||
| ref={fileInputRef} | ||
| type="file" | ||
| accept="image/*" | ||
| className="hidden" | ||
| onChange={handleFileChange} | ||
| /> | ||
| <button | ||
| onClick={() => fileInputRef.current?.click()} | ||
| className={`${accentButtonClass} w-full`} | ||
| > | ||
| <span | ||
| ref={(el) => { | ||
| if (el) setIcon(el, "image"); | ||
| }} | ||
| className="inline-flex items-center" | ||
| aria-hidden | ||
| /> | ||
| Take screenshot | ||
| </button> | ||
|
|
||
| {/* Screenshot preview */} | ||
| {screenshot && previewUrl && ( | ||
| <div className="bg-modifier-form-field border-modifier-border flex items-center gap-2 rounded border p-2"> | ||
| <img | ||
| src={previewUrl} | ||
| alt="Screenshot preview" | ||
| className="h-12 w-12 flex-shrink-0 rounded object-cover" | ||
| /> | ||
| <span className="text-muted flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-xs"> | ||
| {screenshot.name} | ||
| </span> | ||
| <button | ||
| onClick={removeScreenshot} | ||
| aria-label="Remove screenshot" | ||
| className="text-muted flex-shrink-0 cursor-pointer border-none bg-transparent p-0.5 hover:opacity-70" | ||
| > | ||
| <span | ||
| ref={(el) => { | ||
| if (el) setIcon(el, "x"); | ||
| }} | ||
| className="inline-flex items-center" | ||
| aria-hidden | ||
| /> | ||
| </button> | ||
| </div> | ||
| )} | ||
|
|
||
| {/* Email */} | ||
| <div className="flex flex-col gap-1"> | ||
| <label className="text-normal text-xs font-medium"> | ||
| Email <span className="text-error">*</span> | ||
| </label> | ||
| <input | ||
| type="email" | ||
| value={email} | ||
| onChange={handleEmailChange} | ||
| onBlur={handleEmailBlur} | ||
| placeholder="your@email.com" | ||
| className={`${fieldClass} ${emailError ? "border-error" : ""}`} | ||
| /> | ||
| {emailError && <span className="text-error text-xs">{emailError}</span>} | ||
| </div> | ||
|
|
||
| {/* Title */} | ||
| <div className="flex flex-col gap-1"> | ||
| <label className="text-normal text-xs font-medium"> | ||
| Title <span className="text-error">*</span> | ||
| </label> | ||
| <input | ||
| type="text" | ||
| value={title} | ||
| onChange={(e) => setTitle(e.target.value)} | ||
| placeholder="Add a title" | ||
| className={fieldClass} | ||
| /> | ||
| </div> | ||
|
|
||
| {/* Description */} | ||
| <div className="flex flex-col gap-1"> | ||
| <label className="text-normal text-xs font-medium"> | ||
| Description <span className="text-muted font-normal">(optional)</span> | ||
| </label> | ||
| <textarea | ||
| value={description} | ||
| onChange={(e) => setDescription(e.target.value)} | ||
| placeholder="Add a description" | ||
| rows={4} | ||
| className={`${fieldClass} resize-y`} | ||
| /> | ||
| </div> | ||
|
|
||
| {/* Feedback type */} | ||
| <select | ||
| value={feedbackType} | ||
| onChange={(e) => { | ||
| if (isFeedbackType(e.target.value)) { | ||
| setFeedbackType(e.target.value); | ||
| } | ||
| }} | ||
| className={selectClass} | ||
| > | ||
| {FEEDBACK_TYPES.map((type) => ( | ||
| <option key={type} value={type}> | ||
| {FEEDBACK_TYPE_LABELS[type]} | ||
| </option> | ||
| ))} | ||
| </select> | ||
|
|
||
| {/* Submit */} | ||
| <div className="flex justify-end"> | ||
| <button | ||
| onClick={() => void handleSubmit()} | ||
| disabled={isSubmitting || !isSubmittable} | ||
| className={`${accentButtonClass} px-5 disabled:cursor-not-allowed disabled:opacity-40`} | ||
| > | ||
| {isSubmitting ? ( | ||
| "Submitting…" | ||
| ) : ( | ||
| <> | ||
| Submit | ||
| <span | ||
| ref={(el) => { | ||
| if (el) setIcon(el, "send"); | ||
| }} | ||
| className="inline-flex items-center" | ||
| aria-hidden | ||
| /> | ||
| </> | ||
| )} | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export class FeedbackModal extends Modal { | ||
| private plugin: DiscourseGraphPlugin; | ||
| private root: Root | null = null; | ||
|
|
||
| constructor(app: App, plugin: DiscourseGraphPlugin) { | ||
| super(app); | ||
| this.plugin = plugin; | ||
| } | ||
|
|
||
| onOpen(): void { | ||
| this.titleEl.setText("Discourse Graphs feedback"); | ||
| const { contentEl } = this; | ||
| contentEl.empty(); | ||
| this.root = createRoot(contentEl); | ||
| this.root.render( | ||
| <StrictMode> | ||
| <FeedbackContent plugin={this.plugin} onClose={() => this.close()} /> | ||
| </StrictMode>, | ||
| ); | ||
| } | ||
|
|
||
| onClose(): void { | ||
| if (this.root) { | ||
| this.root.unmount(); | ||
| this.root = null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.