File size: 1,659 Bytes
cd6f98e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { StateCreator } from "zustand";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";

import { createSelectors } from "./helpers";

interface Layout {
  showRightSidebar: boolean;
  showLogSidebar: boolean;
}

interface LayoutSlice {
  layout: Layout;
  setLayout: (layout: Partial<Layout>) => void;
}

interface OrganizationRole {
  id: string;
  name: string;
  role: string;
}

interface AuthSlice {
  organization: OrganizationRole | undefined;
  setOrganization: (orgRole: OrganizationRole | undefined) => void;
}

const createLayoutSlice: StateCreator<LayoutSlice> = (set, get) => {
  return {
    ...{
      layout: {
        showRightSidebar: false,
        showLogSidebar: false,
      },
    },
    setLayout: (layout: Partial<Layout>) => {
      if (layout.showLogSidebar) layout.showRightSidebar = false;
      if (layout.showRightSidebar) layout.showLogSidebar = false;

      set((prev) => ({
        layout: {
          ...prev.layout,
          ...layout,
        },
      }));
    },
  };
};

const createAuthSlice: StateCreator<AuthSlice> = (set, get) => {
  return {
    ...{ organization: undefined },
    setOrganization: (orgRole: OrganizationRole | undefined) => {
      set(() => ({
        organization: orgRole,
      }));
    },
  };
};

export const useConfigStore = createSelectors(
  create<LayoutSlice & AuthSlice>()(
    persist(
      (...a) => ({
        ...createLayoutSlice(...a),
        ...createAuthSlice(...a),
      }),
      {
        name: "reworkd-config-2",
        version: 1,
        storage: createJSONStorage(() => localStorage),
      }
    )
  )
);