eCourt-Doc-Edit/web/app/composables/useVersionOpen.ts
Suphonchai Phoonsawat e0558e3cf4 Hotfix: auto-download fallback when Word cold-start drops the open URL
ผู้ใช้จริง (Mac): กด "เปิดใน Word" ตอน Word ยังไม่เปิดค้างอยู่ — Word
cold-start แล้ว URL event หลุด แสดงหน้า Recent แทนเอกสาร (ข้อจำกัด
ms-word:ofe บน macOS; กลไกนี้ผ่าน manual test ตอน Word เปิดอยู่ก่อนแล้ว)

- useVersionOpen.openDocxUrl: หน้าต่างรอ 1.5s → 3s และเมื่อไม่เห็นการ
  สลับแอป → ดาวน์โหลดไฟล์ให้อัตโนมัติ (anchor → presigned attachment)
  + ข้อความแนะนำเปิดจาก Downloads หรือกดซ้ำเมื่อ Word เปิดอยู่แล้ว
  — ใช้ร่วมทุกจุดเปิด docx (version rows, v0, ไฟล์ร่าง) PDF ไม่เกี่ยว
- Tests: timing edge 2999/3000 + href + notice, ไม่ download เมื่อสลับ
  แอปจริง, re-click ยกเลิก timer เก่า (web 59/59 + build)
- Spec FR-15/16 (~3s + auto-download + หมายเหตุ cold-start) +
  acceptance-review เพิ่ม manual check item
- QA reviewer PASS (1 round; minors 3 — แก้ 1 ยอมรับ 2 พร้อมเหตุผล)

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-10 12:22:57 +07:00

105 lines
5.3 KiB
TypeScript

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<string | null>(null)
const downloadError = ref<string | null>(null)
const fallbackUrl = ref<string | null>(null)
const openNotice = ref<string | null>(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 }
}