import { useState, useEffect, useCallback } from 'react' import { View, Text, Pressable, TextInput, StyleSheet, ScrollView, Switch } from 'react-native' import { ChevronLeft } from 'lucide-react-native' import AsyncStorage from '@react-native-async-storage/async-storage' import { colors, spacing, radii, typography } from '../theme/mobile-theme' import { BottomDrawer } from './BottomDrawer' const STORAGE_KEY = 'orca:custom-accessory-keys' export type CustomKey = { id: string label: string bytes: string enter: boolean } type Step = 'choose-type' | 'pick-ctrl' | 'pick-alt' | 'text-macro' const ALPHA_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('') function ctrlBytes(letter: string): string { return String.fromCharCode(letter.toUpperCase().charCodeAt(0) - 64) } function altBytes(letter: string): string { return `\x1b${letter.toLowerCase()}` } type Props = { visible: boolean onClose: () => void onKeysChanged: (keys: CustomKey[]) => void } export async function loadCustomKeys(): Promise { try { const raw = await AsyncStorage.getItem(STORAGE_KEY) return raw ? (JSON.parse(raw) as CustomKey[]) : [] } catch { return [] } } async function saveCustomKeys(keys: CustomKey[]): Promise { await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(keys)) } export function CustomKeyModal({ visible, onClose, onKeysChanged }: Props) { const [step, setStep] = useState('choose-type') const [macroLabel, setMacroLabel] = useState('') const [macroText, setMacroText] = useState('') const [macroEnter, setMacroEnter] = useState(true) useEffect(() => { if (visible) { setStep('choose-type') setMacroLabel('') setMacroText('') setMacroEnter(true) } }, [visible]) const addKey = useCallback( async (key: Omit) => { const existing = await loadCustomKeys() const newKey: CustomKey = { ...key, id: `custom-${Date.now()}` } const updated = [...existing, newKey] await saveCustomKeys(updated) onKeysChanged(updated) onClose() }, [onClose, onKeysChanged] ) const handleCtrlKey = useCallback( (letter: string) => { void addKey({ label: `Ctrl+${letter}`, bytes: ctrlBytes(letter), enter: false }) }, [addKey] ) const handleAltKey = useCallback( (letter: string) => { void addKey({ label: `Alt+${letter}`, bytes: altBytes(letter), enter: false }) }, [addKey] ) const handleMacroSave = useCallback(() => { const label = macroLabel.trim() || macroText.trim().slice(0, 12) const text = macroText if (!label || !text) return const bytes = macroEnter ? `${text}\r` : text void addKey({ label, bytes, enter: false }) }, [addKey, macroLabel, macroText, macroEnter]) const showBack = step !== 'choose-type' return ( {showBack ? ( [styles.backButton, pressed && styles.backButtonPressed]} onPress={() => setStep('choose-type')} accessibilityLabel="Back" > ) : ( )} {step === 'choose-type' && 'Add Shortcut'} {step === 'pick-ctrl' && 'Ctrl + Key'} {step === 'pick-alt' && 'Alt + Key'} {step === 'text-macro' && 'Text Macro'} {step === 'choose-type' && ( [styles.row, pressed && styles.rowPressed]} onPress={() => setStep('pick-ctrl')} > Ctrl + Key Control character shortcuts [styles.row, pressed && styles.rowPressed]} onPress={() => setStep('pick-alt')} > Alt + Key Alt/Option key combos [styles.row, pressed && styles.rowPressed]} onPress={() => setStep('text-macro')} > Text Macro Send custom text command )} {(step === 'pick-ctrl' || step === 'pick-alt') && ( {ALPHA_KEYS.map((letter) => ( [styles.keyCell, pressed && styles.keyCellPressed]} onPress={() => step === 'pick-ctrl' ? handleCtrlKey(letter) : handleAltKey(letter) } > {letter} ))} )} {step === 'text-macro' && ( Label Command Press Enter Add Shortcut )} ) } const styles = StyleSheet.create({ header: { flexDirection: 'row', alignItems: 'center', paddingBottom: spacing.sm }, backButton: { width: 30, height: 30, borderRadius: 15, alignItems: 'center', justifyContent: 'center' }, backButtonPressed: { backgroundColor: colors.bgRaised }, backSpacer: { width: 30 }, title: { flex: 1, fontSize: 15, fontWeight: '600', color: colors.textPrimary, textAlign: 'center' }, group: { backgroundColor: colors.bgPanel, borderRadius: 12, overflow: 'hidden' }, separator: { height: StyleSheet.hairlineWidth, backgroundColor: colors.borderSubtle, marginHorizontal: spacing.md }, row: { paddingVertical: spacing.md, paddingHorizontal: spacing.md + 2 }, rowPressed: { backgroundColor: colors.bgRaised }, rowLabel: { fontSize: typography.bodySize, fontWeight: '500', color: colors.textPrimary, marginBottom: 1 }, rowHint: { fontSize: 12, color: colors.textMuted }, keyGridScroll: { maxHeight: 240 }, keyGrid: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.xs, justifyContent: 'center', padding: spacing.md }, keyCell: { width: 42, height: 38, borderRadius: radii.button, backgroundColor: colors.bgBase, alignItems: 'center', justifyContent: 'center' }, keyCellPressed: { backgroundColor: colors.bgRaised }, keyCellText: { fontSize: 15, fontWeight: '600', color: colors.textPrimary, fontFamily: typography.monoFamily }, macroForm: { padding: spacing.md, gap: spacing.sm }, fieldLabel: { fontSize: 13, fontWeight: '500', color: colors.textSecondary }, fieldInput: { backgroundColor: colors.bgBase, color: colors.textPrimary, borderRadius: radii.input, paddingHorizontal: spacing.md, paddingVertical: spacing.sm, fontSize: 14, fontFamily: typography.monoFamily, borderWidth: 1, borderColor: colors.borderSubtle }, switchRow: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingVertical: spacing.xs }, switchLabel: { fontSize: typography.bodySize, color: colors.textPrimary }, saveButton: { backgroundColor: colors.textPrimary, paddingVertical: spacing.sm + 2, borderRadius: radii.button, alignItems: 'center' }, saveButtonDisabled: { opacity: 0.5 }, saveButtonText: { color: colors.bgBase, fontSize: typography.bodySize, fontWeight: '600' } })