支持标签页folder窗口打开

This commit is contained in:
xintaofei
2026-03-25 21:56:54 +08:00
parent 388f92637c
commit e93b0586b9
2 changed files with 8 additions and 27 deletions

View File

@@ -11,7 +11,7 @@ import {
type ReactNode, type ReactNode,
} from "react" } from "react"
import { toErrorMessage } from "@/lib/app-error" import { toErrorMessage } from "@/lib/app-error"
import { getFolder, listFolderConversations, closeFolderWindow } from "@/lib/api" import { getFolder, listFolderConversations } from "@/lib/api"
import { isDesktop } from "@/lib/transport" import { isDesktop } from "@/lib/transport"
import type { import type {
AgentType, AgentType,
@@ -196,20 +196,11 @@ export function FolderProvider({
} }
}, []) }, [])
// Web mode: register this tab's name so that window.open(url, "folder-{id}") // Web mode: register this tab's name so that window.open("", "folder-{id}")
// from other pages can find and reuse it instead of opening duplicates. // from other pages can find and reuse it instead of opening duplicates.
// Also notify backend when the folder tab closes.
useEffect(() => { useEffect(() => {
if (isDesktop() || !folderId) return if (isDesktop() || !folderId) return
window.name = `folder-${folderId}` window.name = `folder-${folderId}`
const onUnload = () => closeFolderWindow(folderId)
window.addEventListener("pagehide", onUnload)
return () => {
window.removeEventListener("pagehide", onUnload)
}
}, [folderId]) }, [folderId])
const selectConversation = useCallback((id: number, agentType: AgentType) => { const selectConversation = useCallback((id: number, agentType: AgentType) => {

View File

@@ -910,22 +910,12 @@ export async function focusFolderWindow(folderId: number): Promise<void> {
if (getTransport().isDesktop()) { if (getTransport().isDesktop()) {
return getTransport().call("focus_folder_window", { folderId }) return getTransport().call("focus_folder_window", { folderId })
} }
// Web mode: use named window — reuses existing tab if still open, // Web mode: open empty string to focus existing named window without reload.
// otherwise opens a new one. // If the window doesn't exist (was closed), open the folder page.
window.open(`/folder?id=${folderId}`, `folder-${folderId}`) const win = window.open("", `folder-${folderId}`)
} if (!win || win.closed || !win.location.href || win.location.href === "about:blank") {
window.open(`/folder?id=${folderId}`, `folder-${folderId}`)
/** }
* Notify the backend that a folder tab has been closed.
* Uses sendBeacon for reliability during page unload.
*/
export function closeFolderWindow(folderId: number): void {
if (getTransport().isDesktop()) return
const token = localStorage.getItem("codeg_token") ?? ""
navigator.sendBeacon(
`/api/close_folder_window?token=${encodeURIComponent(token)}`,
JSON.stringify({ folderId }),
)
} }
// Conversation CRUD commands // Conversation CRUD commands