| 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 |
uuid: '', |
| 22 |
sdkPartner: '', |
| 23 |
noticesDismissedAt: {}, |
| 24 |
modalNoticesDismissedAt: {}, |
| 25 |
imports: 0, // total imports over time |
| 26 |
runningImports: 0, // timed imports, resets to 0 every month |
| 27 |
allowedImports: 0, // Max imports the Extendify service allows |
| 28 |
freebieImports: 0, // Various free imports from actions (rewards) |
| 29 |
entryPoint: 'not-set', |
| 30 |
enabled: isGlobalLibraryEnabled(), |
| 31 |
canInstallPlugins: false, |
| 32 |
canActivatePlugins: false, |
| 33 |
preferredOptions: { |
| 34 |
taxonomies: {}, |
| 35 |
type: '', |
| 36 |
search: '', |
| 37 |
}, |
| 38 |
preferredOptionsHistory: { |
| 39 |
siteType: [], |
| 40 |
}, |
| 41 |
incrementImports: () => { |
| 42 |
// If the user has freebie imports, use those first |
| 43 |
const freebieImports = |
| 44 |
Number(get().freebieImports) > 0 |
| 45 |
? Number(get().freebieImports) - 1 |
| 46 |
: Number(get().freebieImports) |
| 47 |
// If they don't, then increment the running imports |
| 48 |
const runningImports = |
| 49 |
Number(get().runningImports) + +(freebieImports < 1) |
| 50 |
set({ |
| 51 |
imports: Number(get().imports) + 1, |
| 52 |
runningImports, |
| 53 |
freebieImports, |
| 54 |
}) |
| 55 |
}, |
| 56 |
giveFreebieImports: (amount) => { |
| 57 |
set({ freebieImports: get().freebieImports + amount }) |
| 58 |
}, |
| 59 |
totalAvailableImports: () => { |
| 60 |
return ( |
| 61 |
Number(get().allowedImports) + Number(get().freebieImports) |
| 62 |
) |
| 63 |
}, |
| 64 |
hasAvailableImports: () => { |
| 65 |
return get().apiKey |
| 66 |
? true |
| 67 |
: Number(get().runningImports) < |
| 68 |
Number(get().totalAvailableImports()) |
| 69 |
}, |
| 70 |
remainingImports: () => { |
| 71 |
const remaining = |
| 72 |
Number(get().totalAvailableImports()) - |
| 73 |
Number(get().runningImports) |
| 74 |
// If they have no allowed imports, this might be a first load |
| 75 |
// where it's just fetching templates (and/or their max alllowed) |
| 76 |
if (!get().allowedImports) { |
| 77 |
return null |
| 78 |
} |
| 79 |
return remaining > 0 ? remaining : 0 |
| 80 |
}, |
| 81 |
updatePreferredSiteType: (value) => { |
| 82 |
get().updatePreferredOption('siteType', value) |
| 83 |
if (!value?.slug || value.slug === 'unknown') return |
| 84 |
const current = get().preferredOptionsHistory?.siteType ?? [] |
| 85 |
|
| 86 |
// If the site type isn't already included, prepend it |
| 87 |
if (!current.find((t) => t.slug === value.slug)) { |
| 88 |
const siteType = [value, ...current] |
| 89 |
set({ |
| 90 |
preferredOptionsHistory: Object.assign( |
| 91 |
{}, |
| 92 |
get().preferredOptionsHistory, |
| 93 |
{ siteType: siteType.slice(0, 3) }, |
| 94 |
), |
| 95 |
}) |
| 96 |
} |
| 97 |
}, |
| 98 |
updatePreferredOption: (option, value) => { |
| 99 |
// If the option doesn't exist, assume it's a taxonomy |
| 100 |
if ( |
| 101 |
!Object.prototype.hasOwnProperty.call( |
| 102 |
get().preferredOptions, |
| 103 |
option, |
| 104 |
) |
| 105 |
) { |
| 106 |
value = Object.assign( |
| 107 |
{}, |
| 108 |
get().preferredOptions?.taxonomies ?? {}, |
| 109 |
{ [option]: value }, |
| 110 |
) |
| 111 |
option = 'taxonomies' |
| 112 |
} |
| 113 |
|
| 114 |
set({ |
| 115 |
preferredOptions: { |
| 116 |
...Object.assign({}, get().preferredOptions, { |
| 117 |
[option]: value, |
| 118 |
}), |
| 119 |
}, |
| 120 |
}) |
| 121 |
}, |
| 122 |
// Will mark a modal or footer notice |
| 123 |
markNoticeSeen: (key, type) => { |
| 124 |
set({ |
| 125 |
[`${type}DismissedAt`]: { |
| 126 |
...get()[`${type}DismissedAt`], |
| 127 |
[key]: new Date().toISOString(), |
| 128 |
}, |
| 129 |
}) |
| 130 |
}, |
| 131 |
}), |
| 132 |
{ |
| 133 |
name: 'extendify-user', |
| 134 |
getStorage: () => storage, |
| 135 |
}, |
| 136 |
), |
| 137 |
) |
| 138 |
|