Skip to content

Commit

Permalink
Merge pull request #1860 from bluesky-social/eric/startup
Browse files Browse the repository at this point in the history
Web login/signup and shell
  • Loading branch information
estrattonbailey authored Nov 10, 2023
2 parents 8d7475c + 436a14e commit 6513055
Show file tree
Hide file tree
Showing 31 changed files with 694 additions and 915 deletions.
5 changes: 4 additions & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export function IS_PROD(url: string) {
// until open federation, "production" is defined as the main server
// this definition will not work once federation is enabled!
// -prf
return url.startsWith('https://bsky.social')
return (
url.startsWith('https://bsky.social') ||
url.startsWith('https://api.bsky.app')
)
}

export const PROD_TEAM_HANDLES = [
Expand Down
57 changes: 27 additions & 30 deletions src/lib/hooks/useAccountSwitcher.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
import {useCallback, useState} from 'react'
import {useStores} from 'state/index'
import {useAnalytics} from 'lib/analytics/analytics'
import {StackActions, useNavigation} from '@react-navigation/native'
import {NavigationProp} from 'lib/routes/types'
import {AccountData} from 'state/models/session'
import {reset as resetNavigation} from '../../Navigation'
import * as Toast from 'view/com/util/Toast'
import {useCallback} from 'react'

import {useAnalytics} from '#/lib/analytics/analytics'
import {useStores} from '#/state/index'
import {useSetDrawerOpen} from '#/state/shell/drawer-open'
import {useModalControls} from '#/state/modals'
import {useSessionApi, SessionAccount} from '#/state/session'
import * as Toast from '#/view/com/util/Toast'

export function useAccountSwitcher(): [
boolean,
(v: boolean) => void,
(acct: AccountData) => Promise<void>,
] {
export function useAccountSwitcher() {
const {track} = useAnalytics()
const store = useStores()
const setDrawerOpen = useSetDrawerOpen()
const {closeModal} = useModalControls()
const [isSwitching, setIsSwitching] = useState(false)
const navigation = useNavigation<NavigationProp>()
const {selectAccount, clearCurrentAccount} = useSessionApi()

const onPressSwitchAccount = useCallback(
async (acct: AccountData) => {
async (acct: SessionAccount) => {
track('Settings:SwitchAccountButtonClicked')
setIsSwitching(true)
const success = await store.session.resumeSession(acct)
setDrawerOpen(false)
closeModal()
store.shell.closeAllActiveElements()
if (success) {
resetNavigation()
Toast.show(`Signed in as ${acct.displayName || acct.handle}`)
} else {

try {
await selectAccount(acct)
setDrawerOpen(false)
closeModal()
store.shell.closeAllActiveElements()
Toast.show(`Signed in as ${acct.handle}`)
} catch (e) {
Toast.show('Sorry! We need you to enter your password.')
navigation.navigate('HomeTab')
navigation.dispatch(StackActions.popToTop())
store.session.clear()
clearCurrentAccount() // back user out to login
}
},
[track, setIsSwitching, navigation, store, setDrawerOpen, closeModal],
[
track,
store,
setDrawerOpen,
closeModal,
clearCurrentAccount,
selectAccount,
],
)

return [isSwitching, setIsSwitching, onPressSwitchAccount]
return {onPressSwitchAccount}
}
20 changes: 2 additions & 18 deletions src/state/models/root-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class RootStoreModel {
serialize(): unknown {
return {
appInfo: this.appInfo,
session: this.session.serialize(),
me: this.me.serialize(),
preferences: this.preferences.serialize(),
}
Expand All @@ -80,9 +79,6 @@ export class RootStoreModel {
if (hasProp(v, 'me')) {
this.me.hydrate(v.me)
}
if (hasProp(v, 'session')) {
this.session.hydrate(v.session)
}
if (hasProp(v, 'preferences')) {
this.preferences.hydrate(v.preferences)
}
Expand All @@ -92,18 +88,7 @@ export class RootStoreModel {
/**
* Called during init to resume any stored session.
*/
async attemptSessionResumption() {
logger.debug('RootStoreModel:attemptSessionResumption')
try {
await this.session.attemptSessionResumption()
logger.debug('Session initialized', {
hasSession: this.session.hasSession,
})
this.updateSessionState()
} catch (e: any) {
logger.warn('Failed to initialize session', {error: e})
}
}
async attemptSessionResumption() {}

/**
* Called by the session model. Refreshes session-oriented state.
Expand Down Expand Up @@ -135,11 +120,10 @@ export class RootStoreModel {
}

/**
* Clears all session-oriented state.
* Clears all session-oriented state, previously called on LOGOUT
*/
clearAllSessionState() {
logger.debug('RootStoreModel:clearAllSessionState')
this.session.clear()
resetToTab('HomeTab')
this.me.clear()
}
Expand Down
Loading

0 comments on commit 6513055

Please sign in to comment.