mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
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:
1 parent
c9bf962ac0
commit
8166044d6e
23 files changed
+642
-334
No files matched your search
@@ -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>
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user