forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSellerNotifications.ts
More file actions
91 lines (82 loc) · 2.7 KB
/
Copy pathuseSellerNotifications.ts
File metadata and controls
91 lines (82 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { useCallback, useEffect, useMemo, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { useWallet } from "@/hooks/useWallet";
import { browserStellarConfig } from "@/lib/stellar/browserConfig";
import { getPromptsByCreator } from "@/lib/stellar/promptHashClient";
import {
deriveNotifications,
loadSnapshot,
loadStoredNotifications,
mergeNotifications,
saveSnapshot,
saveStoredNotifications,
summariseActivity,
type SellerActivitySummary,
type SellerNotification,
} from "@/lib/notifications/sellerNotifications";
export interface UseSellerNotifications {
notifications: SellerNotification[];
unreadCount: number;
summary: SellerActivitySummary;
hasListings: boolean;
markAllRead: () => void;
clearAll: () => void;
}
export function useSellerNotifications(): UseSellerNotifications {
const { address } = useWallet();
const [notifications, setNotifications] = useState<SellerNotification[]>([]);
const { data: prompts = [] } = useQuery({
queryKey: ["created-prompts", address],
queryFn: () =>
address ? getPromptsByCreator(browserStellarConfig, address) : [],
enabled: Boolean(address),
refetchInterval: 60_000,
});
// Load the persisted feed whenever the connected wallet changes.
useEffect(() => {
if (!address) {
setNotifications([]);
return;
}
setNotifications(loadStoredNotifications(address));
}, [address]);
// Diff incoming listings against the stored snapshot to surface new events.
useEffect(() => {
if (!address || prompts.length === 0) return;
const previous = loadSnapshot(address);
const fresh = deriveNotifications(previous, prompts, Date.now());
saveSnapshot(address, prompts);
if (fresh.length === 0) return;
setNotifications((current) => {
const merged = mergeNotifications(current, fresh);
saveStoredNotifications(address, merged);
return merged;
});
}, [address, prompts]);
const summary = useMemo(() => summariseActivity(prompts), [prompts]);
const unreadCount = useMemo(
() => notifications.filter((notification) => !notification.read).length,
[notifications],
);
const markAllRead = useCallback(() => {
if (!address) return;
setNotifications((current) => {
const updated = current.map((notification) => ({ ...notification, read: true }));
saveStoredNotifications(address, updated);
return updated;
});
}, [address]);
const clearAll = useCallback(() => {
if (!address) return;
setNotifications([]);
saveStoredNotifications(address, []);
}, [address]);
return {
notifications,
unreadCount,
summary,
hasListings: prompts.length > 0,
markAllRead,
clearAll,
};
}