PluginProbe
Extendify / 3.0.4
Extendify v3.0.4
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 / Shared / state / generate-images.js

generate-images.js in Extendify 3.0.4, at src/Shared/state/generate-images.js

71 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { safeParseJson } from '@shared/lib/parsing';
2 import apiFetch from '@wordpress/api-fetch';
3 import { create } from 'zustand';
4 import { createJSONStorage, devtools, persist } from 'zustand/middleware';
5
6 const path = '/extendify/v1/shared/image-generation';
7 const storage = {
8 getItem: async () => await apiFetch({ path }),
9 setItem: async (_name, state) =>
10 await apiFetch({ path, method: 'POST', data: { state } }),
11 };
12 // Values added here should also be added to Admin.php ln ~200
13 const startingState = {
14 aiImageOptions: {
15 prompt: '',
16 size: '1024x1024',
17 },
18 imageCredits: {
19 remaining: 10,
20 total: 10,
21 refresh: undefined,
22 },
23 };
24 const store = (set) => ({
25 ...startingState,
26 ...safeParseJson(window.extSharedData?.globalState)?.state,
27 updateImageCredits({ remaining, total, refresh }) {
28 set((state) => ({
29 imageCredits: {
30 ...state.imageCredits,
31 // Only update truthy values
32 ...(remaining && { remaining }),
33 ...(total && { total }),
34 ...(refresh && { refresh }),
35 },
36 }));
37 },
38 subtractOneCredit() {
39 set((state) => ({
40 imageCredits: {
41 ...state.imageCredits,
42 remaining: state.imageCredits.remaining - 1,
43 // set to 24 hours from now (in ms)
44 refresh: new Date(Date.now() + 24 * 60 * 60 * 1000).getTime(),
45 },
46 }));
47 },
48 resetImageCredits() {
49 set({ imageCredits: startingState.imageCredits });
50 },
51 setAiImageOption(option, value) {
52 set((state) => ({
53 aiImageOptions: { ...state.aiImageOptions, [option]: value },
54 }));
55 },
56 });
57 const withDevtools = devtools(store, { name: 'Extendify Image Generation' });
58 const withPersist = persist(withDevtools, {
59 name: 'extendify_image_generation',
60 storage: createJSONStorage(() => storage),
61 skipHydration: true,
62 partialize: (state) => {
63 // Remove the prompt
64 return {
65 ...state,
66 aiImageOptions: { ...state.aiImageOptions, prompt: '' },
67 };
68 },
69 });
70 export const useImageGenerationStore = create(withPersist);
71