prevent contexts causing rerenders (#1022)
This commit is contained in:
parent
2ee730f8b5
commit
5c18ef2317
|
@ -35,19 +35,20 @@ export default function useModal () {
|
||||||
setModalStack(modalStack.slice(0, -1))
|
setModalStack(modalStack.slice(0, -1))
|
||||||
modalOptions?.onClose?.()
|
modalOptions?.onClose?.()
|
||||||
return setModalContent(previousModalContent)
|
return setModalContent(previousModalContent)
|
||||||
}, [modalStack, setModalStack])
|
}, [modalStack, setModalStack, modalOptions?.onClose])
|
||||||
|
|
||||||
|
// this is called on every navigation due to below useEffect
|
||||||
const onClose = useCallback(() => {
|
const onClose = useCallback(() => {
|
||||||
setModalContent(null)
|
setModalContent(null)
|
||||||
setModalStack([])
|
setModalStack(ms => ms.length > 0 ? [] : ms)
|
||||||
modalOptions?.onClose?.()
|
modalOptions?.onClose?.()
|
||||||
}, [modalOptions?.onClose])
|
}, [setModalStack, setModalContent, modalOptions?.onClose])
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
router.events.on('routeChangeStart', onClose)
|
router.events.on('routeChangeStart', onClose)
|
||||||
return () => router.events.off('routeChangeStart', onClose)
|
return () => router.events.off('routeChangeStart', onClose)
|
||||||
}, [router, onClose])
|
}, [router.events, onClose])
|
||||||
|
|
||||||
const modal = useMemo(() => {
|
const modal = useMemo(() => {
|
||||||
if (modalContent === null) {
|
if (modalContent === null) {
|
||||||
|
@ -76,7 +77,7 @@ export default function useModal () {
|
||||||
</Modal.Body>
|
</Modal.Body>
|
||||||
</Modal>
|
</Modal>
|
||||||
)
|
)
|
||||||
}, [modalContent, onClose])
|
}, [modalContent, onClose, modalOptions, onBack, modalStack])
|
||||||
|
|
||||||
const showModal = useCallback(
|
const showModal = useCallback(
|
||||||
(getContent, options) => {
|
(getContent, options) => {
|
||||||
|
|
|
@ -531,7 +531,7 @@ export default function Notifications ({ ssrData }) {
|
||||||
}
|
}
|
||||||
}, router.asPath, { ...router.options, shallow: true })
|
}, router.asPath, { ...router.options, shallow: true })
|
||||||
}
|
}
|
||||||
}, [router, lastChecked])
|
}, [router?.query?.checkedAt, lastChecked])
|
||||||
|
|
||||||
if (!dat) return <CommentsFlatSkeleton />
|
if (!dat) return <CommentsFlatSkeleton />
|
||||||
|
|
||||||
|
@ -540,7 +540,7 @@ export default function Notifications ({ ssrData }) {
|
||||||
{notifications.map(n =>
|
{notifications.map(n =>
|
||||||
<Notification
|
<Notification
|
||||||
n={n} key={nid(n)}
|
n={n} key={nid(n)}
|
||||||
fresh={new Date(n.sortTime) > new Date(router?.query?.checkedAt)}
|
fresh={new Date(n.sortTime) > new Date(router?.query?.checkedAt ?? lastChecked)}
|
||||||
/>)}
|
/>)}
|
||||||
<MoreFooter cursor={cursor} count={notifications?.length} fetchMore={fetchMore} Skeleton={CommentsFlatSkeleton} noMoreText='NO MORE' />
|
<MoreFooter cursor={cursor} count={notifications?.length} fetchMore={fetchMore} Skeleton={CommentsFlatSkeleton} noMoreText='NO MORE' />
|
||||||
</>
|
</>
|
||||||
|
|
|
@ -36,16 +36,6 @@ export const ToastProvider = ({ children }) => {
|
||||||
const [toasts, setToasts] = useState([])
|
const [toasts, setToasts] = useState([])
|
||||||
const toastId = useRef(0)
|
const toastId = useRef(0)
|
||||||
|
|
||||||
const dispatchToast = useCallback((toast) => {
|
|
||||||
toast = {
|
|
||||||
...toast,
|
|
||||||
createdAt: +new Date(),
|
|
||||||
id: toastId.current++
|
|
||||||
}
|
|
||||||
setToasts(toasts => ensureFlow(toasts, toast).map(mapHidden(toast)))
|
|
||||||
return () => removeToast(toast)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const removeToast = useCallback(({ id, onCancel, tag }) => {
|
const removeToast = useCallback(({ id, onCancel, tag }) => {
|
||||||
setToasts(toasts => toasts.filter(toast => {
|
setToasts(toasts => toasts.filter(toast => {
|
||||||
if (toast.id === id) {
|
if (toast.id === id) {
|
||||||
|
@ -65,11 +55,21 @@ export const ToastProvider = ({ children }) => {
|
||||||
// remove toasts with same tag if they are not cancelable
|
// remove toasts with same tag if they are not cancelable
|
||||||
return false
|
return false
|
||||||
}))
|
}))
|
||||||
}, [])
|
}, [setToasts])
|
||||||
|
|
||||||
|
const dispatchToast = useCallback((toast) => {
|
||||||
|
toast = {
|
||||||
|
...toast,
|
||||||
|
createdAt: +new Date(),
|
||||||
|
id: toastId.current++
|
||||||
|
}
|
||||||
|
setToasts(toasts => ensureFlow(toasts, toast).map(mapHidden(toast)))
|
||||||
|
return () => removeToast(toast)
|
||||||
|
}, [setToasts, removeToast])
|
||||||
|
|
||||||
const endFlow = useCallback((flowId) => {
|
const endFlow = useCallback((flowId) => {
|
||||||
setToasts(toasts => toasts.filter(toast => toast.flowId !== flowId))
|
setToasts(toasts => toasts.filter(toast => toast.flowId !== flowId))
|
||||||
}, [])
|
}, [setToasts])
|
||||||
|
|
||||||
const toaster = useMemo(() => ({
|
const toaster = useMemo(() => ({
|
||||||
success: (body, options) => {
|
success: (body, options) => {
|
||||||
|
@ -110,13 +110,13 @@ export const ToastProvider = ({ children }) => {
|
||||||
// Only clear toasts with no cancel function on page navigation
|
// Only clear toasts with no cancel function on page navigation
|
||||||
// since navigation should not interfere with being able to cancel an action.
|
// since navigation should not interfere with being able to cancel an action.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleRouteChangeStart = () => setToasts(toasts => toasts.filter(({ onCancel, onUndo }) => onCancel || onUndo), [])
|
const handleRouteChangeStart = () => setToasts(toasts => toasts.length > 0 ? toasts.filter(({ onCancel, onUndo }) => onCancel || onUndo) : toasts)
|
||||||
router.events.on('routeChangeStart', handleRouteChangeStart)
|
router.events.on('routeChangeStart', handleRouteChangeStart)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
router.events.off('routeChangeStart', handleRouteChangeStart)
|
router.events.off('routeChangeStart', handleRouteChangeStart)
|
||||||
}
|
}
|
||||||
}, [router])
|
}, [router.events, setToasts])
|
||||||
|
|
||||||
// this function merges toasts with the same tag into one toast.
|
// this function merges toasts with the same tag into one toast.
|
||||||
// for example: 3x 'zap pending' -> '(3) zap pending'
|
// for example: 3x 'zap pending' -> '(3) zap pending'
|
||||||
|
|
Loading…
Reference in New Issue