Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(console): added drag reorder of prompt messages in the prompt ed… #276

Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 48 additions & 14 deletions apps/console/src/components/prompts/editor/chat/ChatEditMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { useCurrentPrompt } from "~/lib/providers/CurrentPromptContext";
import { useEditorContext } from "~/lib/providers/EditorContext";
import { Button } from "@pezzo/ui";
import { PlusIcon } from "lucide-react";
import {
DragDropContext,
Droppable,
Draggable,
DropResult,
} from "react-beautiful-dnd";

export const ChatEditMode = () => {
const { promptId } = useCurrentPrompt();
const { messagesArray } = useEditorContext();
const { fields, append, remove } = messagesArray;
const { fields, append, remove, move } = messagesArray;

const handleAdd = () => {
append({
Expand All @@ -21,21 +27,49 @@ export const ChatEditMode = () => {
});
};

const onDragEnd = (result: DropResult) => {
if (!result.destination) {
return;
}

move(result.source.index, result.destination.index);
};

return (
<div className="flex flex-col gap-4">
{fields.map((field, index) => (
<ChatMessage
key={field.id}
index={index}
canDelete={fields.length !== 1}
onDelete={() => {
remove(index);
trackEvent("prompt_chat_completion_message_deleted", {
promptId,
});
}}
/>
))}
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="prompts">
{(provided) => (
<div ref={provided.innerRef} {...provided.droppableProps}>
{fields.map((field, index) => (
<Draggable key={field.id} draggableId={field.id} index={index}>
{(provided) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
className="mb-4"
>
<ChatMessage
key={field.id}
index={index}
canDelete={fields.length !== 1}
onDelete={() => {
remove(index);
trackEvent("prompt_chat_completion_message_deleted", {
promptId,
});
}}
/>
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>

<div className="flex items-center justify-center">
<Button className="border-dashed" variant="outline" onClick={handleAdd}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
ArrowDownUpIcon,
BotIcon,
GripVertical,
LucideIcon,
TrashIcon,
UserIcon,
Expand Down Expand Up @@ -73,7 +74,8 @@ export const ChatMessage = ({ index, canDelete = true, onDelete }: Props) => {

return (
<Card className="flex flex-col gap-4 border">
<div className="group flex h-14 items-center justify-between border-b border-muted px-4 py-2 font-medium">
<div className="group flex h-14 items-center justify-start border-b border-muted px-4 py-2 font-medium">
<GripVertical className="-mt-[3px] mr-[2px] h-4 w-4 text-muted-foreground" />
<DropdownMenu>
<DropdownMenuTrigger>
<div className="inline-flex items-center gap-2">
Expand All @@ -97,7 +99,7 @@ export const ChatMessage = ({ index, canDelete = true, onDelete }: Props) => {
{canDelete && (
<TrashIcon
onClick={onDelete}
className="hidden h-4 w-4 cursor-pointer text-destructive group-hover:inline-flex"
className="ml-auto hidden h-4 w-4 cursor-pointer text-destructive group-hover:inline-flex"
/>
)}
</div>
Expand Down
Loading
Loading