-
Notifications
You must be signed in to change notification settings - Fork 897
feat(docs): DR-7574 Add page feedback widget #7613
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
Open
ArthurGamby
wants to merge
2
commits into
main
Choose a base branch
from
dr-7574-docs-add-page-feedback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+136
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,133 @@ | ||
| "use client"; | ||
|
|
||
| import { useState, useEffect, useCallback } from "react"; | ||
| import { ThumbsUp, ThumbsDown, Send } from "lucide-react"; | ||
| import { cn } from "@prisma-docs/ui/lib/cn"; | ||
| import { usePathname } from "fumadocs-core/framework"; | ||
| import posthog from "posthog-js"; | ||
|
|
||
| type FeedbackState = "idle" | "upvoted" | "downvoted" | "submitted"; | ||
|
|
||
| function getStorageKey(path: string) { | ||
| return `prisma-docs:page-feedback:${path}`; | ||
| } | ||
|
|
||
| export function PageFeedback() { | ||
| const pathname = usePathname(); | ||
| const [state, setState] = useState<FeedbackState | null>(null); | ||
| const [comment, setComment] = useState(""); | ||
| const [showTextarea, setShowTextarea] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| try { | ||
| const stored = localStorage.getItem(getStorageKey(pathname)); | ||
| setState(stored ? "submitted" : "idle"); | ||
| } catch { | ||
| setState("idle"); | ||
| } | ||
| }, [pathname]); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const persist = useCallback( | ||
| (vote: "up" | "down", text?: string) => { | ||
| try { | ||
| localStorage.setItem(getStorageKey(pathname), JSON.stringify({ vote, text })); | ||
| } catch {} | ||
| posthog.capture("docs:page_feedback", { | ||
| path: pathname, | ||
| vote, | ||
| comment: text ?? null, | ||
| }); | ||
| }, | ||
| [pathname], | ||
| ); | ||
|
|
||
| const handleUp = () => { | ||
| setState("submitted"); | ||
| persist("up"); | ||
| }; | ||
|
|
||
| const handleDown = () => { | ||
| setState("downvoted"); | ||
| setShowTextarea(true); | ||
| }; | ||
|
|
||
| const handleSubmitComment = () => { | ||
| setState("submitted"); | ||
| persist("down", comment || undefined); | ||
| }; | ||
|
|
||
| if (state === null) return null; | ||
|
|
||
| if (state === "submitted") { | ||
| return ( | ||
| <div className="flex items-center gap-2 rounded-lg border border-fd-border bg-fd-card px-4 py-3 text-sm text-fd-muted-foreground"> | ||
| <span>Thanks for your feedback!</span> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-3 rounded-lg border border-fd-border bg-fd-card px-4 py-4"> | ||
| <div className="flex items-center justify-between"> | ||
| <span className="text-sm font-medium text-fd-foreground">Was this page helpful?</span> | ||
| <div className="flex items-center gap-1.5"> | ||
| <button | ||
| type="button" | ||
| onClick={handleUp} | ||
| className={cn( | ||
| "inline-flex items-center gap-1.5 rounded-md border border-fd-border px-3 py-1.5 text-sm transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground", | ||
| "text-fd-muted-foreground", | ||
| )} | ||
| > | ||
| <ThumbsUp className="size-3.5" /> | ||
| Yes | ||
| </button> | ||
| <button | ||
| type="button" | ||
| onClick={handleDown} | ||
| className={cn( | ||
| "inline-flex items-center gap-1.5 rounded-md border border-fd-border px-3 py-1.5 text-sm transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground", | ||
| showTextarea ? "bg-fd-accent text-fd-accent-foreground" : "text-fd-muted-foreground", | ||
| )} | ||
| > | ||
| <ThumbsDown className="size-3.5" /> | ||
| No | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| {showTextarea && ( | ||
| <div className="flex flex-col gap-2 animate-in fade-in slide-in-from-top-1 duration-200"> | ||
| <textarea | ||
| value={comment} | ||
| onChange={(e) => setComment(e.target.value)} | ||
| placeholder="What was missing or unclear? (optional)" | ||
| rows={3} | ||
| className="w-full resize-none rounded-md border border-fd-border bg-fd-background px-3 py-2 text-sm text-fd-foreground placeholder:text-fd-muted-foreground focus:outline-none focus:ring-2 focus:ring-fd-ring" | ||
| /> | ||
| <div className="flex justify-end gap-2"> | ||
| <button | ||
| type="button" | ||
| onClick={() => { | ||
| setShowTextarea(false); | ||
| setState("idle"); | ||
| setComment(""); | ||
| }} | ||
| className="inline-flex items-center rounded-md border border-fd-border px-3 py-1.5 text-sm font-medium text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground" | ||
| > | ||
| Cancel | ||
| </button> | ||
| <button | ||
| type="button" | ||
| onClick={handleSubmitComment} | ||
| className="inline-flex items-center gap-1.5 rounded-md bg-fd-primary px-3 py-1.5 text-sm font-medium text-fd-primary-foreground transition-colors hover:bg-fd-primary/90" | ||
| > | ||
| <Send className="size-3.5" /> | ||
| Send feedback | ||
| </button> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
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.