PluginProbe
Extendify / 0.5.0
Extendify v0.5.0
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / state / Templates.js

Templates.js in Extendify 0.5.0, at src/state/Templates.js

131 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import create from 'zustand'
2 import { subscribeWithSelector } from 'zustand/middleware'
3 import { useGlobalStore } from './GlobalState'
4 import { useTaxonomyStore } from './Taxonomies'
5 import { useUserStore } from './User'
6
7 const defaultCategoryForType = (tax) =>
8 tax === 'siteType'
9 ? { slug: '', title: 'Unknown' }
10 : { slug: '', title: 'Featured' }
11
12 export const useTemplatesStore = create(
13 subscribeWithSelector((set, get) => ({
14 templates: [],
15 skipNextFetch: false,
16 fetchToken: null,
17 taxonomyDefaultState: {},
18 nextPage: '',
19 searchParams: {
20 taxonomies: {},
21 type: 'pattern',
22 },
23 initTemplateData() {
24 set({ activeTemplate: {} })
25 get().setupDefaultTaxonomies()
26 get().updateType(useGlobalStore.getState().currentType)
27 },
28 appendTemplates: (templates) =>
29 set({
30 templates: [
31 ...new Map(
32 [...get().templates, ...templates].map((item) => [
33 item.id,
34 item,
35 ]),
36 ).values(),
37 ],
38 }),
39 setupDefaultTaxonomies: () => {
40 const taxonomies = useTaxonomyStore.getState().taxonomies
41 let taxonomyDefaultState = Object.entries(taxonomies).reduce(
42 (state, current) => (
43 (state[current[0]] = defaultCategoryForType(current[0])),
44 state
45 ),
46 {},
47 )
48 const tax = {}
49 let preferredTax =
50 useUserStore.getState().preferredOptions?.taxonomies ?? {}
51
52 // Check for old site type and set it if it exists
53 if (preferredTax.tax_categories) {
54 preferredTax = get().getLegacySiteType(preferredTax, taxonomies)
55 }
56 taxonomyDefaultState = Object.assign(
57 {},
58 taxonomyDefaultState,
59
60 // Override with the user's preferred taxonomies
61 preferredTax,
62
63 // Override with the global state
64 useGlobalStore.getState()?.currentTaxonomies ?? {},
65 )
66
67 tax.taxonomies = Object.assign({}, taxonomyDefaultState)
68 set({
69 taxonomyDefaultState: taxonomyDefaultState,
70 searchParams: {
71 ...Object.assign(get().searchParams, tax),
72 },
73 })
74 },
75 updateTaxonomies: (params) => {
76 const data = {}
77 data.taxonomies = Object.assign(
78 {},
79 get().searchParams.taxonomies,
80 params,
81 )
82 if (data?.taxonomies?.siteType) {
83 // This is what the user "prefers", which may be used outside the library
84 // which is persisted to the database, where as the global library state is in local storage
85 useUserStore
86 .getState()
87 .updatePreferredOption(
88 'siteType',
89 data?.taxonomies?.siteType,
90 )
91 }
92 useGlobalStore.getState().updateCurrentTaxonomies(data?.taxonomies)
93 get().updateSearchParams(data)
94 },
95 updateType(type) {
96 useGlobalStore.getState().updateCurrentType(type)
97 get().updateSearchParams({ type })
98 },
99 updateSearchParams: (params) => {
100 // If taxonomies are set to {}, lets use the default
101 if (params?.taxonomies && !Object.keys(params.taxonomies).length) {
102 params.taxonomies = get().taxonomyDefaultState
103 }
104
105 const searchParams = Object.assign({}, get().searchParams, params)
106
107 // If the params are not the same, then update
108 if (
109 JSON.stringify(searchParams) !==
110 JSON.stringify(get().searchParams)
111 ) {
112 set({ templates: [], nextPage: '', searchParams })
113 }
114 },
115 resetTemplates: () => set({ templates: [], nextPage: '' }),
116 getLegacySiteType: (preferredTax, taxonomies) => {
117 const oldSiteType = taxonomies.siteType.find((t) =>
118 [t.slug, t?.title].includes(preferredTax.tax_categories),
119 )
120 // TODO: This is kind of wonky, as we keep track of the state in two places.
121 useUserStore.getState().updatePreferredSiteType(oldSiteType)
122 get().updateTaxonomies({ siteType: oldSiteType })
123 // Remove the legacy term so this only runs once
124 useUserStore
125 .getState()
126 .updatePreferredOption('tax_categories', null)
127 return useUserStore.getState().preferredOptions.taxonomies
128 },
129 })),
130 )
131