|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { create } from 'zustand'; |
|
|
import { devtools } from 'zustand/middleware'; |
|
|
import { immer } from 'zustand/middleware/immer'; |
|
|
import { MdxProfile } from '../types'; |
|
|
import * as libraryAPI from '../api/library'; |
|
|
import i18n from '../i18n/i18n'; |
|
|
|
|
|
interface LibraryState { |
|
|
|
|
|
groups: MdxProfile[]; |
|
|
|
|
|
|
|
|
loading: boolean; |
|
|
error: string | null; |
|
|
|
|
|
|
|
|
loadGroups: () => Promise<void>; |
|
|
getProfile: (groupId: number, profileId: number) => MdxProfile|undefined; |
|
|
getGroup: (groupId: number) => MdxProfile|undefined; |
|
|
refreshLibrary: () => Promise<void>; |
|
|
createGroup: (groupName: string) => Promise<number>; |
|
|
deleteGroup: (groupId: number) => Promise<void>; |
|
|
renameGroup: (groupId: number, newName: string) => Promise<void>; |
|
|
updateProfileDisabledStatus: ( |
|
|
parentGroupId: number, |
|
|
profileId: number, |
|
|
disabled: boolean |
|
|
) => Promise<void>; |
|
|
adjustProfileOrder: ( |
|
|
parentGroupId: number, |
|
|
profileId: number, |
|
|
newIndex: number |
|
|
) => Promise<void>; |
|
|
adjustGroupOrder: (groupId: number, newIndex: number) => Promise<void>; |
|
|
|
|
|
} |
|
|
|
|
|
export const useLibraryStore = create<LibraryState>()( |
|
|
devtools( |
|
|
immer((set, get) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function withAsyncHandler<T extends any[], R>( |
|
|
fn: (...args: T) => Promise<R>, |
|
|
reloadGroups: boolean, |
|
|
errorMessage: string |
|
|
): (...args: T) => Promise<R>; |
|
|
|
|
|
|
|
|
function withAsyncHandler<T extends any[], R>( |
|
|
fn: (...args: T) => Promise<R>, |
|
|
reloadGroups: boolean, |
|
|
errorMessage: string |
|
|
): (...args: T) => Promise<R | undefined>; |
|
|
|
|
|
|
|
|
function withAsyncHandler<T extends any[], R>( |
|
|
fn: (...args: T) => Promise<R>, |
|
|
reloadGroups: boolean, |
|
|
errorMessageKey: string |
|
|
) { |
|
|
|
|
|
return async (...args: T): Promise<R | undefined> => { |
|
|
|
|
|
set({ loading: true, error: null }); |
|
|
|
|
|
try { |
|
|
|
|
|
const result = await fn(...args); |
|
|
if (reloadGroups) { |
|
|
await get().loadGroups(); |
|
|
} |
|
|
return result; |
|
|
} catch (error) { |
|
|
|
|
|
const errorMessage = i18n.t(errorMessageKey); |
|
|
|
|
|
console.error(errorMessage+': '+(error as Error).message || (error as Error).toString()); |
|
|
set({ error: errorMessage }); |
|
|
return undefined; |
|
|
} finally { |
|
|
set({ loading: false }); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return { |
|
|
|
|
|
groups: [], |
|
|
currentProfile: null, |
|
|
currentProfileId: null, |
|
|
loading: false, |
|
|
error: null, |
|
|
|
|
|
|
|
|
loadGroups: withAsyncHandler( |
|
|
async () => { |
|
|
const groups = await libraryAPI.listGroups(); |
|
|
set({ groups }); |
|
|
}, |
|
|
false, |
|
|
'Failed to load dictionary groups' |
|
|
), |
|
|
|
|
|
|
|
|
getProfile: (groupId: number, profileId: number) => { |
|
|
return get().groups |
|
|
.find(group => group.profileId === groupId) |
|
|
?.profiles?.find(profile => profile.profileId === profileId); |
|
|
}, |
|
|
|
|
|
getGroup: (groupId: number) => { |
|
|
return get().groups.find(group => group.profileId === groupId); |
|
|
}, |
|
|
|
|
|
|
|
|
refreshLibrary: withAsyncHandler( |
|
|
libraryAPI.refreshLibrary, |
|
|
true, |
|
|
'Failed to refresh library' |
|
|
), |
|
|
|
|
|
|
|
|
createGroup: withAsyncHandler( |
|
|
async (groupName: string) => { |
|
|
const groupId = await libraryAPI.createDictGroup(groupName); |
|
|
return groupId; |
|
|
}, |
|
|
true, |
|
|
'Failed to create dictionary group', |
|
|
), |
|
|
|
|
|
|
|
|
deleteGroup: withAsyncHandler( |
|
|
async (groupId: number) => { |
|
|
await libraryAPI.deleteDictGroup(groupId); |
|
|
}, |
|
|
true, |
|
|
'Failed to delete dictionary group', |
|
|
), |
|
|
|
|
|
|
|
|
renameGroup: withAsyncHandler( |
|
|
async (groupId: number, newName: string) => { |
|
|
await libraryAPI.renameDictGroup(groupId, newName); |
|
|
}, |
|
|
true, |
|
|
'Failed to rename dictionary group', |
|
|
), |
|
|
|
|
|
|
|
|
updateProfileDisabledStatus: withAsyncHandler( |
|
|
async ( |
|
|
parentGroupId: number, |
|
|
profileId: number, |
|
|
disabled: boolean |
|
|
) => { |
|
|
await libraryAPI.updateProfileDisabledStatus( |
|
|
parentGroupId, |
|
|
profileId, |
|
|
disabled |
|
|
); |
|
|
}, |
|
|
true, |
|
|
'Failed to update dictionary status', |
|
|
), |
|
|
|
|
|
|
|
|
adjustProfileOrder: withAsyncHandler( |
|
|
async ( |
|
|
parentGroupId: number, |
|
|
profileId: number, |
|
|
newIndex: number |
|
|
) => { |
|
|
await libraryAPI.adjustProfileOrder(parentGroupId, profileId, newIndex); |
|
|
}, |
|
|
true, |
|
|
'Failed to adjust dictionary order', |
|
|
), |
|
|
|
|
|
|
|
|
adjustGroupOrder: withAsyncHandler( |
|
|
async (groupId: number, newIndex: number) => { |
|
|
await libraryAPI.adjustGroupOrder(groupId, newIndex); |
|
|
}, |
|
|
true, |
|
|
'Failed to adjust group order', |
|
|
) |
|
|
|
|
|
}; |
|
|
}), |
|
|
{ name: 'LibraryStore' } |
|
|
) |
|
|
); |
|
|
|
|
|
|