Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(ui): surface silent validation and rejection errors across editors and modals#5394
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
14c0b509de8eb91e4a4cac252dd3f0642814476fd3bd9824cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -197,12 +197,28 @@ function ExpandedCellEditor({ | ||
| textareaRef, | ||
| }: ExpandedCellEditorProps) { | ||
| const [draftValue, setDraftValue] = useState(initialValue) | ||
| const [parseError, setParseError] = useState<string | null>(null) | ||
| const handleSave = () => { | ||
| // `displayToStorage` only normalizes dates — it returns null for anything else. | ||
| // Fall back to the raw draft for non-date columns, matching the inline editor. | ||
| const raw = displayToStorage(draftValue) ?? draftValue | ||
| const cleaned = cleanCellValue(raw, column) | ||
| let cleaned: unknown | ||
| try { | ||
| cleaned = cleanCellValue(raw, column) | ||
| } catch { | ||
| setParseError('Invalid JSON') | ||
| return | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** `cleanCellValue` nulls unparseable dates/numbers instead of throwing — reject rather than silently clear. */ | ||
| if ( | ||
| cleaned === null && | ||
| draftValue.trim() !== '' && | ||
| (column.type === 'date' || column.type === 'number') | ||
| ) { | ||
| setParseError(column.type === 'date' ? 'Invalid date' : 'Invalid number') | ||
| return | ||
| } | ||
| onSave(rowId, column.key, cleaned, 'blur') | ||
greptile-apps[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| onClose() | ||
| } | ||
| @@ -219,16 +235,23 @@ function ExpandedCellEditor({ | ||
| <textarea | ||
| ref={textareaRef} | ||
| value={draftValue} | ||
| onChange={(e) => setDraftValue(e.target.value)} | ||
| onChange={(e) => { | ||
| setDraftValue(e.target.value) | ||
| setParseError(null) | ||
| }} | ||
| onKeyDown={handleTextareaKeyDown} | ||
| className='min-h-0 flex-1 resize-none bg-transparent px-2.5 py-2 font-sans text-[var(--text-primary)] text-small outline-none placeholder:text-[var(--text-muted)]' | ||
| spellCheck={false} | ||
| autoCorrect='off' | ||
| /> | ||
| <div className='flex items-center justify-between border-[var(--border)] border-t bg-[var(--surface-2)] px-2 py-1.5'> | ||
| <span className='text-[var(--text-tertiary)] text-caption'> | ||
| <kbd className='font-mono'>↵</kbd> save · <kbd className='font-mono'>esc</kbd> cancel | ||
| </span> | ||
| {parseError ? ( | ||
| <span className='text-[var(--text-error)] text-caption'>{parseError}</span> | ||
| ) : ( | ||
| <span className='text-[var(--text-tertiary)] text-caption'> | ||
| <kbd className='font-mono'>↵</kbd> save · <kbd className='font-mono'>esc</kbd> cancel | ||
| </span> | ||
| )} | ||
| <div className='flex items-center gap-1.5'> | ||
| <Button variant='ghost' size='sm' onClick={onClose}> | ||
| Cancel | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| 'use client' | ||
| import { useCallback, useEffect, useRef, useState } from 'react' | ||
| import { Calendar, cn, Popover, PopoverAnchor, PopoverContent } from '@sim/emcn' | ||
| import { Calendar, cn, Popover, PopoverAnchor, PopoverContent, toast } from '@sim/emcn' | ||
| import type { ColumnDefinition } from '@/lib/table' | ||
| import type { SaveReason } from '../../../types' | ||
| import { | ||
| @@ -44,6 +44,7 @@ function InlineDateEditor({ | ||
| const [draft, setDraft] = useState(() => | ||
| initialCharacter !== undefined ? initialCharacter : storageToDisplay(storedValue) | ||
| ) | ||
| const [invalid, setInvalid] = useState(false) | ||
| const pickerValue = displayToStorage(draft) || storedValue || undefined | ||
| @@ -64,13 +65,24 @@ function InlineDateEditor({ | ||
| const doSave = useCallback( | ||
| (reason: SaveReason, storageVal?: string) => { | ||
| if (doneRef.current) return | ||
| doneRef.current = true | ||
| clearTimeout(blurTimeoutRef.current) | ||
| const raw = storageVal ?? displayToStorage(draft) ?? draft | ||
| const val = raw && !Number.isNaN(Date.parse(raw)) ? raw : null | ||
| onSave(val, reason) | ||
| if (raw && Number.isNaN(Date.parse(raw))) { | ||
| if (reason === 'blur') { | ||
| if (!invalid) toast.error('Invalid date') | ||
| doneRef.current = true | ||
| onCancel() | ||
| } else { | ||
| toast.error('Invalid date') | ||
| setInvalid(true) | ||
| inputRef.current?.focus() | ||
| } | ||
| return | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| doneRef.current = true | ||
| onSave(raw || null, reason) | ||
| }, | ||
| [draft, onSave] | ||
| [draft, invalid, onSave, onCancel] | ||
| ) | ||
| const handleKeyDown = useCallback( | ||
| @@ -116,12 +128,16 @@ function InlineDateEditor({ | ||
| ref={inputRef} | ||
| type='text' | ||
| value={draft} | ||
| onChange={(e) => setDraft(e.target.value)} | ||
| onChange={(e) => { | ||
| setDraft(e.target.value) | ||
| setInvalid(false) | ||
| }} | ||
| onKeyDown={handleKeyDown} | ||
| onBlur={handleBlur} | ||
| placeholder='mm/dd/yyyy' | ||
| className={cn( | ||
| 'w-full min-w-0 select-text border-none bg-transparent p-0 text-[var(--text-primary)] text-small outline-none' | ||
| 'w-full min-w-0 select-text border-none bg-transparent p-0 text-[var(--text-primary)] text-small outline-none', | ||
| invalid && 'text-[var(--text-error)]' | ||
| )} | ||
| /> | ||
| <Popover open onOpenChange={handlePickerOpenChange}> | ||
| @@ -146,6 +162,7 @@ function InlineTextEditor({ | ||
| const [draft, setDraft] = useState(() => | ||
| initialCharacter !== undefined ? initialCharacter : formatValueForInput(value, column.type) | ||
| ) | ||
| const [invalid, setInvalid] = useState(false) | ||
| const doneRef = useRef(false) | ||
| useEffect(() => { | ||
| @@ -161,14 +178,33 @@ function InlineTextEditor({ | ||
| } | ||
| }, []) | ||
| const rejectDraft = (message: string, reason: SaveReason) => { | ||
| if (reason === 'blur') { | ||
| if (!invalid) toast.error(message) | ||
| doneRef.current = true | ||
| onCancel() | ||
| } else { | ||
| toast.error(message) | ||
| setInvalid(true) | ||
| inputRef.current?.focus() | ||
| } | ||
| } | ||
| const doSave = (reason: SaveReason) => { | ||
| if (doneRef.current) return | ||
| doneRef.current = true | ||
| let cleaned: unknown | ||
| try { | ||
| onSave(cleanCellValue(draft, column), reason) | ||
| cleaned = cleanCellValue(draft, column) | ||
| } catch { | ||
| onCancel() | ||
| rejectDraft('Invalid JSON', reason) | ||
| return | ||
| } | ||
| if (column.type === 'number' && cleaned === null && draft.trim() !== '') { | ||
| rejectDraft('Invalid number', reason) | ||
| return | ||
| } | ||
| doneRef.current = true | ||
| onSave(cleaned, reason) | ||
| } | ||
| const handleKeyDown = (e: React.KeyboardEvent) => { | ||
| @@ -193,11 +229,17 @@ function InlineTextEditor({ | ||
| type='text' | ||
| inputMode={isNumber ? 'decimal' : undefined} | ||
| value={draft ?? ''} | ||
| onChange={(e) => setDraft(e.target.value)} | ||
| onChange={(e) => { | ||
| setDraft(e.target.value) | ||
| setInvalid(false) | ||
| }} | ||
| onKeyDown={handleKeyDown} | ||
| onWheel={handleEditorWheel} | ||
| onBlur={() => doSave('blur')} | ||
| className='w-full min-w-0 select-text border-none bg-transparent p-0 text-[var(--text-primary)] text-small outline-none' | ||
| className={cn( | ||
| 'w-full min-w-0 select-text border-none bg-transparent p-0 text-[var(--text-primary)] text-small outline-none', | ||
| invalid && 'text-[var(--text-error)]' | ||
| )} | ||
| /> | ||
| ) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.