-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee4887c
commit 4cac553
Showing
7 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use server' | ||
|
||
import { DatasetsRepository } from '@latitude-data/core/repositories' | ||
import { previewDataset } from '@latitude-data/core/services/datasets/preview' | ||
import disk from '$/lib/disk' | ||
import { z } from 'zod' | ||
|
||
import { authProcedure } from '../procedures' | ||
|
||
export const previewDatasetAction = authProcedure | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
id: z.number(), | ||
}), | ||
) | ||
.handler(async ({ ctx, input }) => { | ||
const repo = new DatasetsRepository(ctx.workspace.id) | ||
const dataset = await repo.find(input.id).then((r) => r.unwrap()) | ||
return await previewDataset({ dataset, disk }).then((r) => r.unwrap()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
apps/web/src/app/(private)/datasets/_components/PreviewDatasetModal/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Dataset } from '@latitude-data/core/browser' | ||
import { Modal, ReactStateDispatch } from '@latitude-data/web-ui' | ||
import useDatasetPreview from '$/stores/datasetPreviews' | ||
|
||
function PreviewModal({ | ||
dataset, | ||
setPreview, | ||
}: { | ||
dataset: Dataset | ||
setPreview: ReactStateDispatch<Dataset | null> | ||
}) { | ||
const { data, isLoading } = useDatasetPreview({ dataset }) | ||
console.log('Preview', data, isLoading) | ||
return ( | ||
<Modal | ||
size='large' | ||
open | ||
title={`${dataset.name} preview`} | ||
description='First 100 rows of the dataset' | ||
onOpenChange={(open: boolean) => !open && setPreview(null)} | ||
> | ||
Hola {isLoading} | ||
</Modal> | ||
) | ||
} | ||
|
||
export default function PreviewDatasetModal({ | ||
dataset, | ||
setPreview, | ||
}: { | ||
dataset: Dataset | null | ||
setPreview: ReactStateDispatch<Dataset | null> | ||
}) { | ||
if (!dataset) return null | ||
|
||
return <PreviewModal dataset={dataset} setPreview={setPreview} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useCallback } from 'react' | ||
|
||
import type { Dataset } from '@latitude-data/core/browser' | ||
import { useToast } from '@latitude-data/web-ui' | ||
import { previewDatasetAction } from '$/actions/datasets/preview' | ||
import useCurrentWorkspace from '$/stores/currentWorkspace' | ||
import { SWRConfiguration } from 'swr' | ||
import useSWRImmutable from 'swr/immutable' | ||
|
||
export default function useDatasetPreview( | ||
{ dataset }: { dataset: Dataset }, | ||
opts?: SWRConfiguration, | ||
) { | ||
const { data: workspace } = useCurrentWorkspace() | ||
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 data | ||
}, [toast]) | ||
|
||
const { data = [], ...rest } = useSWRImmutable( | ||
['workspace', workspace.id, 'datasets_preview', dataset?.id], | ||
fetcher, | ||
opts, | ||
) | ||
|
||
return { | ||
data, | ||
isLoading: rest.isLoading, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Dataset } from '../../browser' | ||
import { DiskWrapper } from '../../lib/disk' | ||
import { syncReadCsv } from '../../lib/readCsv' | ||
|
||
/** | ||
* This service pick the first N rows of a CSV file | ||
*/ | ||
export async function previewDataset({ | ||
dataset, | ||
disk, | ||
limit = 100, | ||
}: { | ||
dataset: Dataset | ||
disk: DiskWrapper | ||
limit?: number | ||
}) { | ||
const diskFile = disk.file(dataset.fileKey) | ||
const bytes = await diskFile.getBytes() | ||
const file = new TextDecoder().decode(bytes) | ||
return syncReadCsv(file, { limit }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters