2024-12-03 17:49:38 +03:00
|
|
|
import { Button } from './ui/button';
|
|
|
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip';
|
2025-01-27 14:19:47 +05:30
|
|
|
import { blockDefinitions, UIBlock } from './block';
|
2025-01-08 00:00:21 +05:30
|
|
|
import { Dispatch, memo, SetStateAction } from 'react';
|
2025-01-27 14:19:47 +05:30
|
|
|
import { BlockActionContext } from './create-block';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
2024-12-03 17:49:38 +03:00
|
|
|
|
|
|
|
|
interface BlockActionsProps {
|
|
|
|
|
block: UIBlock;
|
|
|
|
|
handleVersionChange: (type: 'next' | 'prev' | 'toggle' | 'latest') => void;
|
|
|
|
|
currentVersionIndex: number;
|
|
|
|
|
isCurrentVersion: boolean;
|
2025-01-27 14:19:47 +05:30
|
|
|
mode: 'edit' | 'diff';
|
|
|
|
|
metadata: any;
|
|
|
|
|
setMetadata: Dispatch<SetStateAction<any>>;
|
2024-12-16 18:14:40 +05:30
|
|
|
}
|
|
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
function PureBlockActions({
|
|
|
|
|
block,
|
|
|
|
|
handleVersionChange,
|
|
|
|
|
currentVersionIndex,
|
|
|
|
|
isCurrentVersion,
|
|
|
|
|
mode,
|
2025-01-27 14:19:47 +05:30
|
|
|
metadata,
|
|
|
|
|
setMetadata,
|
2024-12-03 17:49:38 +03:00
|
|
|
}: BlockActionsProps) {
|
2025-01-27 14:19:47 +05:30
|
|
|
const blockDefinition = blockDefinitions.find(
|
|
|
|
|
(definition) => definition.kind === block.kind,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!blockDefinition) {
|
|
|
|
|
throw new Error('Block definition not found!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const actionContext: BlockActionContext = {
|
|
|
|
|
content: block.content,
|
|
|
|
|
handleVersionChange,
|
|
|
|
|
currentVersionIndex,
|
|
|
|
|
isCurrentVersion,
|
|
|
|
|
mode,
|
|
|
|
|
metadata,
|
|
|
|
|
setMetadata,
|
|
|
|
|
};
|
2024-12-03 17:49:38 +03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-row gap-1">
|
2025-01-27 14:19:47 +05:30
|
|
|
{blockDefinition.actions.map((action) => (
|
|
|
|
|
<Tooltip key={action.description}>
|
2024-12-16 18:14:40 +05:30
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
2025-01-27 14:19:47 +05:30
|
|
|
className={cn('h-fit dark:hover:bg-zinc-700', {
|
|
|
|
|
'p-2': !action.label,
|
|
|
|
|
'py-1.5 px-2': action.label,
|
|
|
|
|
})}
|
|
|
|
|
onClick={() => action.onClick(actionContext)}
|
2024-12-16 18:14:40 +05:30
|
|
|
disabled={
|
2025-01-27 14:19:47 +05:30
|
|
|
action.isDisabled ? action.isDisabled(actionContext) : false
|
2024-12-16 18:14:40 +05:30
|
|
|
}
|
|
|
|
|
>
|
2025-01-27 14:19:47 +05:30
|
|
|
{action.icon}
|
|
|
|
|
{action.label}
|
2024-12-16 18:14:40 +05:30
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
2025-01-27 14:19:47 +05:30
|
|
|
<TooltipContent>{action.description}</TooltipContent>
|
2024-12-16 18:14:40 +05:30
|
|
|
</Tooltip>
|
2025-01-27 14:19:47 +05:30
|
|
|
))}
|
2024-12-03 17:49:38 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const BlockActions = memo(PureBlockActions, (prevProps, nextProps) => {
|
2024-12-10 17:54:10 +05:30
|
|
|
if (prevProps.block.status !== nextProps.block.status) return false;
|
|
|
|
|
if (prevProps.currentVersionIndex !== nextProps.currentVersionIndex)
|
|
|
|
|
return false;
|
|
|
|
|
if (prevProps.isCurrentVersion !== nextProps.isCurrentVersion) return false;
|
2024-12-03 17:49:38 +03:00
|
|
|
|
2024-12-10 17:54:10 +05:30
|
|
|
return true;
|
2024-12-03 17:49:38 +03:00
|
|
|
});
|