-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload csv data and parse for sample and plans params
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 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
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,73 @@ | ||
"use client"; | ||
|
||
import { useRef } from "react"; | ||
import { parse } from "papaparse"; | ||
import { cn } from "@sophys-web/ui"; | ||
import { buttonVariants } from "@sophys-web/ui/button"; | ||
import { Input } from "@sophys-web/ui/input"; | ||
import { Label } from "@sophys-web/ui/label"; | ||
import { toast } from "@sophys-web/ui/sonner"; | ||
import type { SampleParams } from "../../lib/schemas/sample"; | ||
import { samplesSchema } from "../../lib/schemas/sample"; | ||
|
||
interface ButtonProps { | ||
children: React.ReactNode; | ||
handleUpload: (data: SampleParams[]) => void; | ||
} | ||
|
||
export function UploadButton(props: ButtonProps) { | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
return ( | ||
<div className="flex flex-col items-center gap-8"> | ||
<Label | ||
className={cn( | ||
buttonVariants({ variant: "link" }), | ||
"flex cursor-pointer items-center", | ||
)} | ||
htmlFor="upload-button" | ||
> | ||
{props.children} | ||
</Label> | ||
<Input | ||
accept=".csv" | ||
className="sr-only" | ||
id="upload-button" | ||
onChange={(e) => { | ||
toast.info("Parsing CSV file..."); | ||
const files = e.target.files; | ||
if (!files) return; | ||
const file = files[0]; | ||
parse(file, { | ||
header: true, | ||
skipEmptyLines: true, | ||
complete: async (results) => { | ||
const { data: csvData, errors } = results; | ||
if (errors.length > 0) { | ||
toast.error("Error parsing CSV file", { | ||
description: errors.map((error) => error.message).join("\n"), | ||
}); | ||
return; | ||
} | ||
const parsedData = await samplesSchema.safeParseAsync(csvData); | ||
if (!parsedData.success) { | ||
toast.error("Error parsing CSV file", { | ||
description: parsedData.error.errors | ||
.map((err) => err.message) | ||
.join("\n"), | ||
}); | ||
return; | ||
} | ||
toast.success("CSV file parsed successfully"); | ||
props.handleUpload(parsedData.data); | ||
}, | ||
}); | ||
if (inputRef.current) { | ||
inputRef.current.value = ""; | ||
} | ||
}} | ||
ref={inputRef} | ||
type="file" | ||
/> | ||
</div> | ||
); | ||
} |
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 { z } from "zod"; | ||
|
||
export const sampleSchema = z.object({ | ||
name: z.string(), | ||
order: z.coerce.number(), | ||
buffer: z.string(), | ||
time: z.coerce.number(), | ||
frames: z.coerce.number(), | ||
volume: z.coerce.number(), | ||
speed: z.coerce.number(), | ||
position: z.string(), | ||
energy: z.coerce.number(), | ||
distance: z.coerce.number(), | ||
detX: z.coerce.number(), | ||
detY: z.coerce.number(), | ||
comments: z.string(), | ||
}); | ||
|
||
export const samplesSchema = z.array(sampleSchema); | ||
|
||
export type SampleParams = z.infer<typeof sampleSchema>; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.