"use client" import { useSession, signIn, signOut } from "next-auth/react" import { useCloudSync } from "@/hooks/use-cloud-sync" import { Cloud, CloudOff, Loader2, Github, LogOut, RefreshCw, Check } from "lucide-react" import { useState } from "react" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" function formatRelative(ts: number | null): string { if (!ts) return "—" const diff = Date.now() - ts if (diff < 5_000) return "vừa xong" if (diff < 60_000) return `${Math.floor(diff / 1000)}s trước` if (diff < 3_600_000) return `${Math.floor(diff / 60_000)} phút trước` if (diff < 86_400_000) return `${Math.floor(diff / 3_600_000)} giờ trước` return `${Math.floor(diff / 86_400_000)} ngày trước` } export function CloudSyncButton() { // Tắt hoàn toàn nếu cloud-sync chưa được cấu hình (env NEXT_PUBLIC_ENABLE_CLOUD_SYNC). // useSession sẽ throw nếu không có SessionProvider phía trên, nên gate ngay tại đây. if (process.env.NEXT_PUBLIC_ENABLE_CLOUD_SYNC !== "1") return null return } function CloudSyncButtonInner() { const { data: session, status: authStatus } = useSession() const { status, lastSyncAt, error, pullAll, pushAll } = useCloudSync() const [open, setOpen] = useState(false) if (authStatus === "loading") { return ( ) } if (authStatus !== "authenticated") { return ( ) } const user = session?.user const Icon = status === "error" ? CloudOff : status === "pulling" || status === "pushing" ? Loader2 : Check const iconClass = `h-3.5 w-3.5 ${status === "pulling" || status === "pushing" ? "animate-spin" : ""} ${ status === "error" ? "text-destructive" : "text-emerald-500" }` return (
{user?.name}
{user?.email}
{ setOpen(false) void pullAll() }} className="gap-2 text-xs" >
Đồng bộ ngay Cuối: {formatRelative(lastSyncAt)}
{ setOpen(false) void pushAll() }} className="gap-2 text-xs" > Đẩy local lên cloud { setOpen(false) void signOut({ redirect: false }) }} className="gap-2 text-xs text-destructive focus:text-destructive" > Đăng xuất {error ? ( <>
{error}
) : null}
) }