import { useState, useEffect, useRef } from 'react'; import { CloudUpload, Loader2, Check, AlertTriangle } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { useGitStatus } from '@/hooks/useGitStatus'; import { useGitSyncState } from '@/hooks/useGitSyncState'; import { useFSPaths } from '@/hooks/useFSPaths'; import { cn } from '@/lib/utils'; import { GitSyncSteps } from './GitSyncSteps'; import { useTranslation } from 'react-i18next'; interface GitSyncButtonProps { projectId: string; className?: string; } export function GitSyncButton({ projectId, className }: GitSyncButtonProps) { const [open, setOpen] = useState(false); const [syncSuccess, setSyncSuccess] = useState(false); const [showWarningPopover, setShowWarningPopover] = useState(false); const warningDismissed = useRef(false); const { t } = useTranslation(); const { data: gitStatus, isLoading: isGitStatusLoading } = useGitStatus(projectId); const { getError, getState } = useGitSyncState(); const { projectsPath } = useFSPaths(); const dir = `${projectsPath}/${projectId}`; const hasError = !!getError(dir); const gitSyncState = getState(dir); const isGitActionOccurring = gitSyncState?.isActive ?? false; const prevIsGitActionOccurring = useRef(isGitActionOccurring); const prevProjectId = useRef(projectId); // Reset state when projectId changes useEffect(() => { if (prevProjectId.current !== projectId) { setSyncSuccess(false); setShowWarningPopover(false); warningDismissed.current = false; prevIsGitActionOccurring.current = isGitActionOccurring; prevProjectId.current = projectId; } }, [projectId, isGitActionOccurring]); // Detect when a git action completes successfully useEffect(() => { // If we were syncing and now we're not, and there's no error, show success if (prevIsGitActionOccurring.current && !isGitActionOccurring && !hasError) { setSyncSuccess(true); const timer = setTimeout(() => setSyncSuccess(false), 2500); return () => clearTimeout(timer); } prevIsGitActionOccurring.current = isGitActionOccurring; }, [isGitActionOccurring, hasError]); // Determine if the user has altered the project (made commits beyond the initial state). // - Template projects (no clonedAtOid): altered when totalCommits > 1 (beyond the initial template commit) // - Cloned projects (has clonedAtOid): altered when the latest commit differs from the clone point const isProjectAltered = !isGitStatusLoading && gitStatus && (() => { if (gitStatus.clonedAtOid) { return gitStatus.latestCommit !== null && gitStatus.latestCommit.oid !== gitStatus.clonedAtOid; } return gitStatus.totalCommits > 1; })(); // Check if we need to show the warning popover const needsRemote = isProjectAltered && !gitStatus?.remotes.find(r => r.name === 'origin'); // Show warning popover when purple indicator is visible (only if not dismissed) useEffect(() => { if (needsRemote && !warningDismissed.current) { setShowWarningPopover(true); } else { setShowWarningPopover(false); } }, [needsRemote]); // Handle button click - dismiss warning popover permanently const handleButtonClick = () => { if (showWarningPopover) { warningDismissed.current = true; setShowWarningPopover(false); } setOpen(true); }; // Determine which indicator to show (only one at a time, in priority order) const renderIndicator = () => { // Checkmark: sync just completed successfully - highest priority if (syncSuccess) { return ; } // Spinner: git action occurring - second priority if (isGitActionOccurring) { return ; } // Red indicator: error state - second priority if (hasError) { return ; } if (isGitStatusLoading || !gitStatus) return null; const hasOrigin = !!gitStatus.remotes.find(r => r.name === 'origin'); const { remoteBranchExists, ahead, behind } = gitStatus; // Yellow indicator: unsynced changes (ahead or behind remote) - third priority const hasUnsyncedChanges = hasOrigin && remoteBranchExists && (ahead > 0 || behind > 0); if (hasUnsyncedChanges) { return ; } // Purple indicator: project altered but no origin remote configured - lowest priority const needsRemote = isProjectAltered && !hasOrigin; if (needsRemote) { return ; } return null; }; return ( <> {/* Warning popover - shown when purple indicator is visible */} {/* Main Git Sync popover */} {renderIndicator()} e.preventDefault()} > setOpen(false)} /> e.preventDefault()} onInteractOutside={(e) => e.preventDefault()} > Your project files are not safe Your project files are stored in your browser session and could be deleted. Sync with git to back up your project. Continue > ); } function IndicatorDot({ color }: { color: 'primary' | 'yellow' | 'red' }) { return ( ); } function IndicatorSpinner() { return ( ); } function IndicatorCheck() { return ( ); }
Your project files are stored in your browser session and could be deleted. Sync with git to back up your project.