| 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 startingState = { |
| 7 |
// These are tests the user is in progress of completing. |
| 8 |
// Not to be confused with tasks that are in progress. |
| 9 |
// ! This should have probably been in Global or elsewhere? |
| 10 |
activeTests: [], |
| 11 |
// These are tasks that the user has seen. When added, |
| 12 |
// they will look like [{ key, firstSeenAt }] |
| 13 |
seenTasks: [], |
| 14 |
// These are tasks the user has already completed |
| 15 |
// [{ key, completedAt }] but it used to just be [key] |
| 16 |
// so use ?.completedAt to check if it's completed with the (.?) |
| 17 |
completedTasks: [], |
| 18 |
inProgressTasks: [], |
| 19 |
// These are the tasks dependencies |
| 20 |
tasksDependencies: { |
| 21 |
...safeParseJson(window.extAssistData.userData.tasksDependencies), |
| 22 |
}, |
| 23 |
// initialize the state with default values |
| 24 |
...(safeParseJson(window.extAssistData.userData.taskData)?.state ?? {}), |
| 25 |
}; |
| 26 |
|
| 27 |
const state = (set, get) => ({ |
| 28 |
...startingState, |
| 29 |
// We need to keep the tasks dependencies updated all the time, |
| 30 |
// the user may complete the task from outside the cards, this will |
| 31 |
// make sure they are always up-to-date. |
| 32 |
tasksDependencies: { |
| 33 |
...safeParseJson(window.extAssistData.userData.tasksDependencies), |
| 34 |
}, |
| 35 |
isCompleted(taskId) { |
| 36 |
const completed = get().completedTasks.some((task) => task?.id === taskId); |
| 37 |
|
| 38 |
// overrides for specific plugin "behind the scenes" tasks |
| 39 |
const { |
| 40 |
completedWoocommerceStore, |
| 41 |
completedSetupGivewp, |
| 42 |
completedSetupAIOSeo, |
| 43 |
completedWPFormsLite, |
| 44 |
completedYourWebShop, |
| 45 |
completedMonsterInsights, |
| 46 |
} = get().tasksDependencies || {}; |
| 47 |
if (taskId === 'setup-givewp') return completedSetupGivewp || completed; |
| 48 |
if (taskId === 'setup-woocommerce-store') |
| 49 |
return completedWoocommerceStore || completed; |
| 50 |
if (taskId === 'setup-aioses') return completedSetupAIOSeo || completed; |
| 51 |
if (taskId === 'setup-wpforms') return completedWPFormsLite || completed; |
| 52 |
if (taskId === 'setup-yourwebshop') |
| 53 |
return completedYourWebShop || completed; |
| 54 |
if (taskId === 'setup-monsterinsights') |
| 55 |
return completedMonsterInsights || completed; |
| 56 |
|
| 57 |
return completed; |
| 58 |
}, |
| 59 |
completeTask(taskId) { |
| 60 |
if (get().isCompleted(taskId)) { |
| 61 |
return; |
| 62 |
} |
| 63 |
set((state) => ({ |
| 64 |
completedTasks: [ |
| 65 |
...state.completedTasks, |
| 66 |
{ |
| 67 |
id: taskId, |
| 68 |
completedAt: new Date().toISOString(), |
| 69 |
}, |
| 70 |
], |
| 71 |
})); |
| 72 |
// Dispatch event to notify others |
| 73 |
window.dispatchEvent( |
| 74 |
new CustomEvent('extendify-assist-task-completed', { |
| 75 |
detail: { ...get() }, |
| 76 |
}), |
| 77 |
); |
| 78 |
}, |
| 79 |
// Marks the task as dismissed: true |
| 80 |
dismissTask(taskId) { |
| 81 |
get().completeTask(taskId); |
| 82 |
set((state) => { |
| 83 |
const { completedTasks } = state; |
| 84 |
const task = completedTasks.find((task) => task.id === taskId); |
| 85 |
return { |
| 86 |
completedTasks: [ |
| 87 |
...completedTasks.filter((task) => task.id !== taskId), |
| 88 |
{ ...task, dismissed: true }, |
| 89 |
], |
| 90 |
}; |
| 91 |
}); |
| 92 |
}, |
| 93 |
isSeen(taskId) { |
| 94 |
return get().seenTasks.some((task) => task?.id === taskId); |
| 95 |
}, |
| 96 |
seeTask(taskId) { |
| 97 |
if (get().isSeen(taskId)) { |
| 98 |
return; |
| 99 |
} |
| 100 |
const task = { |
| 101 |
id: taskId, |
| 102 |
firstSeenAt: new Date().toISOString(), |
| 103 |
}; |
| 104 |
set((state) => ({ |
| 105 |
seenTasks: [...state.seenTasks, task], |
| 106 |
})); |
| 107 |
}, |
| 108 |
uncompleteTask(taskId) { |
| 109 |
set((state) => ({ |
| 110 |
completedTasks: state.completedTasks.filter((task) => task.id !== taskId), |
| 111 |
})); |
| 112 |
}, |
| 113 |
toggleCompleted(taskId) { |
| 114 |
if (get().isCompleted(taskId)) { |
| 115 |
get().uncompleteTask(taskId); |
| 116 |
return; |
| 117 |
} |
| 118 |
get().completeTask(taskId); |
| 119 |
}, |
| 120 |
}); |
| 121 |
|
| 122 |
const path = '/extendify/v1/assist/task-data'; |
| 123 |
const storage = { |
| 124 |
getItem: async () => await apiFetch({ path }), |
| 125 |
setItem: async (_name, state) => |
| 126 |
await apiFetch({ path, method: 'POST', data: { state } }), |
| 127 |
}; |
| 128 |
|
| 129 |
export const useTasksStore = create( |
| 130 |
persist(devtools(state, { name: 'Extendify Assist Tasks' }), { |
| 131 |
storage: createJSONStorage(() => storage), |
| 132 |
skipHydration: true, |
| 133 |
}), |
| 134 |
); |
| 135 |
|