-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Add feedback component. #2852
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
bgravenorst
wants to merge
13
commits into
main
Choose a base branch
from
add-feedback-widget
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.
Open
Add feedback component. #2852
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
605c479
Add feedback component.
bgravenorst 70eb0ce
Add to Quickstart and Tutorials.
bgravenorst 68e1d8c
Append to new line.
bgravenorst b0c0d44
Fix code error.
bgravenorst d6af042
Remove dead code.
bgravenorst b63827d
Fix cursor error.
bgravenorst 1b2c0fa
Run prettier.
bgravenorst d91765c
Cursor issue.
bgravenorst 66d6607
Merge branch 'main' into add-feedback-widget
bgravenorst c822f5e
Fix sanatize.
bgravenorst d0725b9
Merge remote-tracking branch 'origin/main' into add-feedback-widget
bgravenorst 4d75939
Add secondary reason screen to FeedbackWidget
bgravenorst b86c68d
Fix feedback option validation, analytics timing, and tsc-files scope
bgravenorst 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,122 @@ | ||
| import type { VercelRequest, VercelResponse } from '@vercel/node' | ||
| import { sheets } from '@googleapis/sheets' | ||
| import { GoogleAuth } from 'google-auth-library' | ||
|
|
||
| interface FeedbackBody { | ||
| page_url: string | ||
| rating: 'yes' | 'no' | ||
| option?: string | ||
| reason?: string | ||
| } | ||
|
|
||
| function stripHtml(text: string): string { | ||
| let prev = text | ||
| while (true) { | ||
| const next = prev.replace(/<[^>]*>/g, '') | ||
| if (next === prev) return next | ||
| prev = next | ||
| } | ||
| } | ||
|
|
||
| function sanitize(text: string): string { | ||
| return stripHtml(text) | ||
| .replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1') | ||
| .replace(/\[([^\]]*)\]\([^)]*\)/g, '$1') | ||
| .replace(/@/g, '@\u200B') | ||
| .replace(/#(\d)/g, '#\u200B$1') | ||
| .slice(0, 1000) | ||
| .trim() | ||
| } | ||
|
|
||
| function isValidPageUrl(url: string): boolean { | ||
| if (url.startsWith('/')) return /^\/[\w\-./]*$/.test(url) | ||
| try { | ||
| const parsed = new URL(url) | ||
| return parsed.origin === 'https://docs.metamask.io' | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function getDeviceType(ua: string): 'mobile' | 'desktop' { | ||
| return /Mobile|Android|iPhone|iPad/i.test(ua) ? 'mobile' : 'desktop' | ||
| } | ||
|
|
||
| const credentials = JSON.parse( | ||
| Buffer.from(process.env.GOOGLE_SHEETS_CREDENTIALS!, 'base64').toString() | ||
| ) | ||
|
|
||
| const auth = new GoogleAuth({ | ||
| credentials, | ||
| scopes: ['https://www.googleapis.com/auth/spreadsheets'], | ||
| }) | ||
|
|
||
| const sheetsClient = sheets({ version: 'v4', auth }) | ||
|
|
||
| async function appendToSheet(row: string[]) { | ||
| await sheetsClient.spreadsheets.values.append({ | ||
| spreadsheetId: process.env.GOOGLE_SHEET_ID!, | ||
| range: 'Sheet1!A:E', | ||
| valueInputOption: 'RAW', | ||
| insertDataOption: 'INSERT_ROWS', | ||
| requestBody: { values: [row] }, | ||
| }) | ||
| } | ||
|
|
||
| export default async function handler(req: VercelRequest, res: VercelResponse) { | ||
| if (req.method !== 'POST') { | ||
| return res.status(405).json({ error: 'Method not allowed' }) | ||
| } | ||
|
|
||
| if (!req.body || typeof req.body !== 'object') { | ||
| return res.status(400).json({ error: 'Invalid or missing JSON body' }) | ||
| } | ||
|
|
||
| const { page_url: pageUrl, rating, option, reason } = req.body as Partial<FeedbackBody> | ||
|
|
||
| if ( | ||
| typeof pageUrl !== 'string' || | ||
| !pageUrl || | ||
| typeof rating !== 'string' || | ||
| !['yes', 'no'].includes(rating) | ||
| ) { | ||
| return res.status(400).json({ error: 'page_url and rating (yes/no) are required' }) | ||
| } | ||
|
|
||
| if (option !== undefined && typeof option !== 'string') { | ||
| return res.status(400).json({ error: 'option must be a string' }) | ||
| } | ||
|
|
||
| if (reason !== undefined && typeof reason !== 'string') { | ||
| return res.status(400).json({ error: 'reason must be a string' }) | ||
| } | ||
|
|
||
| if (!isValidPageUrl(pageUrl)) { | ||
| return res.status(400).json({ error: 'invalid page_url' }) | ||
| } | ||
|
|
||
| const cleanOption = option ? sanitize(option) : '' | ||
| const cleanReason = reason ? sanitize(reason) : '' | ||
| const feedbackDetail = cleanReason || cleanOption | ||
|
|
||
| if (rating === 'no' && !feedbackDetail) { | ||
| return res.status(400).json({ error: 'option or reason is required for negative feedback' }) | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| const ts = new Date().toISOString() | ||
|
|
||
| try { | ||
| await appendToSheet([ | ||
| ts, | ||
| pageUrl, | ||
| rating, | ||
| feedbackDetail, | ||
| getDeviceType((req.headers['user-agent'] as string) ?? ''), | ||
| ]) | ||
| } catch (err) { | ||
| console.error('Google Sheets append failed:', err) | ||
| return res.status(500).json({ error: 'Failed to save feedback' }) | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| return res.status(200).json({ ok: true }) | ||
| } | ||
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.
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.