feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes

This commit is contained in:
Yun Chan 2026-11-14 11:51:41 +09:00
parent 01ef3293cf
commit 66b6b45ba9
399 changed files with 17828 additions and 11330 deletions

View file

@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import {
ActivityIndicator,
Alert,
@ -18,6 +18,7 @@ import {
PhysicalButton,
} from '@d3ro/ui-native'
import { useI18n, type TranslationKey } from '@d3ro/i18n'
import { formatBytes, formatDurationClockFromMs } from '@d3ro/core/format'
import type { AudioFile, HistoryEntry } from '@d3ro/api-client'
import { useAuth } from '../lib/auth-context'
import {
@ -84,6 +85,10 @@ export default function HistoryDetailScreen({
const { palette } = useMobilePreferences()
const styles = useMemo(() => createStyles(palette), [palette])
const player = useSoundWithStates()
const playerRef = useRef(player)
playerRef.current = player
const mountedRef = useRef(true)
const loadGenerationRef = useRef(0)
const [detail, setDetail] = useState<HistoryDetail | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
@ -99,6 +104,15 @@ export default function HistoryDetailScreen({
const [paused, setPaused] = useState(false)
const [audioBusy, setAudioBusy] = useState(false)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
loadGenerationRef.current += 1
void Promise.resolve(playerRef.current.stopPlayer()).catch(() => undefined)
}
}, [])
const applyEntry = useCallback((entry: HistoryEntry): void => {
setTitle(entry.title ?? '')
setOriginalText(entry.original_text)
@ -106,7 +120,9 @@ export default function HistoryDetailScreen({
}, [])
const load = useCallback(async (): Promise<void> => {
const generation = ++loadGenerationRef.current
if (user === null) {
if (!mountedRef.current) return
setError(t('mobile.account.loginRequired'))
setLoading(false)
return
@ -116,14 +132,17 @@ export default function HistoryDetailScreen({
setError(null)
try {
const nextDetail = await getHistoryDetail(user.id, route.params.historyId)
if (!mountedRef.current || loadGenerationRef.current !== generation) return
setDetail(nextDetail)
applyEntry(nextDetail.entry)
setOffline(false)
setConflict(false)
} catch (requestError) {
if (!mountedRef.current || loadGenerationRef.current !== generation) return
if (requestError instanceof HistoryServiceError && requestError.code === 'network') {
try {
const cached = await loadHistoryCache(user.id)
if (!mountedRef.current || loadGenerationRef.current !== generation) return
const entry = cached?.entries.find((item) => item.id === route.params.historyId)
if (entry !== undefined) {
setDetail({ entry, audioFiles: [], jobs: [] })
@ -135,13 +154,16 @@ export default function HistoryDetailScreen({
// The request error below remains authoritative when secure cache access fails.
}
}
if (!mountedRef.current || loadGenerationRef.current !== generation) return
setError(
requestError instanceof HistoryServiceError && requestError.code === 'not-found'
? t('mobile.hist.notFound')
: t('mobile.hist.loadError'),
)
} finally {
setLoading(false)
if (mountedRef.current && loadGenerationRef.current === generation) {
setLoading(false)
}
}
}, [applyEntry, route.params.historyId, t, user])
@ -194,11 +216,13 @@ export default function HistoryDetailScreen({
detail.entry.revision,
patch,
)
if (!mountedRef.current) return
setDetail((current) => current === null ? null : { ...current, entry: updated })
applyEntry(updated)
setEditing(false)
setConflict(false)
} catch (requestError) {
if (!mountedRef.current) return
if (requestError instanceof HistoryServiceError && requestError.code === 'conflict') {
setConflict(true)
setError(t('mobile.hist.conflict'))
@ -206,7 +230,7 @@ export default function HistoryDetailScreen({
setError(t('mobile.hist.editFailed'))
}
} finally {
setSaving(false)
if (mountedRef.current) setSaving(false)
}
}
@ -223,12 +247,14 @@ export default function HistoryDetailScreen({
previous.revision,
{ is_favorite: optimistic.is_favorite },
)
if (!mountedRef.current) return
setDetail((current) => current === null ? null : { ...current, entry: updated })
} catch {
if (!mountedRef.current) return
setDetail((current) => current === null ? null : { ...current, entry: previous })
setError(t('mobile.hist.favoriteFailed'))
} finally {
setSaving(false)
if (mountedRef.current) setSaving(false)
}
}
@ -254,8 +280,10 @@ export default function HistoryDetailScreen({
setError(null)
try {
await deleteHistoryRevisionSafe(user.id, detail.entry.id, detail.entry.revision)
if (!mountedRef.current) return
navigation.goBack()
} catch {
if (!mountedRef.current) return
setError(t('mobile.hist.deleteFailed'))
setDeleting(false)
}
@ -272,7 +300,7 @@ export default function HistoryDetailScreen({
message,
})
} catch {
setError(t('mobile.hist.shareFailed'))
if (mountedRef.current) setError(t('mobile.hist.shareFailed'))
}
}
@ -283,25 +311,34 @@ export default function HistoryDetailScreen({
try {
if (activeAudioId === audioFile.id && player.state.isPlaying) {
await player.pausePlayer()
setPaused(true)
if (mountedRef.current) setPaused(true)
return
}
if (activeAudioId === audioFile.id && paused) {
await player.resumePlayer()
setPaused(false)
if (mountedRef.current) setPaused(false)
return
}
if (activeAudioId !== null) await player.stopPlayer()
if (activeAudioId !== null) {
await player.stopPlayer()
if (!mountedRef.current) return
}
const signedUrl = await createAudioPlaybackUrl(user.id, audioFile)
if (!mountedRef.current) return
await player.startPlayer(signedUrl)
if (!mountedRef.current) {
void Promise.resolve(player.stopPlayer()).catch(() => undefined)
return
}
setActiveAudioId(audioFile.id)
setPaused(false)
} catch {
if (!mountedRef.current) return
setActiveAudioId(null)
setPaused(false)
setError(t('mobile.hist.audioError'))
} finally {
setAudioBusy(false)
if (mountedRef.current) setAudioBusy(false)
}
}
@ -309,12 +346,13 @@ export default function HistoryDetailScreen({
setAudioBusy(true)
try {
await player.stopPlayer()
if (!mountedRef.current) return
setActiveAudioId(null)
setPaused(false)
} catch {
setError(t('mobile.hist.audioError'))
if (mountedRef.current) setError(t('mobile.hist.audioError'))
} finally {
setAudioBusy(false)
if (mountedRef.current) setAudioBusy(false)
}
}
@ -633,18 +671,10 @@ function MetaRow({ label, value }: { label: string; value: string }): React.Reac
)
}
export function formatDuration(durationMs: number): string {
const totalSeconds = Math.max(0, Math.floor(durationMs / 1000))
const minutes = Math.floor(totalSeconds / 60)
const seconds = totalSeconds % 60
return `${minutes}:${String(seconds).padStart(2, '0')}`
}
export { formatBytes }
export function formatBytes(bytes: number): string {
if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'
if (bytes < 1024) return `${Math.round(bytes)} B`
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
export function formatDuration(durationMs: number): string {
return formatDurationClockFromMs(durationMs)
}
type Palette = ReturnType<typeof useMobilePreferences>['palette']