()
| 225 | const scriptCodeDAO = new ScriptCodeDAO(); |
| 226 | |
| 227 | function useScriptList() { |
| 228 | const [selectedScript, setSelectSciptButtonAndTab] = useState<string>(""); |
| 229 | const [editors, setEditors] = useState<EditorState[]>([]); |
| 230 | const [canLoadScript, setCanLoadScript] = useState<boolean>(false); |
| 231 | const [scriptList, setScriptList] = useState<Script[]>([]); |
| 232 | // 监听后台消息更新状态 |
| 233 | useEffect(() => { |
| 234 | const pageApi = { |
| 235 | async installScript(data: TInstallScript) { |
| 236 | const latest = await scriptDAO.all(); |
| 237 | const latestMap = new Map(latest.map((script) => [script.uuid, script])); |
| 238 | setScriptList((list) => { |
| 239 | const newList: Script[] = []; |
| 240 | for (const entry of list) { |
| 241 | if (entry.uuid !== data.script.uuid) { |
| 242 | const latestScript = latestMap.get(entry.uuid); |
| 243 | if (latestScript) { |
| 244 | newList.push({ |
| 245 | ...entry, |
| 246 | sort: latestScript.sort, |
| 247 | name: latestScript.name, |
| 248 | updatetime: latestScript.updatetime, |
| 249 | status: latestScript.status, |
| 250 | }); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | const installedScript = latestMap.get(data.script.uuid); |
| 255 | if (installedScript) { |
| 256 | newList.push(installedScript); |
| 257 | } |
| 258 | newList.sort((a, b) => a.sort - b.sort); |
| 259 | return newList; |
| 260 | }); |
| 261 | }, |
| 262 | deleteScripts(data: TDeleteScript[]) { |
| 263 | const dels = new Set(data.map((script) => script.uuid)); |
| 264 | setEditors((prev) => { |
| 265 | const newList: EditorState[] = []; |
| 266 | for (const editor of prev) { |
| 267 | if (!dels.has(editor.script.uuid)) { |
| 268 | newList.push(editor); |
| 269 | } |
| 270 | } |
| 271 | // 关键修复:确保关闭后仍有一个 Tab 是激活的 |
| 272 | if (newList.length > 0 && !newList.some((e) => e.active)) { |
| 273 | newList[0] = { ...newList[0], active: true }; |
| 274 | setSelectSciptButtonAndTab(newList[0].script.uuid); |
| 275 | } |
| 276 | return newList; |
| 277 | }); |
| 278 | setScriptList((list) => { |
| 279 | return list.filter((script) => !dels.has(script.uuid)); |
| 280 | }); |
| 281 | }, |
| 282 | enableScripts(data: TEnableScript[]) { |
| 283 | const enableMap = new Map(data.map((e) => [e.uuid, e.enable])); |
| 284 | setScriptList((list) => { |
no test coverage detected