import { Add, CalendarMonth, Description, Upload } from '@mui/icons-material' import { Box, Button, Tooltip, Typography } from '@mui/material' import JSZip from 'jszip' import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react' import { useNavigate } from 'react-router-dom' import { toast } from 'react-toastify' import { appPrivateRoutes, appPublicRoutes } from '../../routes' import styles from './style.module.scss' import { MetadataController, NostrController } from '../../controllers' import { formatTimestamp, getUsersAppData, hexToNpub, npubToHex, parseJson, shorten } from '../../utils' import { LoadingSpinner } from '../../components/LoadingSpinner' import { CreateSignatureEventContent, Meta, ProfileMetadata, Sigit } from '../../types' import { Event, kinds, verifyEvent } from 'nostr-tools' import { UserComponent } from '../../components/username' export const HomePage = () => { const navigate = useNavigate() const fileInputRef = useRef(null) const [isLoading, setIsLoading] = useState(true) const [loadingSpinnerDesc] = useState(`Finding user's app data`) const [authUrl, setAuthUrl] = useState() const [sigits, setSigits] = useState([]) const [profiles, setProfiles] = useState<{ [key: string]: ProfileMetadata }>( {} ) useEffect(() => { const nostrController = NostrController.getInstance() // Set up event listener for authentication event nostrController.on('nsecbunker-auth', (url) => { setAuthUrl(url) }) getUsersAppData() .then((res) => { if (res) { setSigits(Object.values(res)) } }) .finally(() => { setIsLoading(false) }) }, []) const handleUploadClick = () => { if (fileInputRef.current) { fileInputRef.current.click() } } const handleFileChange = async ( event: React.ChangeEvent ) => { const file = event.target.files?.[0] if (file) { // Check if the file extension is .sigit.zip const fileName = file.name const fileExtension = fileName.slice(-10) // ".sigit.zip" has 10 characters if (fileExtension === '.sigit.zip') { const zip = await JSZip.loadAsync(file).catch((err) => { console.log('err in loading zip file :>> ', err) toast.error(err.message || 'An error occurred in loading zip file.') return null }) if (!zip) return // navigate to sign page if zip contains keys.json if ('keys.json' in zip.files) { return navigate(appPrivateRoutes.sign, { state: { uploadedZip: file } }) } // navigate to verify page if zip contains meta.json if ('meta.json' in zip.files) { return navigate(appPublicRoutes.verify, { state: { uploadedZip: file } }) } toast.error('Invalid zip file') return } // navigate to create page navigate(appPrivateRoutes.create, { state: { uploadedFile: file } }) } } if (authUrl) { return (