From 035089232adc0381cd03694002b90121ee244f83 Mon Sep 17 00:00:00 2001 From: highperfocused Date: Sun, 13 Apr 2025 13:28:57 +0200 Subject: [PATCH] cleanup upload-component pt1 --- components/upload-component.tsx | 79 ++------------------------------- 1 file changed, 3 insertions(+), 76 deletions(-) diff --git a/components/upload-component.tsx b/components/upload-component.tsx index cbbbd80..232adfb 100644 --- a/components/upload-component.tsx +++ b/components/upload-component.tsx @@ -1,6 +1,6 @@ "use client" -import { useState, useEffect, useCallback, type ChangeEvent, type FormEvent } from "react" +import { useState, type ChangeEvent, type FormEvent } from "react" import { ReloadIcon } from "@radix-ui/react-icons" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" @@ -255,69 +255,15 @@ async function calculateBlurhash(file: File): Promise { }) } -async function calculateSHA256(file: File): Promise { - // In a browser environment, we'd use the Web Crypto API - // This is a simplified mock implementation - return new Promise((resolve) => { - setTimeout(() => { - // Generate a random SHA256-like hash for demo purposes - const mockHash = Array.from({ length: 64 }, () => "0123456789abcdef"[Math.floor(Math.random() * 16)]).join("") - resolve(mockHash) - }, 500) - }) -} - const UploadComponent = () => { const { createHash } = require("crypto") const loginType = typeof window !== "undefined" ? window.localStorage.getItem("loginType") : null const [previewUrl, setPreviewUrl] = useState("") const [isLoading, setIsLoading] = useState(false) - const [uploadedNoteId, setUploadedNoteId] = useState("") - const [retryCount, setRetryCount] = useState(0) - const [shouldFetch, setShouldFetch] = useState(false) const [serverChoice, setServerChoice] = useState("blossom.band") - const [events, setEvents] = useState([]) - const [isNoteLoading, setIsNoteLoading] = useState(false) const [uploadResponse, setUploadResponse] = useState(null) const [showUploadForm, setShowUploadForm] = useState(true) - useEffect(() => { - if (uploadedNoteId) { - setShouldFetch(true) - } - }, [uploadedNoteId]) - - useEffect(() => { - let timeoutId: NodeJS.Timeout - - if (shouldFetch && events.length === 0 && !isNoteLoading) { - setIsNoteLoading(true) - - // Simulate fetching events - timeoutId = setTimeout(() => { - setIsNoteLoading(false) - // After a few retries, simulate finding the event - if (retryCount >= 2) { - setEvents([{ id: uploadedNoteId }]) - } else { - setRetryCount((prevCount) => prevCount + 1) - } - }, 2000) - } - - return () => { - if (timeoutId) { - clearTimeout(timeoutId) - } - } - }, [shouldFetch, events, isNoteLoading, retryCount, uploadedNoteId]) - - const handleRetry = useCallback(() => { - setRetryCount((prevCount) => prevCount + 1) - setShouldFetch(false) - setTimeout(() => setShouldFetch(true), 100) - }, []) - const handleFileChange = (event: ChangeEvent) => { const file = event.target.files?.[0] if (file) { @@ -336,10 +282,6 @@ const UploadComponent = () => { setUploadResponse(null) setShowUploadForm(true) setPreviewUrl("") - setUploadedNoteId("") - setEvents([]) - setRetryCount(0) - setShouldFetch(false) } async function onSubmit(event: FormEvent) { @@ -347,15 +289,13 @@ const UploadComponent = () => { setIsLoading(true) const formData = new FormData(event.currentTarget) - const desc = formData.get("description") as string const file = formData.get("file") as File let sha256 = "" - let finalNoteContent = desc let finalFileUrl = "" console.log("File:", file) - if (!desc && !file.size) { - alert("Please enter a description and/or upload a file") + if (!file.size) { + alert("Please select a file first") setIsLoading(false) return } @@ -379,7 +319,6 @@ const UploadComponent = () => { const unixNow = () => Math.floor(Date.now() / 1000) const newExpirationValue = () => (unixNow() + 60 * 5).toString() - const pubkey = window.localStorage.getItem("pubkey") const createdAt = Math.floor(Date.now() / 1000) // Create auth event for blossom auth via nostr @@ -420,15 +359,6 @@ const UploadComponent = () => { // Set the upload response data setUploadResponse(responseJson) - let blurhash = "" - if (file && file.type.startsWith("image/")) { - try { - blurhash = await calculateBlurhash(file) - } catch (error) { - console.error("Error calculating blurhash:", error) - } - } - if (finalFileUrl) { const image = new Image() image.src = URL.createObjectURL(file) @@ -436,14 +366,11 @@ const UploadComponent = () => { image.onload = resolve }) - finalNoteContent = desc } setIsLoading(false) if (finalFileUrl != null) { setShowUploadForm(false) - setShouldFetch(true) - setRetryCount(0) } } else { throw new Error("Failed to upload file: " + (await res.text()))