aboutsummaryrefslogtreecommitdiff
path: root/src/stores/useLayoutStore.ts
blob: adcdae72084b0a58ba62a54152869bb7d2b702e5 (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
import { create } from 'zustand'

export type ActivityView = 'explorer' | 'search' | 'extensions' | 'settings'

interface LayoutState {
  activeView: ActivityView | null
  sidePanelVisible: boolean
  sidePanelWidth: number
  setActiveView: (view: ActivityView | null) => void
  toggleSidePanel: () => void
  showSidePanel: () => void
  hideSidePanel: () => void
  setSidePanelWidth: (width: number) => void
}

const SIDEPANEL_MIN = 180
const SIDEPANEL_MAX = 500
const SIDEPANEL_DEFAULT = 240

export const useLayoutStore = create<LayoutState>((set, get) => ({
  activeView: 'explorer',
  sidePanelVisible: true,
  sidePanelWidth: SIDEPANEL_DEFAULT,

  setActiveView: (view: ActivityView | null) => {
    const { activeView, hideSidePanel, showSidePanel } = get()
    if (activeView === view) {
      hideSidePanel()
      set({ activeView: null })
    } else {
      set({ activeView: view })
      showSidePanel()
    }
  },

  toggleSidePanel: () => {
    set((s) => ({ sidePanelVisible: !s.sidePanelVisible }))
  },

  showSidePanel: () => {
    set({ sidePanelVisible: true })
  },

  hideSidePanel: () => {
    set({ sidePanelVisible: false })
  },

  setSidePanelWidth: (width: number) => {
    set({ sidePanelWidth: Math.max(SIDEPANEL_MIN, Math.min(SIDEPANEL_MAX, width)) })
  },
}))