aboutsummaryrefslogtreecommitdiff
path: root/src/stores/useCommandStore.ts
blob: 29754367f20926b5811efbac550d71cbed9f5d8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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()
    }
  },
}))