import { horFlexClasses } from '@freesewing/utils' import { roles } from '@freesewing/config' //Hooks import React, { useEffect, useState } from 'react' import { useAccount } from '@freesewing/react/hooks/useAccount' import { useBackend } from '@freesewing/react/hooks/useBackend' // Components import { Link as DefaultLink } from '@freesewing/react/components/Link' import { LockIcon, PlusIcon } from '@freesewing/react/components/Icon' import { Spinner } from '@freesewing/react/components/Spinner' import { Popout } from '@freesewing/react/components/Popout' import { H1, H2, H3 } from '@freesewing/react/components/Heading' import { Consent } from '@freesewing/react/components/Account' const Wrap = ({ children }) => (
{children}
) const ContactSupport = ({ Link = false }) => { if (!Link) Link = DefaultLink return (
Contact Support
) } const AuthRequired = ({ Link, banner }) => { if (!Link) Link = DefaultLink return ( {banner}

Authentication Required

This functionality requires a FreeSewing account

Sign Up Sign In
) } const AccountInactive = ({ Link, banner }) => { if (!Link) Link = DefaultLink return ( {banner}

Account Inactive

You must activate your account via the signup link we sent you.

If you cannot find the link, you can receive a new one by signing up again.

Sign Up
) } const AccountDisabled = ({ banner }) => ( {banner}

Acccount Disabled

You cannot re-enable a disabled account. You need to contact support to resolve this situation.

) const AccountProhibited = ({ banner }) => ( {banner}

Your account has been disabled

Your account has been administratively disabled.

) const AccountStatusUnknown = ({ banner }) => ( {banner}

Account status warning

Your account status prohibits us from processing your data. Please contact support.

) const RoleLacking = ({ t, requiredRole, role, banner }) => ( {banner}

You lack the required role to access this content

This content requires the {requiredRole} role. Your role is {role} which does not grant you access to this content.

) const ConsentLacking = ({ banner, refresh }) => { const { setAccount, setToken, setSeenUser } = useAccount() const backend = useBackend() const updateConsent = async ({ consent1, consent2 }) => { let consent = 0 if (consent1) consent = 1 if (consent1 && consent2) consent = 2 if (consent > 0) { const result = await backend.updateConsent(consent) if (result.success) { setToken(result.data.token) setAccount({ ...result.data.account, bestBefore: Date.now() + 3600000 }) setSeenUser(result.data.account.username) refresh() } else { console.log('something went wrong', result) refresh() } } } return (
{banner}

Your account lacks consent

This should have been taken care of when onboarding your account, but due to a earlier bug in the registration, a small subsection of accounts ended up in this state.

Please complete the form to give your consent, that may resolve the matter.
If it does not, please contact support so we may help you.

Consent & Privacy

) } export const RoleBlock = ({ children, user = false, Link = false }) => { if (!Link) Link = DefaultLink let requiredRole = 'admin' if (user) requiredRole = user const { account, setAccount, token, admin, stopImpersonating, signOut } = useAccount() const backend = useBackend() const [ready, setReady] = useState(false) const [impersonating, setImpersonating] = useState(false) const [error, setError] = useState(false) const [refreshCount, setRefreshCount] = useState(0) /* * Avoid hydration errors */ useEffect(() => { if (admin?.account?.username && account?.username && !impersonating.admin) setImpersonating({ admin: admin.account.username, user: account.username, }) }, [admin]) useEffect(() => { const verifyUser = async () => { if (!error) { const [status, data] = await backend.ping() if (status === 200 && data.result === 'success') { // Refresh account in local storage setAccount({ ...account, ...data.account, bestBefore: Date.now() + 3600000, }) } else if (status === 451) setError('consentLacking') else { console.log({ status, data }) if (data?.error?.error) setError(data.error.error) else signOut() } setReady(true) } } // Don't hammer the backend. Check once per hour. if (token && !error && (!account.bestBefore || account.bestBefore < Date.now())) verifyUser() setReady(true) }, [admin, refreshCount, signOut]) const refresh = () => { setRefreshCount(refreshCount + 1) setError(false) } if (!ready) const banner = impersonating ? (
Hi {impersonating.admin}, you are currently impersonating {impersonating.user}
) : null const childProps = { banner } if (!token || !account.username) return if (error) { if (error === 'accountInactive') return if (error === 'accountDisabled') return if (error === 'accountBlocked') return if (error === 'consentLacking') return return } if (!roles.levels[account.role] || roles.levels[account.role] < roles.levels[requiredRole]) { return } return children } export const UserVisitorContent = ({ userContent = null, visitorContent = null }) => { const { account, setAccount, token } = useAccount() const backend = useBackend() const [ready, setReady] = useState(false) const [error, setError] = useState(false) const [refreshCount, setRefreshCount] = useState(0) /* * Avoid hydration errors */ useEffect(() => { const verifyUser = async () => { const [status, data] = await backend.ping() if (status === 200 && data.result === 'success') { // Refresh account in local storage setAccount({ ...account, ...data.account, bestBefore: Date.now() + 3600000, }) } else { if (data?.error?.error) setError(data.error.error) } setReady(true) } if (token) { // Don't hammer the backend. Check once per hour. if (!account.bestBefore || account.bestBefore < Date.now()) verifyUser() } setReady(true) }, [refreshCount]) const refresh = () => { setRefreshCount(refreshCount + 1) setError(false) } if (!ready) return return token && account.username ? userContent : visitorContent }