import { useState, useEffect, useRef, Fragment } from 'react'; import { useNavigate, useOutletContext } from 'react-router'; import { Button, Card, CardContent, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, Stack, TextField, Typography, useMediaQuery, useTheme } from '@mui/material'; import InfoIcon from '@mui/icons-material/Info'; import { useObjectState } from '@hyper-hyper-space/react'; import { Contacts, Profile, SpaceLink } from '@hyper-hyper-space/home'; import { HomeContext } from '../HomeSpace'; import { Hash, HashedObject, Space, SpaceEntryPoint } from '@hyper-hyper-space/core'; import { Box } from '@mui/system'; import InfoDialog from '../../../components/InfoDialog'; import { TextSpace } from '../../../model/text/TextSpace'; import { WikiSpace } from '@hyper-hyper-space/wiki-collab'; import HomeItem from './HomeItem'; function EditProfileDialog() { const navigate = useNavigate(); const [open, setOpen] = useState(true); const theme = useTheme(); const fullScreen = useMediaQuery(theme.breakpoints.down('md')); const pictureInputRef = useRef(null); const close = () => { setOpen(false); navigate('..'); }; const { home, owner, resources } = useOutletContext(); const [profile, setProfile] = useState(); const profileState = useObjectState(profile); const [about, setAbout] = useState(); const [picDataUrl, setPicDataUrl] = useState(); const [base64PicData, setBase64PicData] = useState(); const [picMIMEType, setPicMIMEType] = useState(); // data:image/png;base64, useEffect(() => { if (owner !== undefined && resources !== undefined) { const profileHash = new Profile(owner).hash(); resources.store?.load(profileHash).then((loaded?: HashedObject) => { const p = loaded as (Profile | undefined); if (p === undefined) { throw new Error('Could not load profile for this home account!'); } else { setProfile(p); setAbout(p.about?._value); if (p.picture?._value !== undefined && p.pictureMIMEType?._value !== undefined) { setBase64PicData(p.picture?._value); setPicMIMEType(p.pictureMIMEType?._value); } for (const link of p.published?.values() || []) { link.name?.loadAllChanges(); } } }); } }, [owner, resources]); const handleAboutChange = (event: React.ChangeEvent) => { setAbout(event.target.value); }; const handleAboutKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { //e.preventDefault(); // } else { // } }; const dirty = () => { if (profile !== undefined && (profile?.about?._value !== about || profile?.picture?._value !== base64PicData || profile?.pictureMIMEType?._value !== picMIMEType)) { return true; } else { return false; } } const doSave = async () => { if (base64PicData !== undefined) { try { await profileState?.value?.picture?.setValue(base64PicData); } catch (e: any) { alert('Could not fully save your profile, there was a problem setting your picture: ' + e); throw e; } } if (picMIMEType !== undefined) { await profileState?.value?.pictureMIMEType?.setValue(picMIMEType); } try { await profileState?.value?.about?.setValue(about || ''); } catch (e: any) { alert('Could not fully save your profile, there was a problem setting your "about" text: ' + e); throw e; } await profileState?.value?.save(); } const save = async () => { await doSave(); close(); } const cancel = () => { if (dirty()) { setNextAction('close'); } else { close(); } } const changePicture = () => { pictureInputRef.current?.click(); } const onNewPicture: React.ChangeEventHandler = (e: React.ChangeEvent) => { if (e.target.files !== null) { console.log('got a file:') console.log(e.target.files[0]); const fileReader = new FileReader(); fileReader.onload = () => { const dataUrl = fileReader.result?.toString(); if (dataUrl !== undefined) { //setPicDataUrl(dataUrl); const dataAndMIMEType = dataUrl?.slice(5); const mimeTypeLength = dataAndMIMEType?.indexOf(';') const mimeType = dataAndMIMEType.slice(0, mimeTypeLength); const data = decodeURIComponent(dataAndMIMEType.slice(mimeTypeLength+1+7)); setPicMIMEType(mimeType); setBase64PicData(data); console.log('actual value is:') console.log('mime: ' + mimeType) console.log('bytes: ' + data) console.log(fileReader.result?.toString()); } }; fileReader.readAsDataURL(e.target.files[0]); } } useEffect(() => { // data:image/png;base64, if (picMIMEType !== undefined && base64PicData !== undefined) { setPicDataUrl('data:' + picMIMEType + ';base64,' + base64PicData); } }, [picMIMEType, base64PicData]); const [nextAction, setNextAction] = useState(''); const share = () => { if (dirty()) { setNextAction('share'); } else { navigate('../share-profile'); } }; const next = () => { if (nextAction === 'close') { close(); } else if (nextAction === 'share') { navigate('../share-profile'); } } const yes = async () => { await doSave(); next(); } const no = () => { next(); } const [contacts, setContacts] = useState(); const contactsState = useObjectState(contacts); useEffect(() => { if (home !== undefined && resources !== undefined) { const contactsHash = home.contacts?.hash() as Hash; resources.store?.load(contactsHash).then((loaded?: HashedObject) => { const c = loaded as (Contacts | undefined); if (c === undefined) { throw new Error('Could not load contacts for this home account!'); } else { setContacts(c); } }); } }, [home, resources]); const [showWordCodeInfo, setShowWordCodeInfo] = useState(false); const [showPublicFolderInfo, setShowPublicFolderInfo] = useState(false); return ( {showWordCodeInfo && { contactsState?.value?.profileIsPublic?._value && People can look up your profile by entering the code {Space.getWordCodingForHash(owner?.getLastHash() as Hash).join(' ')} into Hyper Hyper Space. You can disable profile lookup in your . } { !(contactsState?.value?.profileIsPublic?._value) && You have disabled profile lookup, so people can't look up your profile by entering {Space.getWordCodingForHash(owner?.getLastHash() as Hash).join(' ')} into Hyper Hyper Space. You can enable profile lookup in your . } } onClose={() => {setShowWordCodeInfo(false);}} /> } {showPublicFolderInfo && The contents of the Public shared folder on your desktop are shown in your profile. People can easily help you keep them online by adding you to their Contacts and re-sharing. } onClose={() => {setShowPublicFolderInfo(false);}} /> } { nextAction !== '' && Save changes to your profile? }
Edit Your Profile
{ picDataUrl === undefined &&
No picture
} { picDataUrl !== undefined && } {owner?.info !== undefined && {Object.entries(owner.info).map((entry:[string, any]) => { return {entry[0]}: {entry[1]}
; }) } code: {Space.getWordCodingForHash(owner.getLastHash() as Hash).join(' ')} {setShowWordCodeInfo(true);}} style={{padding: 0}} color="primary" aria-label="about home info" >
}
{profileState?.value?.published?.size() === 0 && You're not sharing any spaces in your profile. } {profileState?.value?.published !== undefined && profileState?.value?.published?.size() > 0 && {Array.from(profileState?.value?.published.values() || []).map((item: SpaceLink) => { const entry = item.spaceEntryPoint as any as SpaceEntryPoint; const name = item.name; let icon = ""; let title: (string | undefined) = undefined; if (item.spaceEntryPoint instanceof TextSpace) { title = 'Text File'; icon = 'streamline-icon-pencil-write-1@48x48.png'; } else if (item.spaceEntryPoint instanceof WikiSpace) { title = 'Wiki'; icon = 'streamlinehq-book-edit-content-48.png'; } return { window.open('./#/space/' + encodeURIComponent(item.spaceEntryPoint?.getLastHash() as Hash), '_blank') }} menu={[ {name: 'Open', action: () => { window.open('./#/space/' + encodeURIComponent(item.spaceEntryPoint?.getLastHash() as Hash), '_blank') }}, {name: 'Remove from Profile', action: () => { profile?.published?.delete(item).then(() => { profile?.published?.save(); }) }} ]} title={title} dense={true} />; })} }
); } export default EditProfileDialog;