Skip to content

Commit

Permalink
Feat(canvas): WIP file viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
leoank committed Sep 8, 2024
1 parent 863c3ce commit 5474efd
Show file tree
Hide file tree
Showing 5 changed files with 1,164 additions and 31 deletions.
22 changes: 15 additions & 7 deletions canvas/components/custom/file-viewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Eye, Loader2 } from "lucide-react";
import React from "react";
import Image from "next/image";
import { useProjectStore } from "@/stores/project";
import { tableFromIPC } from "apache-arrow";

interface DSVRendererProps {
data: DSVRowArray;
Expand All @@ -39,17 +40,15 @@ const DSVRenderer = (props: DSVRendererProps) => {
);
};

interface ParquetRendererProps {
}
interface ParquetRendererProps {}
const ParquetRenderer = (props: ParquetRendererProps) => {
const { } = props;
const {} = props;
return (
<div
style={{
maxWidth: "calc(100vw - 60px)",
}}
>
</div>
></div>
);
};

Expand All @@ -60,7 +59,9 @@ interface FileViewerProps {
export function FileViewer(props: FileViewerProps) {
const { project } = useProjectStore((state) => ({ project: state.project }));
const { fileName } = props;
const [viewContent, setViewContent] = React.useState("");
const [viewContent, setViewContent] = React.useState<
string | Blob | undefined
>("");
const [isLoading, setIsLoading] = React.useState(true);

async function fetchData() {
Expand All @@ -82,12 +83,17 @@ export function FileViewer(props: FileViewerProps) {
setViewContent(data);
}
} else {
console.log("Failed to load file.", data);
setViewContent(data);
}

setIsLoading(false);
}

function parseParquet(buffer: ArrayBufferLike) {
const table = tableFromIPC(buffer);
return table;
}

const loading = isLoading;
const failedToLoad = !isLoading && !viewContent;
const loaded = !isLoading && viewContent;
Expand All @@ -96,6 +102,7 @@ export function FileViewer(props: FileViewerProps) {
const isTSV = fileName.endsWith(".tsv");
const isTXT = fileName.endsWith(".txt");
const isExcel = fileName.endsWith(".xlsx");
const isParquet = fileName.endsWith(".parquet");

return (
<Dialog>
Expand All @@ -119,6 +126,7 @@ export function FileViewer(props: FileViewerProps) {
{loaded && isCSV && <DSVRenderer data={csvParse(viewContent)} />}
{loaded && isTSV && <DSVRenderer data={tsvParse(viewContent)} />}
{loaded && isTXT && <p> {viewContent} </p>}
{loaded && isParquet && <p> {parseParquet(viewContent).toArray()} </p>}
{loaded && isImage && (
<div className="flex justify-center">
<Image
Expand Down
3 changes: 3 additions & 0 deletions canvas/components/custom/file-viewer/perspective.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use client";


Loading

0 comments on commit 5474efd

Please sign in to comment.