PluginProbe
Extendify / 0.3.0
Extendify v0.3.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.3.0, at src/state/Templates.js

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