From 1454aa719205657338a2a444353948ef97a76bb0 Mon Sep 17 00:00:00 2001 From: Harish Deivanayagam <35465026+HarishDeivanayagam@users.noreply.github.com> Date: Sun, 12 Jan 2025 17:18:34 +0530 Subject: [PATCH] refactor ambigous names --- .../migration.sql | 2 + .../migration.sql | 51 ++++++++++++++ prisma/schema.prisma | 62 +++++++++-------- src/app/console/searchDialog.tsx | 3 +- src/app/console/sheets/[slug]/actions.ts | 68 +++++++++---------- src/app/console/sheets/[slug]/page.tsx | 37 +++++----- src/app/console/sheets/[slug]/sourceIndex.tsx | 22 +++--- src/app/globals.css | 10 +-- src/lib/utils.ts | 8 +++ 9 files changed, 169 insertions(+), 94 deletions(-) create mode 100644 prisma/migrations/20250112110051_add_col_index_src/migration.sql create mode 100644 prisma/migrations/20250112112049_rename_cols_sheet/migration.sql diff --git a/prisma/migrations/20250112110051_add_col_index_src/migration.sql b/prisma/migrations/20250112110051_add_col_index_src/migration.sql new file mode 100644 index 0000000..c25b702 --- /dev/null +++ b/prisma/migrations/20250112110051_add_col_index_src/migration.sql @@ -0,0 +1,2 @@ +-- AddForeignKey +ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_sourceIndexId_fkey" FOREIGN KEY ("sourceIndexId") REFERENCES "IndexedSource"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/migrations/20250112112049_rename_cols_sheet/migration.sql b/prisma/migrations/20250112112049_rename_cols_sheet/migration.sql new file mode 100644 index 0000000..a1aa53a --- /dev/null +++ b/prisma/migrations/20250112112049_rename_cols_sheet/migration.sql @@ -0,0 +1,51 @@ +/* + Warnings: + + - The primary key for the `SheetColumnValue` table will be changed. If it partially fails, the table could be left without primary key constraint. + - You are about to drop the column `columnId` on the `SheetColumnValue` table. All the data in the column will be lost. + - You are about to drop the column `sourceId` on the `SheetColumnValue` table. All the data in the column will be lost. + - You are about to drop the column `sourceIndexId` on the `SheetColumnValue` table. All the data in the column will be lost. + - Added the required column `updatedAt` to the `SheetColumn` table without a default value. This is not possible if the table is not empty. + - Added the required column `sheetColumnId` to the `SheetColumnValue` table without a default value. This is not possible if the table is not empty. + - Added the required column `sheetSourceId` to the `SheetColumnValue` table without a default value. This is not possible if the table is not empty. + - Added the required column `updatedAt` to the `SheetColumnValue` table without a default value. This is not possible if the table is not empty. + - Added the required column `updatedAt` to the `SheetSource` table without a default value. This is not possible if the table is not empty. + +*/ +-- DropForeignKey +ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_columnId_fkey"; + +-- DropForeignKey +ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_sourceId_fkey"; + +-- DropForeignKey +ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_sourceIndexId_fkey"; + +-- AlterTable +ALTER TABLE "SheetColumn" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, +ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; + +-- AlterTable +ALTER TABLE "SheetColumnValue" DROP CONSTRAINT "SheetColumnValue_pkey", +DROP COLUMN "columnId", +DROP COLUMN "sourceId", +DROP COLUMN "sourceIndexId", +ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, +ADD COLUMN "indexedSourceId" TEXT, +ADD COLUMN "sheetColumnId" TEXT NOT NULL, +ADD COLUMN "sheetSourceId" TEXT NOT NULL, +ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL, +ADD CONSTRAINT "SheetColumnValue_pkey" PRIMARY KEY ("sheetColumnId", "sheetSourceId"); + +-- AlterTable +ALTER TABLE "SheetSource" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, +ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; + +-- AddForeignKey +ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_sheetColumnId_fkey" FOREIGN KEY ("sheetColumnId") REFERENCES "SheetColumn"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_sheetSourceId_fkey" FOREIGN KEY ("sheetSourceId") REFERENCES "SheetSource"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "SheetColumnValue" ADD CONSTRAINT "SheetColumnValue_indexedSourceId_fkey" FOREIGN KEY ("indexedSourceId") REFERENCES "IndexedSource"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 03868c4..86887be 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -68,17 +68,17 @@ model Member { } model Sheet { - id String @id @default(uuid()) - name String - createdById String - createdBy User @relation(fields: [createdById], references: [id]) - organizationId String - organization Organization @relation(fields: [organizationId], references: [id]) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - sources SheetSource[] - columns SheetColumn[] - columnValues SheetColumnValue[] + id String @id @default(uuid()) + name String + createdById String + createdBy User @relation(fields: [createdById], references: [id]) + organizationId String + organization Organization @relation(fields: [organizationId], references: [id]) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + sheetSources SheetSource[] + sheetColumns SheetColumn[] + sheetColumnValues SheetColumnValue[] } model Source { @@ -107,6 +107,8 @@ model SheetSource { organizationId String organization Organization @relation(fields: [organizationId], references: [id]) rowValues SheetColumnValue[] + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt } model SheetColumn { @@ -121,32 +123,38 @@ model SheetColumn { dataType ColumnDataType defaultValue String @default("N/A") columnValues SheetColumnValue[] + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt } model SheetColumnValue { - sheetId String - sheet Sheet @relation(fields: [sheetId], references: [id]) - columnId String - column SheetColumn @relation(fields: [columnId], references: [id]) - organizationId String - organization Organization @relation(fields: [organizationId], references: [id]) - value String? - sourceIndexId String? - sourceId String - source SheetSource @relation(fields: [sourceId], references: [id]) + sheetId String + sheet Sheet @relation(fields: [sheetId], references: [id]) + sheetColumnId String + sheetColumn SheetColumn @relation(fields: [sheetColumnId], references: [id]) + organizationId String + organization Organization @relation(fields: [organizationId], references: [id]) + value String? + sheetSourceId String + sheetSource SheetSource @relation(fields: [sheetSourceId], references: [id]) + indexedSourceId String? + indexedSource IndexedSource? @relation(fields: [indexedSourceId], references: [id]) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt - @@id([columnId, sourceId]) + @@id([sheetColumnId, sheetSourceId]) } model IndexedSource { - id String @id @default(uuid()) + id String @id @default(uuid()) indexId String referenceImageFileName String? referenceText String? sourceId String - source Source @relation(fields: [sourceId], references: [id]) + source Source @relation(fields: [sourceId], references: [id]) organizationId String - organization Organization @relation(fields: [organizationId], references: [id]) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + organization Organization @relation(fields: [organizationId], references: [id]) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + sheetColumnValues SheetColumnValue[] } diff --git a/src/app/console/searchDialog.tsx b/src/app/console/searchDialog.tsx index 12a9de7..9447ca7 100644 --- a/src/app/console/searchDialog.tsx +++ b/src/app/console/searchDialog.tsx @@ -7,6 +7,7 @@ import { sendMessageToSearch } from "./actions"; import ReactMarkdown from "react-markdown" import { produce } from "immer" import Image from "next/image"; +import { stripCodeBlockBackTicks } from "@/lib/utils"; export default function SearchDialog({ open }: { open: boolean }) { @@ -131,7 +132,7 @@ export default function SearchDialog({ open }: { open: boolean }) { - {source.referenceText} + {stripCodeBlockBackTicks(source.referenceText || "No reference text")} diff --git a/src/app/console/sheets/[slug]/actions.ts b/src/app/console/sheets/[slug]/actions.ts index 187201f..08742b4 100644 --- a/src/app/console/sheets/[slug]/actions.ts +++ b/src/app/console/sheets/[slug]/actions.ts @@ -49,7 +49,7 @@ export const fetchSheet = async (id: string) => { let columnValues: any = {} for (const value of values) { - columnValues[`${value.sourceId}_${value.columnId}`] = value + columnValues[`${value.sheetSourceId}_${value.sheetColumnId}`] = value } return { @@ -87,7 +87,7 @@ export const updateSheetName = async (id: string, name: string) => { return } -export const updateSourceToSheet = async (sourceIds: string[], sheetId: string) => { +export const updateSourceToSheet = async (selectedSourceIds: string[], sheetId: string) => { const { organizationId, userId } = await getAuthToken() const sheet = await prisma.sheet.findFirstOrThrow({ @@ -109,10 +109,10 @@ export const updateSourceToSheet = async (sourceIds: string[], sheetId: string) const existingSheetSourceIds = sheetSources.map(source => source.sourceId) // Find source ids that are present in sourceIds but not in sheetSourceIds - const newSourceIds = sourceIds.filter(sourceId => !existingSheetSourceIds.includes(sourceId)) + const newSourceIds = selectedSourceIds.filter(sourceId => !existingSheetSourceIds.includes(sourceId)) // Find source ids that are present in sheetSourceIds but not in sourceIds - const oldSourceIds = existingSheetSourceIds.filter(sourceId => !sourceIds.includes(sourceId)) + const oldSourceIds = existingSheetSourceIds.filter(sourceId => !selectedSourceIds.includes(sourceId)) for (const sourceId of newSourceIds) { @@ -124,7 +124,7 @@ export const updateSourceToSheet = async (sourceIds: string[], sheetId: string) } }) - await tx.sheetSource.create({ + const sheetSource = await tx.sheetSource.create({ data: { sourceId: source.id, sheetId: sheet.id, @@ -132,18 +132,18 @@ export const updateSourceToSheet = async (sourceIds: string[], sheetId: string) } }) - const columns = await tx.sheetColumn.findMany({ + const sheetColumns = await tx.sheetColumn.findMany({ where: { sheetId: sheet.id, organizationId } }) - for (const column of columns) { + for (const sheetColumn of sheetColumns) { await tx.sheetColumnValue.create({ data: { - sourceId: source.id, - columnId: column.id, + sheetSourceId: sheetSource.id, + sheetColumnId: sheetColumn.id, value: "", sheetId: sheet.id, organizationId @@ -157,30 +157,29 @@ export const updateSourceToSheet = async (sourceIds: string[], sheetId: string) await prisma.$transaction(async (tx) => { - const source = await tx.source.findFirstOrThrow({ + const sheetSource = await tx.sheetSource.findFirstOrThrow({ where: { - id: sourceId, + sourceId: sourceId, + sheetId: sheet.id, organizationId } }) - - await tx.sheetSource.deleteMany({ + await tx.sheetColumnValue.deleteMany({ where: { - sourceId: source.id, + sheetSourceId: sheetSource.id, sheetId: sheet.id, organizationId } }) - await tx.sheetColumnValue.deleteMany({ + await tx.sheetSource.deleteMany({ where: { - sourceId: source.id, + sourceId: sourceId, sheetId: sheet.id, organizationId } }) - }) } @@ -209,7 +208,7 @@ export async function addColumnToSheet( await prisma.$transaction(async (tx) => { - const column = await tx.sheetColumn.create({ + const sheetColumn = await tx.sheetColumn.create({ data: { sheetId: sheet.id, organizationId, @@ -230,8 +229,8 @@ export async function addColumnToSheet( for (const sheetSource of sheetSources) { await tx.sheetColumnValue.create({ data: { - sourceId: sheetSource.id, - columnId: column.id, + sheetSourceId: sheetSource.id, + sheetColumnId: sheetColumn.id, sheetId: sheet.id, organizationId } @@ -281,7 +280,7 @@ export async function updateColumnToSheet( } -export async function deleteColumnFromSheet(sheetId: string, columnId: string) { +export async function deleteColumnFromSheet(sheetId: string, sheetColumnId: string) { const { organizationId, userId } = await getAuthToken() const sheet = await prisma.sheet.findFirstOrThrow({ where: { @@ -291,9 +290,9 @@ export async function deleteColumnFromSheet(sheetId: string, columnId: string) { } }) - const column = await prisma.sheetColumn.findFirstOrThrow({ + const sheetColumn = await prisma.sheetColumn.findFirstOrThrow({ where: { - id: columnId, + id: sheetColumnId, sheetId: sheetId, organizationId } @@ -301,17 +300,17 @@ export async function deleteColumnFromSheet(sheetId: string, columnId: string) { await prisma.$transaction(async (tx) => { - await prisma.sheetColumn.delete({ + await tx.sheetColumnValue.deleteMany({ where: { - id: column.id, + sheetColumnId: sheetColumn.id, sheetId: sheet.id, organizationId } }) - await tx.sheetColumnValue.deleteMany({ + await prisma.sheetColumn.delete({ where: { - columnId: column.id, + id: sheetColumn.id, sheetId: sheet.id, organizationId } @@ -319,7 +318,6 @@ export async function deleteColumnFromSheet(sheetId: string, columnId: string) { }) - return } @@ -403,7 +401,7 @@ export async function runColumnSourceTask(sheetId: string, sheetColumnId: string let data = "" let refrenceImage = "" - let sourceIndexId = "" + let indexedSourceId = "" if (sourceIndexes.length > 1) { const foundIndex = await queryVectorDB(column.instruction, organizationId, sheetSource.sourceId) @@ -411,12 +409,12 @@ export async function runColumnSourceTask(sheetId: string, sheetColumnId: string if (sourceIndex) { data = sourceIndex.referenceText || "" refrenceImage = sourceIndex.referenceImageFileName || "" - sourceIndexId = sourceIndex.indexId || "" + indexedSourceId = sourceIndex.id || "" } } else { data = sourceIndexes[0].referenceText || "" refrenceImage = sourceIndexes[0].referenceImageFileName || "" - sourceIndexId = sourceIndexes[0].indexId || "" + indexedSourceId = sourceIndexes[0].id || "" } let query = ` @@ -438,20 +436,20 @@ export async function runColumnSourceTask(sheetId: string, sheetColumnId: string await prisma.sheetColumnValue.updateMany({ where: { - columnId: sheetColumnId, - sourceId: sheetSourceId, + sheetColumnId: sheetColumnId, + sheetSourceId: sheetSourceId, sheetId: sheet.id, organizationId }, data: { value: answer, - sourceIndexId: sourceIndexId + indexedSourceId: indexedSourceId } }) return { answer, - sourceIndexId + indexedSourceId: indexedSourceId } } diff --git a/src/app/console/sheets/[slug]/page.tsx b/src/app/console/sheets/[slug]/page.tsx index c4888b6..def44c1 100644 --- a/src/app/console/sheets/[slug]/page.tsx +++ b/src/app/console/sheets/[slug]/page.tsx @@ -19,12 +19,13 @@ import { produce } from "immer" import { Alert } from "@/components/ui/alert" import { Separator } from "@/components/ui/separator" import SourceIndexComponent from "./sourceIndex" +import { ScrollArea } from "@/components/ui/scroll-area" export default function SheetPage() { const { slug } = useParams() const [sheet, setSheet] = useState(null) - const [sources, setSources] = useState<(SheetSource & { source: Source })[]>([]) - const [columns, setColumns] = useState([]) + const [sheetSources, setSheetSources] = useState<(SheetSource & { source: Source })[]>([]) + const [sheetColumns, setSheetColumns] = useState([]) const [columnValues, setColumnValues] = useState<{ [key: string]: SheetColumnValue }>({}) const [nameEdit, setNameEdit] = useState(false) const [sourcesDialogOpen, setSourcesDialogOpen] = useState(false) @@ -44,8 +45,8 @@ export default function SheetPage() { try { const data = await fetchSheet(slug) setSheet(data.sheet) - setSources(data.sources as (SheetSource & { source: Source })[]) - setColumns(data.columns) + setSheetSources(data.sources as (SheetSource & { source: Source })[]) + setSheetColumns(data.columns) setColumnValues(data.columnValues) } catch (error) { console.error("Error fetching sheet data:", error) @@ -99,7 +100,7 @@ export default function SheetPage() { const result = await runColumnSourceTask(sheet.id, columnId, sourceId) setColumnValues(produce((draft) => { draft[`${sourceId}_${columnId}`]["value"] = result.answer - draft[`${sourceId}_${columnId}`]["sourceIndexId"] = result.sourceIndexId + draft[`${sourceId}_${columnId}`]["indexedSourceId"] = result.indexedSourceId })) setRunAlert(produce((draft) => { draft.done += 1 @@ -117,13 +118,13 @@ export default function SheetPage() { const promises = [] setRunAlert(produce((draft) => { - draft.count = columns.length * sources.length + draft.count = sheetColumns.length * sheetSources.length draft.open = true draft.done = 0 })) - for (const column of columns) { - for (const source of sources) { + for (const column of sheetColumns) { + for (const source of sheetSources) { promises.push(handleRunColumnSourceTask(column.id, source.id)) } } @@ -150,11 +151,11 @@ export default function SheetPage() { const promises = [] setRunAlert(produce((draft) => { - draft.count = sources.length + draft.count = sheetSources.length draft.open = true draft.done = 0 })) - for (const source of sources) { + for (const source of sheetSources) { promises.push(handleRunColumnSourceTask(columnId, source.id)) } await Promise.all(promises) @@ -231,7 +232,7 @@ export default function SheetPage() { Source - {columns.map((column) => ( + {sheetColumns.map((column) => ( {column.name} @@ -262,10 +263,10 @@ export default function SheetPage() { - {sources.map((source) => ( + {sheetSources.map((source) => ( {source.source.nickName} - {columns.map((column) => { + {sheetColumns.map((column) => { const columnValue = columnValues[`${source.id}_${column.id}`] @@ -275,7 +276,7 @@ export default function SheetPage() { {columnValue.value === "Loading" && } {columnValue.value === "Error" && } - {columnValue.value || "N/A"} + {columnValue.value || "No value"} @@ -283,9 +284,9 @@ export default function SheetPage() { - + - Column Value + {column.name} @@ -293,7 +294,7 @@ export default function SheetPage() { {columnValues[`${source.id}_${column.id}`].value} - {columnValue.sourceIndexId && } + {columnValue.indexedSourceId && } @@ -323,7 +324,7 @@ export default function SheetPage() { - + diff --git a/src/app/console/sheets/[slug]/sourceIndex.tsx b/src/app/console/sheets/[slug]/sourceIndex.tsx index 405df0e..b9b55c0 100644 --- a/src/app/console/sheets/[slug]/sourceIndex.tsx +++ b/src/app/console/sheets/[slug]/sourceIndex.tsx @@ -3,6 +3,8 @@ import { getSourceIndex } from "./actions" import { IndexedSource, Source } from "@prisma/client" import { PiDownload } from "react-icons/pi" import { Button } from "@/components/ui/button" +import ReactMarkdown from "react-markdown" +import { stripCodeBlockBackTicks } from "@/lib/utils" export default function SourceIndexComponent({ sourceIndexId }: { sourceIndexId: string }) { @@ -15,8 +17,10 @@ export default function SourceIndexComponent({ sourceIndexId }: { sourceIndexId: } useEffect(() => { - fetchData() - }, []) + if (sourceIndexId) { + fetchData() + } + }, [sourceIndexId]) if (!sourceIndex) { @@ -26,12 +30,14 @@ export default function SourceIndexComponent({ sourceIndexId }: { sourceIndexId: return ( - Reference Text - {sourceIndex.referenceText} + Reference Text + + {stripCodeBlockBackTicks(sourceIndex.referenceText || "No reference text")} + - Reference Image - { + Reference Image + { if (sourceIndex.referenceImageFileName) { window.open(sourceIndex.referenceImageFileName, '_blank') } @@ -41,8 +47,8 @@ export default function SourceIndexComponent({ sourceIndexId }: { sourceIndexId: - Source File - { + Source File + { if (sourceIndex.source.fileName) { window.open(sourceIndex.source.fileName, '_blank') } diff --git a/src/app/globals.css b/src/app/globals.css index f894c77..2dfee66 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -91,21 +91,21 @@ body { .mdc h1 { font-family: "Lora", sans-serif; font-weight: 800; - font-size: 35px; - margin-top: 1.5em; + font-size: 30px; + margin-top: 0.5em; margin-bottom: 0.5em; } .mdc h2 { font-weight: 700; - font-size: 30px; + font-size: 25px; margin-top: 1.5em; margin-bottom: 0.5em; } .mdc h3 { font-weight: 700; - font-size: 25px; + font-size: 20px; margin-top: 1em; margin-bottom: 0.5em; } @@ -140,7 +140,7 @@ body { } .mdc p { - font-size: 17px; + font-size: 16px; margin-top: 0.5em; margin-bottom: 0.5em; } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index bd0c391..d59d2f4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,3 +4,11 @@ import { twMerge } from "tailwind-merge" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + + +export function stripCodeBlockBackTicks(text: string) { + // Regular expression to match Markdown code blocks + const codeBlockRegex = /```[a-zA-Z]*\n([\s\S]*?)\n```/g; + // Replace the matched code blocks with their content + return text.replace(codeBlockRegex, (_, code) => code.trim()); +}
{columnValues[`${source.id}_${column.id}`].value}
{sourceIndex.referenceText}