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 / User.js

User.js in Extendify 0.3.0, at src/state/User.js

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