Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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,176 @@
'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 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 () => {
if (selectedBranchIds.length === 0) {
toast.error('Please select at least one branch to revert');
return;
}

setIsRestoring(true);
let successCount = 0;
let failCount = 0;

for (const branchId of selectedBranchIds) {
const checkpoint = checkpoints.find((cp) => cp.branchId === branchId);
if (!checkpoint) {
failCount++;
continue;
}

const result = await restoreCheckpoint(checkpoint, editorEngine);
if (result.success) {
successCount++;
} else {
failCount++;
}
}

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');
}

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">
<Button
variant="outline"
size="sm"
onClick={selectAll}
disabled={isRestoring}
>
Select All
</Button>
<Button
variant="outline"
size="sm"
onClick={selectNone}
disabled={isRestoring}
>
Select None
</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...' : 'Revert Selected'}
Copy link
Contributor

Choose a reason for hiding this comment

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

UI text inconsistency: Update the button label from 'Revert Selected' to 'Restore Selected' to match the modal title.

Suggested change
{isRestoring ? 'Restoring...' : 'Revert Selected'}
{isRestoring ? 'Restoring...' : 'Restore Selected'}

</Button>
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Localize modal and toast copy.

Every string shown to users here (“Restore Multiple Branches”, “Please select at least one branch…”, button labels, success/failure toasts, etc.) is hardcoded. For files under apps/web/client/src/**, we must serve user text via next-intl message keys. Inject the translator and replace these literals with localized strings.
Based on learnings

</DialogFooter>
</DialogContent>
</Dialog>
);
};
Loading