|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { createContext, useContext } from "react" |
| 7 | +import { createStore, useStore } from "zustand" |
| 8 | +import { devtools } from "zustand/middleware" |
| 9 | + |
| 10 | +import createFiltersSlice from "../lib/slices/createFiltersSlice" |
| 11 | +import createAuthDataSlice from "../lib/slices/createAuthDataSlice" |
| 12 | +import createGlobalsSlice from "../lib/slices/createGlobalsSlice" |
| 13 | +import createUserActivitySlice from "../lib/slices/createUserActivitySlice" |
| 14 | + |
| 15 | +// Entity constants |
| 16 | +const ISSUEMATCHES = "IssueMatches" |
| 17 | +const SERVICES = "Services" |
| 18 | +const COMPONENTS = "Components" |
| 19 | + |
| 20 | +// @ts-expect-error TS(2554): Expected 1 arguments, but got 0. |
| 21 | +const StoreContext = createContext() |
| 22 | + |
| 23 | +export const StoreProvider = ({ options, children }: any) => { |
| 24 | + return ( |
| 25 | + <StoreContext.Provider |
| 26 | + value={createStore( |
| 27 | + devtools((set, get) => ({ |
| 28 | + ...createGlobalsSlice(set, get, options), |
| 29 | + ...createAuthDataSlice(set, get), |
| 30 | + // @ts-expect-error TS(2554): Expected 1 arguments, but got 2. |
| 31 | + ...createUserActivitySlice(set, get), |
| 32 | + ...createFiltersSlice(set, get), |
| 33 | + })) |
| 34 | + )} |
| 35 | + > |
| 36 | + {children} |
| 37 | + </StoreContext.Provider> |
| 38 | + ) |
| 39 | +} |
| 40 | +// @ts-ignore |
| 41 | +const useAppStore = (selector: any) => useStore(useContext(StoreContext), selector) |
| 42 | + |
| 43 | +// Globals exports |
| 44 | +export const useGlobalsEmbedded = () => useAppStore((state: any) => state.globals.embedded) |
| 45 | +export const useGlobalsQueryClientFnReady = () => useAppStore((state: any) => state.globals.queryClientFnReady) |
| 46 | +export const useGlobalsActiveView = () => useAppStore((state: any) => state.globals.activeView) |
| 47 | +export const useGlobalsQueryOptions = (viewName: any) => |
| 48 | + useAppStore((state: any) => state.globals.views[viewName].queryOptions) |
| 49 | +export const useGlobalsApiEndpoint = () => useAppStore((state: any) => state.globals.apiEndpoint) |
| 50 | +export const useGlobalsShowPanel = () => useAppStore((state: any) => state.globals.showPanel) |
| 51 | +export const useGlobalsShowServiceDetail = () => useAppStore((state: any) => state.globals.showServiceDetail) |
| 52 | +export const useGlobalsShowIssueDetail = () => useAppStore((state: any) => state.globals.showIssueDetail) |
| 53 | +export const useGlobalsActions = () => useAppStore((state: any) => state.globals.actions) |
| 54 | + |
| 55 | +// AUTH exports |
| 56 | +export const useAuthData = () => useAppStore((state: any) => state.auth.data) |
| 57 | +export const useAuthIsProcessing = () => useAppStore((state: any) => state.auth.isProcessing) |
| 58 | +export const useAuthLoggedIn = () => useAppStore((state: any) => state.auth.loggedIn) |
| 59 | +export const useAuthError = () => useAppStore((state: any) => state.auth.error) |
| 60 | +export const useAuthLastAction = () => useAppStore((state: any) => state.auth.lastAction) |
| 61 | +export const useAuthAppLoaded = () => useAppStore((state: any) => state.auth.appLoaded) |
| 62 | +export const useAuthAppIsLoading = () => useAppStore((state: any) => state.auth.appIsLoading) |
| 63 | +export const useAuthActions = () => useAppStore((state: any) => state.auth.actions) |
| 64 | + |
| 65 | +// UserActivity exports |
| 66 | +export const useUserIsActive = () => useAppStore((state: any) => state.userActivity.isActive) |
| 67 | + |
| 68 | +export const useUserActivityActions = () => useAppStore((state: any) => state.userActivity.actions) |
| 69 | + |
| 70 | +// Filter exports for Issues |
| 71 | +export const useIssueMatchesFilterLabels = () => useAppStore((state: any) => state.filters[ISSUEMATCHES].labels) |
| 72 | +export const useIssueMatchesActiveFilters = () => useAppStore((state: any) => state.filters[ISSUEMATCHES].activeFilters) |
| 73 | +export const useIssueMatchesSearchTerm = () => useAppStore((state: any) => state.filters[ISSUEMATCHES].search) |
| 74 | +export const useIssueMatchesFilterLabelValues = () => |
| 75 | + useAppStore((state: any) => state.filters[ISSUEMATCHES].filterLabelValues) |
| 76 | +export const useIssueMatchesPredefinedFilters = () => |
| 77 | + useAppStore((state: any) => state.filters[ISSUEMATCHES].predefinedFilters) |
| 78 | +export const useIssueMatchesActivePredefinedFilter = () => |
| 79 | + useAppStore((state: any) => state.filters[ISSUEMATCHES].activePredefinedFilter) |
| 80 | + |
| 81 | +// Filter exports for Services |
| 82 | +export const useServiceFilterLabels = () => useAppStore((state: any) => state.filters[SERVICES].labels) |
| 83 | +export const useServiceActiveFilters = () => useAppStore((state: any) => state.filters[SERVICES].activeFilters) |
| 84 | +export const useServiceSearchTerm = () => useAppStore((state: any) => state.filters[SERVICES].search) |
| 85 | +export const useServiceFilterLabelValues = () => useAppStore((state: any) => state.filters[SERVICES].filterLabelValues) |
| 86 | +export const useServicePredefinedFilters = () => useAppStore((state: any) => state.filters[SERVICES].predefinedFilters) |
| 87 | +export const useServiceActivePredefinedFilter = () => |
| 88 | + useAppStore((state: any) => state.filters[SERVICES].activePredefinedFilter) |
| 89 | + |
| 90 | +// Filter exports for Components |
| 91 | +export const useComponentFilterLabels = () => useAppStore((state: any) => state.filters[COMPONENTS].labels) |
| 92 | +export const useComponentActiveFilters = () => useAppStore((state: any) => state.filters[COMPONENTS].activeFilters) |
| 93 | +export const useComponentSearchTerm = () => useAppStore((state: any) => state.filters[COMPONENTS].search) |
| 94 | +export const useComponentFilterLabelValues = () => |
| 95 | + useAppStore((state: any) => state.filters[COMPONENTS].filterLabelValues) |
| 96 | +export const useComponentPredefinedFilters = () => |
| 97 | + useAppStore((state: any) => state.filters[COMPONENTS].predefinedFilters) |
| 98 | +export const useComponentActivePredefinedFilter = () => |
| 99 | + useAppStore((state: any) => state.filters[COMPONENTS].activePredefinedFilter) |
| 100 | + |
| 101 | +// Filter actions |
| 102 | +export const useFilterActions = () => useAppStore((state: any) => state.filters.actions) |
| 103 | + |
| 104 | +export default StoreProvider |
0 commit comments