Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Sep 6, 2024
1 parent 4cac553 commit 8510bd8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { Dataset } from '@latitude-data/core/browser'
import { Modal, ReactStateDispatch } from '@latitude-data/web-ui'

import '@latitude-data/web-ui'

import {
Modal,
ReactStateDispatch,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
Text,
} from '@latitude-data/web-ui'
import useDatasetPreview from '$/stores/datasetPreviews'

function PreviewModal({
Expand All @@ -10,7 +23,7 @@ function PreviewModal({
setPreview: ReactStateDispatch<Dataset | null>
}) {
const { data, isLoading } = useDatasetPreview({ dataset })
console.log('Preview', data, isLoading)
const rows = data?.data ?? []
return (
<Modal
size='large'
Expand All @@ -19,7 +32,30 @@ function PreviewModal({
description='First 100 rows of the dataset'
onOpenChange={(open: boolean) => !open && setPreview(null)}
>
Hola {isLoading}
{isLoading ? (<div>....</div>) : (
<Table>
<TableHeader>
<TableRow verticalPadding>
{data.headers.map((header, i) => (
<TableHead key={`${header}-${i}`}>
<Text.H4>{header}</Text.H4>
</TableHead>
))}
</TableRow>
</TableHeader>
<TableBody>
{rows.map((row, rowIndex) => {
return (
<TableRow key={rowIndex} verticalPadding>
{row.record.map((cell, cellIndex) => (
<TableCell key={cellIndex}>{cell}</TableCell>
))}
</TableRow>
)
})}
</TableBody>
</Table>
)}
</Modal>
)
}
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/stores/datasetPreviews.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback } from 'react'

import type { Dataset } from '@latitude-data/core/browser'
import { CsvParsedData } from '@latitude-data/core/lib/readCsv'
import { useToast } from '@latitude-data/web-ui'
import { previewDatasetAction } from '$/actions/datasets/preview'
import useCurrentWorkspace from '$/stores/currentWorkspace'
Expand All @@ -15,15 +16,14 @@ export default function useDatasetPreview(
const { toast } = useToast()
const fetcher = useCallback(async () => {
const [data, error] = await previewDatasetAction({ id: dataset.id })
console.log('FETCHING')
if (error) {
toast({
title: 'Error',
description: error.message,
variant: 'destructive',
})

return []
return { headers: [], data: [], rowCount: 0 }
}

return data
Expand All @@ -36,7 +36,7 @@ export default function useDatasetPreview(
)

return {
data,
data: data as CsvParsedData,
isLoading: rest.isLoading,
}
}
5 changes: 5 additions & 0 deletions packages/core/src/lib/readCsv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type ParseResult = {
record: Record<string, string>
info: { columns: { name: string }[] }
}
export type CsvParsedData = {
headers: string[]
rowCount: number
data: ParseResult[]
}
export async function syncReadCsv(
file: File | string,
{ delimiter = ';', limit = -1 }: ParseCsvOptions = {},
Expand Down

0 comments on commit 8510bd8

Please sign in to comment.