diff --git a/bun.lockb b/bun.lockb index e2dd291..588c8de 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 1ca6151..cb0850d 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "radix-ui": "^1.0.1", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-dropzone": "^14.2.3", "react-force-graph": "^1.44.3", "react-hot-toast": "^2.4.1", "tailwind-merge": "^2.3.0", diff --git a/src/App.tsx b/src/App.tsx index 970d458..6a263d5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,22 +1,23 @@ import './App.css' -import { +import { // useDataShopData, useLocalSampleData, - usePathAnalysisData + usePathAnalysisData } from '@/lib/dataFetchingHooks'; import { useEffect, useState } from 'react'; import { convertDataTypesIncomingData, processPathAnalysisData, processDataShopData } from '@/lib/dataProcessingUtils'; import { GlobalDataType, GraphData } from './lib/types'; import DirectedGraph from './components/DirectedGraph'; +import DropZone from './components/DropZone'; function App() { const sectionId = "area_perimeter_mix_rectangle_derived"; const problemId = "area_perimeter_mix_rectangle_derived-020"; - const { pathAnalysisData, isPathAnalysisDataLoading } = usePathAnalysisData(sectionId, problemId); + // const { pathAnalysisData, isPathAnalysisDataLoading } = usePathAnalysisData(sectionId, problemId); // const { dataShopData, isDataShopDataLoading } = useDataShopData(); const filePath = "analyzing_different_forms_of_expressions.json" - const { localSampleData, isLocalSampleDataLoading } = useLocalSampleData(filePath) + // const { localSampleData, isLocalSampleDataLoading } = useLocalSampleData(filePath) const [processedPathAnalysisData, setProcessedPathAnalysisData] = useState(null); const [processedShopData, setProcessedShopData] = useState(null); @@ -29,40 +30,42 @@ function App() { // } // }, [isDataShopDataLoading, dataShopData]) - useEffect(() => { - if (isLocalSampleDataLoading) { - return; - } - if (localSampleData) { - const _processData = async () => { - const processedData = await processDataShopData(localSampleData as GlobalDataType[]) - setProcessedShopData(processedData); - } - _processData(); - } - }, [isLocalSampleDataLoading, localSampleData]) + // useEffect(() => { + // if (isLocalSampleDataLoading) { + // return; + // } + // if (localSampleData) { + // const _processData = async () => { + // const processedData = await processDataShopData(localSampleData as GlobalDataType[]) + // setProcessedShopData(processedData); + // } + // _processData(); + // } + // }, [isLocalSampleDataLoading, localSampleData]) - useEffect(() => { - const _processData = async () => { - if (pathAnalysisData) { - setProcessedPathAnalysisData(await processPathAnalysisData(convertDataTypesIncomingData(pathAnalysisData))); - } - } - _processData(); + // useEffect(() => { + // const _processData = async () => { + // if (pathAnalysisData) { + // setProcessedPathAnalysisData(await processPathAnalysisData(convertDataTypesIncomingData(pathAnalysisData))); + // } + // } + // _processData(); - }, [isPathAnalysisDataLoading, pathAnalysisData]) + // }, [isPathAnalysisDataLoading, pathAnalysisData]) - if (isPathAnalysisDataLoading || !processedPathAnalysisData || !processedShopData) { - return
Loading...
- } + // if (isPathAnalysisDataLoading || !processedPathAnalysisData || !processedShopData) { + // return
Loading...
+ // } return ( <>
- {/* path analysis window */} +
+ +
- + {/* */}
diff --git a/src/components/DropZone.tsx b/src/components/DropZone.tsx new file mode 100644 index 0000000..4df743b --- /dev/null +++ b/src/components/DropZone.tsx @@ -0,0 +1,51 @@ +import { useCallback } from 'react'; +import { Accept, useDropzone } from 'react-dropzone'; +import { GlobalDataType } from '@/lib/types'; +import { parseData } from '@/lib/utils'; + +export default function DropZone() { + const onDrop = useCallback((acceptedFiles: File[]) => { + acceptedFiles.forEach((file: File) => { + const reader = new FileReader() + + reader.onabort = () => console.warn('file reading was aborted') + reader.onerror = () => console.error('file reading has failed') + reader.onload = () => { + // Do whatever you want with the file contents after .readAsText() + const textStr = reader.result + const array: GlobalDataType[] = parseData(textStr) + console.log(array) + } + reader.readAsText(file) + }) + + }, []) + + const acceptedFileTypes: Accept = { + 'text/plain': ['.txt', '.csv', '.tsv', '.json'], + } + + const { getRootProps, getInputProps, isDragActive, isFocused, isDragReject } = useDropzone({ + onDrop, + accept: acceptedFileTypes, + }); + + return ( +
+ + { + (isDragActive && !isDragReject) ? +

Drop em here

: +

Drag 'n' drop some files here, or click to select files

+ } + { + isDragReject && +

File type not accepted, please try again

+ + } +
+ ); +} \ No newline at end of file diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d084cca..aa44d82 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,6 +1,19 @@ import { type ClassValue, clsx } from "clsx" import { twMerge } from "tailwind-merge" +import { GlobalDataType } from "./types" +import Papa from "papaparse" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + + +export function parseData(readerResult: string | ArrayBuffer | null, delimiter: "\t" | "," | "|" = "\t"): GlobalDataType[] { + const textStr = readerResult + const results = Papa.parse(textStr as string, { + header: true, + delimiter: delimiter + }) + const array: GlobalDataType[] = results.data as GlobalDataType[] + return array +} \ No newline at end of file