mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 10:51:13 -05:00
Fix rollback system
This commit is contained in:
@@ -10,6 +10,7 @@ import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { VersionComparisonDialog } from './VersionComparisonDialog';
|
||||
import { RollbackDialog } from './RollbackDialog';
|
||||
import { useEntityVersions } from '@/hooks/useEntityVersions';
|
||||
import { useUserRole } from '@/hooks/useUserRole';
|
||||
import type { EntityType } from '@/types/versioning';
|
||||
|
||||
interface EntityVersionHistoryProps {
|
||||
@@ -28,6 +29,7 @@ const changeTypeColors = {
|
||||
|
||||
export function EntityVersionHistory({ entityType, entityId, entityName }: EntityVersionHistoryProps) {
|
||||
const { versions, loading, rollbackToVersion } = useEntityVersions(entityType, entityId);
|
||||
const { isModerator } = useUserRole();
|
||||
const [selectedVersions, setSelectedVersions] = useState<string[]>([]);
|
||||
const [compareDialogOpen, setCompareDialogOpen] = useState(false);
|
||||
const [rollbackDialogOpen, setRollbackDialogOpen] = useState(false);
|
||||
@@ -128,7 +130,7 @@ export function EntityVersionHistory({ entityType, entityId, entityName }: Entit
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!version.is_current && (
|
||||
{!version.is_current && isModerator() && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
@@ -136,6 +138,7 @@ export function EntityVersionHistory({ entityType, entityId, entityName }: Entit
|
||||
e.stopPropagation();
|
||||
handleRollback(version.version_id);
|
||||
}}
|
||||
title="Moderator only: Restore this version"
|
||||
>
|
||||
<RotateCcw className="h-4 w-4 mr-1" />
|
||||
Restore
|
||||
|
||||
@@ -43,9 +43,9 @@ export function RollbackDialog({
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Restore Previous Version</DialogTitle>
|
||||
<DialogTitle>Restore Previous Version (Moderator Action)</DialogTitle>
|
||||
<DialogDescription>
|
||||
You are about to restore "{entityName}" to a previous version. This will create a new version with the restored data.
|
||||
You are about to restore "{entityName}" to a previous version. This will create a new version with the restored data. This action will be logged in the audit trail.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
|
||||
@@ -164,10 +164,16 @@ export function useEntityVersions(entityType: EntityType, entityId: string) {
|
||||
p_reason: reason
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (error) {
|
||||
// Check for authorization error (insufficient_privilege)
|
||||
if (error.code === '42501') {
|
||||
throw new Error('Only moderators can restore previous versions');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (isMountedRef.current) {
|
||||
toast.success('Successfully rolled back to previous version');
|
||||
toast.success('Successfully restored to previous version');
|
||||
await fetchVersions();
|
||||
}
|
||||
return data;
|
||||
@@ -175,7 +181,9 @@ export function useEntityVersions(entityType: EntityType, entityId: string) {
|
||||
const errorMsg = getErrorMessage(error);
|
||||
logger.error('Failed to rollback version', { entityType, entityId, targetVersionId, error: errorMsg });
|
||||
if (isMountedRef.current) {
|
||||
toast.error(errorMsg);
|
||||
toast.error('Failed to restore version', {
|
||||
description: errorMsg
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user