| 1 |
import create from 'zustand' |
| 2 |
import { persist } from 'zustand/middleware' |
| 3 |
import { User } from '../api/User' |
| 4 |
|
| 5 |
const storage = { |
| 6 |
getItem: async () => await User.getData(), |
| 7 |
setItem: async (_name, value) => await User.setData(value), |
| 8 |
removeItem: () => {}, |
| 9 |
} |
| 10 |
|
| 11 |
const isGlobalLibraryEnabled = () => |
| 12 |
window.extendifyData.sitesettings === null || |
| 13 |
window.extendifyData?.sitesettings?.state?.enabled |
| 14 |
|
| 15 |
export const useUserStore = create( |
| 16 |
persist( |
| 17 |
(set, get) => ({ |
| 18 |
firstLoadedOn: new Date().toISOString(), |
| 19 |
email: '', |
| 20 |
apiKey: '', |
| 21 |
imports: 0, |
| 22 |
uuid: '', |
| 23 |
sdkPartner: '', |
| 24 |
registration: { |
| 25 |
email: '', |
| 26 |
optedOut: false, |
| 27 |
}, |
| 28 |
noticesDismissedAt: {}, |
| 29 |
allowedImports: 0, |
| 30 |
entryPoint: 'not-set', |
| 31 |
enabled: isGlobalLibraryEnabled(), |
| 32 |
canInstallPlugins: false, |
| 33 |
canActivatePlugins: false, |
| 34 |
preferredOptions: { |
| 35 |
taxonomies: {}, |
| 36 |
type: '', |
| 37 |
search: '', |
| 38 |
}, |
| 39 |
preferredOptionsHistory: { |
| 40 |
siteType: [], |
| 41 |
}, |
| 42 |
incrementImports: () => set({ imports: get().imports + 1 }), |
| 43 |
hasAvailableImports: () => |
| 44 |
get().apiKey |
| 45 |
? true |
| 46 |
: Number(get().imports) < Number(get().allowedImports), |
| 47 |
remainingImports: () => { |
| 48 |
if (get().apiKey) { |
| 49 |
return 'unlimited' |
| 50 |
} |
| 51 |
const remaining = |
| 52 |
Number(get().allowedImports) - Number(get().imports) |
| 53 |
return remaining > 0 ? remaining : 0 |
| 54 |
}, |
| 55 |
updatePreferredSiteType: (value) => { |
| 56 |
get().updatePreferredOption('siteType', value) |
| 57 |
if (!value?.slug || value.slug === 'unknown') return |
| 58 |
const current = get().preferredOptionsHistory?.siteType ?? [] |
| 59 |
|
| 60 |
// If the site type isn't already included, prepend it |
| 61 |
if (!current.find((t) => t.slug === value.slug)) { |
| 62 |
const siteType = [value, ...current] |
| 63 |
set({ |
| 64 |
preferredOptionsHistory: Object.assign( |
| 65 |
{}, |
| 66 |
get().preferredOptionsHistory, |
| 67 |
{ siteType: siteType.slice(0, 3) }, |
| 68 |
), |
| 69 |
}) |
| 70 |
} |
| 71 |
}, |
| 72 |
updatePreferredOption: (option, value) => { |
| 73 |
// If the option doesn't exist, assume it's a taxonomy |
| 74 |
if ( |
| 75 |
!Object.prototype.hasOwnProperty.call( |
| 76 |
get().preferredOptions, |
| 77 |
option, |
| 78 |
) |
| 79 |
) { |
| 80 |
value = Object.assign( |
| 81 |
{}, |
| 82 |
get().preferredOptions?.taxonomies ?? {}, |
| 83 |
{ [option]: value }, |
| 84 |
) |
| 85 |
option = 'taxonomies' |
| 86 |
} |
| 87 |
|
| 88 |
set({ |
| 89 |
preferredOptions: { |
| 90 |
...Object.assign({}, get().preferredOptions, { |
| 91 |
[option]: value, |
| 92 |
}), |
| 93 |
}, |
| 94 |
}) |
| 95 |
}, |
| 96 |
}), |
| 97 |
{ |
| 98 |
name: 'extendify-user', |
| 99 |
getStorage: () => storage, |
| 100 |
}, |
| 101 |
), |
| 102 |
) |
| 103 |
|