mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-18 21:19:44 +00:00
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import axios from "axios";
|
|
import { create } from "zustand";
|
|
import { combine } from "zustand/middleware";
|
|
import { CreateShortcutResponse, ListShortcutsResponse, Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
|
|
|
interface State {
|
|
shortcutMapById: Record<number, Shortcut>;
|
|
}
|
|
|
|
const getDefaultState = (): State => {
|
|
return {
|
|
shortcutMapById: {},
|
|
};
|
|
};
|
|
|
|
const useShortcutStore = create(
|
|
combine(getDefaultState(), (set, get) => ({
|
|
fetchShortcutList: async (instanceUrl: string, accessToken: string) => {
|
|
const {
|
|
data: { shortcuts },
|
|
} = await axios.get<ListShortcutsResponse>(`${instanceUrl}/api/v2/shortcuts`, {
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
});
|
|
const shortcutMap = get().shortcutMapById;
|
|
shortcuts.forEach((shortcut) => {
|
|
shortcutMap[shortcut.id] = shortcut;
|
|
});
|
|
set({ shortcutMapById: shortcutMap });
|
|
return shortcuts;
|
|
},
|
|
getShortcutList: () => {
|
|
return Object.values(get().shortcutMapById);
|
|
},
|
|
createShortcut: async (instanceUrl: string, accessToken: string, create: Shortcut) => {
|
|
const {
|
|
data: { shortcut },
|
|
} = await axios.post<CreateShortcutResponse>(`${instanceUrl}/api/v2/shortcuts`, create, {
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
});
|
|
if (!shortcut) {
|
|
throw new Error(`Failed to create shortcut`);
|
|
}
|
|
const shortcutMap = get().shortcutMapById;
|
|
shortcutMap[shortcut.id] = shortcut;
|
|
set({ shortcutMapById: shortcutMap });
|
|
return shortcut;
|
|
},
|
|
}))
|
|
);
|
|
|
|
export default useShortcutStore;
|