-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseOfflineStorage.ts
More file actions
46 lines (43 loc) · 1.08 KB
/
useOfflineStorage.ts
File metadata and controls
46 lines (43 loc) · 1.08 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
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
interface OfflineArticle {
id: string;
title: string;
content: string;
image: string;
timestamp: number;
}
interface OfflineState {
articles: OfflineArticle[];
addArticle: (article: OfflineArticle) => void;
removeArticle: (id: string) => void;
getArticle: (id: string) => OfflineArticle | undefined;
hasArticle: (id: string) => boolean;
}
export const useOfflineStorage = create<OfflineState>()(
persist(
(set, get) => ({
articles: [],
addArticle: (article) => {
set((state) => ({
articles: [...state.articles, article]
}));
},
removeArticle: (id) => {
set((state) => ({
articles: state.articles.filter(article => article.id !== id)
}));
},
getArticle: (id) => {
return get().articles.find(article => article.id === id);
},
hasArticle: (id) => {
return get().articles.some(article => article.id === id);
}
}),
{
name: 'offline-storage',
version: 1
}
)
);