mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React, {useCallback, useEffect} from 'react'
2import EventEmitter from 'eventemitter3'
3import {ScrollProvider} from '#/lib/ScrollContext'
4import {NativeScrollEvent} from 'react-native'
5import {useSetMinimalShellMode, useMinimalShellMode} from '#/state/shell'
6import {useShellLayout} from '#/state/shell/shell-layout'
7import {isNative, isWeb} from 'platform/detection'
8import {useSharedValue, interpolate} from 'react-native-reanimated'
9
10const WEB_HIDE_SHELL_THRESHOLD = 200
11
12function clamp(num: number, min: number, max: number) {
13 'worklet'
14 return Math.min(Math.max(num, min), max)
15}
16
17export function MainScrollProvider({children}: {children: React.ReactNode}) {
18 const {headerHeight} = useShellLayout()
19 const mode = useMinimalShellMode()
20 const setMode = useSetMinimalShellMode()
21 const startDragOffset = useSharedValue<number | null>(null)
22 const startMode = useSharedValue<number | null>(null)
23 const didJustRestoreScroll = useSharedValue<boolean>(false)
24
25 useEffect(() => {
26 if (isWeb) {
27 return listenToForcedWindowScroll(() => {
28 startDragOffset.value = null
29 startMode.value = null
30 didJustRestoreScroll.value = true
31 })
32 }
33 })
34
35 const onBeginDrag = useCallback(
36 (e: NativeScrollEvent) => {
37 'worklet'
38 if (isNative) {
39 startDragOffset.value = e.contentOffset.y
40 startMode.value = mode.value
41 }
42 },
43 [mode, startDragOffset, startMode],
44 )
45
46 const onEndDrag = useCallback(
47 (e: NativeScrollEvent) => {
48 'worklet'
49 if (isNative) {
50 startDragOffset.value = null
51 startMode.value = null
52 if (e.contentOffset.y < headerHeight.value / 2) {
53 // If we're close to the top, show the shell.
54 setMode(false)
55 } else {
56 // Snap to whichever state is the closest.
57 setMode(Math.round(mode.value) === 1)
58 }
59 }
60 },
61 [startDragOffset, startMode, setMode, mode, headerHeight],
62 )
63
64 const onScroll = useCallback(
65 (e: NativeScrollEvent) => {
66 'worklet'
67 if (isNative) {
68 if (startDragOffset.value === null || startMode.value === null) {
69 if (mode.value !== 0 && e.contentOffset.y < headerHeight.value) {
70 // If we're close enough to the top, always show the shell.
71 // Even if we're not dragging.
72 setMode(false)
73 }
74 return
75 }
76
77 // The "mode" value is always between 0 and 1.
78 // Figure out how much to move it based on the current dragged distance.
79 const dy = e.contentOffset.y - startDragOffset.value
80 const dProgress = interpolate(
81 dy,
82 [-headerHeight.value, headerHeight.value],
83 [-1, 1],
84 )
85 const newValue = clamp(startMode.value + dProgress, 0, 1)
86 if (newValue !== mode.value) {
87 // Manually adjust the value. This won't be (and shouldn't be) animated.
88 mode.value = newValue
89 }
90 } else {
91 if (didJustRestoreScroll.value) {
92 didJustRestoreScroll.value = false
93 // Don't hide/show navbar based on scroll restoratoin.
94 return
95 }
96 // On the web, we don't try to follow the drag because we don't know when it ends.
97 // Instead, show/hide immediately based on whether we're scrolling up or down.
98 const dy = e.contentOffset.y - (startDragOffset.value ?? 0)
99 startDragOffset.value = e.contentOffset.y
100
101 if (dy < 0 || e.contentOffset.y < WEB_HIDE_SHELL_THRESHOLD) {
102 setMode(false)
103 } else if (dy > 0) {
104 setMode(true)
105 }
106 }
107 },
108 [
109 headerHeight,
110 mode,
111 setMode,
112 startDragOffset,
113 startMode,
114 didJustRestoreScroll,
115 ],
116 )
117
118 return (
119 <ScrollProvider
120 onBeginDrag={onBeginDrag}
121 onEndDrag={onEndDrag}
122 onScroll={onScroll}>
123 {children}
124 </ScrollProvider>
125 )
126}
127
128const emitter = new EventEmitter()
129
130if (isWeb) {
131 const originalScroll = window.scroll
132 window.scroll = function () {
133 emitter.emit('forced-scroll')
134 return originalScroll.apply(this, arguments as any)
135 }
136
137 const originalScrollTo = window.scrollTo
138 window.scrollTo = function () {
139 emitter.emit('forced-scroll')
140 return originalScrollTo.apply(this, arguments as any)
141 }
142}
143
144function listenToForcedWindowScroll(listener: () => void) {
145 emitter.addListener('forced-scroll', listener)
146 return () => {
147 emitter.removeListener('forced-scroll', listener)
148 }
149}