"use client" import { useAccount } from "@/lib/providers/jazz-provider" import { useState, useCallback, useEffect, useRef } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { toast } from "sonner" import { Icon } from "../la-editor/components/ui/icon" import { motion } from "framer-motion" import { useKeybind } from "@/lib/providers/keybind-provider" // Import the hook const MODIFIER_KEYS = ["Control", "Alt", "Shift", "Meta"] const HotkeyInput = ({ label, value, onChange }: { label: string value: string onChange: (value: string) => void }) => { const [recording, setRecording] = useState(false) const [currentKeys, setCurrentKeys] = useState([]) const [isHovering, setIsHovering] = useState(false) const recordingTimeoutRef = useRef(null) const stopRecording = useCallback(() => { setRecording(false) setCurrentKeys([]) }, []) const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { e.preventDefault() if (!recording) return const key = e.key === " " ? "Space" : e.key if (!currentKeys.includes(key)) { setCurrentKeys(prev => { const newKeys = [...prev, key] return newKeys.slice(-3) }) } // Clear the timeout on each keydown if (recordingTimeoutRef.current) { clearTimeout(recordingTimeoutRef.current) } }, [recording, currentKeys] ) const handleKeyUp = useCallback( (e: React.KeyboardEvent) => { if (!recording) return const key = e.key === " " ? "Space" : e.key if (MODIFIER_KEYS.includes(key)) return if (currentKeys.length > 0) { onChange(currentKeys.join("+")) // Set a timeout to stop recording if no key is pressed recordingTimeoutRef.current = setTimeout(stopRecording, 500) } }, [recording, currentKeys, onChange, stopRecording] ) const handleClearKeybind = () => { onChange("") setCurrentKeys([]) } useEffect(() => { if (recording) { const handleKeyDownEvent = (e: KeyboardEvent) => handleKeyDown(e as unknown as React.KeyboardEvent) const handleKeyUpEvent = (e: KeyboardEvent) => handleKeyUp(e as unknown as React.KeyboardEvent) window.addEventListener("keydown", handleKeyDownEvent) window.addEventListener("keyup", handleKeyUpEvent) return () => { window.removeEventListener("keydown", handleKeyDownEvent) window.removeEventListener("keyup", handleKeyUpEvent) if (recordingTimeoutRef.current) { clearTimeout(recordingTimeoutRef.current) } } } }, [recording, handleKeyDown, handleKeyUp]) return (
setIsHovering(true)} onMouseLeave={() => setIsHovering(false)} > setRecording(true)} onBlur={stopRecording} /> {isHovering && value && ( )}
) } export const SettingsRoute = () => { const [inboxHotkey, setInboxHotkey] = useState("") const [topInboxHotkey, setTopInboxHotkey] = useState("") const { addKeybind, removeKeybind } = useKeybind() const prevInboxHotkeyRef = useRef("") const prevTopInboxHotkeyRef = useRef("") const updateKeybind = useCallback( (prevKey: string, newKey: string, action: string, setter: React.Dispatch>) => { if (prevKey) removeKeybind(prevKey) if (newKey) { const existingKeybind = [inboxHotkey, topInboxHotkey].find(hotkey => hotkey === newKey && hotkey !== prevKey) if (existingKeybind) { removeKeybind(existingKeybind) if (existingKeybind === inboxHotkey) { setInboxHotkey("") prevInboxHotkeyRef.current = "" } else { setTopInboxHotkey("") prevTopInboxHotkeyRef.current = "" } toast.info("Keybind conflict resolved", { description: `The keybind "${newKey}" was removed from its previous action.` }) } addKeybind({ key: newKey, callback: () => console.log(`${action} action`) }) setter(newKey) // Update the state with the new keybind } }, [addKeybind, removeKeybind, inboxHotkey, topInboxHotkey] ) const saveSettings = () => { updateKeybind(prevInboxHotkeyRef.current, inboxHotkey, "Save to Inbox", setInboxHotkey) updateKeybind(prevTopInboxHotkeyRef.current, topInboxHotkey, "Save to Inbox (Top)", setTopInboxHotkey) prevInboxHotkeyRef.current = inboxHotkey prevTopInboxHotkeyRef.current = topInboxHotkey } return (

Settings

) }