import { useEffect, useState } from "react"; import { useShotService } from "@/app/service/Interaction/ShotService"; import { useSearchParams } from 'next/navigation'; import { useRoleServiceHook } from "@/app/service/Interaction/RoleService"; import { useRoleShotServiceHook } from "@/app/service/Interaction/RoleShotService"; import { useScriptService } from "@/app/service/Interaction/ScriptService"; export const useEditData = (tabType: string, originalText?: string) => { const searchParams = useSearchParams(); const projectId = searchParams.get('episodeId') || ''; const [loading, setLoading] = useState(true); const [scriptData, setScriptData] = useState([]); const [shotData, setShotData] = useState([]); const [roleData, setRoleData] = useState([]); const { scriptBlocksMemo, // 渲染剧本数据 initializeFromProject, setAnyAttribute, applyScript } = useScriptService(); const { videoSegments, getVideoSegmentList, setSelectedSegment, regenerateVideoSegment, filterRole, calculateRecognitionBoxes } = useShotService(); const { roleList, selectedRole, userRoleLibrary, fetchRoleList, selectRole, fetchUserRoleLibrary, optimizeRoleText, updateRoleText, regenerateRole, uploadImageAndUpdateRole, saveRoleToLibrary, changeTabCallback, } = useRoleServiceHook(); const { shotSelectionList, selectedRoleId, isAllVideoSegmentSelected, selectedVideoSegmentCount, fetchRoleShots, toggleSelectAllShots, toggleShotSelection, applyRoleToSelectedShots, clearShotSelection, setSelectedRole, setDraftRoleList } = useRoleShotServiceHook(projectId, selectedRole || undefined,roleList || undefined); useEffect(() => { console.log('useEditData-----selectedRole', selectedRole); if(selectedRole){ setSelectedRole(selectedRole); setDraftRoleList(roleList || []); } }, [selectedRole]); useEffect(() => { if (tabType === 'script') { initializeFromProject(projectId, originalText || '').then(() => { setLoading(false); }).catch((err) => { console.log('useEditData-----err', err); setScriptData([]); setLoading(false); }); } else if (tabType === 'shot') { getVideoSegmentList(projectId).then(() => { setLoading(false); }).catch((err) => { console.log('useEditData-----err', err); setShotData([]); setLoading(false); }); } else if (tabType === 'role') { fetchRoleList(projectId).then(() => { setLoading(false); }).catch((err) => { console.log('useEditData-----err', err); setRoleData([]); setLoading(false); }); } }, [tabType]); useEffect(() => { if (scriptBlocksMemo.length > 0) { setScriptData(scriptBlocksMemo); } }, [scriptBlocksMemo]); useEffect(() => { console.log('useEditData-----videoSegments', videoSegments); setShotData(videoSegments); }, [videoSegments]); useEffect(() => { setRoleData(roleList); // setRoleData(mockRoleData); }, [roleList]); return { loading, // script scriptData, setAnyAttribute, applyScript, // shot shotData, setSelectedSegment, regenerateVideoSegment, filterRole, calculateRecognitionBoxes, // role roleData, selectRole, selectedRole, userRoleLibrary, optimizeRoleText, updateRoleText, regenerateRole, fetchUserRoleLibrary, uploadImageAndUpdateRole, changeTabCallback, // role shot shotSelectionList, selectedRoleId, isAllVideoSegmentSelected, selectedVideoSegmentCount, fetchRoleShots, toggleSelectAllShots, toggleShotSelection, applyRoleToSelectedShots, clearShotSelection, saveRoleToLibrary, setSelectedRole } }