Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(mobile): update CapCard and refresh control components

Richie McIlroy35d83666816e
diff --git a/apps/mobile/src/components/CapCard.test.ts b/apps/mobile/src/components/CapCard.test.ts
index e966ed662..724268d41 100644
--- a/apps/mobile/src/components/CapCard.test.ts
+++ b/apps/mobile/src/components/CapCard.test.ts
@@ -516,9 +516,7 @@ describe("CapCard", () => {
if (!metricsRow) throw new Error("Analytics action was not rendered");
const stopPropagation = vi.fn();
- expect(metricsRow.props.accessibilityHint).toBe(
- "Opens analytics in a browser sheet",
- );
+ expect(metricsRow.props.accessibilityHint).toBe("Opens native analytics");
expect(metricsRow.props.accessibilityState).toEqual({
disabled: false,
});
diff --git a/apps/mobile/src/components/CapCard.tsx b/apps/mobile/src/components/CapCard.tsx
index db68dc164..fe61a439a 100644
--- a/apps/mobile/src/components/CapCard.tsx
+++ b/apps/mobile/src/components/CapCard.tsx
@@ -1,6 +1,6 @@
import { Image } from "expo-image";
import { SymbolView } from "expo-symbols";
-import { useEffect, useRef, useState } from "react";
+import { memo, useEffect, useRef, useState } from "react";
import {
ActivityIndicator,
Pressable,
@@ -15,17 +15,18 @@ import { getCapCardViewModel } from "./capCardViewModel";
type CapCardProps = {
cap: MobileCapSummary;
- onPress: () => void;
- onCopyPress?: () => void;
- onSharePress?: () => void;
- onVisibilityPress?: () => void;
- onAnalyticsPress?: () => void;
- onMenuPress?: () => void;
+ onPress: (cap: MobileCapSummary) => void;
+ onCopyPress?: (cap: MobileCapSummary) => void;
+ onSharePress?: (cap: MobileCapSummary) => void;
+ onVisibilityPress?: (cap: MobileCapSummary) => void;
+ onAnalyticsPress?: (cap: MobileCapSummary) => void;
+ onMenuPress?: (cap: MobileCapSummary) => void;
visibilityBusy?: boolean;
visibilityDisabled?: boolean;
visibilityDisabledHint?: string;
visibilityValue?: string;
visibilityAccessibilityValue?: string;
+ thumbnailAuthorization?: string;
now?: Date;
};
@@ -135,7 +136,7 @@ function UploadProgressIndicator({
);
}
-export function CapCard({
+export const CapCard = memo(function CapCard({
cap,
onPress,
onCopyPress,
@@ -148,6 +149,7 @@ export function CapCard({
visibilityDisabledHint,
visibilityValue,
visibilityAccessibilityValue,
+ thumbnailAuthorization,
now,
}: CapCardProps) {
const viewModel = getCapCardViewModel(cap, now);
@@ -179,7 +181,7 @@ export function CapCard({
const copyLink = () => {
if (!onCopyPress) return;
- onCopyPress();
+ onCopyPress(cap);
setCopyPressed(true);
if (copyResetTimer.current) clearTimeout(copyResetTimer.current);
copyResetTimer.current = setTimeout(() => {
@@ -192,8 +194,8 @@ export function CapCard({
<Pressable
accessibilityRole="button"
accessibilityLabel={viewModel.accessibilityLabel}
- onPress={onPress}
- onLongPress={onMenuPress}
+ onPress={() => onPress(cap)}
+ onLongPress={onMenuPress ? () => onMenuPress(cap) : undefined}
style={({ pressed }) => [styles.card, pressed && styles.pressed]}
>
<View style={styles.thumbnailWrap}>
@@ -230,7 +232,7 @@ export function CapCard({
hitSlop={compactHitSlop}
onPress={(event) => {
event.stopPropagation();
- onSharePress();
+ onSharePress(cap);
}}
style={({ pressed }) => [
styles.actionIconButton,
@@ -253,7 +255,7 @@ export function CapCard({
hitSlop={compactHitSlop}
onPress={(event) => {
event.stopPropagation();
- onMenuPress();
+ onMenuPress(cap);
}}
style={({ pressed }) => [
styles.actionIconButton,
@@ -277,7 +279,13 @@ export function CapCard({
contentFit="cover"
onError={() => setFailedThumbnailUrl(cap.thumbnailUrl)}
recyclingKey={cap.id}
- source={{ uri: cap.thumbnailUrl }}
+ source={{
+ uri: cap.thumbnailUrl,
+ cacheKey: cap.thumbnailCacheKey ?? `cap-thumbnail:${cap.id}`,
+ headers: thumbnailAuthorization
+ ? { Authorization: thumbnailAuthorization }
+ : undefined,
+ }}
style={styles.thumbnail}
transition={160}
/>
@@ -342,7 +350,7 @@ export function CapCard({
hitSlop={compactHitSlop}
onPress={(event) => {
event.stopPropagation();
- onVisibilityPress();
+ onVisibilityPress(cap);
}}
style={({ pressed }) => [
styles.shareStateButton,
@@ -383,13 +391,13 @@ export function CapCard({
accessibilityRole="button"
accessibilityLabel={`View analytics for ${cap.title}`}
accessibilityHint={
- onAnalyticsPress ? "Opens analytics in a browser sheet" : undefined
+ onAnalyticsPress ? "Opens native analytics" : undefined
}
accessibilityState={{ disabled: !onAnalyticsPress }}
disabled={!onAnalyticsPress}
onPress={(event) => {
event.stopPropagation();
- onAnalyticsPress?.();
+ onAnalyticsPress?.(cap);
}}
style={({ pressed }) => [
styles.metricsRow,
@@ -431,7 +439,7 @@ export function CapCard({
</View>
</Pressable>
);
-}
+});
const styles = StyleSheet.create({
card: {
diff --git a/apps/mobile/src/components/CapRefreshControl.test.tsx b/apps/mobile/src/components/CapRefreshControl.test.tsx
index 372a9adf5..617743e3f 100644
--- a/apps/mobile/src/components/CapRefreshControl.test.tsx
+++ b/apps/mobile/src/components/CapRefreshControl.test.tsx
@@ -2,7 +2,7 @@ import type { ReactElement, ReactNode } from "react";
import { RefreshControl } from "react-native";
import TestRenderer, { act, type ReactTestRenderer } from "react-test-renderer";
import { describe, expect, it, vi } from "vitest";
-import { CapRefreshControl } from "./CapRefreshControl";
+import { CapRefreshControl, CapRefreshOverlay } from "./CapRefreshControl";
type HostProps = {
children?: ReactNode;
@@ -31,6 +31,7 @@ vi.mock("react-native", async () => {
React.createElement(name, props, children);
return {
+ Platform: { OS: "ios" },
RefreshControl: createHost("RefreshControl"),
StyleSheet: {
create: <T extends Record<string, unknown>>(styles: T) => styles,
@@ -38,8 +39,28 @@ vi.mock("react-native", async () => {
};
});
+vi.mock("react-native-reanimated", async () => {
+ const React = await import("react");
+ return {
+ default: {
+ View: ({ children, ...props }: HostProps) =>
+ React.createElement("Animated.View", props, children),
+ },
+ FadeIn: { duration: () => ({}) },
+ FadeOut: { duration: () => ({}) },
+ };
+});
+
+vi.mock("./CapLoadingIndicator", async () => {
+ const React = await import("react");
+ return {
+ CapLoadingIndicator: (props: HostProps) =>
+ React.createElement("CapLoadingIndicator", props),
+ };
+});
+
describe("CapRefreshControl", () => {
- it("uses Cap web colors for native pull-to-refresh", async () => {
+ it("hides the native iOS spinner so the Cap overlay can replace it", async () => {
const onRefresh = vi.fn();
const renderer = await renderComponent(
<CapRefreshControl refreshing onRefresh={onRefresh} />,
@@ -47,11 +68,26 @@ describe("CapRefreshControl", () => {
const refreshControl = renderer.root.findByType(RefreshControl);
expect(refreshControl.props).toMatchObject({
- colors: ["#0d74ce"],
onRefresh,
- progressBackgroundColor: "#fcfcfc",
refreshing: true,
- tintColor: "#0d74ce",
+ tintColor: "transparent",
});
});
});
+
+describe("CapRefreshOverlay", () => {
+ it("shows the compact Cap loader while refreshing", async () => {
+ const renderer = await renderComponent(<CapRefreshOverlay refreshing />);
+ const loader = renderer.root.findByProps({ size: 32 });
+
+ expect(loader.props).toMatchObject({ size: 32 });
+ });
+
+ it("renders nothing when not refreshing", async () => {
+ const renderer = await renderComponent(
+ <CapRefreshOverlay refreshing={false} />,
+ );
+
+ expect(renderer.toJSON()).toBeNull();
+ });
+});
diff --git a/apps/mobile/src/components/CapRefreshControl.tsx b/apps/mobile/src/components/CapRefreshControl.tsx
index 6c55078bc..b52371075 100644
--- a/apps/mobile/src/components/CapRefreshControl.tsx
+++ b/apps/mobile/src/components/CapRefreshControl.tsx
@@ -1,11 +1,16 @@
-import { RefreshControl } from "react-native";
+import { Platform, RefreshControl, StyleSheet } from "react-native";
+import Animated, { FadeOut } from "react-native-reanimated";
import { colors } from "@/theme";
+import { CapLoadingIndicator } from "./CapLoadingIndicator";
type CapRefreshControlProps = {
refreshing: boolean;
onRefresh: () => void;
};
+// iOS hides the native spinner so CapRefreshOverlay can show the Cap logo in
+// the pull gap; Android's SwipeRefreshLayout draws its own overlay circle, so
+// it keeps the tinted native control.
export function CapRefreshControl({
refreshing,
onRefresh,
@@ -16,7 +21,37 @@ export function CapRefreshControl({
onRefresh={onRefresh}
progressBackgroundColor={colors.gray1}
refreshing={refreshing}
- tintColor={colors.blue11}
+ tintColor={Platform.OS === "ios" ? "transparent" : colors.blue11}
/>
);
}
+
+type CapRefreshOverlayProps = {
+ refreshing: boolean;
+};
+
+export function CapRefreshOverlay({ refreshing }: CapRefreshOverlayProps) {
+ if (Platform.OS !== "ios" || !refreshing) return null;
+ return (
+ <Animated.View
+ exiting={FadeOut.duration(120)}
+ style={styles.overlay}
+ testID="cap-refresh-overlay"
+ >
+ <CapLoadingIndicator size={32} />
+ </Animated.View>
+ );
+}
+
+const styles = StyleSheet.create({
+ overlay: {
+ alignItems: "center",
+ height: 60,
+ justifyContent: "center",
+ left: 0,
+ pointerEvents: "none",
+ position: "absolute",
+ right: 0,
+ top: 0,
+ },
+});