Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Hook up upload logic for feedback attachments

Joshen Lim0f6cee88b671
diff --git a/studio/components/layouts/ProjectLayout/LayoutHeader/FeedbackDropdown/FeedbackDropdown.utils.ts b/studio/components/layouts/ProjectLayout/LayoutHeader/FeedbackDropdown/FeedbackDropdown.utils.ts
new file mode 100644
index 00000000000..00d9c890d80
--- /dev/null
+++ b/studio/components/layouts/ProjectLayout/LayoutHeader/FeedbackDropdown/FeedbackDropdown.utils.ts
@@ -0,0 +1,61 @@
+import { compact } from 'lodash'
+import { createClient } from '@supabase/supabase-js'
+import { uuidv4 } from 'lib/helpers'
+
+const SUPPORT_API_URL = process.env.NEXT_PUBLIC_SUPPORT_API_URL || ''
+const SUPPORT_API_KEY = process.env.NEXT_PUBLIC_SUPPORT_ANON_KEY || ''
+const supportSupabaseClient = createClient(SUPPORT_API_URL, SUPPORT_API_KEY, {
+ auth: {
+ persistSession: false,
+ autoRefreshToken: false,
+ // @ts-ignore
+ multiTab: false,
+ detectSessionInUrl: false,
+ localStorage: {
+ getItem: (key: string) => undefined,
+ setItem: (key: string, value: string) => {},
+ removeItem: (key: string) => {},
+ },
+ },
+})
+
+export const convertB64toBlob = (image: string) => {
+ const contentType = 'image/png'
+ const byteCharacters = atob(image.substr(`data:${contentType};base64,`.length))
+ const byteArrays = []
+
+ for (let offset = 0; offset < byteCharacters.length; offset += 1024) {
+ const slice = byteCharacters.slice(offset, offset + 1024)
+
+ const byteNumbers = new Array(slice.length)
+ for (let i = 0; i < slice.length; i++) {
+ byteNumbers[i] = slice.charCodeAt(i)
+ }
+
+ const byteArray = new Uint8Array(byteNumbers)
+
+ byteArrays.push(byteArray)
+ }
+ const blob = new Blob(byteArrays, { type: contentType })
+ return blob
+}
+
+export const uploadAttachment = async (ref: string, image: string) => {
+ const blob = convertB64toBlob(image)
+ const name = `${ref || 'no-project'}/${uuidv4()}.png`
+ const options = { cacheControl: '3600' }
+ const { data: file, error: uploadError } = await supportSupabaseClient.storage
+ .from('feedback-attachments')
+ .upload(name, blob, options)
+
+ if (uploadError) {
+ console.error('Failed to upload:', uploadError)
+ return undefined
+ }
+
+ const { data } = await supportSupabaseClient.storage
+ .from('feedback-attachments')
+ .createSignedUrls([file.path], 10 * 365 * 24 * 60 * 60)
+
+ return data?.[0].signedUrl
+}
diff --git a/studio/components/layouts/ProjectLayout/LayoutHeader/FeedbackDropdown/FeedbackWidget.tsx b/studio/components/layouts/ProjectLayout/LayoutHeader/FeedbackDropdown/FeedbackWidget.tsx
index a4cf6842b6f..94054cc1442 100644
--- a/studio/components/layouts/ProjectLayout/LayoutHeader/FeedbackDropdown/FeedbackWidget.tsx
+++ b/studio/components/layouts/ProjectLayout/LayoutHeader/FeedbackDropdown/FeedbackWidget.tsx
@@ -8,6 +8,7 @@ import { Button, Input, Popover, IconCamera, IconX } from 'ui'
import { useStore } from 'hooks'
import { post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
+import { convertB64toBlob, uploadAttachment } from './FeedbackDropdown.utils'
interface Props {
onClose: () => void
@@ -45,10 +46,7 @@ const FeedbackWidget: FC<Props> = ({ onClose }) => {
}
toPng(document.body, { filter })
- .then((dataUrl: any) => {
- console.log('Capture screenshot', dataUrl)
- setScreenshot(dataUrl)
- })
+ .then((dataUrl: any) => setScreenshot(dataUrl))
.catch((error: any) => {
ui.setNotification({
error,
@@ -63,10 +61,21 @@ const FeedbackWidget: FC<Props> = ({ onClose }) => {
}
const sendFeedback = async () => {
- if (feedback.length) {
+ if (feedback.length === 0 && screenshot !== undefined) {
+ return ui.setNotification({
+ category: 'error',
+ message: 'Please include a message in your feedback.',
+ duration: 4000,
+ })
+ } else if (feedback.length > 0) {
setSending(true)
+ const attachmentUrl = screenshot
+ ? await uploadAttachment(ref as string, screenshot)
+ : undefined
+ const formattedFeedback =
+ attachmentUrl !== undefined ? `${feedback}\n\nAttachments:\n${attachmentUrl}` : feedback
await post(`${API_URL}/feedback/send`, {
- message: feedback,
+ message: formattedFeedback,
pathname: router.asPath,
category: 'Feedback',
projectRef: ref,
@@ -101,14 +110,22 @@ const FeedbackWidget: FC<Props> = ({ onClose }) => {
{screenshot !== undefined ? (
<div
style={{ backgroundImage: `url("${screenshot}")` }}
- className="rounded h-[26px] w-[30px] border border-scale-600 relative bg-cover bg-center bg-no-repeat"
+ onClick={() => {
+ const blob = convertB64toBlob(screenshot)
+ const blobUrl = URL.createObjectURL(blob)
+ window.open(blobUrl, '_blank')
+ }}
+ className="cursor-pointer rounded h-[26px] w-[30px] border border-scale-600 relative bg-cover bg-center bg-no-repeat"
>
<div
className={[
'cursor-pointer rounded-full bg-red-900 h-3 w-3',
'flex items-center justify-center absolute -top-1 -right-1',
].join(' ')}
- onClick={() => setScreenshot(undefined)}
+ onClick={(event) => {
+ event.stopPropagation()
+ setScreenshot(undefined)
+ }}
>
<IconX size={8} strokeWidth={3} />
</div>
@@ -131,10 +148,12 @@ const FeedbackWidget: FC<Props> = ({ onClose }) => {
<div
className={[
'bg-scale-100 rounded py-1 px-2 leading-none shadow', // background
- 'border-scale-200 border', //border
+ 'w-[130px] text-center border-scale-200 border', //border
].join(' ')}
>
- <span className="text-scale-1200 text-xs text-center">Capture screenshot</span>
+ <span className="text-scale-1200 text-xs">
+ Capture screenshot of current view
+ </span>
</div>
</Tooltip.Content>
</Tooltip.Root>