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

floating-working-control.tsx

255 lines8.9 KB

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