Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/web/client/public/onlook-preload-script.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
'use client';

import { useState } from 'react';

import type { GitMessageCheckpoint } from '@onlook/models';
import { Button } from '@onlook/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@onlook/ui/dialog';
import { Icons } from '@onlook/ui/icons';
import { toast } from '@onlook/ui/sonner';
import { cn } from '@onlook/ui/utils';

import { useEditorEngine } from '@/components/store/editor';
import { restoreCheckpoint } from '@/components/store/editor/git';

interface MultiBranchRevertModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
checkpoints: GitMessageCheckpoint[];
}

export const MultiBranchRevertModal = ({
open,
onOpenChange,
checkpoints,
}: MultiBranchRevertModalProps) => {
const editorEngine = useEditorEngine();
const [selectedBranchIds, setSelectedBranchIds] = useState<string[]>([]);
const [isRestoring, setIsRestoring] = useState(false);

const allAreSelected = selectedBranchIds.length === checkpoints.length;

const toggleBranch = (branchId: string) => {
setSelectedBranchIds((prev) =>
prev.includes(branchId) ? prev.filter((id) => id !== branchId) : [...prev, branchId],
);
};

const selectAll = () => {
setSelectedBranchIds(checkpoints.map((cp) => cp.branchId).filter((id): id is string => !!id));
};

const selectNone = () => {
setSelectedBranchIds([]);
};

const handleRevert = async () => {
try {
if (selectedBranchIds.length === 0) {
toast.error('Please select at least one branch to revert');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent terminology: The error message uses 'revert' while other parts of the UI (and function names) use 'restore'. Please consider using a single term for clarity.

Suggested change
toast.error('Please select at least one branch to revert');
toast.error('Please select at least one branch to restore');

return;
}

setIsRestoring(true);

const restorePromises = selectedBranchIds.map(async (branchId) => {
const checkpoint = checkpoints.find((cp) => cp.branchId === branchId);
if (!checkpoint) {
return { success: false };
}
return restoreCheckpoint(checkpoint, editorEngine);
});

const results = await Promise.all(restorePromises);
const successCount = results.filter((r) => r.success).length;
const failCount = results.length - successCount;

if (successCount > 0) {
toast.success(
`Successfully restored ${successCount} branch${successCount > 1 ? 'es' : ''}`,
{
description:
failCount > 0
? `${failCount} branch${failCount > 1 ? 'es' : ''} failed to restore`
: undefined,
},
);
} else if (failCount > 0) {
toast.error('Failed to restore all selected branches');
}
} catch (error) {
toast.error('Failed to restore branches', {
description: error instanceof Error ? error.message : 'Unknown error',
});
} finally {

setIsRestoring(false);
onOpenChange(false);
setSelectedBranchIds([]);
}
};

return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-md">
<DialogHeader>
<DialogTitle>Restore Multiple Branches</DialogTitle>
<DialogDescription className="pt-2">
Select the branches you want to restore to their previous state.
</DialogDescription>
</DialogHeader>
<div className="flex flex-col gap-2 py-4">
<div className="mb-1 flex justify-end gap-1">
{allAreSelected ? (
<Button
variant="outline"
size="sm"
onClick={selectNone}
disabled={isRestoring}
>
Select None
</Button>
) : (
<Button
variant="outline"
size="sm"
onClick={selectAll}
disabled={isRestoring}
>
Select All
</Button>
)}
</div>
<div className="flex flex-col gap-2">
{checkpoints.map((checkpoint) => {
// Skip legacy checkpoints without branchId (shouldn't happen in multi-branch modal)
if (!checkpoint.branchId) return null;
const isSelected = selectedBranchIds.includes(checkpoint.branchId);
return (
<button
key={checkpoint.branchId}
onClick={() => toggleBranch(checkpoint.branchId!)}
disabled={isRestoring}
className={cn(
'flex items-center justify-between rounded-md border px-3 py-2.5 text-left transition-all',
'hover:bg-background-secondary/50',
isSelected
? 'border-primary/50 bg-primary/5'
: 'border-border/50',
isRestoring && 'cursor-not-allowed opacity-50',
)}
>
<span className="text-sm">
{editorEngine.branches.getBranchById(checkpoint.branchId)
?.name ?? checkpoint.branchId}
</span>
{isSelected && <Icons.Check className="text-primary h-4 w-4" />}
</button>
);
})}
</div>
</div>
<DialogFooter className="flex-col gap-3 sm:flex-row sm:gap-2">
<Button
variant="outline"
onClick={() => {
onOpenChange(false);
setSelectedBranchIds([]);
}}
Comment on lines +163 to +165
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isRestoring state should be reset when the modal is closed via the Cancel button. Consider adding setIsRestoring(false) to ensure the state is properly cleaned up, preventing potential issues if the modal is reopened later.

onClick={() => {
  onOpenChange(false);
  setSelectedBranchIds([]);
  setIsRestoring(false);
}}
Suggested change
onOpenChange(false);
setSelectedBranchIds([]);
}}
onOpenChange(false);
setSelectedBranchIds([]);
setIsRestoring(false);
}}

Spotted by Diamond

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

disabled={isRestoring}
className="order-2 sm:order-1"
>
Cancel
</Button>
<Button
variant="outline"
onClick={handleRevert}
disabled={isRestoring || selectedBranchIds.length === 0}
className="order-1 sm:order-2"
>
{isRestoring ? 'Restoring...' : 'Restore Selected'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};
Loading