import { create } from 'zustand'; import { query } from '@/modules/query'; import { toast } from 'sonner'; import { cnb } from '@/agents/app' interface DisplayModule { activity: boolean; contributors: boolean; release: boolean; } interface Languages { language: string; color: string; } interface Data { id: string; name: string; freeze: boolean; status: number; visibility_level: string; flags: string; created_at: string; updated_at: string; description: string; site: string; topics: string; license: string; display_module: DisplayModule; star_count: number; fork_count: number; mark_count: number; last_updated_at?: string | null; web_url: string; path: string; tags: any; open_issue_count: number; open_pull_request_count: number; languages: Languages; second_languages: Languages; last_update_username: string; last_update_nickname: string; access: string; stared: boolean; star_time: string; pinned: boolean; pinned_time: string; } type State = { formData: Record; setFormData: (data: Record) => void; showEdit: boolean; setShowEdit: (showEdit: boolean) => void; loading: boolean; setLoading: (loading: boolean) => void; list: Data[]; editRepo: Data | null; setEditRepo: (repo: Data | null) => void; showEditDialog: boolean; setShowEditDialog: (show: boolean) => void; getList: () => Promise; updateRepoInfo: (data: Partial) => Promise; } export const useRepoStore = create((set, get) => { return { formData: {}, setFormData: (data) => set({ formData: data }), showEdit: false, setShowEdit: (showEdit) => set({ showEdit }), loading: false, setLoading: (loading) => set({ loading }), list: [], editRepo: null, setEditRepo: (repo) => set({ editRepo: repo }), showEditDialog: false, setShowEditDialog: (show) => set({ showEditDialog: show }), getItem: async (id) => { const { setLoading } = get(); setLoading(true); try { const res = await query.post({ path: 'demo', key: 'item', data: { id } }) if (res.code === 200) { return res; } else { toast.error(res.message || '请求失败'); } } finally { setLoading(false); } }, getList: async () => { const { setLoading } = get(); setLoading(true); try { const res = await cnb.repo.getRepoList({}) if (res.code === 200) { const list = res.data! || [] set({ list }); } else { toast.error(res.message || '请求失败'); } return res; } finally { setLoading(false); } }, updateRepoInfo: async (data) => { const repo = data.path!; const updateData = { description: data.description, license: data?.license as any, site: data.site, topics: data.topics?.split?.(','), } const res = await cnb.repo.updateRepoInfo(repo, updateData) if (res.code === 200) { toast.success('更新成功'); } else { toast.error(res.message || '更新失败'); } }, } })