PluginProbe
Extendify / trunk
Extendify vtrunk
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 / Assist / state / domain-activities.js

domain-activities.js in Extendify trunk, at src/Assist/state/domain-activities.js

64 lines 1.3 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 initialState = {
7 activities: [],
8 ...(safeParseJson(
9 window.extAssistData.userData.domainsRecommendationsActivities,
10 )?.state ?? {}),
11 };
12
13 const state = (set, get) => ({
14 ...initialState,
15 setDomainActivity: ({
16 domain,
17 position,
18 type = 'primary',
19 action = 'clicked',
20 }) => {
21 set({
22 activities: [
23 ...get().activities,
24 {
25 domain: domain?.toLowerCase(),
26 position,
27 type,
28 action,
29 date: new Date().toISOString(),
30 },
31 ],
32 });
33 },
34 });
35
36 const debounce = (func, delay) => {
37 let timeoutId;
38 return (...params) => {
39 clearTimeout(timeoutId);
40 timeoutId = setTimeout(() => func(...params), delay);
41 };
42 };
43
44 const path = '/extendify/v1/assists/domains-recommendations-activities';
45 const storage = {
46 getItem: async () => await apiFetch({ path }),
47 setItem: debounce(
48 async (_name, state) =>
49 await apiFetch({ path, method: 'POST', data: { state } }),
50 500,
51 ),
52 };
53
54 export const useDomainActivities = create(
55 persist(
56 devtools(state, { name: 'Extendify Domains Recommendations insights' }),
57 {
58 storage: createJSONStorage(() => storage),
59 skipHydration: true,
60 },
61 ),
62 state,
63 );
64