fix: Optional OCR-Assisted Draft Pre-Fill

This commit is contained in:
voltsrage
2026-06-28 01:48:15 +08:00
parent c160c5f912
commit c26ef22670
16 changed files with 186 additions and 69 deletions
@@ -1,40 +1,56 @@
import { computed, watch, onUnmounted, type Ref } from 'vue'
import { useIntervalFn } from '@vueuse/core'
import { ref, watch, onUnmounted, type Ref } from 'vue'
import { getBlob } from '../api/client'
import { useBatchStore } from '../stores/batches'
const PRESIGNED_URL_TTL_MS = 15 * 60 * 1000
const REFRESH_BUFFER_MS = 60_000
export function usePresignedUrl(batchId: Ref<string | undefined>) {
const batchStore = useBatchStore()
const documentUrl = ref<string | null>(null)
const documentError = ref<string | null>(null)
const documentUrl = computed(() => batchStore.documentUrl)
let currentBlobUrl: string | null = null
async function refreshUrl(): Promise<void> {
if (batchId.value) {
await batchStore.getBatch(batchId.value)
function revokeBlobUrl(): void {
if (currentBlobUrl) {
URL.revokeObjectURL(currentBlobUrl)
currentBlobUrl = null
}
}
const { pause, resume } = useIntervalFn(
() => { void refreshUrl() },
PRESIGNED_URL_TTL_MS - REFRESH_BUFFER_MS,
{ immediate: false }
)
async function refreshUrl(): Promise<void> {
revokeBlobUrl()
documentUrl.value = null
documentError.value = null
if (!batchId.value) return
try {
await batchStore.getBatch(batchId.value)
const blob = await getBlob(`digitization-batches/${batchId.value}/document`)
if (!blob.size) {
throw new Error('Document is empty')
}
currentBlobUrl = URL.createObjectURL(blob)
documentUrl.value = currentBlobUrl
} catch (e: unknown) {
documentError.value = e instanceof Error ? e.message : 'Failed to load document'
}
}
watch(
batchId,
async (id) => {
pause()
if (id) {
await refreshUrl()
resume()
} else {
revokeBlobUrl()
documentUrl.value = null
documentError.value = null
}
},
{ immediate: true }
)
onUnmounted(pause)
onUnmounted(revokeBlobUrl)
return { documentUrl, refreshUrl }
return { documentUrl, documentError, refreshUrl }
}