| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- 'use client'
- import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
- import { useTranslations } from 'next-intl'
- import {
- Camera,
- ChevronLeft,
- GripVertical,
- Headset,
- Loader2,
- Paperclip,
- Send,
- X,
- } from 'lucide-react'
- import { Button } from '@/components/ui/button'
- import { Input } from '@/components/ui/input'
- import { Textarea } from '@/components/ui/textarea'
- import { Label } from '@/components/ui/label'
- import { compressImage } from '@/lib/compress-image'
- import {
- ATTACHMENT_ACCEPT,
- MAX_ATTACHMENTS,
- MAX_MESSAGE_LENGTH,
- MAX_SUBJECT_LENGTH,
- MAX_TOTAL_ATTACHMENT_BYTES,
- } from '@/features/support/Lib/supportRequest'
- import {
- clearSupportDraft,
- readSupportDraft,
- writeSupportDraft,
- } from '@/features/support/Lib/supportDraft'
- import {
- SUPPORT_OPEN_EVENT,
- SUPPORT_VISIBILITY_EVENT,
- type SupportOpenDetail,
- isSupportBubbleHidden,
- setSupportBubbleHidden,
- } from '@/features/support/Lib/supportVisibility'
- import { useDraggablePosition } from '@/features/support/Lib/useDraggablePosition'
- import { useTouchScreen } from '@/features/support/Lib/useTouchScreen'
- type Status = 'idle' | 'sending' | 'sent'
- const TRIGGER_SIZE = 48
- const PANEL_WIDTH = 380
- const GAP = 12
- /**
- * Error codes the endpoint can return that have their own copy. Anything
- * outside this list falls back to the generic message: next-intl throws on a
- * missing key, so an unrecognised code must never reach `t()` directly.
- */
- const KNOWN_ERROR_CODES = [
- 'subject-required',
- 'subject-too-long',
- 'message-required',
- 'message-too-long',
- 'too-many-attachments',
- 'attachment-type-not-allowed',
- 'attachments-too-large',
- ] as const
- function errorKey(code: unknown): string {
- return (KNOWN_ERROR_CODES as readonly string[]).includes(String(code))
- ? `errors.${code}`
- : 'errors.sendFailed'
- }
- export function SupportBubble({ userId }: { userId: string }) {
- const t = useTranslations('support')
- const [isOpen, setIsOpen] = useState(false)
- // Assume hidden until localStorage has been read. Rendering the button and
- // then pulling it away is worse than a frame without it.
- const [hidden, setHidden] = useState(true)
- const [status, setStatus] = useState<Status>('idle')
- const [error, setError] = useState<string | null>(null)
- const [subject, setSubject] = useState('')
- const [message, setMessage] = useState('')
- const [files, setFiles] = useState<File[]>([])
- const [capturing, setCapturing] = useState(false)
- const fileInputRef = useRef<HTMLInputElement>(null)
- const { position, dragging, consumeDrag, dragHandlers } = useDraggablePosition(TRIGGER_SIZE)
- useEffect(() => {
- const sync = () => setHidden(isSupportBubbleHidden())
- sync()
- // The settings page toggles the same key. `storage` covers other tabs; the
- // custom event covers this one, which `storage` does not fire for.
- window.addEventListener('storage', sync)
- window.addEventListener(SUPPORT_VISIBILITY_EVENT, sync)
- return () => {
- window.removeEventListener('storage', sync)
- window.removeEventListener(SUPPORT_VISIBILITY_EVENT, sync)
- }
- }, [])
- // On a phone or tablet the floating button sits on top of what is being
- // tapped, so neither it nor its edge tab is drawn there. Support is still a
- // tap away in Settings, and the form opens as usual when a page asks for it.
- // Unknown for the first render: nothing is drawn until it is known, so the
- // button never shows for a frame on a phone.
- const touchScreen = useTouchScreen()
- /**
- * The panel follows the button. It is right-aligned to the button and opens
- * upward when the button sits low, which is where it usually will after being
- * dragged clear of a save button.
- */
- const panelStyle = useMemo((): React.CSSProperties | undefined => {
- if (!position || typeof window === 'undefined') return undefined
- const viewportWidth = window.innerWidth
- const viewportHeight = window.innerHeight
- const width = Math.min(PANEL_WIDTH, viewportWidth - 2 * GAP)
- const left = Math.max(
- GAP,
- Math.min(position.x + TRIGGER_SIZE - width, viewportWidth - width - GAP)
- )
- return position.y > viewportHeight / 2
- ? { left, bottom: viewportHeight - position.y + GAP, width }
- : { left, top: position.y + TRIGGER_SIZE + GAP, width }
- }, [position])
- const totalBytes = files.reduce((sum, f) => sum + f.size, 0)
- // What was being written when the page was last replaced under the form: a
- // tab that lost its session comes back from the sign-in page, and a new
- // build asks for a reload. Read after mount, like the visibility above, so
- // the server and the first client render agree.
- const [draftRestored, setDraftRestored] = useState(false)
- useEffect(() => {
- const draft = readSupportDraft(userId)
- if (draft) {
- setSubject((current) => current || draft.subject)
- setMessage((current) => current || draft.message)
- }
- setDraftRestored(true)
- }, [userId])
- // Kept whenever the fields change, whether typed or handed over by a page
- // that opened the form. Not before the stored draft has been read back, or
- // the empty first render would wipe it.
- useEffect(() => {
- if (draftRestored) writeSupportDraft(userId, { subject, message })
- }, [draftRestored, userId, subject, message])
- const reset = useCallback(() => {
- setSubject('')
- setMessage('')
- setFiles([])
- setError(null)
- setStatus('idle')
- clearSupportDraft()
- }, [])
- // The settings page opens the widget for someone who cannot find it, and
- // other pages open it with a subject, and sometimes a message, already
- // filled in. Each only lands on an empty field: someone halfway through
- // describing a problem does not lose their words because they clicked
- // another button.
- useEffect(() => {
- const open = (event: Event) => {
- const detail = (event as CustomEvent<SupportOpenDetail>).detail
- const subject = detail?.subject
- const body = detail?.message
- if (subject || body) {
- if (status === 'sent') reset()
- if (subject) setSubject((current) => current || subject)
- if (body) setMessage((current) => current || body)
- }
- setIsOpen(true)
- }
- window.addEventListener(SUPPORT_OPEN_EVENT, open)
- return () => window.removeEventListener(SUPPORT_OPEN_EVENT, open)
- }, [status, reset])
- const handleClose = useCallback(() => {
- setIsOpen(false)
- if (status !== 'sent') setError(null)
- }, [status])
- const handleDismiss = useCallback(() => {
- setSupportBubbleHidden(true)
- setIsOpen(false)
- }, [])
- const addFiles = useCallback(
- async (incoming: File[]) => {
- setError(null)
- const room = MAX_ATTACHMENTS - files.length
- if (room <= 0) {
- setError(t('errors.too-many-attachments', { max: MAX_ATTACHMENTS }))
- return
- }
- // Images are shrunk before they are counted, so a phone photo does not
- // eat the whole budget on its own.
- const processed = await Promise.all(
- incoming
- .slice(0, room)
- .map((file) =>
- file.type.startsWith('image/') ? compressImage(file) : Promise.resolve(file)
- )
- )
- const next = [...files, ...processed]
- if (next.reduce((sum, f) => sum + f.size, 0) > MAX_TOTAL_ATTACHMENT_BYTES) {
- setError(t('errors.attachments-too-large'))
- return
- }
- setFiles(next)
- },
- [files, t]
- )
- const handleScreenshot = useCallback(async () => {
- setCapturing(true)
- setError(null)
- // Close the panel so the capture shows the page the user is reporting on,
- // not the form covering it.
- setIsOpen(false)
- try {
- // Loaded on demand: this is a large dependency and most sessions never
- // open the widget, let alone take a screenshot.
- const { snapdom } = await import('@zumer/snapdom')
- // Two frames, so the panel has actually left the screen before capture.
- await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)))
- const blob = await snapdom.toBlob(document.body, {
- type: 'jpeg',
- quality: 0.7,
- backgroundColor: '#ffffff',
- })
- const file = new File([blob], `screenshot-${Date.now()}.jpg`, { type: 'image/jpeg' })
- await addFiles([file])
- } catch {
- setError(t('errors.screenshotFailed'))
- } finally {
- setCapturing(false)
- setIsOpen(true)
- }
- }, [addFiles, t])
- const handleSubmit = useCallback(
- async (event: React.FormEvent) => {
- event.preventDefault()
- setStatus('sending')
- setError(null)
- const body = new FormData()
- body.set('subject', subject)
- body.set('message', message)
- body.set('pageUrl', window.location.href)
- for (const file of files) body.append('files', file)
- try {
- const response = await fetch('/api/protected/support', { method: 'POST', body })
- if (!response.ok) {
- const payload = await response.json().catch(() => ({}))
- setError(t(errorKey(payload.error), { max: MAX_ATTACHMENTS }))
- setStatus('idle')
- return
- }
- setStatus('sent')
- setSubject('')
- setMessage('')
- setFiles([])
- // Sent, so it must not come back as a draft on the next page load.
- clearSupportDraft()
- } catch {
- setError(t('errors.sendFailed'))
- setStatus('idle')
- }
- },
- [subject, message, files, t]
- )
- // position is null until localStorage and the viewport have been read.
- if (!position || touchScreen === null) return null
- // Only the form, and only while it is open. The dismissal below is about
- // the button, so it has no say here.
- if (touchScreen && !isOpen) return null
- // Dismissed collapses to a tab tucked against the right edge rather than
- // disappearing. Removing the button outright leaves no way back except the
- // settings page, which is not somewhere you look when you want support.
- if (hidden && !touchScreen) {
- return (
- <button
- type="button"
- onClick={() => {
- setSupportBubbleHidden(false)
- setIsOpen(true)
- }}
- className="group fixed right-0 bottom-24 z-50 flex h-9 items-center gap-0 rounded-l-md border border-r-0 border-border/60 bg-background/80 pl-1.5 pr-1 text-muted-foreground/60 shadow-sm backdrop-blur transition-colors hover:bg-background hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring md:bottom-8"
- aria-label={t('restore')}
- >
- <ChevronLeft className="h-3.5 w-3.5 shrink-0" />
- {/* Collapsed to an arrow until pointed at, so it stays out of the way
- but still says what it does rather than relying on a tooltip. */}
- <span className="max-w-0 overflow-hidden whitespace-nowrap text-xs font-medium opacity-0 transition-all duration-200 group-hover:max-w-[12rem] group-hover:pr-1 group-hover:opacity-100 group-focus-visible:max-w-[12rem] group-focus-visible:pr-1 group-focus-visible:opacity-100">
- {t('restore')}
- </span>
- </button>
- )
- }
- return (
- <>
- {!isOpen && (
- <button
- type="button"
- {...dragHandlers}
- onClick={() => {
- // Swallow the click that ends a drag, so moving the button out of
- // the way does not also open the form.
- if (consumeDrag()) return
- setIsOpen(true)
- }}
- style={{ left: position.x, top: position.y, width: TRIGGER_SIZE, height: TRIGGER_SIZE }}
- className={`fixed z-50 flex touch-none items-center justify-center rounded-full bg-primary text-primary-foreground shadow-lg shadow-primary/25 ring-1 ring-inset ring-white/15 transition-[box-shadow,transform] hover:shadow-xl hover:shadow-primary/30 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ${
- dragging ? 'scale-105 cursor-grabbing' : 'cursor-grab hover:-translate-y-0.5'
- }`}
- aria-label={t('open')}
- title={t('dragHint')}
- >
- <Headset className="h-[22px] w-[22px]" strokeWidth={1.75} />
- </button>
- )}
- {isOpen && (
- <div
- // With no button to follow, the form sits at the top of the screen:
- // clear of the bottom navigation, and of the on-screen keyboard that
- // covers the lower half as soon as a field is tapped. Below a
- // laptop's width it hangs from the left edge: a page that is a few
- // pixels too wide pushes the right edge, and the close button with
- // it, off a phone or small tablet.
- style={touchScreen ? undefined : panelStyle}
- className={`fixed z-50 overflow-hidden rounded-xl border bg-popover shadow-2xl ring-1 ring-black/5 dark:ring-white/10 ${
- touchScreen
- ? 'top-[calc(0.75rem+env(safe-area-inset-top))] left-3 w-[min(380px,calc(100vw-1.5rem))] lg:right-3 lg:left-auto'
- : ''
- }`}
- >
- <div
- className={`flex items-center gap-1 border-b bg-muted/40 py-2 pr-2 ${
- touchScreen ? 'pl-3' : 'pl-1'
- }`}
- >
- {/* Hidden from assistive tech rather than announced as a button.
- Dragging is pointer-only, so exposing it as a control would
- advertise something a keyboard user cannot operate. Nothing is
- lost: every control in the panel is reachable regardless of
- where it sits, and the position is only a convenience. */}
- {!touchScreen && (
- <span
- {...dragHandlers}
- aria-hidden="true"
- title={t('move')}
- className={`flex h-7 w-6 shrink-0 touch-none items-center justify-center text-muted-foreground/60 hover:text-muted-foreground ${
- dragging ? 'cursor-grabbing' : 'cursor-grab'
- }`}
- >
- <GripVertical className="h-4 w-4" />
- </span>
- )}
- <Headset className="h-4 w-4 shrink-0 text-primary" strokeWidth={1.75} />
- <span className="flex-1 truncate text-sm font-medium">{t('title')}</span>
- <button
- type="button"
- onClick={handleClose}
- className="rounded-md p-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
- aria-label={t('close')}
- >
- <X className="h-4 w-4" />
- </button>
- </div>
- <div className="max-h-[min(70vh,32rem)] overflow-y-auto p-4">
- {status === 'sent' ? (
- <div className="py-6 text-center">
- <p className="font-medium">{t('sentTitle')}</p>
- <p className="mt-1 text-sm text-muted-foreground">{t('sentDescription')}</p>
- <div className="mt-4 flex justify-center gap-2">
- <Button type="button" variant="outline" size="sm" onClick={reset}>
- {t('sendAnother')}
- </Button>
- <Button type="button" size="sm" onClick={handleClose}>
- {t('close')}
- </Button>
- </div>
- </div>
- ) : (
- <form onSubmit={handleSubmit} className="space-y-3">
- <div className="space-y-1.5">
- <Label htmlFor="support-subject" className="text-xs text-muted-foreground">
- {t('subjectLabel')}
- </Label>
- <Input
- id="support-subject"
- value={subject}
- onChange={(e) => setSubject(e.target.value)}
- maxLength={MAX_SUBJECT_LENGTH}
- required
- placeholder={t('subjectPlaceholder')}
- />
- </div>
- <div className="space-y-1.5">
- <Label htmlFor="support-message" className="text-xs text-muted-foreground">
- {t('messageLabel')}
- </Label>
- <Textarea
- id="support-message"
- value={message}
- onChange={(e) => setMessage(e.target.value)}
- maxLength={MAX_MESSAGE_LENGTH}
- required
- rows={4}
- className="resize-none"
- placeholder={t('messagePlaceholder')}
- />
- </div>
- <div className="flex gap-2">
- <Button
- type="button"
- variant="outline"
- size="sm"
- className="flex-1"
- onClick={handleScreenshot}
- disabled={capturing || files.length >= MAX_ATTACHMENTS}
- >
- {capturing ? (
- <Loader2 className="mr-1 h-4 w-4 animate-spin" />
- ) : (
- <Camera className="mr-1 h-4 w-4" />
- )}
- {t('takeScreenshot')}
- </Button>
- <Button
- type="button"
- variant="outline"
- size="sm"
- className="flex-1"
- onClick={() => fileInputRef.current?.click()}
- disabled={files.length >= MAX_ATTACHMENTS}
- >
- <Paperclip className="mr-1 h-4 w-4" />
- {t('addFiles')}
- </Button>
- <input
- ref={fileInputRef}
- type="file"
- multiple
- accept={ATTACHMENT_ACCEPT}
- className="hidden"
- onChange={(e) => {
- void addFiles(Array.from(e.target.files ?? []))
- e.target.value = ''
- }}
- />
- </div>
- {files.length > 0 && (
- <ul className="space-y-1">
- {files.map((file, index) => (
- <li
- key={`${file.name}-${index}`}
- className="flex items-center justify-between gap-2 rounded-md border bg-muted/30 px-2 py-1 text-xs"
- >
- <span className="truncate" title={file.name}>
- {file.name}
- </span>
- <button
- type="button"
- className="shrink-0 text-muted-foreground hover:text-destructive"
- onClick={() => setFiles(files.filter((_, i) => i !== index))}
- aria-label={t('removeAttachment')}
- >
- <X className="h-3.5 w-3.5" />
- </button>
- </li>
- ))}
- <li className="px-0.5 pt-0.5 text-[11px] text-muted-foreground">
- {t('attachmentSummary', {
- count: files.length,
- max: MAX_ATTACHMENTS,
- size: (totalBytes / (1024 * 1024)).toFixed(1),
- })}
- </li>
- </ul>
- )}
- {error && <p className="text-sm text-destructive">{error}</p>}
- <Button type="submit" className="w-full" disabled={status === 'sending'}>
- {status === 'sending' ? (
- <Loader2 className="mr-1 h-4 w-4 animate-spin" />
- ) : (
- <Send className="mr-1 h-4 w-4" />
- )}
- {status === 'sending' ? t('sending') : t('send')}
- </Button>
- {/* There is no button to hide on a phone or tablet. */}
- {!touchScreen && (
- <button
- type="button"
- onClick={handleDismiss}
- className="w-full text-center text-xs text-muted-foreground transition-colors hover:text-foreground hover:underline"
- >
- {t('hideButton')}
- </button>
- )}
- </form>
- )}
- </div>
- </div>
- )}
- </>
- )
- }
|