Skip to content

Commit

Permalink
feat: Update csv rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
PintoGideon committed Nov 27, 2024
1 parent e9e65af commit 4cda7d7
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 44 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"micromark-extension-gfm": "^3.0.0",
"niivue-react": "github:niivue/niivue-react",
"pako": "^1.0.11",
"papaparse": "^5.4.1",
"preval.macro": "^5.0.0",
"query-string": "^9.1.0",
"rc-tree": "^5.8.8",
Expand Down
18 changes: 13 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/FeedTree/FeedTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const SCALE_EXTENT = { min: 0.1, max: 1.5 };
const INITIAL_SCALE = 1;
const SEPARATION = {
siblings: 0.75,
nonSiblings: 2.0,
nonSiblings: 0.75,
};
const SVG_CLASS_NAME = "feed-tree__svg";
const GRAPH_CLASS_NAME = "feed-tree__graph";
Expand Down
126 changes: 88 additions & 38 deletions src/components/Preview/displays/TextDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,108 @@
import React, { Fragment } from "react";
import Papa from "papaparse"; // Import PapaParse for CSV parsing
import React from "react";
import type { IFileBlob } from "../../../api/model";
import useSize from "../../FeedTree/useSize";

type AllProps = {
selectedFile?: IFileBlob;
};

const TextDisplay: React.FunctionComponent<AllProps> = (props: AllProps) => {
const divRef = React.useRef(null);
const { selectedFile } = props;
const TextDisplay: React.FunctionComponent<AllProps> = ({ selectedFile }) => {
const divRef = React.useRef<HTMLDivElement>(null);
useSize(divRef);

const [content, setContent] = React.useState<string | null>(null);
const [csvData, setCsvData] = React.useState<string[][] | null>(null);

React.useEffect(() => {
const textDisplay = document.getElementById("text-display");

if (textDisplay) {
const displayContent = async () => {
if (selectedFile) {
const reader = new FileReader();
reader.addEventListener(
"load",
() => {
textDisplay.innerText = reader.result as string;
},
false,
);
const blob = await selectedFile.getFileBlob();
reader.readAsText(blob);
}
};

displayContent();
}
const displayContent = async () => {
if (selectedFile) {
const blob = await selectedFile.getFileBlob();
const reader = new FileReader();

reader.addEventListener(
"load",
() => {
const result = reader.result as string;
const fileName = selectedFile.data?.fname || "";
const isCSVFile = fileName.endsWith(".csv");

if (isCSVFile) {
// Parse CSV
const parsed = Papa.parse(result, { header: false });
setCsvData(parsed.data);
setContent(null);
} else {
setContent(result);
setCsvData(null);
}
},
false,
);

reader.readAsText(blob);
}
};

displayContent();
}, [selectedFile]);

return (
<Fragment>
<div
ref={divRef}
style={{
display: "block",
overflowY: "scroll",
width: "100%",
}}
>
<span
id="text-display"
<div
ref={divRef}
style={{
display: "block",
overflow: "hidden", // Hide scrollbars on the outer container
width: "100%",
height: "100%",
}}
>
{csvData ? (
// Wrap table in a div to manage overflow
<div
style={{
overflow: "auto", // Enable vertical and horizontal scrolling
width: "100%",
height: "100%",
}}
>
<table
style={{
borderCollapse: "collapse",
width: "max-content", // Allow table to take natural width
minWidth: "100%", // Ensure table fills the container width initially
}}
>
<tbody>
{csvData.map((row, rowIndex) => (
<tr key={rowIndex}>
{row.map((cell, cellIndex) => (
<td
key={cellIndex}
style={{ border: "1px solid #ccc", padding: "4px" }}
>
{cell}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
) : (
// Render text content
<pre
style={{
fontFamily: "monospace",
color: "white",
whiteSpace: "pre-wrap",
margin: 0,
}}
/>
</div>
</Fragment>
>
{content}
</pre>
)}
</div>
);
};

Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ declare module "hammerjs";
declare module "preval.macro";
declare module "@cornerstonejs/dicom-image-loader";
declare module "dcmjs";
declare module "papaparse";

0 comments on commit 4cda7d7

Please sign in to comment.