Fix eslint (#46)

* Fix mobile image upload and paste with improved file type handling

* Replace older sessions header with separator line

* Add ESLint no-console rules and fix lint errors

- Add eslint configuration for backend with no-console rule
- Add reusable error type guard utility
- Replace all error: any with error: unknown and type guards across backend
- Update logger to use console.warn/console.error only
- Remove unused variables and dead code functions
- Fix regex escape characters
- Update AGENTS.md with lint commands and requirements
This commit is contained in:
Chris Scott authored and GitHub committed 2026-01-03 15:19:28 -05:00
1 parent c9bf962ac0
commit 8166044d6e
23 files changed
+642 -334

No files matched your search

@@ -147,7 +147,7 @@ useEffect(() => {
}
}, [initialFileError])
const loadFiles = async (path: string) => {
const loadFiles = useCallback(async (path: string) => {
setLoading(true)
setError(null)
@@ -166,7 +166,7 @@ useEffect(() => {
} finally {
setLoading(false)
}
}
}, [onDirectoryLoad])
const handleFileSelect = useCallback(async (file: FileInfo) => {
if (file.isDirectory) {
@@ -274,7 +274,7 @@ useEffect(() => {
} : null)
await loadFiles(currentPath)
}, [currentPath, uploadSingleFile])
}, [currentPath, uploadSingleFile, loadFiles])
const handleUpload = useCallback(async (fileList: FileList) => {
const items = getUploadItemsFromFileList(fileList)
@@ -301,7 +301,7 @@ useEffect(() => {
} catch (err) {
setError(err instanceof Error ? err.message : 'Create failed')
}
}, [currentPath])
}, [currentPath, loadFiles])
const handleDelete = useCallback(async (path: string) => {
try {
@@ -318,7 +318,7 @@ useEffect(() => {
} catch (err) {
setError(err instanceof Error ? err.message : 'Delete failed')
}
}, [currentPath])
}, [currentPath, loadFiles])
const handleRename = useCallback(async (oldPath: string, newPath: string) => {
try {
@@ -336,7 +336,7 @@ useEffect(() => {
} catch (err) {
setError(err instanceof Error ? err.message : 'Rename failed')
}
}, [currentPath])
}, [currentPath, loadFiles])
const handleDragEnter = (e: React.DragEvent) => {
e.preventDefault()
@@ -370,7 +370,7 @@ useEffect(() => {
useEffect(() => {
loadFiles(basePath)
}, [basePath])
}, [basePath, loadFiles])
useEffect(() => {
const handleFileSaved = (event: CustomEvent<{ path: string; content: string }>) => {
@@ -383,19 +383,18 @@ useEffect(() => {
return () => window.removeEventListener('fileSaved', handleFileSaved as EventListener)
}, [selectedFile, handleFileSelect])
// Handle escape key to close modal
useEffect(() => {
if (!isPreviewModalOpen) return
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape' && isPreviewModalOpen) {
if (e.key === 'Escape') {
handleCloseModal()
}
}
if (isPreviewModalOpen) {
document.addEventListener('keydown', handleEscape)
return () => document.removeEventListener('keydown', handleEscape)
}
}, [isPreviewModalOpen])
document.addEventListener('keydown', handleEscape)
return () => document.removeEventListener('keydown', handleEscape)
}, [isPreviewModalOpen, handleCloseModal])
const filteredFiles = files?.children?.filter(file =>
file.name.toLowerCase().includes(searchQuery.toLowerCase())
+46 -27
View File
@@ -23,6 +23,16 @@ import type { components } from '@/api/opencode-types'
import type { MessageWithParts, FileInfo, ImageAttachment } from '@/api/types'
const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp", "image/heic", "image/heif"]
const revokeBlobUrls = (attachments: ImageAttachment[]) => {
attachments.forEach((attachment) => {
if (attachment.dataUrl.startsWith('blob:')) {
URL.revokeObjectURL(attachment.dataUrl)
}
})
}
const ACCEPTED_FILE_TYPES = [...ACCEPTED_IMAGE_TYPES, "application/pdf"]
@@ -64,31 +74,6 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
onPromptChange
}, ref) {
const [prompt, setPrompt] = useState('')
useImperativeHandle(ref, () => ({
setPromptValue: (value: string) => {
setPrompt(value)
if (textareaRef.current) {
textareaRef.current.style.height = 'auto'
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`
textareaRef.current.focus()
}
},
clearPrompt: () => {
setPrompt('')
setAttachedFiles(new Map())
setImageAttachments([])
setSelectedAgent(null)
if (textareaRef.current) {
textareaRef.current.style.height = 'auto'
textareaRef.current.focus()
}
},
triggerFileUpload: () => {
fileInputRef.current?.click()
}
}), [])
const [isBashMode, setIsBashMode] = useState(false)
const [showSuggestions, setShowSuggestions] = useState(false)
const [suggestionQuery, setSuggestionQuery] = useState('')
@@ -104,6 +89,31 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
const textareaRef = useRef<HTMLTextAreaElement>(null)
const fileInputRef = useRef<HTMLInputElement>(null)
useImperativeHandle(ref, () => ({
setPromptValue: (value: string) => {
setPrompt(value)
if (textareaRef.current) {
textareaRef.current.style.height = 'auto'
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`
textareaRef.current.focus()
}
},
clearPrompt: () => {
setPrompt('')
setAttachedFiles(new Map())
revokeBlobUrls(imageAttachments)
setImageAttachments([])
setSelectedAgent(null)
if (textareaRef.current) {
textareaRef.current.style.height = 'auto'
textareaRef.current.focus()
}
},
triggerFileUpload: () => {
fileInputRef.current?.click()
}
}), [imageAttachments])
const sendPrompt = useSendPrompt(opcodeUrl, directory)
const sendShell = useSendShell(opcodeUrl, directory)
const abortSession = useAbortSession(opcodeUrl, directory, sessionID)
@@ -170,6 +180,7 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
})
setPrompt('')
setAttachedFiles(new Map())
revokeBlobUrls(imageAttachments)
setImageAttachments([])
setSelectedAgent(null)
if (textareaRef.current) {
@@ -222,6 +233,7 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
setPrompt('')
setAttachedFiles(new Map())
revokeBlobUrls(imageAttachments)
setImageAttachments([])
setSelectedAgent(null)
if (textareaRef.current) {
@@ -386,7 +398,13 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
}
const removeImageAttachment = (id: string) => {
setImageAttachments((prev) => prev.filter((a) => a.id !== id))
setImageAttachments((prev) => {
const attachment = prev.find((a) => a.id === id)
if (attachment?.dataUrl.startsWith('blob:')) {
URL.revokeObjectURL(attachment.dataUrl)
}
return prev.filter((a) => a.id !== id)
})
}
const handlePaste = async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
@@ -481,7 +499,7 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
const handleFileInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.currentTarget.files?.[0]
if (file) {
if (file && ACCEPTED_FILE_TYPES.includes(file.type)) {
addImageAttachment(file)
}
event.currentTarget.value = ''
@@ -573,6 +591,7 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
setMentionQuery('')
setMentionRange(null)
setPrompt('')
revokeBlobUrls(imageAttachments)
setImageAttachments([])
if (textareaRef.current) {
textareaRef.current.style.height = 'auto'
@@ -1,7 +1,8 @@
import { useState, useEffect } from 'react'
import { useState, useEffect, useMemo, useRef, useCallback } from 'react'
import { useSettings } from '@/hooks/useSettings'
import { Loader2 } from 'lucide-react'
import { DEFAULT_KEYBOARD_SHORTCUTS } from '@/api/types/settings'
import { useMobile } from '@/hooks/useMobile'
import { Loader2, X } from 'lucide-react'
import { DEFAULT_KEYBOARD_SHORTCUTS, DEFAULT_LEADER_KEY } from '@/api/types/settings'
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0
const CMD_KEY = isMac ? 'Cmd' : 'Ctrl'
@@ -10,94 +11,121 @@ const normalizeShortcut = (shortcut: string): string => {
return shortcut.replace(/Cmd/g, CMD_KEY)
}
const DEFAULT_DIRECT_SHORTCUTS = ['submit', 'abort']
export function KeyboardShortcuts() {
const { preferences, isLoading, updateSettings } = useSettings()
const isMobile = useMobile()
const [recordingKey, setRecordingKey] = useState<string | null>(null)
const [recordingLeader, setRecordingLeader] = useState(false)
const [tempShortcuts, setTempShortcuts] = useState<Record<string, string>>({})
const [tempLeaderKey, setTempLeaderKey] = useState<string | null>(null)
const [currentKeys, setCurrentKeys] = useState<string>('')
const shortcuts = { ...DEFAULT_KEYBOARD_SHORTCUTS, ...preferences?.keyboardShortcuts, ...tempShortcuts }
const leaderKey = tempLeaderKey ?? preferences?.leaderKey ?? DEFAULT_LEADER_KEY
const directShortcuts = preferences?.directShortcuts ?? DEFAULT_DIRECT_SHORTCUTS
const handleKeyDown = (e: KeyboardEvent, action: string) => {
e.preventDefault()
const keys = []
if (e.ctrlKey) keys.push('Ctrl')
if (e.metaKey) keys.push('Cmd')
if (e.altKey) keys.push('Alt')
if (e.shiftKey) keys.push('Shift')
// Get the actual key pressed (excluding modifier keys)
const mainKey = e.key
if (!['Control', 'Meta', 'Alt', 'Shift'].includes(mainKey)) {
// Handle special keys
let displayKey = mainKey
if (mainKey === ' ') displayKey = 'Space'
else if (mainKey === 'ArrowUp') displayKey = 'Up'
else if (mainKey === 'ArrowDown') displayKey = 'Down'
else if (mainKey === 'ArrowLeft') displayKey = 'Left'
else if (mainKey === 'ArrowRight') displayKey = 'Right'
else if (mainKey === 'Enter') displayKey = 'Return'
else if (mainKey === 'Escape') displayKey = 'Esc'
else if (mainKey === 'Tab') displayKey = 'Tab'
else if (mainKey === 'Backspace') displayKey = 'Backspace'
else if (mainKey === 'Delete') displayKey = 'Delete'
else if (mainKey.length === 1) displayKey = mainKey.toUpperCase()
keys.push(displayKey)
// Only complete recording if we have a non-modifier key
if (keys.length > 0) {
const shortcut = keys.join('+')
setTempShortcuts(prev => ({ ...prev, [action]: shortcut }))
setRecordingKey(null)
setCurrentKeys('')
updateSettings({
keyboardShortcuts: { ...shortcuts, [action]: shortcut }
})
}
} else {
// Show current modifier keys being held
setCurrentKeys(keys.join('+'))
}
}
const shortcuts = useMemo(() => ({
...DEFAULT_KEYBOARD_SHORTCUTS,
...preferences?.keyboardShortcuts,
...tempShortcuts
}), [preferences?.keyboardShortcuts, tempShortcuts])
const handleKeyUp = (e: KeyboardEvent) => {
// Clear current keys display when modifiers are released
if (['Control', 'Meta', 'Alt', 'Shift'].includes(e.key)) {
setCurrentKeys('')
}
}
const shortcutsRef = useRef(shortcuts)
shortcutsRef.current = shortcuts
const updateSettingsRef = useRef(updateSettings)
updateSettingsRef.current = updateSettings
const startRecording = (action: string) => {
setRecordingKey(action)
setRecordingLeader(false)
setCurrentKeys('')
}
const stopRecording = () => {
const startRecordingLeader = () => {
setRecordingLeader(true)
setRecordingKey(null)
setCurrentKeys('')
}
const stopRecording = useCallback(() => {
setRecordingKey(null)
setRecordingLeader(false)
setCurrentKeys('')
}, [])
const clearShortcut = useCallback((action: string) => {
setTempShortcuts(prev => ({ ...prev, [action]: '' }))
updateSettingsRef.current({
keyboardShortcuts: { ...shortcutsRef.current, [action]: '' }
})
}, [])
useEffect(() => {
if (recordingKey) {
const handleGlobalKeyDown = (e: KeyboardEvent) => {
handleKeyDown(e, recordingKey)
}
if (!recordingKey && !recordingLeader) return
const handleKeyDown = (e: KeyboardEvent) => {
e.preventDefault()
const handleGlobalKeyUp = (e: KeyboardEvent) => {
handleKeyUp(e)
}
const keys = []
if (e.ctrlKey) keys.push('Ctrl')
if (e.metaKey) keys.push('Cmd')
if (e.altKey) keys.push('Alt')
if (e.shiftKey) keys.push('Shift')
document.addEventListener('keydown', handleGlobalKeyDown)
document.addEventListener('keyup', handleGlobalKeyUp)
return () => {
document.removeEventListener('keydown', handleGlobalKeyDown)
document.removeEventListener('keyup', handleGlobalKeyUp)
const mainKey = e.key
if (!['Control', 'Meta', 'Alt', 'Shift'].includes(mainKey)) {
let displayKey = mainKey
if (mainKey === ' ') displayKey = 'Space'
else if (mainKey === 'ArrowUp') displayKey = 'Up'
else if (mainKey === 'ArrowDown') displayKey = 'Down'
else if (mainKey === 'ArrowLeft') displayKey = 'Left'
else if (mainKey === 'ArrowRight') displayKey = 'Right'
else if (mainKey === 'Enter') displayKey = 'Return'
else if (mainKey === 'Escape') displayKey = 'Esc'
else if (mainKey === 'Tab') displayKey = 'Tab'
else if (mainKey === 'Backspace') displayKey = 'Backspace'
else if (mainKey === 'Delete') displayKey = 'Delete'
else if (mainKey.length === 1) displayKey = mainKey.toUpperCase()
keys.push(displayKey)
if (keys.length > 0) {
const shortcut = keys.join('+')
if (recordingLeader) {
setTempLeaderKey(shortcut)
setRecordingLeader(false)
setCurrentKeys('')
updateSettingsRef.current({ leaderKey: shortcut })
} else if (recordingKey) {
setTempShortcuts(prev => ({ ...prev, [recordingKey]: shortcut }))
setRecordingKey(null)
setCurrentKeys('')
updateSettingsRef.current({
keyboardShortcuts: { ...shortcutsRef.current, [recordingKey]: shortcut }
})
}
}
} else {
setCurrentKeys(keys.join('+'))
}
}
}, [recordingKey, handleKeyDown, handleKeyUp])
const handleKeyUp = (e: KeyboardEvent) => {
if (['Control', 'Meta', 'Alt', 'Shift'].includes(e.key)) {
setCurrentKeys('')
}
}
document.addEventListener('keydown', handleKeyDown)
document.addEventListener('keyup', handleKeyUp)
return () => {
document.removeEventListener('keydown', handleKeyDown)
document.removeEventListener('keyup', handleKeyUp)
}
}, [recordingKey, recordingLeader])
if (isLoading) {
return (
@@ -107,17 +135,83 @@ export function KeyboardShortcuts() {
)
}
const formatShortcutDisplay = (action: string, keys: string) => {
if (!keys) return 'Not set'
if (directShortcuts.includes(action)) {
return normalizeShortcut(keys)
}
return `${normalizeShortcut(leaderKey)} → ${normalizeShortcut(keys)}`
}
const toggleDirectShortcut = (action: string) => {
const newDirectShortcuts = directShortcuts.includes(action)
? directShortcuts.filter(s => s !== action)
: [...directShortcuts, action]
updateSettings({ directShortcuts: newDirectShortcuts })
}
if (isMobile) {
return (
<div className="bg-card border border-border rounded-lg p-6">
<h2 className="text-lg font-semibold text-foreground mb-4">Keyboard Shortcuts</h2>
<p className="text-sm text-muted-foreground">
Keyboard shortcuts are not available on mobile devices.
</p>
</div>
)
}
return (
<div className="bg-card border border-border rounded-lg p-6">
<h2 className="text-lg font-semibold text-foreground mb-6">Keyboard Shortcuts</h2>
<div className="space-y-4">
<div className="flex items-center justify-between py-3 border-b border-border">
<div className="space-y-1">
<p className="text-foreground font-medium">Leader Key</p>
<p className="text-xs text-muted-foreground">Press this first, then the shortcut key</p>
</div>
{recordingLeader ? (
<div className="flex items-center gap-2">
<input
type="text"
className="px-3 py-1.5 bg-accent border border-primary rounded text-sm text-foreground font-mono outline-none"
placeholder="Press keys..."
value={currentKeys || ''}
autoFocus
onBlur={stopRecording}
readOnly
/>
<button
onClick={stopRecording}
className="px-2 py-1 text-xs text-muted-foreground hover:text-foreground"
>
Cancel
</button>
</div>
) : (
<button
onClick={startRecordingLeader}
className="px-3 py-1.5 bg-primary/20 border border-primary/50 hover:border-primary rounded text-sm text-foreground font-mono transition-colors"
>
{normalizeShortcut(leaderKey)}
</button>
)}
</div>
{Object.entries(shortcuts).map(([action, keys]) => (
<div key={action} className="flex items-center justify-between py-3 border-b border-border last:border-0">
<div className="space-y-1">
<p className="text-foreground font-medium capitalize">
{action.replace(/([A-Z])/g, ' $1').trim()}
</p>
<button
onClick={() => toggleDirectShortcut(action)}
className="text-xs text-muted-foreground hover:text-foreground transition-colors"
>
{directShortcuts.includes(action) ? 'Direct (click to require leader)' : 'Requires leader key (click to make direct)'}
</button>
</div>
{recordingKey === action ? (
@@ -139,19 +233,30 @@ export function KeyboardShortcuts() {
</button>
</div>
) : (
<button
onClick={() => startRecording(action)}
className="px-3 py-1.5 bg-accent border border-border hover:border-border rounded text-sm text-foreground font-mono transition-colors"
>
{normalizeShortcut(keys)}
</button>
<div className="flex items-center gap-2">
<button
onClick={() => startRecording(action)}
className={`px-3 py-1.5 bg-accent border border-border hover:border-border rounded text-sm font-mono transition-colors ${keys ? 'text-foreground' : 'text-muted-foreground italic'}`}
>
{formatShortcutDisplay(action, keys)}
</button>
{keys && (
<button
onClick={() => clearShortcut(action)}
className="p-1 text-muted-foreground hover:text-destructive transition-colors"
title="Clear shortcut"
>
<X className="w-4 h-4" />
</button>
)}
</div>
)}
</div>
))}
</div>
<p className="mt-6 text-sm text-muted-foreground">
Click on any shortcut to record a new key combination
Click on any shortcut to record a new key combination. Click on the status text below each action to toggle whether it requires the leader key.
</p>
</div>
)