Create logged-out view for group chat invites (#10598)
Co-authored-by: Samuel Newman <mozzius@protonmail.com>
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import {createContext, useContext, useState} from 'react'
|
||||
|
||||
import {logger} from '#/logger'
|
||||
|
||||
type StarterPackLanding = {
|
||||
type: 'starterpack'
|
||||
uri: string
|
||||
isClip?: boolean
|
||||
}
|
||||
|
||||
type GroupChatJoinRequestLanding = {
|
||||
type: 'groupchat'
|
||||
uri: string
|
||||
code: string
|
||||
}
|
||||
|
||||
type LandingType = StarterPackLanding | GroupChatJoinRequestLanding | undefined
|
||||
|
||||
type SetContext = (v: LandingType) => void
|
||||
|
||||
const stateContext = createContext<LandingType>(undefined)
|
||||
stateContext.displayName = 'ActiveLandingStateContext'
|
||||
const setContext = createContext<SetContext>((_: LandingType) => {})
|
||||
setContext.displayName = 'ActiveLandingSetContext'
|
||||
|
||||
export function Provider({children}: {children: React.ReactNode}) {
|
||||
const [state, setState] = useState<LandingType>()
|
||||
|
||||
return (
|
||||
<stateContext.Provider value={state}>
|
||||
<setContext.Provider value={setState}>{children}</setContext.Provider>
|
||||
</stateContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
// Core hooks
|
||||
export const useActiveLanding = () => useContext(stateContext)
|
||||
export const useSetActiveLanding = () => useContext(setContext)
|
||||
|
||||
// Filtered hooks for convenience
|
||||
export const useActiveStarterPack = () => {
|
||||
const landing = useActiveLanding()
|
||||
return landing?.type === 'starterpack' ? landing : undefined
|
||||
}
|
||||
|
||||
export const useSetActiveStarterPack = () => {
|
||||
const setLanding = useSetActiveLanding()
|
||||
const currentLanding = useActiveLanding()
|
||||
return (pack: {uri: string; isClip?: boolean} | undefined) => {
|
||||
if (!pack) {
|
||||
setLanding(undefined)
|
||||
} else {
|
||||
if (currentLanding && currentLanding.type !== 'starterpack') {
|
||||
logger.debug(
|
||||
`[landing] Replacing ${currentLanding.type} landing with starterpack`,
|
||||
)
|
||||
}
|
||||
setLanding({type: 'starterpack', ...pack})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const useActiveGroupChatJoinRequest = () => {
|
||||
const landing = useActiveLanding()
|
||||
return landing?.type === 'groupchat' ? landing : undefined
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import {createContext, useContext, useMemo, useState} from 'react'
|
||||
|
||||
import {useSession} from '#/state/session'
|
||||
import {useActiveStarterPack} from '#/state/shell/starter-pack'
|
||||
import {useActiveLanding} from '#/state/shell/landing'
|
||||
import {IS_WEB} from '#/env'
|
||||
|
||||
type State = {
|
||||
@@ -26,7 +26,12 @@ type Controls = {
|
||||
/**
|
||||
* The did of the account to populate the login form with.
|
||||
*/
|
||||
requestedAccount?: (string & {}) | 'none' | 'new' | 'starterpack'
|
||||
requestedAccount?:
|
||||
| (string & {})
|
||||
| 'none'
|
||||
| 'new'
|
||||
| 'starterpack'
|
||||
| 'groupchat'
|
||||
}) => void
|
||||
/**
|
||||
* Clears the requested account so that next time the logged out view is
|
||||
@@ -48,17 +53,34 @@ const ControlsContext = createContext<Controls>({
|
||||
})
|
||||
ControlsContext.displayName = 'LoggedOutControlsContext'
|
||||
|
||||
function getRequestedAccountFromLanding(
|
||||
landing: ReturnType<typeof useActiveLanding>,
|
||||
hasSession: boolean,
|
||||
): string | undefined {
|
||||
if (hasSession || !landing) return undefined
|
||||
|
||||
switch (landing.type) {
|
||||
case 'starterpack':
|
||||
return IS_WEB ? 'starterpack' : 'new'
|
||||
case 'groupchat':
|
||||
return 'groupchat'
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||
const activeStarterPack = useActiveStarterPack()
|
||||
const activeLanding = useActiveLanding()
|
||||
const {hasSession} = useSession()
|
||||
const shouldShowStarterPack = Boolean(activeStarterPack?.uri) && !hasSession
|
||||
|
||||
const requestedAccount = getRequestedAccountFromLanding(
|
||||
activeLanding,
|
||||
hasSession,
|
||||
)
|
||||
|
||||
const [state, setState] = useState<State>({
|
||||
showLoggedOut: shouldShowStarterPack,
|
||||
requestedAccountSwitchTo: shouldShowStarterPack
|
||||
? IS_WEB
|
||||
? 'starterpack'
|
||||
: 'new'
|
||||
: undefined,
|
||||
showLoggedOut: Boolean(requestedAccount),
|
||||
requestedAccountSwitchTo: requestedAccount,
|
||||
})
|
||||
|
||||
const controls = useMemo<Controls>(
|
||||
|
||||
Reference in New Issue
Block a user