Use non-reactive callbacks for some methods (#1948)

* Use non-reactive callbacks for some methods

* Remove unnecessary state

* Use non-reactive callbacks for modals
zio/stable
dan 2023-11-17 06:18:37 +00:00 committed by GitHub
parent a10adf8908
commit a8aed2c38e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 39 deletions

View File

@ -1,5 +1,6 @@
import React from 'react' import React from 'react'
import {AppBskyActorDefs} from '@atproto/api' import {AppBskyActorDefs} from '@atproto/api'
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
interface Lightbox { interface Lightbox {
name: string name: string
@ -42,18 +43,15 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
null, null,
) )
const openLightbox = React.useCallback( const openLightbox = useNonReactiveCallback((lightbox: Lightbox) => {
(lightbox: Lightbox) => { setActiveLightbox(lightbox)
setActiveLightbox(lightbox) })
},
[setActiveLightbox],
)
const closeLightbox = React.useCallback(() => { const closeLightbox = useNonReactiveCallback(() => {
let wasActive = !!activeLightbox let wasActive = !!activeLightbox
setActiveLightbox(null) setActiveLightbox(null)
return wasActive return wasActive
}, [setActiveLightbox, activeLightbox]) })
const state = React.useMemo( const state = React.useMemo(
() => ({ () => ({

View File

@ -5,6 +5,7 @@ import {Image as RNImage} from 'react-native-image-crop-picker'
import {ImageModel} from '#/state/models/media/image' import {ImageModel} from '#/state/models/media/image'
import {GalleryModel} from '#/state/models/media/gallery' import {GalleryModel} from '#/state/models/media/gallery'
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
export interface ConfirmModal { export interface ConfirmModal {
name: 'confirm' name: 'confirm'
@ -236,43 +237,33 @@ export let unstable__closeModal: () => boolean = () => {
} }
export function Provider({children}: React.PropsWithChildren<{}>) { export function Provider({children}: React.PropsWithChildren<{}>) {
const [isModalActive, setIsModalActive] = React.useState(false)
const [activeModals, setActiveModals] = React.useState<Modal[]>([]) const [activeModals, setActiveModals] = React.useState<Modal[]>([])
const openModal = React.useCallback( const openModal = useNonReactiveCallback((modal: Modal) => {
(modal: Modal) => { setActiveModals(modals => [...modals, modal])
setActiveModals(activeModals => [...activeModals, modal]) })
setIsModalActive(true)
},
[setIsModalActive, setActiveModals],
)
const closeModal = React.useCallback(() => { const closeModal = useNonReactiveCallback(() => {
let totalActiveModals = 0 let wasActive = activeModals.length > 0
let wasActive = isModalActive setActiveModals(modals => {
setActiveModals(activeModals => { return modals.slice(0, -1)
activeModals = activeModals.slice(0, -1)
totalActiveModals = activeModals.length
return activeModals
}) })
setIsModalActive(totalActiveModals > 0)
return wasActive return wasActive
}, [setIsModalActive, setActiveModals, isModalActive]) })
const closeAllModals = React.useCallback(() => { const closeAllModals = useNonReactiveCallback(() => {
setActiveModals([]) setActiveModals([])
setIsModalActive(false) })
}, [setActiveModals, setIsModalActive])
unstable__openModal = openModal unstable__openModal = openModal
unstable__closeModal = closeModal unstable__closeModal = closeModal
const state = React.useMemo( const state = React.useMemo(
() => ({ () => ({
isModalActive, isModalActive: activeModals.length > 0,
activeModals, activeModals,
}), }),
[isModalActive, activeModals], [activeModals],
) )
const methods = React.useMemo( const methods = React.useMemo(

View File

@ -1,5 +1,6 @@
import React from 'react' import React from 'react'
import {AppBskyEmbedRecord} from '@atproto/api' import {AppBskyEmbedRecord} from '@atproto/api'
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
export interface ComposerOptsPostRef { export interface ComposerOptsPostRef {
uri: string uri: string
@ -47,18 +48,23 @@ const controlsContext = React.createContext<ControlsContext>({
export function Provider({children}: React.PropsWithChildren<{}>) { export function Provider({children}: React.PropsWithChildren<{}>) {
const [state, setState] = React.useState<StateContext>() const [state, setState] = React.useState<StateContext>()
const openComposer = useNonReactiveCallback((opts: ComposerOpts) => {
setState(opts)
})
const closeComposer = useNonReactiveCallback(() => {
let wasOpen = !!state
setState(undefined)
return wasOpen
})
const api = React.useMemo( const api = React.useMemo(
() => ({ () => ({
openComposer(opts: ComposerOpts) { openComposer,
setState(opts) closeComposer,
},
closeComposer() {
let wasOpen = !!state
setState(undefined)
return wasOpen
},
}), }),
[setState, state], [openComposer, closeComposer],
) )
return ( return (