Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into eric/persistent-state
Browse files Browse the repository at this point in the history
* origin:
  Extract shell state into separate context (#1824)
  Correct meta tag attributes (#1829)
  • Loading branch information
estrattonbailey committed Nov 7, 2023
2 parents c5e2a63 + bfe196b commit d9fa42f
Show file tree
Hide file tree
Showing 52 changed files with 343 additions and 213 deletions.
4 changes: 2 additions & 2 deletions bskyweb/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@
<link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png">
{% block html_head_extra -%}{%- endblock %}
<meta name="application-name" name="Bluesky">
<meta name="generator" name="bskyweb">
<meta name="application-name" content="Bluesky">
<meta name="generator" content="bskyweb">
</head>
<body>
{%- block body_all %}
Expand Down
5 changes: 4 additions & 1 deletion src/App.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as analytics from 'lib/analytics/analytics'
import * as Toast from 'view/com/util/Toast'
import {queryClient} from 'lib/react-query'
import {TestCtrls} from 'view/com/testing/TestCtrls'
import {Provider as ShellStateProvider} from 'state/shell'

SplashScreen.preventAutoHideAsync()

Expand Down Expand Up @@ -81,7 +82,9 @@ function App() {

return (
<PersistedStateProvider data={persistedState}>
<InnerApp />
<ShellStateProvider>
<InnerApp />
</ShellStateProvider>
</PersistedStateProvider>
)
}
Expand Down
5 changes: 4 additions & 1 deletion src/App.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {Shell} from 'view/shell/index'
import {ToastContainer} from 'view/com/util/Toast.web'
import {ThemeProvider} from 'lib/ThemeContext'
import {queryClient} from 'lib/react-query'
import {Provider as ShellStateProvider} from 'state/shell'

const InnerApp = observer(function AppImpl() {
const persisted = usePersisted()
Expand Down Expand Up @@ -71,7 +72,9 @@ function App() {

return (
<PersistedStateProvider data={persistedState}>
<InnerApp />
<ShellStateProvider>
<InnerApp />
</ShellStateProvider>
</PersistedStateProvider>
)
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/hooks/useAccountSwitcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ 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 {useSetDrawerOpen} from '#/state/shell/drawer-open'

export function useAccountSwitcher(): [
boolean,
(v: boolean) => void,
(acct: AccountData) => Promise<void>,
] {
const {track} = useAnalytics()

const store = useStores()
const setDrawerOpen = useSetDrawerOpen()
const [isSwitching, setIsSwitching] = useState(false)
const navigation = useNavigation<NavigationProp>()

Expand All @@ -23,6 +24,7 @@ export function useAccountSwitcher(): [
track('Settings:SwitchAccountButtonClicked')
setIsSwitching(true)
const success = await store.session.resumeSession(acct)
setDrawerOpen(false)
store.shell.closeAllActiveElements()
if (success) {
resetNavigation()
Expand All @@ -34,7 +36,7 @@ export function useAccountSwitcher(): [
store.session.clear()
}
},
[track, setIsSwitching, navigation, store],
[track, setIsSwitching, navigation, store, setDrawerOpen],
)

return [isSwitching, setIsSwitching, onPressSwitchAccount]
Expand Down
10 changes: 6 additions & 4 deletions src/lib/hooks/useMinimalShellMode.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import {autorun} from 'mobx'
import {useStores} from 'state/index'
import {
Easing,
interpolate,
Expand All @@ -9,8 +8,10 @@ import {
withTiming,
} from 'react-native-reanimated'

import {useMinimalShellMode as useMinimalShellModeState} from '#/state/shell/minimal-mode'

export function useMinimalShellMode() {
const store = useStores()
const minimalShellMode = useMinimalShellModeState()
const minimalShellInterp = useSharedValue(0)
const footerMinimalShellTransform = useAnimatedStyle(() => {
return {
Expand Down Expand Up @@ -38,7 +39,7 @@ export function useMinimalShellMode() {

React.useEffect(() => {
return autorun(() => {
if (store.shell.minimalShellMode) {
if (minimalShellMode) {
minimalShellInterp.value = withTiming(1, {
duration: 125,
easing: Easing.bezier(0.25, 0.1, 0.25, 1),
Expand All @@ -50,9 +51,10 @@ export function useMinimalShellMode() {
})
}
})
}, [minimalShellInterp, store.shell.minimalShellMode])
}, [minimalShellInterp, minimalShellMode])

return {
minimalShellMode,
footerMinimalShellTransform,
headerMinimalShellTransform,
fabMinimalShellTransform,
Expand Down
31 changes: 17 additions & 14 deletions src/lib/hooks/useOnMainScroll.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {useState, useCallback, useRef} from 'react'
import {NativeSyntheticEvent, NativeScrollEvent} from 'react-native'
import {RootStoreModel} from 'state/index'
import {s} from 'lib/styles'
import {useWebMediaQueries} from './useWebMediaQueries'
import {useSetMinimalShellMode, useMinimalShellMode} from '#/state/shell'

const Y_LIMIT = 10

Expand All @@ -19,12 +19,12 @@ export type OnScrollCb = (
) => void
export type ResetCb = () => void

export function useOnMainScroll(
store: RootStoreModel,
): [OnScrollCb, boolean, ResetCb] {
export function useOnMainScroll(): [OnScrollCb, boolean, ResetCb] {
let lastY = useRef(0)
let [isScrolledDown, setIsScrolledDown] = useState(false)
const {dyLimitUp, dyLimitDown} = useDeviceLimits()
const minimalShellMode = useMinimalShellMode()
const setMinimalShellMode = useSetMinimalShellMode()

return [
useCallback(
Expand All @@ -33,13 +33,10 @@ export function useOnMainScroll(
const dy = y - (lastY.current || 0)
lastY.current = y

if (!store.shell.minimalShellMode && dy > dyLimitDown && y > Y_LIMIT) {
store.shell.setMinimalShellMode(true)
} else if (
store.shell.minimalShellMode &&
(dy < dyLimitUp * -1 || y <= Y_LIMIT)
) {
store.shell.setMinimalShellMode(false)
if (!minimalShellMode && dy > dyLimitDown && y > Y_LIMIT) {
setMinimalShellMode(true)
} else if (minimalShellMode && (dy < dyLimitUp * -1 || y <= Y_LIMIT)) {
setMinimalShellMode(false)
}

if (
Expand All @@ -54,13 +51,19 @@ export function useOnMainScroll(
setIsScrolledDown(false)
}
},
[store.shell, dyLimitDown, dyLimitUp, isScrolledDown],
[
dyLimitDown,
dyLimitUp,
isScrolledDown,
minimalShellMode,
setMinimalShellMode,
],
),
isScrolledDown,
useCallback(() => {
setIsScrolledDown(false)
store.shell.setMinimalShellMode(false)
setMinimalShellMode(false)
lastY.current = 1e8 // NOTE we set this very high so that the onScroll logic works right -prf
}, [store, setIsScrolledDown]),
}, [setIsScrolledDown, setMinimalShellMode]),
]
}
19 changes: 0 additions & 19 deletions src/lib/routes/back-handler.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/state/models/ui/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,6 @@ export interface ComposerOpts {

export class ShellUiModel {
colorMode: ColorMode = 'system'
minimalShellMode = false
isDrawerOpen = false
isDrawerSwipeDisabled = false
isModalActive = false
activeModals: Modal[] = []
isLightboxActive = false
Expand Down Expand Up @@ -313,10 +310,6 @@ export class ShellUiModel {
}
}

setMinimalShellMode(v: boolean) {
this.minimalShellMode = v
}

/**
* returns true if something was closed
* (used by the android hardware back btn)
Expand All @@ -334,10 +327,6 @@ export class ShellUiModel {
this.closeComposer()
return true
}
if (this.isDrawerOpen) {
this.closeDrawer()
return true
}
return false
}

Expand All @@ -354,21 +343,6 @@ export class ShellUiModel {
if (this.isComposerActive) {
this.closeComposer()
}
if (this.isDrawerOpen) {
this.closeDrawer()
}
}

openDrawer() {
this.isDrawerOpen = true
}

closeDrawer() {
this.isDrawerOpen = false
}

setIsDrawerSwipeDisabled(v: boolean) {
this.isDrawerSwipeDisabled = v
}

openModal(modal: Modal) {
Expand Down
24 changes: 24 additions & 0 deletions src/state/shell/drawer-open.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react'

type StateContext = boolean
type SetContext = (v: boolean) => void

const stateContext = React.createContext<StateContext>(false)
const setContext = React.createContext<SetContext>((_: boolean) => {})

export function Provider({children}: React.PropsWithChildren<{}>) {
const [state, setState] = React.useState(false)
return (
<stateContext.Provider value={state}>
<setContext.Provider value={setState}>{children}</setContext.Provider>
</stateContext.Provider>
)
}

export function useIsDrawerOpen() {
return React.useContext(stateContext)
}

export function useSetDrawerOpen() {
return React.useContext(setContext)
}
24 changes: 24 additions & 0 deletions src/state/shell/drawer-swipe-disabled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react'

type StateContext = boolean
type SetContext = (v: boolean) => void

const stateContext = React.createContext<StateContext>(false)
const setContext = React.createContext<SetContext>((_: boolean) => {})

export function Provider({children}: React.PropsWithChildren<{}>) {
const [state, setState] = React.useState(false)
return (
<stateContext.Provider value={state}>
<setContext.Provider value={setState}>{children}</setContext.Provider>
</stateContext.Provider>
)
}

export function useIsDrawerSwipeDisabled() {
return React.useContext(stateContext)
}

export function useSetDrawerSwipeDisabled() {
return React.useContext(setContext)
}
21 changes: 21 additions & 0 deletions src/state/shell/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react'
import {Provider as DrawerOpenProvider} from './drawer-open'
import {Provider as DrawerSwipableProvider} from './drawer-swipe-disabled'
import {Provider as MinimalModeProvider} from './minimal-mode'

export {useIsDrawerOpen, useSetDrawerOpen} from './drawer-open'
export {
useIsDrawerSwipeDisabled,
useSetDrawerSwipeDisabled,
} from './drawer-swipe-disabled'
export {useMinimalShellMode, useSetMinimalShellMode} from './minimal-mode'

export function Provider({children}: React.PropsWithChildren<{}>) {
return (
<DrawerOpenProvider>
<DrawerSwipableProvider>
<MinimalModeProvider>{children}</MinimalModeProvider>
</DrawerSwipableProvider>
</DrawerOpenProvider>
)
}
24 changes: 24 additions & 0 deletions src/state/shell/minimal-mode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react'

type StateContext = boolean
type SetContext = (v: boolean) => void

const stateContext = React.createContext<StateContext>(false)
const setContext = React.createContext<SetContext>((_: boolean) => {})

export function Provider({children}: React.PropsWithChildren<{}>) {
const [state, setState] = React.useState(false)
return (
<stateContext.Provider value={state}>
<setContext.Provider value={setState}>{children}</setContext.Provider>
</stateContext.Provider>
)
}

export function useMinimalShellMode() {
return React.useContext(stateContext)
}

export function useSetMinimalShellMode() {
return React.useContext(setContext)
}
6 changes: 4 additions & 2 deletions src/view/com/auth/LoggedOut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {usePalette} from 'lib/hooks/usePalette'
import {useStores} from 'state/index'
import {useAnalytics} from 'lib/analytics/analytics'
import {SplashScreen} from './SplashScreen'
import {useSetMinimalShellMode} from '#/state/shell/minimal-mode'

enum ScreenState {
S_LoginOrCreateAccount,
Expand All @@ -19,15 +20,16 @@ enum ScreenState {
export const LoggedOut = observer(function LoggedOutImpl() {
const pal = usePalette('default')
const store = useStores()
const setMinimalShellMode = useSetMinimalShellMode()
const {screen} = useAnalytics()
const [screenState, setScreenState] = React.useState<ScreenState>(
ScreenState.S_LoginOrCreateAccount,
)

React.useEffect(() => {
screen('Login')
store.shell.setMinimalShellMode(true)
}, [store, screen])
setMinimalShellMode(true)
}, [screen, setMinimalShellMode])

if (
store.session.isResumingSession ||
Expand Down
Loading

0 comments on commit d9fa42f

Please sign in to comment.