From eea9d8191e24a36008f519e56e6b53ad5f0fbc74 Mon Sep 17 00:00:00 2001 From: Kyle Corbitt Date: Mon, 26 Jun 2023 10:17:24 -0700 Subject: [PATCH] prompt reordering works --- .../OutputsTable/NewVariantButton.tsx | 35 ++++--- .../OutputsTable/VariantConfigEditor.tsx | 5 +- src/components/OutputsTable/VariantHeader.tsx | 92 +++++++++++++------ src/components/OutputsTable/index.tsx | 17 ++-- .../api/routers/promptVariants.router.ts | 69 ++++++++++++++ 5 files changed, 158 insertions(+), 60 deletions(-) diff --git a/src/components/OutputsTable/NewVariantButton.tsx b/src/components/OutputsTable/NewVariantButton.tsx index 8089c86..02cf232 100644 --- a/src/components/OutputsTable/NewVariantButton.tsx +++ b/src/components/OutputsTable/NewVariantButton.tsx @@ -18,24 +18,21 @@ export default function NewVariantButton() { }, [mutation]); return ( - - - + ); } diff --git a/src/components/OutputsTable/VariantConfigEditor.tsx b/src/components/OutputsTable/VariantConfigEditor.tsx index 4dcd0a0..2fc19ad 100644 --- a/src/components/OutputsTable/VariantConfigEditor.tsx +++ b/src/components/OutputsTable/VariantConfigEditor.tsx @@ -94,11 +94,8 @@ export default function VariantConfigEditor(props: { variant: PromptVariant }) { wordWrap: "on", folding: false, scrollbar: { - vertical: "hidden", alwaysConsumeMouseWheel: false, verticalScrollbarSize: 0, - - // Don't let you scroll to an empty line }, wordWrapBreakAfterCharacters: "", wordWrapBreakBeforeCharacters: "", @@ -145,7 +142,7 @@ export default function VariantConfigEditor(props: { variant: PromptVariant }) {
{isChanged && ( - + diff --git a/src/components/OutputsTable/index.tsx b/src/components/OutputsTable/index.tsx index 33724b3..66d56c1 100644 --- a/src/components/OutputsTable/index.tsx +++ b/src/components/OutputsTable/index.tsx @@ -1,13 +1,14 @@ -import { RouterOutputs, api } from "~/utils/api"; -import { Scenario, type PromptVariant } from "./types"; -import OutputCell from "./OutputCell"; -import ScenarioEditor from "./ScenarioEditor"; +import { Box, Grid, GridItem, Heading, type SystemStyleObject } from "@chakra-ui/react"; import React, { useState } from "react"; -import { Box, Grid, GridItem, Heading, SystemStyleObject } from "@chakra-ui/react"; +import { api } from "~/utils/api"; import NewScenarioButton from "./NewScenarioButton"; import NewVariantButton from "./NewVariantButton"; -import VariantHeader from "./VariantHeader"; +import OutputCell from "./OutputCell"; +import ScenarioEditor from "./ScenarioEditor"; import VariantConfigEditor from "./VariantConfigEditor"; +import VariantHeader from "./VariantHeader"; +import type { Scenario, PromptVariant } from "./types"; +import { cellPadding } from "../constants"; const stickyHeaderStyle: SystemStyleObject = { position: "sticky", @@ -76,8 +77,8 @@ export default function OutputsTable({ experimentId }: { experimentId: string | }} > - - + + Scenario diff --git a/src/server/api/routers/promptVariants.router.ts b/src/server/api/routers/promptVariants.router.ts index b1f981a..bd32d66 100644 --- a/src/server/api/routers/promptVariants.router.ts +++ b/src/server/api/routers/promptVariants.router.ts @@ -143,4 +143,73 @@ export const promptVariantsRouter = createTRPCRouter({ return newVariant; }), + + reorder: publicProcedure + .input( + z.object({ + draggedId: z.string(), + droppedId: z.string(), + }) + ) + .mutation(async ({ input }) => { + const dragged = await prisma.promptVariant.findUnique({ + where: { + id: input.draggedId, + }, + }); + + const dropped = await prisma.promptVariant.findUnique({ + where: { + id: input.droppedId, + }, + }); + + if (!dragged || !dropped || dragged.experimentId !== dropped.experimentId) { + throw new Error( + `Prompt Variant with id ${input.draggedId} or ${input.droppedId} does not exist` + ); + } + + const visibleItems = await prisma.promptVariant.findMany({ + where: { + experimentId: dragged.experimentId, + visible: true, + }, + orderBy: { + sortIndex: "asc", + }, + }); + + // Remove the dragged item from its current position + const orderedItems = visibleItems.filter((item) => item.id !== dragged.id); + + // Find the index of the dragged item and the dropped item + const dragIndex = visibleItems.findIndex((item) => item.id === dragged.id); + const dropIndex = orderedItems.findIndex((item) => item.id === dropped.id); + + // Determine the new index for the dragged item + let newIndex; + if (dragIndex < dropIndex) { + newIndex = dropIndex + 1; // Insert after the dropped item + } else { + newIndex = dropIndex; // Insert before the dropped item + } + + // Insert the dragged item at the new position + orderedItems.splice(newIndex, 0, dragged); + + // Now, we need to update all the items with their new sortIndex + await prisma.$transaction( + orderedItems.map((item, index) => { + return prisma.promptVariant.update({ + where: { + id: item.id, + }, + data: { + sortIndex: index, + }, + }); + }) + ); + }), });