diff --git a/src/components/Dropzone.tsx b/src/components/Dropzone.tsx new file mode 100644 index 0000000..71e4698 --- /dev/null +++ b/src/components/Dropzone.tsx @@ -0,0 +1,62 @@ +import { component$, useVisibleTask$ } from "@builder.io/qwik"; +import { useDropzone } from "~/hooks/dropzone"; +import { useNanostore$ } from "~/hooks/nanostores"; +import { api } from "~/lib/api"; +import { dashboardFiles } from "~/lib/stores"; +import { StereoFile } from "~/lib/types"; + +export default component$(() => { + const dashfiles = useNanostore$(dashboardFiles); + + const { + highlight, + onDragOver, + onDragLeave, + onInputChange, + triggerFileInput, + fileInputRef, + } = useDropzone(); + + useVisibleTask$(() => { + const dropzone = document.getElementById("dropzone"); + if (!dropzone) return; + const handler = async (e: DragEvent) => { + e.preventDefault(); + highlight.value = false; + const files = Array.from(e.dataTransfer?.files || []); + for (const file of files) { + const name = file.name.replace(/[^a-zA-Z0-9_.-]/g, "_"); + const f = new File([file], name, { type: file.type }); + await api.upload(f); + dashfiles.value = await api.list(); + } + }; + dropzone.addEventListener("drop", handler); + return () => dropzone.removeEventListener("drop", handler); + }); + + return ( +
+

drop file ehre

+ +
+ ); +}); \ No newline at end of file diff --git a/src/components/File.tsx b/src/components/File.tsx index 3d82ba3..00158f4 100644 --- a/src/components/File.tsx +++ b/src/components/File.tsx @@ -65,14 +65,18 @@ export default component$(({ file }: FileProps) => { return (
- + { (file.Name.endsWith(".png") || file.Name.endsWith(".jpg") || file.Name.endsWith(".jpeg")) && ( {file.Name} )} diff --git a/src/hooks/dropzone.tsx b/src/hooks/dropzone.tsx new file mode 100644 index 0000000..e1817fe --- /dev/null +++ b/src/hooks/dropzone.tsx @@ -0,0 +1,28 @@ +import { $, useSignal } from '@builder.io/qwik'; + +export const useDropzone = () => { + const highlight = useSignal(false); + + const onInputChange = $(async (e: Event) => { + e.preventDefault(); + }); + + const fileInputRef = useSignal(undefined); + + return { + highlight, + onDragOver: $((e: DragEvent) => { + e.preventDefault(); + highlight.value = true; + }), + onDragLeave: $((e: DragEvent) => { + e.preventDefault(); + highlight.value = false; + }), + onInputChange, + triggerFileInput: $(() => { + fileInputRef.value?.click(); + }), + fileInputRef, + }; +} \ No newline at end of file diff --git a/src/routes/dashboard/index.tsx b/src/routes/dashboard/index.tsx index 4e9d6ea..d7f2567 100644 --- a/src/routes/dashboard/index.tsx +++ b/src/routes/dashboard/index.tsx @@ -1,6 +1,7 @@ import { component$, useVisibleTask$ } from "@builder.io/qwik"; import { routeLoader$, type DocumentHead } from "@builder.io/qwik-city"; import Controlbar from "~/components/Controlbar"; +// import Dropzone from "~/components/Dropzone"; import File from "~/components/File"; import { SolarUploadLinear, SvgSpinnersBarsRotateFade } from "~/components/Icons"; import { useNanostore$ } from "~/hooks/nanostores"; @@ -28,6 +29,7 @@ export default component$(() => { return ( <> + {/* */} {!loaded.value ? (
@@ -44,7 +46,7 @@ export default component$(() => {
) : ( -
+
{files.value.map((file) => ( ))}