Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Sep 5, 2024
1 parent 8facbfe commit 80d05bb
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 86 deletions.
6 changes: 2 additions & 4 deletions apps/web/src/actions/datasets/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ export const createDatasetAction = authProcedure
{ type: 'formData' },
)
.handler(async ({ input, ctx }) => {
const result = await createDataset({
return createDataset({
workspace: ctx.workspace,
author: ctx.user,
disk: disk,
data: {
name: input.name,
file: input.dataset_file,
},
})

return result.unwrap()
}).then(r => r.unwrap())
})
13 changes: 13 additions & 0 deletions apps/web/src/actions/datasets/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use server'

import { DatasetsRepository } from '@latitude-data/core/repositories'

import { authProcedure } from '../procedures'

export const getDatasetsAction = authProcedure
.createServerAction()
.handler(async ({ ctx }) => {
const datasetsScope = new DatasetsRepository(ctx.workspace.id)

return datasetsScope.findAll().then((r) => r.unwrap())
})
2 changes: 1 addition & 1 deletion apps/web/src/app/(private)/dashboard/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default async function DashboardLayout({
return (
<AppLayout
navigationLinks={NAV_LINKS}
currentUser={{ ...user }}
currentUser={user}
breadcrumbs={breadcrumbs}
sectionLinks={MAIN_NAV_LINKS}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use client'

import { Dataset } from '@latitude-data/core/browser'
import {
Button,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
Text,
} from '@latitude-data/web-ui'
import useDatasets from '$/stores/datasets'

export function DatasetsTable({
datasets: serverDatasets,
}: {
datasets: Dataset[]
}) {
const { data: datasets } = useDatasets(undefined, {
fallbackData: serverDatasets,
})
return (
<Table>
<TableHeader>
<TableRow verticalPadding>
<TableHead>Name</TableHead>
<TableHead>Rows</TableHead>
<TableHead>Columns</TableHead>
<TableHead />
</TableRow>
</TableHeader>
<TableBody>
{datasets.map((dataset) => (
<TableRow key={dataset.id} verticalPadding>
<TableCell>
<Text.H4>{dataset.name}</Text.H4>
</TableCell>
<TableCell>
<Text.H4>{dataset.fileMetadata.rowCount}</Text.H4>
</TableCell>
<TableCell>
<Text.H4>{dataset.fileMetadata.headers.length}</Text.H4>
</TableCell>
<TableCell align='center'>
<Button variant='nope' iconProps={{ name: 'trash', color: 'destructive' }} />
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)
}
38 changes: 20 additions & 18 deletions apps/web/src/app/(private)/datasets/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ReactNode } from 'react'

import { DatasetsRepository } from '@latitude-data/core/repositories'
import {
Container,
ListingHeader,
TableBlankSlate,
} from '@latitude-data/web-ui'
import { DatasetsTable } from '$/app/(private)/datasets/_components/DatasetsTable'
import { AppLayout } from '$/components/layouts'
import { getCurrentUser } from '$/services/auth/getCurrentUser'
import { getSession } from '$/services/auth/getSession'
import { ROUTES } from '$/services/routes'
import Link from 'next/link'
import { redirect } from 'next/navigation'

import { MAIN_NAV_LINKS, NAV_LINKS } from '../_lib/constants'

Expand All @@ -19,15 +19,13 @@ export default async function DatasetsList({
}: Readonly<{
children: ReactNode
}>) {
const data = await getSession()
if (!data.session) return redirect(ROUTES.auth.login)

const { user } = await getCurrentUser()

const { workspace, user } = await getCurrentUser()
const datasetsScope = new DatasetsRepository(workspace.id)
const datasets = await datasetsScope.findAll().then((r) => r.unwrap())
return (
<AppLayout
navigationLinks={NAV_LINKS}
currentUser={{ ...user }}
currentUser={user}
sectionLinks={MAIN_NAV_LINKS}
>
<Container>
Expand All @@ -40,16 +38,20 @@ export default async function DatasetsList({
</Link>
}
/>
<TableBlankSlate
description='There are no datasets yet. Create one to start testing your prompts.'
link={
<Link href={ROUTES.datasets.new.root}>
<TableBlankSlate.Button>
Create your first dataset
</TableBlankSlate.Button>
</Link>
}
/>
{datasets.length > 0 ? (
<DatasetsTable datasets={datasets} />
) : (
<TableBlankSlate
description='There are no datasets yet. Create one to start testing your prompts.'
link={
<Link href={ROUTES.datasets.new.root}>
<TableBlankSlate.Button>
Create your first dataset
</TableBlankSlate.Button>
</Link>
}
/>
)}
</Container>
</AppLayout>
)
Expand Down
12 changes: 6 additions & 6 deletions apps/web/src/app/(private)/datasets/new/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import {
FormWrapper,
Input,
Modal,
// useToast,
} from '@latitude-data/web-ui'
import { createDatasetAction } from '$/actions/datasets/create'
import useLatitudeAction from '$/hooks/useLatitudeAction'
import { useNavigate } from '$/hooks/useNavigate'
import { ROUTES } from '$/services/routes'
import useDatasets from '$/stores/datasets'

export default function NewDataset() {
const data = { name: '' }
const navigate = useNavigate()
const { error, executeFormAction } = useLatitudeAction(createDatasetAction)
const errors = error?.fieldErrors
const { createError, createFormAction } = useDatasets({
onCreateSuccess: () => navigate.push(ROUTES.datasets.root),
})
const errors = createError?.fieldErrors
return (
<Modal
open
Expand All @@ -37,7 +37,7 @@ export default function NewDataset() {
<form
className='min-w-0'
id='createDatasetForm'
action={executeFormAction}
action={createFormAction}
>
<FormWrapper>
<Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react'

import { Input, Text } from '@latitude-data/web-ui'
import { Input } from '@latitude-data/web-ui'
import useCurrentWorkspace from '$/stores/currentWorkspace'
import { useDebouncedCallback } from 'use-debounce'

Expand All @@ -22,7 +22,6 @@ export default function WorkspaceName() {
// TODO: i18n
return (
<div className='flex flex-col gap-4 max-w-[50%]'>
<Text.H4B>Workspace</Text.H4B>
<Input
defaultValue={workspace.name}
label='Workspace name'
Expand Down
50 changes: 50 additions & 0 deletions apps/web/src/stores/datasets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useCallback } from 'react'

import type { Dataset } from '@latitude-data/core/browser'
import { useToast } from '@latitude-data/web-ui'
import { createDatasetAction } from '$/actions/datasets/create'
import { getDatasetsAction } from '$/actions/datasets/fetch'
import useLatitudeAction from '$/hooks/useLatitudeAction'
import useCurrentWorkspace from '$/stores/currentWorkspace'
import useSWR, { SWRConfiguration } from 'swr'

export default function useDatasets(
{ onCreateSuccess }: { onCreateSuccess?: (dataset: Dataset) => void } = {},
opts?: SWRConfiguration,
) {
const { data: workspace } = useCurrentWorkspace()
const { toast } = useToast()
const fetcher = useCallback(async () => {
const [data, error] = await getDatasetsAction()
if (error) {
toast({
title: 'Error',
description: error.message,
variant: 'destructive',
})

return []
}

return data
}, [toast])
const {
data = [],
mutate,
...rest
} = useSWR<Dataset[]>(['workspace', workspace.id, 'datasets'], fetcher, opts)
const { error: createError, executeFormAction: createFormAction } =
useLatitudeAction(createDatasetAction, {
onSuccess: ({ data: dataset }) => {
toast({
title: 'Success',
description: 'Dataset uploaded successfully! 🎉',
})

mutate([...data, dataset])
onCreateSuccess?.(dataset)
},
})

return { data, mutate, createFormAction, createError, ...rest }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS "latitude"."datasets" (
"id" text PRIMARY KEY NOT NULL,
"id" bigserial PRIMARY KEY NOT NULL,
"name" varchar(256) NOT NULL,
"workspace_id" bigint NOT NULL,
"author_id" text,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/drizzle/meta/0044_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "0102bda8-846e-4742-8be5-0812926d2f6e",
"id": "aa24d972-2c55-4c99-9ee2-90b1077d07ef",
"prevId": "d4a086e1-d04b-4551-8f19-30825d9434e9",
"version": "7",
"dialect": "postgresql",
Expand Down Expand Up @@ -1159,7 +1159,7 @@
"columns": {
"id": {
"name": "id",
"type": "text",
"type": "bigserial",
"primaryKey": true,
"notNull": true
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@
{
"idx": 44,
"version": "7",
"when": 1725543694894,
"tag": "0044_real_rick_jones",
"when": 1725546410158,
"tag": "0044_dazzling_legion",
"breakpoints": true
}
]
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/repositories/datasetsRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { eq, getTableColumns } from 'drizzle-orm'

import { datasets } from '../schema'
import Repository from './repository'

const datasetColumns = getTableColumns(datasets)
export class DatasetsRepository extends Repository<typeof datasetColumns> {
get scope() {
return this.db
.select(datasetColumns)
.from(datasets)
.where(eq(datasets.workspaceId, this.workspaceId))
.as('datasetsScope')
}
}
1 change: 1 addition & 0 deletions packages/core/src/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './providerLogsRepository'
export * from './documentLogsRepository'
export * from './membershipsRepository'
export * from './evaluationsRepository'
export * from './datasetsRepository'
4 changes: 2 additions & 2 deletions packages/core/src/schema/models/datasets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bigint, jsonb, text, varchar } from 'drizzle-orm/pg-core'
import { bigint, bigserial, jsonb, text, varchar } from 'drizzle-orm/pg-core'
import { type FileSnapshot } from 'flydrive/types'

import { latitudeSchema } from '../db-schema'
Expand All @@ -9,7 +9,7 @@ import { workspaces } from './workspaces'
type FileMetadata = FileSnapshot & { headers: string[]; rowCount: number }

export const datasets = latitudeSchema.table('datasets', {
id: text('id').primaryKey(),
id: bigserial('id', { mode: 'number' }).notNull().primaryKey(),
name: varchar('name', { length: 256 }).notNull().unique(),
workspaceId: bigint('workspace_id', { mode: 'number' })
.notNull()
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,4 @@ export type EvaluationDto = Evaluation & {
>
}

export type Dataset = InferSelectModel<typeof datasets> & {
workspace: Workspace
author: User
}
export type Dataset = InferSelectModel<typeof datasets>
Loading

0 comments on commit 80d05bb

Please sign in to comment.