import { safeParseJson } from '@shared/lib/parsing';
import apiFetch from '@wordpress/api-fetch';
import { create } from 'zustand';
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
// initialize the state with default values
...(safeParseJson(window.extHelpCenterData.userData.supportArticlesData)
const state = (set, get) => ({
pushArticle: (article) => {
const { slug, title } = article;
const lastViewedAt = new Date().toISOString();
const firstViewedAt = lastViewedAt;
const viewed = state.viewedArticles.find((a) => a.slug === slug);
// Remove the article if it's already in the list
...state.viewedArticles.filter((a) => a.slug !== slug),
// Either add the article or update the count
? { ...viewed, count: viewed.count + 1, lastViewedAt }
// Persist the detailed history to the server (don't wait for response)
path: '/extendify/v1/help-center/support-articles-data',
data: { state: { viewedArticles } },
articles: [article, ...state.articles],
recentArticles: [article, ...state.recentArticles.slice(0, 9)],
popArticle: () => set((state) => ({ articles: state.articles.slice(1) })),
clearArticles: () => set({ articles: [] }),
reset: () => set({ articles: [], searchTerm: '' }),
updateTitle: (slug, title) =>
articles: state.articles.map((article) => {
// We don't always know the title until after we fetch the article data
if (article.slug === slug) {
clearSearchTerm: () => set({ searchTerm: '' }),
setSearchTerm: (searchTerm) => set({ searchTerm }),
export const useKnowledgeBaseStore = create(
persist(devtools(state, { name: 'Extendify Help Center Knowledge Base' }), {
name: `extendify-help-center-knowledge-base-${window.extSharedData.siteId}`,
storage: createJSONStorage(() => sessionStorage),