import { useState, useEffect, useRef, Fragment } from 'react'; import { useNavigate, useOutletContext } from 'react-router'; import { Dialog, useTheme, useMediaQuery, DialogTitle, Stack, Typography, Button, DialogContent, FormControl, FormLabel, FormGroup, FormControlLabel, Switch, Input, IconButton, TextField, DialogActions } from '@mui/material'; import ContentCopyIcon from '@mui/icons-material/ContentCopy'; import ShareIcon from '@mui/icons-material/Share'; import { Hash, HashedObject, Identity, Space } from '@hyper-hyper-space/core'; import { useObjectState } from '@hyper-hyper-space/react'; import { Contacts } from '@hyper-hyper-space/home'; import { HomeContext } from '../HomeSpace'; import Context from '@mui/base/TabsUnstyled/TabsContext'; function ShareProfileDialog() { const navigate = useNavigate(); const [open, setOpen] = useState(true); const theme = useTheme(); const fullScreen = useMediaQuery(theme.breakpoints.down('md')); const close = () => { setOpen(false); navigate('..'); }; const { owner, home, resources } = useOutletContext(); const [contacts, setContacts] = useState(); const [profileIsPublic, setProfileIsPublic] = useState(); 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); setProfileIsPublic(c.profileIsPublic?._value); } }); } }, [home, resources]); const [shareUrl, setShareUrl] = useState(); useEffect(() => { if (owner !== undefined) { setShareUrl(document.location.origin + document.location.pathname + '#/add-contact/'+encodeURIComponent(JSON.stringify(Contacts.exportIdentity(owner)))); } }, [owner]); const handleIsPublicChange = (event: React.ChangeEvent) => { setProfileIsPublic(event.target.checked); contacts?.profileIsPublic?.setValue(event.target.checked); contacts?.profileIsPublic?.saveQueuedOps(); }; const shareUrlInputRef = useRef(); const exportTextFieldRef = useRef(); const copyUrl = () => { (navigator.clipboard as any).writeText(shareUrl); shareUrlInputRef.current?.setSelectionRange(0, shareUrlInputRef.current?.value.length); shareUrlInputRef.current?.select(); } const share = () => { navigator.share({url: shareUrl}); } const copyExport = () => { (navigator.clipboard as any).writeText(JSON.stringify(Contacts.exportIdentity(owner as Identity))); exportTextFieldRef.current?.setSelectionRange(0, exportTextFieldRef.current?.value.length); exportTextFieldRef.current?.select(); } const [exportProfile, setExportProfile] = useState(false); return ( Share your profile Your profile code is {Space.getWordCodingForHash(owner?.getLastHash() as Hash).join(' ')}. {contacts !== undefined && owner !== undefined && Public access } label="Allow profile lookup using 3-word code" labelPlacement="start" /> { shareUrl !== undefined && Profile sharing link { (navigator as any).canShare !== undefined && (navigator as any).canShare({url: shareUrl}) && } } Profile export { !exportProfile && For importing in other systems. } { exportProfile && Needs to be sent without changes. } } ); } export default ShareProfileDialog;