/ chandeskpro / src / stores / providerStore.ts
swadhinbiswas Initial release v0.1.0 · 1/16/2026
ed7a4e4
136 lines · 4.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Provider Store
 * 
 * Zustand store for managing the active imageboard provider.
 */

import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import { providerRegistry, type ProviderInfo } from '../providers'

interface ProviderState {
    // Current active provider
    activeProviderId: string

    // Per-provider state (board selections, favorites, etc.)
    providerState: Record<string, {
        currentBoard: string | null
        currentThread: number | null
        favorites: string[]
    }>

    // Actions
    setActiveProvider: (id: string) => void
    setCurrentBoard: (board: string | null) => void
    setCurrentThread: (threadId: number | null) => void
    addFavorite: (board: string) => void
    removeFavorite: (board: string) => void
    toggleFavorite: (board: string) => void

    // Getters
    getActiveProvider: () => ReturnType<typeof providerRegistry.get>
    getCurrentBoard: () => string | null
    getCurrentThread: () => number | null
    getFavorites: () => string[]
}

export const useProviderStore = create<ProviderState>()(
    persist(
        (set, get) => ({
            activeProviderId: '4chan',
            providerState: {
                '4chan': { currentBoard: null, currentThread: null, favorites: ['g', 'v', 'a'] },
                '7chan': { currentBoard: null, currentThread: null, favorites: [] },
            },

            setActiveProvider: (id) => {
                if (providerRegistry.has(id)) {
                    set({ activeProviderId: id })
                }
            },

            setCurrentBoard: (board) => {
                const { activeProviderId, providerState } = get()
                const current = providerState[activeProviderId] || { currentBoard: null, currentThread: null, favorites: [] }
                set({
                    providerState: {
                        ...providerState,
                        [activeProviderId]: { ...current, currentBoard: board, currentThread: null }
                    }
                })
            },

            setCurrentThread: (threadId) => {
                const { activeProviderId, providerState } = get()
                const current = providerState[activeProviderId] || { currentBoard: null, currentThread: null, favorites: [] }
                set({
                    providerState: {
                        ...providerState,
                        [activeProviderId]: { ...current, currentThread: threadId }
                    }
                })
            },

            addFavorite: (board) => {
                const { activeProviderId, providerState } = get()
                const current = providerState[activeProviderId] || { currentBoard: null, currentThread: null, favorites: [] }
                if (!current.favorites.includes(board)) {
                    set({
                        providerState: {
                            ...providerState,
                            [activeProviderId]: { ...current, favorites: [...current.favorites, board] }
                        }
                    })
                }
            },

            removeFavorite: (board) => {
                const { activeProviderId, providerState } = get()
                const current = providerState[activeProviderId] || { currentBoard: null, currentThread: null, favorites: [] }
                set({
                    providerState: {
                        ...providerState,
                        [activeProviderId]: {
                            ...current,
                            favorites: current.favorites.filter(f => f !== board)
                        }
                    }
                })
            },

            toggleFavorite: (board) => {
                const { getFavorites, addFavorite, removeFavorite } = get()
                if (getFavorites().includes(board)) {
                    removeFavorite(board)
                } else {
                    addFavorite(board)
                }
            },

            getActiveProvider: () => {
                return providerRegistry.get(get().activeProviderId)
            },

            getCurrentBoard: () => {
                const { activeProviderId, providerState } = get()
                return providerState[activeProviderId]?.currentBoard || null
            },

            getCurrentThread: () => {
                const { activeProviderId, providerState } = get()
                return providerState[activeProviderId]?.currentThread || null
            },

            getFavorites: () => {
                const { activeProviderId, providerState } = get()
                return providerState[activeProviderId]?.favorites || []
            },
        }),
        {
            name: 'provider-storage',
        }
    )
)

export default useProviderStore