Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(mobile): add loom import screen

Richie McIlroy02bd826d5737
diff --git a/apps/mobile/app/loom-import.tsx b/apps/mobile/app/loom-import.tsx
new file mode 100644
index 000000000..8b41a88cf
--- /dev/null
+++ b/apps/mobile/app/loom-import.tsx
@@ -0,0 +1,316 @@
+import * as Clipboard from "expo-clipboard";
+import { router, Stack } from "expo-router";
+import { SymbolView } from "expo-symbols";
+import { useState } from "react";
+import { Pressable, StyleSheet, Text, TextInput, View } from "react-native";
+import { MobileApiError } from "@/api/mobile";
+import { useAuth } from "@/auth/AuthContext";
+import { SignInPanel } from "@/auth/SignInPanel";
+import { ActionButton } from "@/components/ActionButton";
+import { GlassSurface } from "@/components/GlassSurface";
+import { Screen } from "@/components/Screen";
+import { colors, fonts, radius, squircle } from "@/theme";
+
+export const isLoomShareUrl = (value: string) => {
+ try {
+ const url = new URL(value.trim());
+ const hostnameValid =
+ url.hostname === "loom.com" || url.hostname.endsWith(".loom.com");
+ const videoId = url.pathname.split("/").filter(Boolean).at(-1);
+ return (
+ url.protocol === "https:" &&
+ hostnameValid &&
+ Boolean(videoId && videoId.length >= 10)
+ );
+ } catch {
+ return false;
+ }
+};
+
+const importErrorMessage = (error: unknown) => {
+ if (error instanceof MobileApiError) {
+ if (error.status === 403) {
+ return "Loom import is available on Cap Pro. Your current plan does not include this feature.";
+ }
+ if (error.status === 400) {
+ return "That Loom video could not be imported. It may be private, already imported, or temporarily unavailable.";
+ }
+ }
+ return "The import could not start. Check your connection and try again.";
+};
+
+export default function LoomImportScreen() {
+ const auth = useAuth();
+ const [loomUrl, setLoomUrl] = useState("");
+ const [submitting, setSubmitting] = useState(false);
+ const [error, setError] = useState<string | null>(null);
+ const valid = isLoomShareUrl(loomUrl);
+
+ const paste = async () => {
+ try {
+ const value = await Clipboard.getStringAsync();
+ setLoomUrl(value.trim());
+ setError(null);
+ } catch {
+ setError(
+ "The clipboard could not be read. Paste the Loom link manually.",
+ );
+ }
+ };
+
+ const submit = async () => {
+ if (auth.status !== "signedIn" || submitting) return;
+ if (!valid) {
+ setError(
+ "Paste a valid Loom share link that starts with https://loom.com.",
+ );
+ return;
+ }
+
+ setSubmitting(true);
+ setError(null);
+ try {
+ const result = await auth.client.importLoom(loomUrl.trim());
+ void auth.refresh().catch(() => undefined);
+ router.replace({ pathname: "/caps/[id]", params: { id: result.id } });
+ } catch (cause) {
+ setError(importErrorMessage(cause));
+ } finally {
+ setSubmitting(false);
+ }
+ };
+
+ if (auth.status === "signedOut") {
+ return (
+ <Screen scroll safeEdges={["left", "right", "bottom"]}>
+ <SignInPanel title="Sign in to import from Loom" />
+ </Screen>
+ );
+ }
+
+ return (
+ <>
+ <Stack.Screen
+ options={{
+ headerShown: true,
+ title: "Import from Loom",
+ headerBackTitle: "Back",
+ headerShadowVisible: false,
+ headerStyle: { backgroundColor: colors.appBackground },
+ }}
+ />
+ <Screen
+ automaticallyAdjustKeyboardInsets
+ scroll
+ safeEdges={["left", "right", "bottom"]}
+ >
+ <View style={styles.heading}>
+ <View style={styles.icon}>
+ <SymbolView name="arrow.down.doc" size={28} tintColor="#625DF5" />
+ </View>
+ <Text style={styles.title}>Bring a Loom video into Cap</Text>
+ <Text style={styles.subtitle}>
+ Paste a public Loom share link. The transfer continues on Cap’s
+ servers, so the video never downloads to your phone.
+ </Text>
+ </View>
+ <GlassSurface
+ fallbackStyle={styles.cardFallback}
+ style={styles.card}
+ tintColor={colors.gray1}
+ >
+ <Text style={styles.label}>Loom share link</Text>
+ <View style={styles.inputRow}>
+ <TextInput
+ accessibilityLabel="Loom share link"
+ autoCapitalize="none"
+ autoCorrect={false}
+ editable={!submitting}
+ keyboardType="url"
+ onChangeText={(value) => {
+ setLoomUrl(value);
+ setError(null);
+ }}
+ onSubmitEditing={() => void submit()}
+ placeholder="https://www.loom.com/share/..."
+ placeholderTextColor={colors.gray8}
+ returnKeyType="go"
+ style={styles.input}
+ value={loomUrl}
+ />
+ <Pressable
+ accessibilityRole="button"
+ accessibilityLabel="Paste Loom link"
+ disabled={submitting}
+ onPress={() => void paste()}
+ style={({ pressed }) => [
+ styles.pasteButton,
+ pressed && styles.pasteButtonPressed,
+ ]}
+ >
+ <Text style={styles.pasteLabel}>Paste</Text>
+ </Pressable>
+ </View>
+ <View style={styles.detailRow}>
+ <SymbolView
+ name="checkmark.shield"
+ size={17}
+ tintColor={colors.green9}
+ />
+ <Text style={styles.detailText}>
+ Your original Loom video is left unchanged.
+ </Text>
+ </View>
+ <View style={styles.detailRow}>
+ <SymbolView name="bolt" size={17} tintColor={colors.blue9} />
+ <Text style={styles.detailText}>
+ Processing begins in the background as soon as the import starts.
+ </Text>
+ </View>
+ </GlassSurface>
+ {error ? (
+ <View accessibilityLiveRegion="polite" style={styles.errorCard}>
+ <SymbolView
+ name="exclamationmark.triangle"
+ size={18}
+ tintColor={colors.red9}
+ />
+ <Text style={styles.errorText}>{error}</Text>
+ </View>
+ ) : null}
+ <ActionButton
+ disabled={!valid}
+ label={submitting ? "Starting import" : "Import video"}
+ loading={submitting}
+ onPress={() => void submit()}
+ style={styles.importButton}
+ variant="blue"
+ />
+ <Text style={styles.proNote}>Loom import requires Cap Pro.</Text>
+ </Screen>
+ </>
+ );
+}
+
+const styles = StyleSheet.create({
+ heading: {
+ alignItems: "center",
+ paddingTop: 22,
+ paddingBottom: 24,
+ paddingHorizontal: 12,
+ },
+ icon: {
+ width: 58,
+ height: 58,
+ alignItems: "center",
+ justifyContent: "center",
+ borderRadius: radius.lg,
+ backgroundColor: "#EFEEFF",
+ marginBottom: 16,
+ ...squircle,
+ },
+ title: {
+ fontFamily: fonts.medium,
+ fontSize: 24,
+ lineHeight: 30,
+ color: colors.gray12,
+ textAlign: "center",
+ },
+ subtitle: {
+ fontFamily: fonts.regular,
+ fontSize: 14,
+ lineHeight: 20,
+ color: colors.gray10,
+ textAlign: "center",
+ marginTop: 8,
+ },
+ card: {
+ borderRadius: radius.lg,
+ borderWidth: StyleSheet.hairlineWidth,
+ borderColor: colors.gray4,
+ padding: 16,
+ gap: 14,
+ ...squircle,
+ },
+ cardFallback: {
+ backgroundColor: colors.gray1,
+ },
+ label: {
+ fontFamily: fonts.medium,
+ fontSize: 14,
+ color: colors.gray12,
+ },
+ inputRow: {
+ flexDirection: "row",
+ alignItems: "center",
+ gap: 8,
+ },
+ input: {
+ flex: 1,
+ height: 48,
+ borderRadius: radius.md,
+ borderWidth: StyleSheet.hairlineWidth,
+ borderColor: colors.gray5,
+ backgroundColor: colors.white,
+ paddingHorizontal: 13,
+ fontFamily: fonts.regular,
+ fontSize: 16,
+ color: colors.gray12,
+ ...squircle,
+ },
+ pasteButton: {
+ height: 48,
+ justifyContent: "center",
+ paddingHorizontal: 14,
+ borderRadius: radius.md,
+ backgroundColor: colors.gray3,
+ ...squircle,
+ },
+ pasteButtonPressed: {
+ backgroundColor: colors.gray5,
+ },
+ pasteLabel: {
+ fontFamily: fonts.medium,
+ fontSize: 14,
+ color: colors.gray12,
+ },
+ detailRow: {
+ flexDirection: "row",
+ alignItems: "flex-start",
+ gap: 9,
+ },
+ detailText: {
+ flex: 1,
+ fontFamily: fonts.regular,
+ fontSize: 13,
+ lineHeight: 18,
+ color: colors.gray10,
+ },
+ errorCard: {
+ flexDirection: "row",
+ alignItems: "flex-start",
+ gap: 10,
+ padding: 14,
+ borderRadius: radius.md,
+ backgroundColor: colors.red3,
+ marginTop: 14,
+ ...squircle,
+ },
+ errorText: {
+ flex: 1,
+ fontFamily: fonts.regular,
+ fontSize: 14,
+ lineHeight: 20,
+ color: colors.red11,
+ },
+ importButton: {
+ marginTop: 18,
+ },
+ proNote: {
+ fontFamily: fonts.regular,
+ fontSize: 12,
+ color: colors.gray9,
+ textAlign: "center",
+ marginTop: 10,
+ },
+});
diff --git a/apps/mobile/src/screens/loom-import.test.tsx b/apps/mobile/src/screens/loom-import.test.tsx
new file mode 100644
index 000000000..2a3fca916
--- /dev/null
+++ b/apps/mobile/src/screens/loom-import.test.tsx
@@ -0,0 +1,235 @@
+import React, { type ReactElement, type ReactNode } from "react";
+import TestRenderer, { act, type ReactTestRenderer } from "react-test-renderer";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import LoomImportScreen, { isLoomShareUrl } from "../../app/loom-import";
+
+type HostProps = {
+ children?: ReactNode;
+ [key: string]: unknown;
+};
+
+const auth = vi.hoisted(() => ({
+ value: {
+ status: "signedIn" as const,
+ client: { importLoom: vi.fn() },
+ refresh: vi.fn(),
+ },
+}));
+
+(
+ globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }
+).IS_REACT_ACT_ENVIRONMENT = true;
+
+const renderComponent = async (
+ node: ReactElement,
+): Promise<ReactTestRenderer> => {
+ let renderer: ReactTestRenderer | null = null;
+ await act(async () => {
+ renderer = TestRenderer.create(node);
+ });
+ return renderer as unknown as ReactTestRenderer;
+};
+
+vi.mock("react-native", async () => {
+ const React = await import("react");
+ const createHost =
+ (name: string) =>
+ ({ children, ...props }: HostProps) =>
+ React.createElement(name, props, children);
+ return {
+ Pressable: createHost("Pressable"),
+ StyleSheet: {
+ create: <T extends Record<string, unknown>>(styles: T) => styles,
+ hairlineWidth: 1,
+ },
+ Text: createHost("Text"),
+ TextInput: createHost("TextInput"),
+ View: createHost("View"),
+ };
+});
+
+vi.mock("expo-clipboard", () => ({
+ getStringAsync: vi.fn(() => Promise.resolve("")),
+}));
+
+vi.mock("expo-router", async () => {
+ const React = await import("react");
+ return {
+ router: { replace: vi.fn() },
+ Stack: {
+ Screen: (props: HostProps) => React.createElement("StackScreen", props),
+ },
+ };
+});
+
+vi.mock("expo-symbols", async () => {
+ const React = await import("react");
+ return {
+ SymbolView: (props: HostProps) => React.createElement("SymbolView", props),
+ };
+});
+
+vi.mock("@/api/mobile", () => ({
+ MobileApiError: class MobileApiError extends Error {
+ constructor(
+ message: string,
+ readonly status: number,
+ ) {
+ super(message);
+ }
+ },
+}));
+
+vi.mock("@/auth/AuthContext", () => ({
+ useAuth: () => auth.value,
+}));
+
+vi.mock("@/auth/SignInPanel", async () => {
+ const React = await import("react");
+ return { SignInPanel: () => React.createElement("SignInPanel") };
+});
+
+vi.mock("@/components/ActionButton", async () => {
+ const React = await import("react");
+ return {
+ ActionButton: ({ label, ...props }: HostProps & { label: string }) =>
+ React.createElement("ActionButton", {
+ ...props,
+ accessibilityLabel: label,
+ }),
+ };
+});
+
+vi.mock("@/components/GlassSurface", async () => {
+ const React = await import("react");
+ return {
+ GlassSurface: ({ children }: HostProps) =>
+ React.createElement("GlassSurface", null, children),
+ };
+});
+
+vi.mock("@/components/Screen", async () => {
+ const React = await import("react");
+ return {
+ Screen: ({ children }: HostProps) =>
+ React.createElement("Screen", null, children),
+ };
+});
+
+describe("LoomImportScreen", () => {
+ beforeEach(() => {
+ auth.value.client.importLoom.mockReset();
+ auth.value.client.importLoom.mockResolvedValue({
+ id: "video_456",
+ shareUrl: "https://cap.so/s/video_456",
+ });
+ auth.value.refresh.mockReset();
+ auth.value.refresh.mockResolvedValue(undefined);
+ });
+
+ it("accepts only genuine HTTPS Loom video links", () => {
+ expect(isLoomShareUrl("https://www.loom.com/share/abcdefghij")).toBe(true);
+ expect(isLoomShareUrl("https://loom.com.evil/share/abcdefghij")).toBe(
+ false,
+ );
+ expect(isLoomShareUrl("https://www.loom.com/share/short")).toBe(false);
+ });
+
+ it("starts the server import and opens the new Cap", async () => {
+ const renderer = await renderComponent(
+ React.createElement(LoomImportScreen),
+ );
+ const [input] = renderer.root.findAllByProps({
+ accessibilityLabel: "Loom share link",
+ });
+ if (!input) throw new Error("Loom input was not rendered");
+
+ await act(async () => {
+ input.props.onChangeText("https://www.loom.com/share/abcdefghij");
+ });
+ const [button] = renderer.root.findAllByProps({
+ accessibilityLabel: "Import video",
+ });
+ if (!button) throw new Error("Import button was not rendered");
+
+ await act(async () => {
+ button.props.onPress();
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ const { router } = await import("expo-router");
+ expect(auth.value.client.importLoom).toHaveBeenCalledWith(
+ "https://www.loom.com/share/abcdefghij",
+ );
+ expect(router.replace).toHaveBeenCalledWith({
+ pathname: "/caps/[id]",
+ params: { id: "video_456" },
+ });
+ });
+
+ it("opens the imported Cap even when account refresh fails", async () => {
+ auth.value.refresh.mockRejectedValueOnce(new Error("Refresh unavailable"));
+ const renderer = await renderComponent(
+ React.createElement(LoomImportScreen),
+ );
+ const [input] = renderer.root.findAllByProps({
+ accessibilityLabel: "Loom share link",
+ });
+ if (!input) throw new Error("Loom input was not rendered");
+
+ await act(async () => {
+ input.props.onChangeText("https://www.loom.com/share/abcdefghij");
+ });
+ const [button] = renderer.root.findAllByProps({
+ accessibilityLabel: "Import video",
+ });
+ if (!button) throw new Error("Import button was not rendered");
+
+ await act(async () => {
+ button.props.onPress();
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ const { router } = await import("expo-router");
+ expect(router.replace).toHaveBeenCalledWith({
+ pathname: "/caps/[id]",
+ params: { id: "video_456" },
+ });
+ });
+
+ it("describes Pro access without an external purchase call to action", async () => {
+ const { MobileApiError } = await import("@/api/mobile");
+ auth.value.client.importLoom.mockRejectedValueOnce(
+ new MobileApiError("Forbidden", 403, null),
+ );
+ const renderer = await renderComponent(
+ React.createElement(LoomImportScreen),
+ );
+ const [input] = renderer.root.findAllByProps({
+ accessibilityLabel: "Loom share link",
+ });
+ if (!input) throw new Error("Loom input was not rendered");
+
+ await act(async () => {
+ input.props.onChangeText("https://www.loom.com/share/abcdefghij");
+ });
+ const [button] = renderer.root.findAllByProps({
+ accessibilityLabel: "Import video",
+ });
+ if (!button) throw new Error("Import button was not rendered");
+
+ await act(async () => {
+ button.props.onPress();
+ await Promise.resolve();
+ await Promise.resolve();
+ });
+
+ const text = JSON.stringify(renderer.toJSON());
+ expect(text).toContain(
+ "Loom import is available on Cap Pro. Your current plan does not include this feature.",
+ );
+ expect(text).not.toContain("Upgrade on the web");
+ });
+});