import { useCallback, useEffect, useState } from 'react';
import { RoleContext, PushServerContext } from '../../context';
import { WhoAmI } from '../WhoAmI';
import { SecretPassword } from '../SecretPassword';
import { GetJson, PostJson } from '../Fetch';
import { Chrome } from './Chrome';
export function WithServerHello({ children }) {
const [loggingOut, setLoggingOut] = useState(null);
const [helloKey, setHelloKey] = useState(0);
const [whoamiKey, setWhoamiKey] = useState(0);
const [whoamiInfo, setWhoamiInfo] = useState(null);
const childrenFor = useCallback((did, role, parentChildren) => {
if (role === 'public') {
return ;
}
return parentChildren;
})
const reloadConnect = useCallback(e => {
e.preventDefault();
setWhoamiKey(n => n + 1);
});
const handleLogout = useCallback(async () => {
setLoggingOut(true);
try {
const host = import.meta.env.VITE_NOTIFICATIONS_HOST;
await fetch(`${host}/logout`, {
method: 'POST',
credentials: 'include',
});
// TODO: cancel subscription, clear storage, etc
} catch (e) {
console.error('logout fail', e);
}
setLoggingOut(null);
setHelloKey(n => n + 1);
});
if (loggingOut !== null) {
return Logging out…
;
}
return (
{
if (role === 'anonymous') {
return whoamiInfo === null
? (
Reload connect prompt
) : (
(
{childrenFor(did, role, children)}
)}
/>
)
} else {
return (
{childrenFor(did, role, children)}
);
}
}}
/>
);
}