Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

floating-working-control.tsx

264 lines9.4 KB

1import { formatDuration } from "@t3tools/shared/orchestrationTiming";
2import { GlassContainer, GlassView } from "expo-glass-effect";
3import { useEffect, useState } from "react";
4import { ActivityIndicator, Text as SystemText, View } from "react-native";
5import Animated, {
6 Easing,
7 FadeIn,
8 FadeOut,
9 ReduceMotion,
10 useAnimatedStyle,
11 useSharedValue,
12 withTiming,
13} from "react-native-reanimated";
14import { withUniwind } from "uniwind";
15​
16import { AppText as Text } from "../../components/AppText";
17import { SymbolView } from "../../components/AppSymbol";
18import { ControlPill } from "../../components/ControlPill";
19import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
20​
21const CONTROL_HEIGHT = 38.5; // h-11 with the mobile 14px rem
22// The collapsed composer capsule starts 6 below its overlay's top edge, so
23// the pill sits at (gap - 6) above the overlay to leave the same gap to the
24// capsule as the feed's end inset leaves between it and the last row.
25const CONTROL_GAP = 8;
26const COMPOSER_CAPSULE_INSET = 6;
27const GLASS_MERGE_SPACING = 12;
28const CONTROL_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
29const CONTROL_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
30const CONTROL_TIMING = {
31 duration: 240,
32 easing: Easing.out(Easing.cubic),
33 reduceMotion: ReduceMotion.System,
34} as const;
35const CONTROL_SEPARATION = (16 + CONTROL_HEIGHT) / 2;
36​
37// Expo reapplies glass after native layout and window reattachment, when UIKit
38// can otherwise leave the label visible but lose the material behind it.
39const UniwindGlassView = withUniwind(GlassView, {
40 style: { fromClassName: "className" },
41});
42const UniwindGlassContainer = withUniwind(GlassContainer, {
43 style: { fromClassName: "className" },
44});
45const AnimatedGlassView = Animated.createAnimatedComponent(UniwindGlassView);
46​
47const CONTROL_OVERLAY_OFFSET = CONTROL_HEIGHT + CONTROL_GAP - COMPOSER_CAPSULE_INSET;
48export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTROL_GAP;
49​
50/**
51 * What the floating pill says. Syncing and working share one element so the
52 * label swaps in place instead of one pill fading out for another.
53 */
54export type FloatingWorkingStatus =
55 | { readonly kind: "working"; readonly startedAt: string }
56 | { readonly kind: "syncing"; readonly label: string }
57 | { readonly kind: "compacting" };
58​
59export function FloatingWorkingControl(props: {
60 readonly colorScheme: "light" | "dark";
61 readonly status: FloatingWorkingStatus | null;
62 readonly showScrollToEnd: boolean;
63 readonly onScrollToEnd: () => void;
64}) {
65 const separationProgress = useSharedValue(props.showScrollToEnd ? 1 : 0);
66​
67 useEffect(() => {
68 separationProgress.value = withTiming(props.showScrollToEnd ? 1 : 0, CONTROL_TIMING);
69 }, [props.showScrollToEnd, separationProgress]);
70​
71 const timerStyle = useAnimatedStyle(() => ({
72 transform: [{ translateX: CONTROL_SEPARATION * (1 - separationProgress.value) }],
73 }));
74 const arrowTransformStyle = useAnimatedStyle(() => ({
75 transform: [{ translateX: -CONTROL_SEPARATION * (1 - separationProgress.value) }],
76 }));
77 const arrowContentStyle = useAnimatedStyle(() => ({
78 opacity: separationProgress.value,
79 }));
80​
81 if (props.status === null && !props.showScrollToEnd) {
82 return null;
83 }
84​
85 return (
86 <Animated.View
87 pointerEvents="box-none"
88 className="absolute left-0 right-0 z-20 items-center"
89 style={{ top: -CONTROL_OVERLAY_OFFSET }}
90 entering={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_ENTERING}
91 exiting={NATIVE_LIQUID_GLASS_SUPPORTED ? undefined : CONTROL_EXITING}
92 >
93 {props.status !== null && NATIVE_LIQUID_GLASS_SUPPORTED ? (
94 <UniwindGlassContainer
95 spacing={GLASS_MERGE_SPACING}
96 pointerEvents="box-none"
97 className="flex-row items-center gap-4"
98 >
99 <AnimatedGlassView
100 colorScheme={props.colorScheme}
101 glassEffectStyle="regular"
102 pointerEvents="none"
103 className="h-11 justify-center overflow-hidden rounded-full"
104 style={timerStyle}
105 >
106 <FloatingStatusLabel status={props.status} />
107 </AnimatedGlassView>
108​
109 <AnimatedGlassView
110 colorScheme={props.colorScheme}
111 glassEffectStyle="regular"
112 isInteractive
113 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
114 accessibilityElementsHidden={!props.showScrollToEnd}
115 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
116 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
117 style={arrowTransformStyle}
118 >
119 <Animated.View style={arrowContentStyle}>
120 <ScrollToEndButton disabled={!props.showScrollToEnd} onPress={props.onScrollToEnd} />
121 </Animated.View>
122 </AnimatedGlassView>
123 </UniwindGlassContainer>
124 ) : props.status !== null ? (
125 <View pointerEvents="box-none" className="flex-row items-center gap-4">
126 <Animated.View
127 pointerEvents="none"
128 className="h-11 justify-center rounded-full border border-border bg-card shadow-md shadow-black/10"
129 style={timerStyle}
130 >
131 <FloatingStatusLabel status={props.status} />
132 </Animated.View>
133​
134 <Animated.View
135 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
136 accessibilityElementsHidden={!props.showScrollToEnd}
137 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
138 style={[arrowTransformStyle, arrowContentStyle]}
139 >
140 <ControlPill
141 accessibilityLabel="Scroll to end"
142 activateOnPressIn
143 className="h-11 w-11 border border-border bg-card shadow-md shadow-black/10"
144 disabled={!props.showScrollToEnd}
145 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
146 onPress={props.onScrollToEnd}
147 />
148 </Animated.View>
149 </View>
150 ) : NATIVE_LIQUID_GLASS_SUPPORTED ? (
151 <UniwindGlassView
152 colorScheme={props.colorScheme}
153 glassEffectStyle="regular"
154 isInteractive
155 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
156 >
157 <ScrollToEndButton onPress={props.onScrollToEnd} />
158 </UniwindGlassView>
159 ) : (
160 <ControlPill
161 accessibilityLabel="Scroll to end"
162 activateOnPressIn
163 className="h-11 w-11 border border-border bg-card shadow-md shadow-black/10"
164 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
165 onPress={props.onScrollToEnd}
166 />
167 )}
168 </Animated.View>
169 );
171​
172function CompactingLabel() {
173 return (
174 <View
175 accessible
176 accessibilityLabel="Compacting"
177 className="h-11 flex-row items-center gap-1.5 px-4"
178 >
179 <SymbolView
180 name="arrow.down.right.and.arrow.up.left"
181 size={13}
182 tintColorClassName="foreground"
183 type="monochrome"
184 />
185 <Text className="font-t3-medium text-xs text-foreground">Compacting…</Text>
186 </View>
187 );
189​
190function FloatingStatusLabel(props: { readonly status: FloatingWorkingStatus }) {
191 if (props.status.kind === "syncing") {
192 return (
193 <View
194 accessible
195 accessibilityLabel={props.status.label}
196 className="h-11 flex-row items-center gap-2 px-4"
197 >
198 <ActivityIndicator size="small" colorClassName="accent-icon-muted" />
199 <Text className="font-t3-medium text-xs text-foreground">{props.status.label}</Text>
200 </View>
201 );
202 }
203 if (props.status.kind === "compacting") {
204 return <CompactingLabel />;
205 }
206 return <WorkingDuration startedAt={props.status.startedAt} />;
208​
209function WorkingDuration(props: { readonly startedAt: string }) {
210 const [nowMs, setNowMs] = useState(() => Date.now());
211​
212 useEffect(() => {
213 setNowMs(Date.now());
214 const intervalId = setInterval(() => setNowMs(Date.now()), 1_000);
215 return () => clearInterval(intervalId);
216 }, [props.startedAt]);
217​
218 const duration = formatWorkingDuration(props.startedAt, nowMs);
219 const label = `Working for ${duration}`;
220​
221 return (
222 <View accessible accessibilityLabel={label} className="h-11 flex-row items-center px-4">
223 <Text className="font-t3-medium text-xs text-foreground">Working for </Text>
224 <SystemText
225 className="text-xs text-foreground"
226 style={{ fontVariant: ["tabular-nums"], fontWeight: "500" }}
227 >
228 {duration}
229 </SystemText>
230 </View>
231 );
233​
234function formatWorkingDuration(startedAt: string, nowMs: number): string {
235 const startedAtMs = Date.parse(startedAt);
236 if (!Number.isFinite(startedAtMs) || nowMs <= startedAtMs) {
237 return "0s";
238 }
239​
240 const totalSeconds = Math.floor((nowMs - startedAtMs) / 1_000);
241 if (totalSeconds < 60) {
242 return `${totalSeconds}s`;
243 }
244 if (totalSeconds >= 3_600) {
245 return formatDuration(totalSeconds * 1_000);
246 }
247​
248 const minutes = Math.floor(totalSeconds / 60);
249 const seconds = String(totalSeconds % 60).padStart(2, "0");
250 return `${minutes}m ${seconds}s`;
252​
253function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) {
254 return (
255 <ControlPill
256 accessibilityLabel="Scroll to end"
257 activateOnPressIn
258 className="h-11 w-11 bg-transparent"
259 disabled={props.disabled}
260 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
261 onPress={props.onPress}
262 />
263 );