226 lines
6.8 KiB
TypeScript
226 lines
6.8 KiB
TypeScript
// src/renderer/components/CloudSyncSection.tsx
|
|
// Phase V2-4: Settings에 표시되는 Cloud Sync 섹션
|
|
// 로그인 상태 / Sync Now 버튼 / 마지막 동기화 / 환경변수 설정
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import { Box, Button, Stack, Alert, CircularProgress } from '@mui/material'
|
|
import { Cloud, CloudCheck, Globe, GitBranch } from 'lucide-react'
|
|
import { d3roPalette, typoSx } from '@d3ro/ui/theme'
|
|
|
|
interface CloudSyncState {
|
|
authenticated: boolean
|
|
userEmail: string | null
|
|
lastSyncAt: number | null
|
|
syncing: boolean
|
|
}
|
|
|
|
interface SyncProgress {
|
|
current: number
|
|
total: number
|
|
table: string
|
|
}
|
|
|
|
export function CloudSyncSection(): React.ReactElement {
|
|
const [state, setState] = useState<CloudSyncState>({
|
|
authenticated: false,
|
|
userEmail: null,
|
|
lastSyncAt: null,
|
|
syncing: false
|
|
})
|
|
const [progress, setProgress] = useState<SyncProgress | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [info, setInfo] = useState<string | null>(null)
|
|
const [busy, setBusy] = useState(false)
|
|
|
|
// 초기 상태 로드 + 이벤트 구독
|
|
useEffect(() => {
|
|
void window.electronAPI.cloudSync.getState().then((r) => {
|
|
if (r.success) setState(r.data)
|
|
})
|
|
|
|
const unsubAuth = window.electronAPI.cloudSync.onAuthChanged((payload) => {
|
|
setState((prev) => ({
|
|
...prev,
|
|
authenticated: payload.user !== null,
|
|
userEmail: payload.user?.email ?? null
|
|
}))
|
|
})
|
|
const unsubProgress = window.electronAPI.cloudSync.onSyncProgress((p) => {
|
|
setProgress(p)
|
|
})
|
|
const unsubComplete = window.electronAPI.cloudSync.onSyncComplete((p) => {
|
|
setProgress(null)
|
|
setInfo(`동기화 완료: ${p.pushed}건${p.errors.length > 0 ? ` (오류 ${p.errors.length})` : ''}`)
|
|
setBusy(false)
|
|
setState((prev) => ({ ...prev, syncing: false, lastSyncAt: Date.now() }))
|
|
})
|
|
const unsubError = window.electronAPI.cloudSync.onSyncError((p) => {
|
|
setProgress(null)
|
|
setError(p.error)
|
|
setBusy(false)
|
|
setState((prev) => ({ ...prev, syncing: false }))
|
|
})
|
|
return () => {
|
|
unsubAuth()
|
|
unsubProgress()
|
|
unsubComplete()
|
|
unsubError()
|
|
}
|
|
}, [])
|
|
|
|
async function handleSignIn(provider: 'google' | 'github'): Promise<void> {
|
|
setError(null)
|
|
setBusy(true)
|
|
try {
|
|
const r = await window.electronAPI.cloudSync.signIn({ provider })
|
|
if (!r.success) {
|
|
setError(r.error.message)
|
|
} else {
|
|
setInfo('브라우저에서 로그인을 완료해주세요...')
|
|
}
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
async function handleSignOut(): Promise<void> {
|
|
setBusy(true)
|
|
try {
|
|
await window.electronAPI.cloudSync.signOut()
|
|
setInfo('로그아웃되었습니다')
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
async function handleSync(): Promise<void> {
|
|
setError(null)
|
|
setBusy(true)
|
|
setState((prev) => ({ ...prev, syncing: true }))
|
|
try {
|
|
await window.electronAPI.cloudSync.pushAll()
|
|
} finally {
|
|
// 완료/에러 이벤트로 setBusy(false) 처리됨
|
|
}
|
|
}
|
|
|
|
async function handlePull(): Promise<void> {
|
|
setError(null)
|
|
setBusy(true)
|
|
setState((prev) => ({ ...prev, syncing: true }))
|
|
try {
|
|
await window.electronAPI.cloudSync.pullAll()
|
|
} finally {
|
|
// 완료/에러 이벤트로 setBusy(false) 처리됨
|
|
}
|
|
}
|
|
|
|
const lastSyncText = state.lastSyncAt
|
|
? new Date(state.lastSyncAt).toLocaleString('ko-KR')
|
|
: '없음'
|
|
|
|
return (
|
|
<Box sx={{ p: 3, bgcolor: d3roPalette.bg.elevated, borderRadius: 2 }}>
|
|
<Stack direction="row" alignItems="center" spacing={1.5} sx={{ mb: 2 }}>
|
|
{state.authenticated ? (
|
|
<CloudCheck size={24} style={{ color: d3roPalette.tag.green }} />
|
|
) : (
|
|
<Cloud size={24} style={{ color: d3roPalette.text.label }} />
|
|
)}
|
|
<Box sx={{ ...typoSx('heading'), color: d3roPalette.text.primary }}>Cloud Sync</Box>
|
|
</Stack>
|
|
|
|
{!state.authenticated && (
|
|
<Stack spacing={2}>
|
|
<Box sx={{ ...typoSx('label'), color: d3roPalette.text.label, mb: 1 }}>OAuth 로그인</Box>
|
|
<Stack direction="row" spacing={1}>
|
|
<Button
|
|
variant="contained"
|
|
startIcon={<Globe size={18} />}
|
|
onClick={() => void handleSignIn('google')}
|
|
disabled={busy}
|
|
>
|
|
Google
|
|
</Button>
|
|
<Button
|
|
variant="outlined"
|
|
startIcon={<GitBranch size={18} />}
|
|
onClick={() => void handleSignIn('github')}
|
|
disabled={busy}
|
|
>
|
|
GitHub
|
|
</Button>
|
|
</Stack>
|
|
</Stack>
|
|
)}
|
|
|
|
{state.authenticated && (
|
|
<Stack spacing={2}>
|
|
<Box sx={{ color: d3roPalette.text.primary, fontSize: 13 }}>
|
|
로그인됨: <strong>{state.userEmail ?? '(이메일 없음)'}</strong>
|
|
</Box>
|
|
<Box sx={{ color: d3roPalette.text.muted, fontSize: 12 }}>
|
|
마지막 동기화: {lastSyncText}
|
|
</Box>
|
|
|
|
{progress && (
|
|
<Box>
|
|
<Box sx={{ color: d3roPalette.text.label, fontSize: 12, mb: 0.5 }}>
|
|
{progress.table}: {progress.current}/{progress.total}
|
|
</Box>
|
|
<Box
|
|
sx={{
|
|
height: 4,
|
|
bgcolor: d3roPalette.bg.inset,
|
|
borderRadius: 2,
|
|
overflow: 'hidden'
|
|
}}
|
|
>
|
|
<Box
|
|
sx={{
|
|
height: '100%',
|
|
width: `${progress.total > 0 ? (progress.current / progress.total) * 100 : 0}%`,
|
|
bgcolor: d3roPalette.accent.main,
|
|
transition: 'width 200ms'
|
|
}}
|
|
/>
|
|
</Box>
|
|
</Box>
|
|
)}
|
|
|
|
<Stack direction="row" spacing={1}>
|
|
<Button
|
|
variant="contained"
|
|
onClick={() => void handleSync()}
|
|
disabled={busy || state.syncing}
|
|
startIcon={state.syncing ? <CircularProgress size={16} /> : null}
|
|
>
|
|
{state.syncing ? '동기화 중...' : 'Push'}
|
|
</Button>
|
|
<Button
|
|
variant="outlined"
|
|
onClick={() => void handlePull()}
|
|
disabled={busy || state.syncing}
|
|
>
|
|
Pull
|
|
</Button>
|
|
<Button variant="outlined" onClick={() => void handleSignOut()} disabled={busy} color="warning">
|
|
로그아웃
|
|
</Button>
|
|
</Stack>
|
|
</Stack>
|
|
)}
|
|
|
|
{error && (
|
|
<Alert severity="error" sx={{ mt: 2 }} variant="outlined">
|
|
{error}
|
|
</Alert>
|
|
)}
|
|
{info && !error && (
|
|
<Alert severity="info" sx={{ mt: 2 }} variant="outlined">
|
|
{info}
|
|
</Alert>
|
|
)}
|
|
</Box>
|
|
)
|
|
}
|