fix: persist config after every change
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import type { StateCreator } from 'zustand'
|
||||
import { MODULE_NAME } from '@/constants'
|
||||
|
||||
export type WordList = string[]
|
||||
|
||||
export type WordLists = Record<string, string[]>
|
||||
|
||||
export interface Config {
|
||||
wordLists: Record<string, string[]>
|
||||
wordLists: WordLists
|
||||
}
|
||||
|
||||
export interface ConfigSlice extends Config {
|
||||
addWordList: (name: string, words: string[]) => void
|
||||
updateWordList: (name: string, words: string[]) => void
|
||||
removeWordList: (name: string) => void
|
||||
}
|
||||
|
||||
@@ -16,17 +19,18 @@ export const createConfigSlice: StateCreator<ConfigSlice> = (set) => {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: SillyTavern's extensionSettings is not properly typed
|
||||
const extensionSettings: Record<string, any> = context.extensionSettings
|
||||
|
||||
const settings: Config =
|
||||
extensionSettings[MODULE_NAME] === undefined
|
||||
? { wordLists: {} }
|
||||
: extensionSettings[MODULE_NAME]
|
||||
if (extensionSettings[MODULE_NAME] === undefined) {
|
||||
extensionSettings[MODULE_NAME] = {
|
||||
wordLists: {}
|
||||
}
|
||||
}
|
||||
|
||||
const settings = extensionSettings[MODULE_NAME]
|
||||
|
||||
return {
|
||||
wordLists: settings.wordLists,
|
||||
addWordList: (name, words) =>
|
||||
set({ wordLists: { ...settings.wordLists, [name]: words } }),
|
||||
updateWordList: (name, words) =>
|
||||
set({ wordLists: { ...settings.wordLists, [name]: words } }),
|
||||
removeWordList: (name) => {
|
||||
const newWordLists = { ...settings.wordLists }
|
||||
delete newWordLists[name]
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { createStore } from 'zustand'
|
||||
import { create } from 'zustand'
|
||||
import { subscribeWithSelector } from 'zustand/middleware'
|
||||
import { MODULE_NAME } from '@/constants'
|
||||
import { type ConfigSlice, createConfigSlice } from '@/store/configSlice'
|
||||
|
||||
export const useStore = createStore<ConfigSlice>()(
|
||||
export const useStore = create<ConfigSlice>()(
|
||||
subscribeWithSelector(createConfigSlice)
|
||||
)
|
||||
|
||||
@@ -13,6 +13,7 @@ useStore.subscribe(
|
||||
const context = SillyTavern.getContext()
|
||||
// biome-ignore lint/suspicious/noExplicitAny: SillyTavern's extensionSettings is not properly typed
|
||||
const extensionSettings: Record<string, any> = context.extensionSettings
|
||||
extensionSettings[MODULE_NAME] = { wordLists }
|
||||
extensionSettings[MODULE_NAME].wordLists = wordLists
|
||||
context.saveSettingsDebounced()
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user