diff options
| author | zwlucas <lucas.fariamo08@gmail.com> | 2026-06-05 20:52:54 +0000 |
|---|---|---|
| committer | zwlucas <lucas.fariamo08@gmail.com> | 2026-06-05 20:52:54 +0000 |
| commit | 09f964451d7d92e9891430ec4595c1276d486aab (patch) | |
| tree | 28da4483f5c28924a8c47fceb648b1baebe88224 /src/stores/useCommandStore.ts | |
| download | yace-09f964451d7d92e9891430ec4595c1276d486aab.tar.gz yace-09f964451d7d92e9891430ec4595c1276d486aab.zip | |
Signed-off-by: zwlucas <lucas.fariamo08@gmail.com>
Diffstat (limited to 'src/stores/useCommandStore.ts')
| -rw-r--r-- | src/stores/useCommandStore.ts | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/stores/useCommandStore.ts b/src/stores/useCommandStore.ts new file mode 100644 index 0000000..2975436 --- /dev/null +++ b/src/stores/useCommandStore.ts @@ -0,0 +1,137 @@ +import { create } from 'zustand' +import { useTabsStore } from './useTabsStore' +import { useFilesStore } from './useFilesStore' +import { useLayoutStore } from './useLayoutStore' + +export interface Command { + id: string + label: string + shortcut?: string + category: string + action: () => void +} + +export interface CommandState { + isOpen: boolean + query: string + selectedIndex: number + commands: Command[] + open: () => void + close: () => void + toggle: () => void + setQuery: (query: string) => void + setSelectedIndex: (index: number) => void + executeSelected: () => void +} + +function buildCommands(): Command[] { + const filesStore = useFilesStore.getState() + const layoutStore = useLayoutStore.getState() + + return [ + { + id: 'open-folder', + label: 'Open Folder', + shortcut: 'Ctrl+Shift+K', + category: 'File', + action: () => filesStore.openFolder(), + }, + { + id: 'toggle-sidebar', + label: 'Toggle Sidebar', + shortcut: 'Ctrl+B', + category: 'View', + action: () => layoutStore.toggleSidePanel(), + }, + { + id: 'save-file', + label: 'Save File', + shortcut: 'Ctrl+S', + category: 'File', + action: () => { + const state = useTabsStore.getState() + if (state.activeTabId) state.saveFile(state.activeTabId) + }, + }, + { + id: 'close-tab', + label: 'Close Current Tab', + shortcut: 'Ctrl+W', + category: 'File', + action: () => { + const state = useTabsStore.getState() + if (state.activeTabId) state.closeTab(state.activeTabId) + }, + }, + { + id: 'close-all-tabs', + label: 'Close All Tabs', + shortcut: '', + category: 'File', + action: () => { + const state = useTabsStore.getState() + state.tabs.forEach((t) => state.closeTab(t.id)) + }, + }, + { + id: 'show-explorer', + label: 'Focus Explorer', + shortcut: '', + category: 'View', + action: () => layoutStore.setActiveView('explorer'), + }, + { + id: 'show-search', + label: 'Show Search', + shortcut: '', + category: 'View', + action: () => layoutStore.setActiveView('search'), + }, + ] +} + +export const useCommandStore = create<CommandState>((set, get) => ({ + isOpen: false, + query: '', + selectedIndex: 0, + commands: [], + + open: () => { + set({ isOpen: true, query: '', selectedIndex: 0, commands: buildCommands() }) + }, + + close: () => { + set({ isOpen: false, query: '', selectedIndex: 0 }) + }, + + toggle: () => { + const { isOpen } = get() + if (isOpen) { + get().close() + } else { + get().open() + } + }, + + setQuery: (query: string) => { + set({ query, selectedIndex: 0 }) + }, + + setSelectedIndex: (index: number) => { + set({ selectedIndex: index }) + }, + + executeSelected: () => { + const { commands, query, selectedIndex } = get() + const filtered = commands.filter( + (c) => + c.label.toLowerCase().includes(query.toLowerCase()) || + c.category.toLowerCase().includes(query.toLowerCase()) + ) + const cmd = filtered[selectedIndex] + if (cmd) { + cmd.action() + get().close() + } + }, +})) |