mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Store the HEAD commit OID as 'shakespeare.clonedAt' git config at clone time, then use it to determine whether the user has actually altered the project before showing the backup warning.
210 lines
7.4 KiB
TypeScript
210 lines
7.4 KiB
TypeScript
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 <IndicatorCheck />;
|
|
}
|
|
|
|
// Spinner: git action occurring - second priority
|
|
if (isGitActionOccurring) {
|
|
return <IndicatorSpinner />;
|
|
}
|
|
|
|
// Red indicator: error state - second priority
|
|
if (hasError) {
|
|
return <IndicatorDot color="red" />;
|
|
}
|
|
|
|
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 <IndicatorDot color="yellow" />;
|
|
}
|
|
|
|
// Purple indicator: project altered but no origin remote configured - lowest priority
|
|
const needsRemote = isProjectAltered && !hasOrigin;
|
|
if (needsRemote) {
|
|
return <IndicatorDot color="primary" />;
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
return (
|
|
<>
|
|
{/* Warning popover - shown when purple indicator is visible */}
|
|
<Popover open={showWarningPopover} modal={false}>
|
|
<PopoverTrigger asChild>
|
|
<div className="relative">
|
|
{/* Main Git Sync popover */}
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className={cn("size-8 p-0 group relative", className)}
|
|
aria-label="Sync with Git"
|
|
onClick={handleButtonClick}
|
|
title={t('cloudSyncButtonTooltip')}
|
|
>
|
|
<CloudUpload className={cn("size-5 group-hover:text-foreground", open ? "text-foreground" : "text-muted-foreground")} />
|
|
{renderIndicator()}
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent
|
|
align="start"
|
|
className="w-96"
|
|
onOpenAutoFocus={(e) => e.preventDefault()}
|
|
>
|
|
<GitSyncSteps projectId={projectId} onClose={() => setOpen(false)} />
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
</PopoverTrigger>
|
|
<PopoverContent
|
|
side="bottom"
|
|
align="center"
|
|
className="w-80 pointer-events-none z-40"
|
|
onOpenAutoFocus={(e) => e.preventDefault()}
|
|
onInteractOutside={(e) => e.preventDefault()}
|
|
>
|
|
<div className="space-y-3 pointer-events-auto">
|
|
<div className="flex items-center gap-2">
|
|
<AlertTriangle className="size-5 text-primary shrink-0" />
|
|
<h4 className="font-semibold text-sm">Your project files are not safe</h4>
|
|
</div>
|
|
<p className="text-sm text-muted-foreground">
|
|
Your project files are stored in your browser session and could be deleted. Sync with git to back up your project.
|
|
</p>
|
|
<Button
|
|
onClick={handleButtonClick}
|
|
className="w-full"
|
|
size="sm"
|
|
>
|
|
Continue
|
|
</Button>
|
|
</div>
|
|
</PopoverContent>
|
|
</Popover>
|
|
</>
|
|
);
|
|
}
|
|
|
|
function IndicatorDot({ color }: { color: 'primary' | 'yellow' | 'red' }) {
|
|
return (
|
|
<span className={cn(
|
|
"absolute top-1 right-1 h-2 w-2 rounded-full",
|
|
{
|
|
'bg-primary': color === 'primary',
|
|
'bg-yellow-500': color === 'yellow',
|
|
'bg-red-500': color === 'red',
|
|
},
|
|
)} />
|
|
);
|
|
}
|
|
|
|
function IndicatorSpinner() {
|
|
return (
|
|
<Loader2 className="absolute top-1 right-1 size-2.5 animate-spin text-blue-600 dark:text-blue-400" />
|
|
);
|
|
}
|
|
|
|
function IndicatorCheck() {
|
|
return (
|
|
<Check className="absolute top-1 right-1 size-2.5 text-green-600 dark:text-green-400" />
|
|
);
|
|
} |