fix(mobile): harden recorder, queue, context, and screen async lifecycles
This commit is contained in:
parent
3f9e412257
commit
bd3c1a98d9
25 changed files with 2279 additions and 68 deletions
|
|
@ -176,16 +176,25 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
return () => { void supabase.removeChannel(channel) }
|
||||
}, [load, user?.id])
|
||||
|
||||
const actorUserIdRef = useRef<string | null>(user?.id ?? null)
|
||||
useEffect(() => {
|
||||
actorUserIdRef.current = user?.id ?? null
|
||||
}, [user?.id])
|
||||
|
||||
async function toggleActive(command: SyncedCommand): Promise<void> {
|
||||
if (!user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
setMutationId(command.id)
|
||||
try {
|
||||
setActiveId(await setActiveSyncedCommand(user.id, activeId === command.id ? null : command.id))
|
||||
const nextActiveId = await setActiveSyncedCommand(actorUserId, activeId === command.id ? null : command.id)
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
setActiveId(nextActiveId)
|
||||
setSyncError(null)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
} finally {
|
||||
setMutationId(null)
|
||||
if (alive.current) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -207,42 +216,48 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
|
||||
async function save(): Promise<void> {
|
||||
if (!editor || !user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
setMutationId(editor.command?.id ?? 'create')
|
||||
setEditorError(null)
|
||||
try {
|
||||
const saved = editor.command
|
||||
? await updateSyncedCommand(user.id, editor.command, editor)
|
||||
: await createSyncedCommand(user.id, {
|
||||
? await updateSyncedCommand(actorUserId, editor.command, editor)
|
||||
: await createSyncedCommand(actorUserId, {
|
||||
...editor,
|
||||
sortOrder: Math.max(1000, ...commands.map((item) => item.sortOrder + 10)),
|
||||
})
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
const next = [...commands.filter((item) => item.id !== saved.id), saved]
|
||||
.sort((a, b) => a.sortOrder - b.sortOrder || a.createdAt.localeCompare(b.createdAt))
|
||||
setCommands(next)
|
||||
setSelectedId(saved.id)
|
||||
setEditor(null)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
const code = error instanceof CommandSyncError ? error.code : 'server'
|
||||
setEditorError(code)
|
||||
if (code === 'conflict') void load(false, true)
|
||||
} finally {
|
||||
setMutationId(null)
|
||||
if (alive.current) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(command: SyncedCommand): Promise<void> {
|
||||
if (!user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
setMutationId(command.id)
|
||||
try {
|
||||
await deleteSyncedCommand(user.id, command)
|
||||
await deleteSyncedCommand(actorUserId, command)
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
const next = commands.filter((item) => item.id !== command.id)
|
||||
setCommands(next)
|
||||
if (selectedId === command.id) setSelectedId(next[0]?.id ?? null)
|
||||
if (activeId === command.id) setActiveId(null)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
} finally {
|
||||
setMutationId(null)
|
||||
if (alive.current) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -259,18 +274,23 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
|
||||
async function move(command: SyncedCommand, direction: 'up' | 'down'): Promise<void> {
|
||||
if (!user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
setMutationId(command.id)
|
||||
try {
|
||||
setCommands(await reorderSyncedCommand(user.id, command, direction))
|
||||
const reordered = await reorderSyncedCommand(actorUserId, command, direction)
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
setCommands(reordered)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
} finally {
|
||||
setMutationId(null)
|
||||
if (alive.current) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
const run = useCallback(async (): Promise<void> => {
|
||||
if (!selected || running) return
|
||||
const actorUserId = actorUserIdRef.current
|
||||
const token = session?.access_token ?? ''
|
||||
if (!token) {
|
||||
setRunError('AUTH_REQUIRED')
|
||||
|
|
@ -283,12 +303,15 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
setRunError(null)
|
||||
setRetryable(false)
|
||||
try {
|
||||
setOutput(await executeSyncedCommand(selected, input, {
|
||||
const result = await executeSyncedCommand(selected, input, {
|
||||
accessToken: token,
|
||||
model: preferences.preferredLlmModel,
|
||||
signal: controller.signal,
|
||||
}))
|
||||
})
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
setOutput(result)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
const failure = error instanceof CommandExecutionError
|
||||
? error
|
||||
: new CommandExecutionError('SERVER_ERROR', true)
|
||||
|
|
|
|||
|
|
@ -89,6 +89,7 @@ export default function HistoryDetailScreen({
|
|||
playerRef.current = player
|
||||
const mountedRef = useRef(true)
|
||||
const loadGenerationRef = useRef(0)
|
||||
const playbackGenerationRef = useRef(0)
|
||||
const [detail, setDetail] = useState<HistoryDetail | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
|
@ -109,6 +110,7 @@ export default function HistoryDetailScreen({
|
|||
return () => {
|
||||
mountedRef.current = false
|
||||
loadGenerationRef.current += 1
|
||||
playbackGenerationRef.current += 1
|
||||
void Promise.resolve(playerRef.current.stopPlayer()).catch(() => undefined)
|
||||
}
|
||||
}, [])
|
||||
|
|
@ -306,53 +308,62 @@ export default function HistoryDetailScreen({
|
|||
|
||||
const controlAudio = async (audioFile: AudioFile): Promise<void> => {
|
||||
if (user === null || audioBusy) return
|
||||
const playbackGeneration = ++playbackGenerationRef.current
|
||||
setAudioBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
if (activeAudioId === audioFile.id && player.state.isPlaying) {
|
||||
await player.pausePlayer()
|
||||
if (mountedRef.current) setPaused(true)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) setPaused(true)
|
||||
return
|
||||
}
|
||||
if (activeAudioId === audioFile.id && paused) {
|
||||
await player.resumePlayer()
|
||||
if (mountedRef.current) setPaused(false)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) setPaused(false)
|
||||
return
|
||||
}
|
||||
if (activeAudioId !== null) {
|
||||
await player.stopPlayer()
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
}
|
||||
const signedUrl = await createAudioPlaybackUrl(user.id, audioFile)
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
await player.startPlayer(signedUrl)
|
||||
if (!mountedRef.current) {
|
||||
void Promise.resolve(player.stopPlayer()).catch(() => undefined)
|
||||
return
|
||||
}
|
||||
if (playbackGeneration !== playbackGenerationRef.current) return
|
||||
setActiveAudioId(audioFile.id)
|
||||
setPaused(false)
|
||||
} catch {
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
setActiveAudioId(null)
|
||||
setPaused(false)
|
||||
setError(t('mobile.hist.audioError'))
|
||||
} finally {
|
||||
if (mountedRef.current) setAudioBusy(false)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) {
|
||||
setAudioBusy(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const stopAudio = async (): Promise<void> => {
|
||||
const playbackGeneration = ++playbackGenerationRef.current
|
||||
setAudioBusy(true)
|
||||
try {
|
||||
await player.stopPlayer()
|
||||
if (!mountedRef.current) return
|
||||
if (!mountedRef.current || playbackGeneration !== playbackGenerationRef.current) return
|
||||
setActiveAudioId(null)
|
||||
setPaused(false)
|
||||
} catch {
|
||||
if (mountedRef.current) setError(t('mobile.hist.audioError'))
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) {
|
||||
setError(t('mobile.hist.audioError'))
|
||||
}
|
||||
} finally {
|
||||
if (mountedRef.current) setAudioBusy(false)
|
||||
if (mountedRef.current && playbackGeneration === playbackGenerationRef.current) {
|
||||
setAudioBusy(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// src/screens/HomeScreen.tsx — 홈(오늘). 데스크톱 HomePage 와 같은 문법:
|
||||
// 제목·날짜 → 녹음 시작 안내 → 한 줄 요약 → 최근 기록 5 · 최근 회의 3 → (무료) 요금제 한 줄.
|
||||
// 카드·통계 격자 없이 제목 + 1px 선. 조회한 값만 보여 주고, 못 불러오면 그렇다고 말한다.
|
||||
import { useCallback, useMemo, useState } from 'react'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import {
|
||||
Pressable,
|
||||
RefreshControl,
|
||||
|
|
@ -91,21 +91,46 @@ export default function HomeScreen(): React.ReactElement {
|
|||
const [meetingsError, setMeetingsError] = useState(false)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [refreshing, setRefreshing] = useState(false)
|
||||
const mountedRef = useRef(true)
|
||||
const refreshGenerationRef = useRef(0)
|
||||
const currentUserIdRef = useRef<string | null>(user?.id ?? null)
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
refreshGenerationRef.current += 1
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
currentUserIdRef.current = user?.id ?? null
|
||||
}, [user?.id])
|
||||
|
||||
const refresh = useCallback(async (pullToRefresh = false): Promise<void> => {
|
||||
if (!user?.id) {
|
||||
const generation = ++refreshGenerationRef.current
|
||||
const targetUserId = user?.id ?? null
|
||||
if (!targetUserId) {
|
||||
setStats(null)
|
||||
setMeetings(null)
|
||||
setStatsError(true)
|
||||
setMeetingsError(true)
|
||||
setLoading(false)
|
||||
setRefreshing(false)
|
||||
return
|
||||
}
|
||||
if (pullToRefresh) setRefreshing(true)
|
||||
else setLoading(true)
|
||||
const [statsResult, meetingsResult] = await Promise.allSettled([
|
||||
loadDashboardStats(user.id),
|
||||
listMeetingsPage({ userId: user.id, page: 0, pageSize: RECENT_MEETING_LIMIT }),
|
||||
loadDashboardStats(targetUserId),
|
||||
listMeetingsPage({ userId: targetUserId, page: 0, pageSize: RECENT_MEETING_LIMIT }),
|
||||
refreshEntitlement(),
|
||||
])
|
||||
if (
|
||||
!mountedRef.current
|
||||
|| generation !== refreshGenerationRef.current
|
||||
|| currentUserIdRef.current !== targetUserId
|
||||
) return
|
||||
if (statsResult.status === 'fulfilled') {
|
||||
setStats(statsResult.value)
|
||||
setStatsError(false)
|
||||
|
|
|
|||
|
|
@ -127,6 +127,7 @@ export default function KnowledgeScreen(): React.ReactElement {
|
|||
|
||||
const loadMore = async (): Promise<void> => {
|
||||
if (user === null || loading || loadingMore || !hasMore) return
|
||||
const generation = requestGeneration.current
|
||||
const targetPage = page + 1
|
||||
setLoadingMore(true)
|
||||
try {
|
||||
|
|
@ -135,14 +136,16 @@ export default function KnowledgeScreen(): React.ReactElement {
|
|||
search: activeDocumentSearch,
|
||||
page: targetPage,
|
||||
})
|
||||
if (generation !== requestGeneration.current) return
|
||||
setDocuments((current) => mergeKnowledgeDocuments(current, result.documents))
|
||||
setPage(targetPage)
|
||||
setTotal(result.total)
|
||||
setHasMore(result.hasMore)
|
||||
} catch (requestError) {
|
||||
if (generation !== requestGeneration.current) return
|
||||
setError(errorCode(requestError))
|
||||
} finally {
|
||||
setLoadingMore(false)
|
||||
if (generation === requestGeneration.current) setLoadingMore(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@ export default function MeetingsScreen({
|
|||
const templateRequestGeneration = useRef(0)
|
||||
const editorBusyRef = useRef(false)
|
||||
const editorUserIdRef = useRef(user?.id ?? null)
|
||||
const mountedRef = useRef(true)
|
||||
const handledCreateRequests = useRef(new Set<number>())
|
||||
const openCreateRef = useRef<() => void>(() => undefined)
|
||||
const [meetings, setMeetings] = useState<Meeting[]>([])
|
||||
|
|
@ -85,6 +86,13 @@ export default function MeetingsScreen({
|
|||
const [editorError, setEditorError] = useState<string | null>(null)
|
||||
const [editorBusy, setEditorBusy] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
const nextUserId = user?.id ?? null
|
||||
if (editorUserIdRef.current === nextUserId) return
|
||||
|
|
@ -152,6 +160,7 @@ export default function MeetingsScreen({
|
|||
|
||||
const loadMore = async (): Promise<void> => {
|
||||
if (user === null || loading || loadingMore || !hasMore) return
|
||||
const generation = requestGeneration.current
|
||||
const targetPage = page + 1
|
||||
setLoadingMore(true)
|
||||
try {
|
||||
|
|
@ -160,14 +169,18 @@ export default function MeetingsScreen({
|
|||
search: committedSearch,
|
||||
page: targetPage,
|
||||
})
|
||||
if (generation !== requestGeneration.current || !mountedRef.current) return
|
||||
setMeetings((current) => mergeMeetings(current, result.meetings))
|
||||
setPage(targetPage)
|
||||
setTotal(result.total)
|
||||
setHasMore(result.hasMore)
|
||||
} catch (requestError) {
|
||||
if (generation !== requestGeneration.current || !mountedRef.current) return
|
||||
setError(meetingErrorMessage(requestError, t))
|
||||
} finally {
|
||||
setLoadingMore(false)
|
||||
if (generation === requestGeneration.current && mountedRef.current) {
|
||||
setLoadingMore(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -257,6 +270,7 @@ export default function MeetingsScreen({
|
|||
const saveEditor = async (): Promise<void> => {
|
||||
if (user === null || editor === null || editorBusyRef.current) return
|
||||
const mode = editor
|
||||
const actorUserId = user.id
|
||||
editorBusyRef.current = true
|
||||
setEditorBusy(true)
|
||||
setEditorError(null)
|
||||
|
|
@ -269,13 +283,14 @@ export default function MeetingsScreen({
|
|||
.split(/[\r\n,]+/)
|
||||
.map((attendee) => attendee.trim())
|
||||
.filter((attendee) => attendee.length > 0)
|
||||
const created = await createMeetingWorkspaceWithMetadata(user.id, {
|
||||
const created = await createMeetingWorkspaceWithMetadata(actorUserId, {
|
||||
title: editorTitle,
|
||||
attendees,
|
||||
language: editorLanguage,
|
||||
templateId: editorTemplateId,
|
||||
idempotencyKey: editorCreationKey,
|
||||
})
|
||||
if (!mountedRef.current || editorUserIdRef.current !== actorUserId) return
|
||||
setMeetings((current) => mergeMeetings([created], current))
|
||||
setTotal((current) => current + 1)
|
||||
setEditor(null)
|
||||
|
|
@ -295,16 +310,20 @@ export default function MeetingsScreen({
|
|||
}
|
||||
} else {
|
||||
const updated = await updateMeetingTitleRevisionSafe(mode.meeting, editorTitle)
|
||||
if (!mountedRef.current || editorUserIdRef.current !== actorUserId) return
|
||||
setMeetings((current) => current.map((meeting) => (
|
||||
meeting.id === updated.id ? updated : meeting
|
||||
)))
|
||||
setEditor(null)
|
||||
}
|
||||
} catch (requestError) {
|
||||
if (!mountedRef.current || editorUserIdRef.current !== actorUserId) return
|
||||
setEditorError(meetingErrorMessage(requestError, t))
|
||||
} finally {
|
||||
editorBusyRef.current = false
|
||||
setEditorBusy(false)
|
||||
if (mountedRef.current && editorUserIdRef.current === actorUserId) {
|
||||
editorBusyRef.current = false
|
||||
setEditorBusy(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -703,6 +703,10 @@ export default function RecordScreen({ route, navigation }: RecordScreenProps):
|
|||
await processInput(input);
|
||||
} catch (importError) {
|
||||
if (!mountedRef.current) return;
|
||||
if (importError instanceof AudioPipelineError && importError.code === 'cancelled') {
|
||||
setState('idle');
|
||||
return;
|
||||
}
|
||||
setError(errorMessage(importError, t('mobile.rec.sharedImportFailed')));
|
||||
setState('error');
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { useCallback, useMemo, useRef, useState } from 'react'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import {
|
||||
ActivityIndicator,
|
||||
KeyboardAvoidingView,
|
||||
|
|
@ -37,6 +37,8 @@ export default function TeamsScreen({ navigation }: TeamsScreenProps): React.Rea
|
|||
const { palette } = useMobilePreferences()
|
||||
const styles = useMemo(() => createStyles(palette), [palette])
|
||||
const loadGeneration = useRef(0)
|
||||
const mountedRef = useRef(true)
|
||||
const userIdRef = useRef<string | null>(user?.id ?? null)
|
||||
const [teams, setTeams] = useState<TeamSummary[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [refreshing, setRefreshing] = useState(false)
|
||||
|
|
@ -70,6 +72,17 @@ export default function TeamsScreen({ navigation }: TeamsScreenProps): React.Rea
|
|||
}
|
||||
}, [user])
|
||||
|
||||
useEffect(() => {
|
||||
mountedRef.current = true
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
userIdRef.current = user?.id ?? null
|
||||
}, [user?.id])
|
||||
|
||||
useFocusEffect(useCallback(() => {
|
||||
void load(false)
|
||||
return () => { loadGeneration.current += 1 }
|
||||
|
|
@ -77,18 +90,21 @@ export default function TeamsScreen({ navigation }: TeamsScreenProps): React.Rea
|
|||
|
||||
const submitCreate = async (): Promise<void> => {
|
||||
if (user === null || editorBusy) return
|
||||
const actorUserId = user.id
|
||||
setEditorBusy(true)
|
||||
setEditorError(null)
|
||||
try {
|
||||
const team = await createTeam(user.id, name)
|
||||
const team = await createTeam(actorUserId, name)
|
||||
if (!mountedRef.current || userIdRef.current !== actorUserId) return
|
||||
setTeams((current) => [team, ...current.filter((row) => row.id !== team.id)])
|
||||
setName('')
|
||||
setEditorOpen(false)
|
||||
navigation.navigate('TeamDetail', { teamId: team.id })
|
||||
} catch (requestError) {
|
||||
if (!mountedRef.current || userIdRef.current !== actorUserId) return
|
||||
setEditorError(teamErrorCode(requestError))
|
||||
} finally {
|
||||
setEditorBusy(false)
|
||||
if (mountedRef.current) setEditorBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue