PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
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 0.7.0 All 126 releases
extendify / src / Notifications / state.js

state.js in Extendify 3.1.5, at src/Notifications/state.js

67 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import { create } from 'zustand';
3 import { devtools, persist } from 'zustand/middleware';
4
5 const storage = {
6 setItem: (_name, store) =>
7 apiFetch({
8 path: '/extendify/v1/shared/update-user-meta',
9 method: 'POST',
10 data: {
11 option: 'notification_state',
12 value: { cards: store.state.cards },
13 },
14 }),
15 };
16
17 const now = () => new Date().toISOString();
18
19 const merge = (cards, slug, changes) => ({
20 ...cards,
21 [slug]: { ...cards[slug], ...changes },
22 });
23
24 const state = (set, get) => ({
25 // A per-view log would grow with every admin screen and ship in every page.
26 cards: { ...(window.extSharedData?.notificationState?.cards ?? {}) },
27 isDismissedBanner: (slug) => Boolean(get().cards[slug]?.dismissedAt),
28 dismissBanner: (slug) => {
29 if (get().isDismissedBanner(slug)) return;
30 set((current) => ({
31 cards: merge(current.cards, slug, { dismissedAt: now() }),
32 }));
33 },
34 recordNotificationView: (slug) => {
35 set((current) => {
36 const card = current.cards[slug];
37 return {
38 cards: merge(current.cards, slug, {
39 views: (card?.views ?? 0) + 1,
40 firstSeenAt: card?.firstSeenAt ?? now(),
41 lastSeenAt: now(),
42 }),
43 };
44 });
45 },
46 recordNotificationClick: (slug) => {
47 set((current) => {
48 const card = current.cards[slug];
49 return {
50 cards: merge(current.cards, slug, {
51 clicks: (card?.clicks ?? 0) + 1,
52 firstClickedAt: card?.firstClickedAt ?? now(),
53 lastClickedAt: now(),
54 }),
55 };
56 });
57 },
58 });
59
60 export const useNotificationsStore = create(
61 persist(devtools(state, { name: 'Extendify Notifications' }), {
62 name: 'extendify-notifications',
63 storage,
64 skipHydration: true,
65 }),
66 );
67