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