import { Identity } from "@hyper-hyper-space/core"; import { Contacts } from "@hyper-hyper-space/home"; import { Box, Button, Stack, TextField, Typography } from "@mui/material"; import { Component, Fragment, ReactElement, useEffect, useState } from "react"; import { useParams } from "react-router"; type ImportExportedProfileProps = { renderIdentity?: (id?: Identity) => ReactElement } export default function ImportExportedProfile({ renderIdentity }: ImportExportedProfileProps) { const params = useParams(); const urlProfile = params.profile; const [exportedIdentity, setExportedIdentity] = useState(urlProfile || ''); const [importedIdentity, setImportedIdentity] = useState(); const [importError, setImportError] = useState(false); const exportedProfileOnChange = (e: React.ChangeEvent) => { const newValue = e.currentTarget.value; setExportedIdentity(newValue); }; useEffect(() => { const doImport = async () => { try { const id = await Contacts.importIdentity(JSON.parse(exportedIdentity)); setImportError(false); setImportedIdentity(id); } catch (e) { console.log(e); setImportError(true); setImportedIdentity(undefined); } } if (exportedIdentity.length > 0) { doImport(); } else { setImportedIdentity(undefined); setImportError(false); } }, [exportedIdentity]); return ( {importError && Cannot import: entered an invalid export. } {importedIdentity !== undefined && {importedIdentity.info?.type || 'Identity'} named {importedIdentity.info?.name} {renderIdentity && renderIdentity(importedIdentity)} } ) }