mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat(sessions): full-screen mobile question sheet with persisted draft answers
This commit is contained in:
1 parent
1659664d53
commit
b01e8707ee
5 files changed
+443
-121
No files matched your search
@@ -1,13 +1,15 @@
|
||||
import { useEffect, useId, useRef, useState, type ReactNode } from 'react'
|
||||
import { useCallback, useEffect, useId, useRef, useState, type ReactNode } from 'react'
|
||||
import { X, ChevronDown, ChevronUp, Check, Loader2 } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { ExternalFieldCard } from '@/components/ui/external-field-card'
|
||||
import type { FormAnswer, FormField, FormInfo } from '@opencode-manager/shared/opencode'
|
||||
import { buildAnswer, hasMissingAnswers, visibleFields } from '@/lib/formFields'
|
||||
import { clearFormDraft, loadFormDraft, saveFormDraft, type FormDraft } from '@/lib/formDrafts'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import { useShortcutActions } from '@/contexts/KeyboardShortcutsContext'
|
||||
import { useMobile, useSwipeDismiss } from '@/hooks/useMobile'
|
||||
|
||||
type AnswerableFormField = Exclude<FormField, { type: 'external' }>
|
||||
|
||||
@@ -31,13 +33,7 @@ function usesCustomInput(field: AnswerableFormField): boolean {
|
||||
return field.type === 'string' && field.custom === true && (field.options?.length ?? 0) > 0
|
||||
}
|
||||
|
||||
interface FormPromptState {
|
||||
answer: FormAnswer
|
||||
customValues: Record<string, string[]>
|
||||
customDrafts: Record<string, string>
|
||||
}
|
||||
|
||||
function buildInitialState(fields: AnswerableFormField[]): FormPromptState {
|
||||
function buildInitialState(fields: AnswerableFormField[]): FormDraft {
|
||||
const answer: FormAnswer = {}
|
||||
const customValues: Record<string, string[]> = {}
|
||||
const customDrafts: Record<string, string> = {}
|
||||
@@ -70,12 +66,33 @@ interface FormPromptProps {
|
||||
|
||||
export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptProps) {
|
||||
const candidateFields = form.fields.filter(isAnswerableField)
|
||||
const [state, setState] = useState<FormPromptState>(() => buildInitialState(candidateFields))
|
||||
const [state, setState] = useState<FormDraft>(() => loadFormDraft(form.id) ?? buildInitialState(candidateFields))
|
||||
const { answer, customValues, customDrafts } = state
|
||||
const [expandedCustom, setExpandedCustom] = useState<string | null>(null)
|
||||
const [isMinimized, setIsMinimized] = useState(false)
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const draftClearedRef = useRef(false)
|
||||
const mobileHeaderRef = useRef<HTMLDivElement>(null)
|
||||
const formControlId = useId()
|
||||
const isMobile = useMobile()
|
||||
|
||||
const handleMinimize = useCallback(() => {
|
||||
setIsMinimized(true)
|
||||
onMinimize?.()
|
||||
}, [onMinimize])
|
||||
|
||||
const { bind, swipeStyles } = useSwipeDismiss(handleMinimize, {
|
||||
enabled: !isMinimized,
|
||||
threshold: 96,
|
||||
suspendsRouteSwipe: false,
|
||||
})
|
||||
|
||||
useEffect(() => bind(mobileHeaderRef.current), [bind])
|
||||
|
||||
useEffect(() => {
|
||||
if (draftClearedRef.current) return
|
||||
saveFormDraft(form.id, state)
|
||||
}, [form.id, state])
|
||||
|
||||
const controlId = (key: string) => `${formControlId}-${key}`
|
||||
|
||||
@@ -132,11 +149,6 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
const canSubmit = !hasMissingAnswers(form.fields, effectiveAnswers)
|
||||
const buildSubmission = (): FormAnswer => buildAnswer(form.fields, effectiveAnswers)
|
||||
|
||||
const handleMinimize = () => {
|
||||
setIsMinimized(true)
|
||||
onMinimize?.()
|
||||
}
|
||||
|
||||
useShortcutActions({
|
||||
abort: !isMinimized && !expandedCustom ? handleMinimize : undefined,
|
||||
})
|
||||
@@ -146,6 +158,8 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
setIsSubmitting(true)
|
||||
try {
|
||||
await onReply(form.id, buildSubmission())
|
||||
draftClearedRef.current = true
|
||||
clearFormDraft(form.id)
|
||||
} catch {
|
||||
showToast.error('Failed to submit form')
|
||||
} finally {
|
||||
@@ -157,6 +171,8 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
setIsSubmitting(true)
|
||||
try {
|
||||
await onCancel(form.id)
|
||||
draftClearedRef.current = true
|
||||
clearFormDraft(form.id)
|
||||
} catch {
|
||||
showToast.error('Failed to dismiss form')
|
||||
} finally {
|
||||
@@ -168,6 +184,7 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
if (e.defaultPrevented) return
|
||||
if (e.key === 'Escape' && !isMinimized && !expandedCustom) {
|
||||
if (e.target instanceof Element && e.target.closest('[role="dialog"], [role="alertdialog"]')) return
|
||||
setIsMinimized(true)
|
||||
onMinimize?.()
|
||||
}
|
||||
@@ -187,7 +204,7 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
const selected = typeof value === 'boolean' ? value : undefined
|
||||
return (
|
||||
<FieldGroup key={field.key} label={label} description={field.description} required={required}>
|
||||
<div className="flex gap-1.5 sm:gap-2">
|
||||
<div className="flex gap-3 md:gap-2">
|
||||
<OptionButton selected={selected === true} label="Yes" onClick={() => setFieldAnswer(field.key, true)} />
|
||||
<OptionButton selected={selected === false} label="No" onClick={() => setFieldAnswer(field.key, false)} />
|
||||
</div>
|
||||
@@ -209,7 +226,7 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
max={typeof field.maximum === 'number' ? field.maximum : undefined}
|
||||
step={field.type === 'integer' ? 1 : 'any'}
|
||||
onChange={(e) => setFieldAnswer(field.key, e.target.value === '' ? '' : Number(e.target.value))}
|
||||
className="w-full rounded-lg border border-border bg-muted/60 dark:bg-muted/5 px-3 py-2 text-sm focus:border-primary focus:outline-none"
|
||||
className="w-full rounded-lg border border-border bg-muted/60 dark:bg-muted/5 px-3.5 py-3 text-base focus:border-primary focus:outline-none md:px-3 md:py-2 md:text-sm"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
@@ -221,7 +238,7 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
const customValue = customDrafts[field.key] ?? ''
|
||||
return (
|
||||
<FieldGroup key={field.key} label={label} description={field.description} required={required}>
|
||||
<div className="space-y-1.5 sm:space-y-2">
|
||||
<div className="space-y-3 md:space-y-2">
|
||||
{field.options.map((option) => (
|
||||
<OptionButton
|
||||
key={option.value}
|
||||
@@ -260,7 +277,7 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
const customValue = customDrafts[field.key] ?? ''
|
||||
return (
|
||||
<FieldGroup key={field.key} label={label} description={field.description} required={required}>
|
||||
<div className="space-y-1.5 sm:space-y-2">
|
||||
<div className="space-y-3 md:space-y-2">
|
||||
{field.options.map((option) => (
|
||||
<OptionButton
|
||||
key={option.value}
|
||||
@@ -302,19 +319,54 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
value={typeof value === 'string' ? value : ''}
|
||||
onChange={(e) => setFieldAnswer(field.key, e.target.value)}
|
||||
placeholder={field.placeholder ?? 'Type your answer...'}
|
||||
className="min-h-[60px] sm:min-h-[80px] text-[16px] sm:text-xs md:text-sm resize-none border-primary/30 focus:border-primary"
|
||||
className="min-h-[60px] md:min-h-[80px] text-[16px] md:text-sm resize-none border-primary/30 focus:border-primary"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full bg-background bg-gradient-to-br from-primary/10 to-primary/20 border-2 border-primary/40 rounded-xl shadow-lg shadow-primary/20 mb-1 overflow-hidden">
|
||||
<div className="flex items-center px-3 py-2 sm:px-4 sm:py-2.5 border-b border-primary/20 bg-primary/5">
|
||||
<div
|
||||
className="flex flex-1 min-h-0 flex-col overflow-hidden rounded-t-[22px] border-t border-primary/40 bg-background bg-gradient-to-b from-primary/15 to-card mb-1 md:flex-none md:rounded-xl md:border-2 md:border-primary/40 md:bg-gradient-to-br md:from-primary/10 md:to-primary/20 md:shadow-lg md:shadow-primary/20"
|
||||
style={isMobile ? swipeStyles : undefined}
|
||||
>
|
||||
<div ref={mobileHeaderRef} className="shrink-0 md:hidden">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleMinimize}
|
||||
aria-label="Drag to minimize"
|
||||
className="flex h-[22px] w-full items-center justify-center"
|
||||
>
|
||||
<span aria-hidden="true" className="h-[5px] w-10 rounded-full bg-muted-foreground/40" />
|
||||
</button>
|
||||
<div className="flex h-11 items-center gap-1 px-1.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCancel}
|
||||
disabled={isSubmitting}
|
||||
className="min-h-11 min-w-11 px-2 text-left text-base text-primary disabled:opacity-50"
|
||||
>
|
||||
Dismiss
|
||||
</button>
|
||||
<span className="min-w-0 flex-1 truncate text-center text-[13px] font-semibold uppercase tracking-wide text-muted-foreground">
|
||||
{form.title || 'Form'}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => (isMinimized ? setIsMinimized(false) : handleMinimize())}
|
||||
aria-label="Minimize form"
|
||||
className="flex h-11 w-11 shrink-0 items-center justify-center text-muted-foreground"
|
||||
>
|
||||
<ChevronDown className="h-5 w-5" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="hidden md:flex items-center px-3 py-2 md:px-4 md:py-2.5 border-b border-primary/20 bg-primary/5">
|
||||
<button
|
||||
onClick={() => (isMinimized ? setIsMinimized(false) : handleMinimize())}
|
||||
aria-expanded={!isMinimized}
|
||||
className="flex items-center gap-1.5 flex-1 text-left text-xs sm:text-sm font-semibold text-primary"
|
||||
className="flex items-center gap-1.5 flex-1 text-left text-xs md:text-sm font-semibold text-primary"
|
||||
>
|
||||
{isMinimized ? (
|
||||
<ChevronUp className="w-3 h-3 opacity-60 flex-shrink-0" />
|
||||
@@ -327,14 +379,14 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
onClick={handleCancel}
|
||||
disabled={isSubmitting}
|
||||
aria-label="Dismiss form"
|
||||
className="p-1.5 sm:p-2 hover:bg-destructive/20 text-muted-foreground hover:text-destructive transition-colors"
|
||||
className="p-1.5 md:p-2 hover:bg-destructive/20 text-muted-foreground hover:text-destructive transition-colors"
|
||||
>
|
||||
<X className="w-3.5 h-3.5 sm:w-4 sm:h-4" />
|
||||
<X className="w-3.5 h-3.5 md:w-4 md:h-4" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{!isMinimized && (
|
||||
<div className="p-2 sm:p-3 max-h-[50vh] sm:max-h-[70vh] overflow-y-auto overflow-x-hidden bg-background/60 dark:bg-background/30 space-y-3">
|
||||
<div className="flex-1 min-h-0 overflow-y-auto overflow-x-hidden overscroll-contain px-4 pt-2 pb-6 space-y-7 md:flex-none md:p-3 md:max-h-[70vh] md:space-y-3 md:bg-background/60 dark:md:bg-background/30">
|
||||
{renderFields.map((field) =>
|
||||
field.type === 'external' ? (
|
||||
<ExternalFieldCard key={field.key} field={field} />
|
||||
@@ -345,12 +397,12 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex gap-1.5 sm:gap-2 px-2 py-2 sm:px-3 sm:py-3 border-t border-primary/20">
|
||||
<div className="relative flex shrink-0 gap-2 px-4 pt-3 pb-3 before:pointer-events-none before:absolute before:-top-8 before:inset-x-0 before:h-8 before:bg-gradient-to-b before:from-transparent before:to-card md:border-t md:border-primary/20 md:px-3 md:py-3 md:before:hidden">
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={handleCancel}
|
||||
disabled={isSubmitting}
|
||||
className="flex-1 h-8 sm:h-10 text-xs sm:text-sm bg-muted hover:bg-muted/80 text-foreground"
|
||||
className="hidden md:inline-flex flex-1 h-10 text-sm bg-muted hover:bg-muted/80 text-foreground"
|
||||
>
|
||||
Dismiss
|
||||
</Button>
|
||||
@@ -358,9 +410,9 @@ export function FormPrompt({ form, onReply, onCancel, onMinimize }: FormPromptPr
|
||||
size="sm"
|
||||
onClick={handleSubmit}
|
||||
disabled={isSubmitting || !canSubmit}
|
||||
className="flex-1 h-8 sm:h-10 text-xs sm:text-sm bg-success hover:bg-success/90 text-success-foreground"
|
||||
className="flex-1 h-[52px] rounded-[14px] text-[17px] font-bold bg-success hover:bg-success/90 text-success-foreground md:h-10 md:rounded-md md:text-sm md:font-medium"
|
||||
>
|
||||
{isSubmitting ? <Loader2 className="w-3.5 h-3.5 sm:w-4 sm:h-4 animate-spin" /> : 'Submit'}
|
||||
{isSubmitting ? <Loader2 className="w-3.5 h-3.5 md:w-4 md:h-4 animate-spin" /> : 'Submit'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -379,13 +431,13 @@ function FieldLabel({
|
||||
required: boolean
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-0.5">
|
||||
<label htmlFor={id} className="block text-xs sm:text-sm font-semibold text-foreground">
|
||||
<div className="space-y-1 md:space-y-0.5">
|
||||
<label htmlFor={id} className="block text-xl font-bold leading-tight text-foreground md:text-sm md:font-semibold">
|
||||
{label}
|
||||
{required && <span className="text-destructive ml-0.5">*</span>}
|
||||
</label>
|
||||
{description && (
|
||||
<p id={`${id}-description`} className="text-[10px] sm:text-xs text-foreground/70">
|
||||
<p id={`${id}-description`} className="text-[15px] text-foreground/70 md:text-xs">
|
||||
{description}
|
||||
</p>
|
||||
)}
|
||||
@@ -405,12 +457,12 @@ function FieldGroup({
|
||||
children: ReactNode
|
||||
}) {
|
||||
return (
|
||||
<fieldset className="space-y-2">
|
||||
<legend className="text-xs sm:text-sm font-semibold text-foreground">
|
||||
<fieldset className="space-y-3 md:space-y-2">
|
||||
<legend className="text-xl font-bold leading-tight text-foreground md:text-sm md:font-semibold">
|
||||
{label}
|
||||
{required && <span className="text-destructive ml-0.5">*</span>}
|
||||
</legend>
|
||||
{description && <p className="text-[10px] sm:text-xs text-foreground/70">{description}</p>}
|
||||
{description && <p className="text-[15px] text-foreground/70 md:text-xs">{description}</p>}
|
||||
{children}
|
||||
</fieldset>
|
||||
)
|
||||
@@ -433,32 +485,35 @@ function OptionButton({
|
||||
aria-pressed={selected}
|
||||
onClick={onClick}
|
||||
className={cn(
|
||||
'w-full text-left p-2 sm:p-3 rounded-lg transition-all duration-200 active:scale-[0.98]',
|
||||
'relative w-full min-h-14 text-left p-4 pl-[18px] rounded-[14px] transition-all duration-200 active:scale-[0.98] md:min-h-0 md:p-3 md:pl-3 md:rounded-lg',
|
||||
selected
|
||||
? 'bg-primary/20'
|
||||
? 'bg-primary/20 ring-1 ring-inset ring-primary/60 md:ring-0'
|
||||
: 'bg-muted/40 dark:bg-muted/5 hover:bg-muted/60 dark:hover:bg-muted/10',
|
||||
)}
|
||||
>
|
||||
<div className="flex items-start gap-1.5 sm:gap-2">
|
||||
{selected && (
|
||||
<span aria-hidden="true" className="absolute inset-y-0 left-0 w-1 rounded-l-[14px] bg-primary md:hidden" />
|
||||
)}
|
||||
<div className="flex items-start gap-3 md:gap-2">
|
||||
<div
|
||||
className={cn(
|
||||
'w-4 h-4 sm:w-5 sm:h-5 rounded-full border-2 flex items-center justify-center flex-shrink-0 transition-colors',
|
||||
'w-6 h-6 md:w-5 md:h-5 rounded-full border-2 flex items-center justify-center flex-shrink-0 transition-colors',
|
||||
selected ? 'border-primary bg-primary' : 'border-muted-foreground',
|
||||
)}
|
||||
>
|
||||
{selected && <Check className="w-2.5 h-2.5 sm:w-3 sm:h-3 text-primary-foreground" />}
|
||||
{selected && <Check className="w-3.5 h-3.5 md:w-3 md:h-3 text-primary-foreground" />}
|
||||
</div>
|
||||
<div className="min-w-0">
|
||||
<span
|
||||
className={cn(
|
||||
'text-xs sm:text-sm font-semibold',
|
||||
'text-base font-semibold md:text-sm',
|
||||
selected ? 'text-primary' : 'text-foreground',
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</span>
|
||||
{description && (
|
||||
<p className="text-[10px] sm:text-xs text-foreground/70 mt-0.5">{description}</p>
|
||||
<p className="text-sm leading-snug text-foreground/70 mt-0.5 md:text-xs">{description}</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
@@ -499,24 +554,27 @@ function CustomOption({
|
||||
aria-pressed={selected}
|
||||
onClick={() => (expanded ? onCollapse() : onExpand())}
|
||||
className={cn(
|
||||
'w-full text-left p-2 sm:p-3 rounded-lg transition-all duration-200',
|
||||
'relative w-full min-h-14 text-left p-4 pl-[18px] rounded-[14px] transition-all duration-200 md:min-h-0 md:p-3 md:pl-3 md:rounded-lg',
|
||||
expanded || selected
|
||||
? 'bg-primary/20'
|
||||
? 'bg-primary/20 ring-1 ring-inset ring-primary/60 md:ring-0'
|
||||
: 'bg-muted/40 dark:bg-muted/5 hover:bg-muted/60 dark:hover:bg-muted/10',
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center gap-1.5 sm:gap-2">
|
||||
{(expanded || selected) && (
|
||||
<span aria-hidden="true" className="absolute inset-y-0 left-0 w-1 rounded-l-[14px] bg-primary md:hidden" />
|
||||
)}
|
||||
<div className="flex items-center gap-3 md:gap-2">
|
||||
<div
|
||||
className={cn(
|
||||
'w-4 h-4 sm:w-5 sm:h-5 rounded-full border-2 flex items-center justify-center flex-shrink-0 transition-colors',
|
||||
'w-6 h-6 md:w-5 md:h-5 rounded-full border-2 flex items-center justify-center flex-shrink-0 transition-colors',
|
||||
selected ? 'border-primary bg-primary' : 'border-muted-foreground',
|
||||
)}
|
||||
>
|
||||
{selected && <Check className="w-2.5 h-2.5 sm:w-3 sm:h-3 text-primary-foreground" />}
|
||||
{selected && <Check className="w-3.5 h-3.5 md:w-3 md:h-3 text-primary-foreground" />}
|
||||
</div>
|
||||
<span
|
||||
className={cn(
|
||||
'text-xs sm:text-sm font-semibold',
|
||||
'text-base font-semibold md:text-sm',
|
||||
expanded || selected ? 'text-primary' : 'text-foreground',
|
||||
)}
|
||||
>
|
||||
@@ -526,14 +584,14 @@ function CustomOption({
|
||||
</button>
|
||||
|
||||
{expanded && (
|
||||
<div className="ml-5 sm:ml-7 space-y-1.5 sm:space-y-2 animate-in slide-in-from-top-2 duration-200">
|
||||
<div className="ml-0 md:ml-7 space-y-3 md:space-y-2 animate-in slide-in-from-top-2 duration-200">
|
||||
<Textarea
|
||||
ref={textareaRef}
|
||||
aria-label={`Other answer for ${fieldLabel}`}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
placeholder="Type your own answer..."
|
||||
className="min-h-[60px] sm:min-h-[80px] text-[16px] sm:text-xs md:text-sm resize-none border-primary/30 focus:border-primary"
|
||||
className="min-h-[60px] md:min-h-[80px] text-[16px] md:text-sm resize-none border-primary/30 focus:border-primary"
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault()
|
||||
@@ -548,7 +606,7 @@ function CustomOption({
|
||||
)}
|
||||
|
||||
{!expanded && selected && (
|
||||
<div className="ml-5 sm:ml-7 text-[10px] sm:text-xs text-muted-foreground">
|
||||
<div className="ml-0 md:ml-7 text-xs text-muted-foreground">
|
||||
{value}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user