import { onBeforeUnmount, ref } from 'vue' import { versionDownloadPath, wordOpenUrl, type VersionFormat } from '../utils/versionDownload' /** Shared by history and generate success. Links stay available even if the focus heuristic fires. */ export function useVersionOpen() { const { apiJson } = useApi() const openingVersionId = ref(null) const downloadError = ref(null) const fallbackUrl = ref(null) const openNotice = ref(null) let cleanup = () => {} let generation = 0 let pendingTab: Window | null = null /** * รอสังเกตการสลับไป Word (blur/visibilitychange) กี่มิลลิวินาทีก่อนสรุปว่าเปิดไม่สำเร็จ — * ยาวพอให้ Word cold-start โอกาสเปิดได้ทัน แต่ไม่นานจนผู้ใช้งง (ms-word:ofe บน macOS ตอน Word * ยังไม่เปิดค้างอยู่มักทำ URL event หลุด → เห็นหน้า Recent แทนเอกสาร — ข้อจำกัดของ platform) */ const WORD_SWITCH_WAIT_MS = 3000 function resetOpen() { generation++ pendingTab?.close() pendingTab = null cleanup() openingVersionId.value = null downloadError.value = null fallbackUrl.value = null openNotice.value = null } /** ให้เบราว์เซอร์เซฟ URL ลง Downloads (presigned URL ส่ง disposition: attachment อยู่แล้ว — * หน้าเว็บอยู่ที่เดิม ไม่ navigate ไปไหน) */ function saveToDownloads(url: string) { const a = document.createElement('a') a.href = url document.body.append(a) a.click() a.remove() } /** เปิด presigned docx URL ใน Word ผ่าน ms-word:ofe — ถ้าไม่เห็นการสลับแอปภายใน WORD_SWITCH_WAIT_MS * ถือว่าเปิดไม่สำเร็จ: ดาวน์โหลดไฟล์ให้อัตโนมัติ + แจ้งผู้ใช้ (กดซ้ำตอน Word เปิดอยู่แล้วจะสำเร็จ) * ใช้กับลิงก์ที่ถืออยู่เองได้เลย (ไฟล์ร่าง FR-16: response ของ generate-draft คือ URL แล้ว) */ function openDocxUrl(url: string) { resetOpen() fallbackUrl.value = url let switched = false const onBlur = () => { switched = true } const onVisibility = () => { if (document.hidden) switched = true } window.addEventListener('blur', onBlur) document.addEventListener('visibilitychange', onVisibility) const timer = window.setTimeout(() => { if (!switched) { openNotice.value = 'Word ไม่ได้เปิดเอกสารอัตโนมัติ (มักเกิดตอน Word เพิ่งถูกเปิดขึ้นครั้งแรก) — ดาวน์โหลดไฟล์ให้แล้วที่โฟลเดอร์ Downloads ให้เปิดจากที่นั่น หรือกดเปิดใน Word อีกครั้งเมื่อ Word เปิดอยู่แล้ว' saveToDownloads(url) } cleanup() }, WORD_SWITCH_WAIT_MS) cleanup = () => { window.clearTimeout(timer) window.removeEventListener('blur', onBlur) window.removeEventListener('visibilitychange', onVisibility) } window.location.assign(wordOpenUrl(url)) } async function openVersion(documentId: string, versionId: string, format: VersionFormat = 'docx') { if (openingVersionId.value) return resetOpen() const current = generation openingVersionId.value = versionId // Reserve a tab during the user gesture, before the authenticated request loses activation. let tab: Window | null = null try { tab = format === 'pdf' ? window.open('about:blank', '_blank') : null if (tab) tab.opener = null pendingTab = tab const link = await apiJson<{ url: string; expiresAt: string }>(versionDownloadPath(documentId, versionId, format)) if (current !== generation) { tab?.close(); return } if (!link) throw new Error('ขอลิงก์เปิดไฟล์ไม่สำเร็จ') fallbackUrl.value = link.url if (format === 'pdf') { if (tab && !tab.closed) { tab.location.replace(link.url) pendingTab = null } else openNotice.value = 'เบราว์เซอร์บล็อกแท็บใหม่ กรุณากดลิงก์เปิดไฟล์ด้านล่าง' } else { openDocxUrl(link.url) } } catch (err) { tab?.close() if (pendingTab === tab) pendingTab = null if (current === generation) downloadError.value = err instanceof Error ? err.message : 'เปิดไฟล์ไม่สำเร็จ' } finally { if (current === generation) openingVersionId.value = null } } onBeforeUnmount(resetOpen) return { openingVersionId, downloadError, fallbackUrl, openNotice, openVersion, openDocxUrl, resetOpen } }