轮询修改,媒体过期判断
This commit is contained in:
@@ -3,16 +3,23 @@ import type { Project, GenerationRecord, OptimizeParams, GenerateParams, Optimiz
|
||||
import * as api from '../api';
|
||||
import { useAuthStore } from './useAuthStore';
|
||||
|
||||
const emptyRecordsPage = (): api.GenerationRecordPageListOut => ({
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
total: 0,
|
||||
items: [],
|
||||
});
|
||||
|
||||
interface AppState {
|
||||
projects: Project[];
|
||||
records: GenerationRecord[];
|
||||
records: api.GenerationRecordPageListOut;
|
||||
loading: boolean;
|
||||
|
||||
fetchProjects: () => Promise<void>;
|
||||
createProject: (name: string, industry: Industry) => Promise<Project>;
|
||||
deleteProject: (id: string) => Promise<void>;
|
||||
|
||||
fetchRecords: (projectId?: string) => Promise<void>;
|
||||
fetchRecords: (params?: api.GetRecordsPageParams) => Promise<void>;
|
||||
optimizePrompt: (projectId: string, params: OptimizeParams) => Promise<OptimizeResult>;
|
||||
generateVideo: (recordId: string, params: GenerateParams) => Promise<GenerationRecord>;
|
||||
updateRecordReferences: (recordId: string, references: MediaReference[]) => void;
|
||||
@@ -20,7 +27,7 @@ interface AppState {
|
||||
|
||||
export const useAppStore = create<AppState>((set, get) => ({
|
||||
projects: [],
|
||||
records: [],
|
||||
records: emptyRecordsPage(),
|
||||
loading: false,
|
||||
|
||||
fetchProjects: async () => {
|
||||
@@ -44,10 +51,10 @@ export const useAppStore = create<AppState>((set, get) => ({
|
||||
set({ projects: get().projects.filter((p) => p.id !== id) });
|
||||
},
|
||||
|
||||
fetchRecords: async (projectId) => {
|
||||
fetchRecords: async (params = {}) => {
|
||||
set({ loading: true });
|
||||
try {
|
||||
const records = await api.getRecords(projectId);
|
||||
const records = await api.getRecordsPage(params);
|
||||
set({ records, loading: false });
|
||||
} catch {
|
||||
set({ loading: false });
|
||||
@@ -57,22 +64,39 @@ export const useAppStore = create<AppState>((set, get) => ({
|
||||
optimizePrompt: async (projectId, params) => {
|
||||
const result = await api.optimizePrompt(projectId, params);
|
||||
try { await useAuthStore.getState().checkAuth(); } catch { /* */ }
|
||||
set({ records: [result.record, ...get().records] });
|
||||
|
||||
const currentRecords = get().records;
|
||||
set({
|
||||
records: {
|
||||
...currentRecords,
|
||||
total: currentRecords.total + 1,
|
||||
items: [result.record, ...currentRecords.items],
|
||||
},
|
||||
});
|
||||
return result;
|
||||
},
|
||||
|
||||
generateVideo: async (recordId, params) => {
|
||||
const record = await api.generateVideo(recordId, params);
|
||||
try { await useAuthStore.getState().checkAuth(); } catch { /* */ }
|
||||
|
||||
const currentRecords = get().records;
|
||||
set({
|
||||
records: get().records.map((r) => (r.id === recordId ? record : r)),
|
||||
records: {
|
||||
...currentRecords,
|
||||
items: currentRecords.items.map((r) => (r.id === recordId ? record : r)),
|
||||
},
|
||||
});
|
||||
return record;
|
||||
},
|
||||
|
||||
updateRecordReferences: (recordId, references) => {
|
||||
const currentRecords = get().records;
|
||||
set({
|
||||
records: get().records.map((r) => (r.id === recordId ? { ...r, references } : r)),
|
||||
records: {
|
||||
...currentRecords,
|
||||
items: currentRecords.items.map((r) => (r.id === recordId ? { ...r, references } : r)),
|
||||
},
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user