Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): add Google Drive storage settings flows

Richie McIlroy7c2c3a3ef1ca
diff --git a/apps/desktop/src/App.tsx b/apps/desktop/src/App.tsx
index 748f75811..7bb178212 100644
--- a/apps/desktop/src/App.tsx
+++ b/apps/desktop/src/App.tsx
@@ -54,6 +54,12 @@ const SettingsIntegrationsPage = lazy(
const SettingsS3ConfigPage = lazy(
() => import("./routes/(window-chrome)/settings/integrations/s3-config"),
);
+const SettingsGoogleDriveConfigPage = lazy(
+ () =>
+ import(
+ "./routes/(window-chrome)/settings/integrations/google-drive-config"
+ ),
+);
const OnboardingPage = lazy(
() => import("./routes/(window-chrome)/onboarding"),
);
@@ -173,6 +179,10 @@ function Inner() {
path="/integrations/s3-config"
component={SettingsS3ConfigPage}
/>
+ <Route
+ path="/integrations/google-drive-config"
+ component={SettingsGoogleDriveConfigPage}
+ />
</Route>
<Route path="/onboarding" component={OnboardingPage} />
<Route path="/upgrade" component={UpgradePage} />
diff --git a/apps/desktop/src/routes/(window-chrome)/settings/integrations/config-header.tsx b/apps/desktop/src/routes/(window-chrome)/settings/integrations/config-header.tsx
new file mode 100644
index 000000000..f46f593cf
--- /dev/null
+++ b/apps/desktop/src/routes/(window-chrome)/settings/integrations/config-header.tsx
@@ -0,0 +1,22 @@
+import { Button } from "@cap/ui-solid";
+import { useNavigate } from "@solidjs/router";
+import IconLucideArrowLeft from "~icons/lucide/arrow-left";
+
+export function IntegrationConfigHeader(props: { title: string }) {
+ const navigate = useNavigate();
+
+ return (
+ <div class="flex shrink-0 justify-between items-center pb-3">
+ <Button
+ variant="gray"
+ size="sm"
+ class="gap-1.5"
+ onClick={() => navigate("/settings/integrations")}
+ >
+ <IconLucideArrowLeft class="size-3.5" />
+ Back
+ </Button>
+ <p class="text-sm font-medium text-gray-12">{props.title}</p>
+ </div>
+ );
+}
diff --git a/apps/desktop/src/routes/(window-chrome)/settings/integrations/google-drive-config.tsx b/apps/desktop/src/routes/(window-chrome)/settings/integrations/google-drive-config.tsx
new file mode 100644
index 000000000..7d75a3c94
--- /dev/null
+++ b/apps/desktop/src/routes/(window-chrome)/settings/integrations/google-drive-config.tsx
@@ -0,0 +1,417 @@
+import { Button } from "@cap/ui-solid";
+import { useMutation } from "@tanstack/solid-query";
+import { createResource, createSignal, Show, Suspense } from "solid-js";
+import { commands } from "~/utils/tauri";
+import { apiClient, protectedHeaders } from "~/utils/web-api";
+import { IntegrationConfigHeader } from "./config-header";
+
+const byteUnits = ["B", "KB", "MB", "GB", "TB", "PB"] as const;
+const googleDriveConnectionPollIntervalMs = 1500;
+const googleDriveConnectionPollTimeoutMs = 120000;
+
+const formatBytes = (value?: string | null) => {
+ if (!value) return null;
+
+ const bytes = Number(value);
+ if (!Number.isFinite(bytes)) return null;
+ if (bytes === 0) return "0 B";
+
+ let size = bytes;
+ let unitIndex = 0;
+ while (size >= 1024 && unitIndex < byteUnits.length - 1) {
+ size /= 1024;
+ unitIndex += 1;
+ }
+
+ const decimals = size >= 10 || unitIndex === 0 ? 0 : 1;
+ return `${size.toFixed(decimals)} ${byteUnits[unitIndex]}`;
+};
+
+const formatTimestamp = (value: string) => {
+ const date = new Date(value);
+ if (Number.isNaN(date.getTime())) return null;
+
+ return new Intl.DateTimeFormat(undefined, {
+ dateStyle: "medium",
+ timeStyle: "short",
+ }).format(date);
+};
+
+const wait = (ms: number) =>
+ new Promise((resolve) => {
+ setTimeout(resolve, ms);
+ });
+
+const fetchStorageIntegrations = async (refreshStorageQuota = false) => {
+ const response = await apiClient.desktop.getStorageIntegrations({
+ query: refreshStorageQuota ? { refreshStorageQuota: true } : undefined,
+ headers: await protectedHeaders(),
+ });
+
+ if (response.status !== 200)
+ throw new Error("Failed to fetch storage integrations");
+
+ return response.body;
+};
+
+const fetchS3Config = async () => {
+ const response = await apiClient.desktop.getS3Config({
+ headers: await protectedHeaders(),
+ });
+
+ if (response.status !== 200) throw new Error("Failed to fetch S3 config");
+
+ return response.body.config;
+};
+
+export default function GoogleDriveConfigPage() {
+ const [isWaitingForConnection, setIsWaitingForConnection] =
+ createSignal(false);
+ const [isRefreshing, setIsRefreshing] = createSignal(false);
+ const [storage, { mutate: setStorage }] = createResource(() =>
+ fetchStorageIntegrations(),
+ );
+
+ const googleDrive = () => storage()?.googleDrive;
+ const storageQuota = () => googleDrive()?.storageQuota ?? null;
+ const isConnected = () => googleDrive()?.connected === true;
+ const isActive = () => storage()?.activeProvider === "googleDrive";
+
+ const [s3Config, { mutate: setS3Config }] = createResource(fetchS3Config);
+
+ const hasS3Config = () => {
+ const config = s3Config();
+ return !!config?.accessKeyId && !!config.bucketName;
+ };
+
+ const updateStorage = async (refreshStorageQuota = false) => {
+ const nextStorage = await fetchStorageIntegrations(refreshStorageQuota);
+ setStorage(nextStorage);
+ return nextStorage;
+ };
+
+ const updateS3Config = async () => {
+ const nextConfig = await fetchS3Config();
+ setS3Config(nextConfig);
+ return nextConfig;
+ };
+
+ const refetch = async () => {
+ setIsRefreshing(true);
+ await Promise.all([updateStorage(true), updateS3Config()]).finally(() => {
+ setIsRefreshing(false);
+ });
+ };
+
+ const quotaUsageLabel = () => {
+ const quota = storageQuota();
+ const usage = formatBytes(quota?.usage);
+ if (!quota || !usage) return null;
+
+ const limit = formatBytes(quota.limit);
+ return limit ? `${usage} of ${limit} used` : `${usage} used`;
+ };
+
+ const quotaUsagePercent = () => {
+ const quota = storageQuota();
+ if (!quota?.limit || !quota.usage) return null;
+
+ const limit = Number(quota.limit);
+ const usage = Number(quota.usage);
+ if (!Number.isFinite(limit) || !Number.isFinite(usage) || limit <= 0)
+ return null;
+
+ return Math.min(Math.max((usage / limit) * 100, 0), 100);
+ };
+
+ const quotaTimestampLabel = () => {
+ const quota = storageQuota();
+ if (!quota) return null;
+
+ const timestamp = formatTimestamp(quota.fetchedAt);
+ if (!timestamp) return null;
+
+ return `${quota.stale ? "Cached" : "Updated"} ${timestamp}`;
+ };
+
+ const waitForGoogleDriveConnection = async () => {
+ setIsWaitingForConnection(true);
+ try {
+ const timeoutAt = Date.now() + googleDriveConnectionPollTimeoutMs;
+ while (Date.now() < timeoutAt) {
+ await wait(googleDriveConnectionPollIntervalMs);
+ const nextStorage = await updateStorage();
+ if (nextStorage?.googleDrive.connected) {
+ await updateS3Config();
+ return;
+ }
+ }
+ await commands.globalMessageDialog(
+ "Finish connecting Google Drive in your browser, then return here and refresh.",
+ );
+ } finally {
+ setIsWaitingForConnection(false);
+ }
+ };
+
+ const connect = useMutation(() => ({
+ mutationFn: async () => {
+ const response = await apiClient.desktop.connectGoogleDriveStorage({
+ body: {},
+ headers: await protectedHeaders(),
+ });
+
+ if (response.status === 403) {
+ await commands.showWindow("Upgrade");
+ return null;
+ }
+
+ if (response.status !== 200)
+ throw new Error("Failed to start Google Drive connection");
+
+ await commands.openExternalLink(response.body.url);
+ return response.body;
+ },
+ onSuccess: (body) => {
+ if (!body) return;
+ waitForGoogleDriveConnection().catch((error) => {
+ console.error("Failed to wait for Google Drive connection:", error);
+ });
+ },
+ }));
+
+ const testConnection = useMutation(() => ({
+ mutationFn: async () => {
+ const response = await apiClient.desktop.testGoogleDriveStorage({
+ body: {},
+ headers: await protectedHeaders(),
+ });
+
+ if (response.status !== 200)
+ throw new Error("Google Drive connection test failed");
+
+ return response.body;
+ },
+ onSuccess: async (body) => {
+ await commands.globalMessageDialog(
+ body.email
+ ? `Google Drive connection is working for ${body.email}`
+ : "Google Drive connection is working",
+ );
+ },
+ }));
+
+ const setActive = useMutation(() => ({
+ mutationFn: async (provider: "s3" | "googleDrive") => {
+ const response = await apiClient.desktop.setActiveStorageProvider({
+ body: { provider },
+ headers: await protectedHeaders(),
+ });
+
+ if (response.status !== 200)
+ throw new Error("Failed to update active storage provider");
+
+ return response.body;
+ },
+ onSuccess: async () => {
+ await refetch();
+ },
+ }));
+
+ const disconnect = useMutation(() => ({
+ mutationFn: async () => {
+ const response = await apiClient.desktop.disconnectGoogleDriveStorage({
+ headers: await protectedHeaders(),
+ });
+
+ if (response.status !== 200)
+ throw new Error("Failed to disconnect Google Drive");
+
+ return response.body;
+ },
+ onSuccess: async () => {
+ await refetch();
+ await commands.globalMessageDialog("Google Drive disconnected");
+ },
+ }));
+
+ const busy = () =>
+ storage.loading ||
+ s3Config.loading ||
+ isRefreshing() ||
+ connect.isPending ||
+ isWaitingForConnection() ||
+ testConnection.isPending ||
+ setActive.isPending ||
+ disconnect.isPending;
+
+ return (
+ <div class="flex flex-col p-4 h-full">
+ <IntegrationConfigHeader title="Google Drive" />
+ <div class="rounded-xl border bg-gray-2 border-gray-4 custom-scroll">
+ <div class="flex-1">
+ <Suspense
+ fallback={
+ <div class="flex justify-center items-center w-full h-screen">
+ <IconCapLogo class="animate-spin size-16" />
+ </div>
+ }
+ >
+ <div class="p-4 space-y-4 animate-in fade-in">
+ <div class="pb-4 border-b border-gray-3">
+ <p class="text-sm text-gray-11">
+ Google Drive stores new uploads in a private Cap folder in
+ your Drive. Existing Cap-hosted and S3 videos keep using their
+ current storage.
+ </p>
+ </div>
+
+ <div class="space-y-3">
+ <div class="flex justify-between items-start gap-4">
+ <div>
+ <p class="text-sm font-medium text-gray-12">
+ {isConnected()
+ ? googleDrive()?.displayName
+ : "Google Drive"}
+ </p>
+ <p class="text-[13px] text-gray-10">
+ {isConnected()
+ ? isActive()
+ ? "Active for new uploads"
+ : "Connected but not active"
+ : "Not connected"}
+ </p>
+ </div>
+ <Button
+ variant="gray"
+ disabled={busy()}
+ onClick={() => refetch()}
+ >
+ {isRefreshing() ? "Refreshing..." : "Refresh"}
+ </Button>
+ </div>
+
+ <Show
+ when={isConnected()}
+ fallback={
+ <Button
+ variant="primary"
+ disabled={busy()}
+ onClick={() => connect.mutate()}
+ >
+ {isWaitingForConnection()
+ ? "Waiting..."
+ : connect.isPending
+ ? "Opening..."
+ : "Connect Google Drive"}
+ </Button>
+ }
+ >
+ <Show when={storageQuota()}>
+ <div class="pt-3 space-y-2 border-t border-gray-3">
+ <div class="flex justify-between items-start gap-4">
+ <div>
+ <p class="text-[13px] font-medium text-gray-12">
+ Storage
+ </p>
+ <Show when={quotaUsageLabel()}>
+ {(label) => (
+ <p class="text-[13px] text-gray-10">{label()}</p>
+ )}
+ </Show>
+ </div>
+ <Show when={quotaTimestampLabel()}>
+ {(label) => (
+ <p class="text-[12px] text-gray-9 text-right">
+ {label()}
+ </p>
+ )}
+ </Show>
+ </div>
+ <Show when={quotaUsagePercent() !== null}>
+ <div class="overflow-hidden h-1.5 rounded-full bg-gray-4">
+ <div
+ class="h-full rounded-full bg-blue-9"
+ style={{
+ width: `${quotaUsagePercent() ?? 0}%`,
+ }}
+ />
+ </div>
+ </Show>
+ <div class="grid grid-cols-2 gap-x-4 gap-y-1 text-[12px]">
+ <Show when={formatBytes(storageQuota()?.remaining)}>
+ {(remaining) => (
+ <>
+ <p class="text-gray-10">Remaining</p>
+ <p class="text-right text-gray-11">
+ {remaining()}
+ </p>
+ </>
+ )}
+ </Show>
+ <Show when={formatBytes(storageQuota()?.usageInDrive)}>
+ {(usageInDrive) => (
+ <>
+ <p class="text-gray-10">Drive files</p>
+ <p class="text-right text-gray-11">
+ {usageInDrive()}
+ </p>
+ </>
+ )}
+ </Show>
+ <Show
+ when={formatBytes(storageQuota()?.usageInDriveTrash)}
+ >
+ {(usageInDriveTrash) => (
+ <>
+ <p class="text-gray-10">Trash</p>
+ <p class="text-right text-gray-11">
+ {usageInDriveTrash()}
+ </p>
+ </>
+ )}
+ </Show>
+ </div>
+ </div>
+ </Show>
+ <div class="flex flex-wrap gap-2">
+ <Button
+ variant="primary"
+ disabled={busy() || isActive()}
+ onClick={() => setActive.mutate("googleDrive")}
+ >
+ {isActive() ? "Active" : "Use Google Drive"}
+ </Button>
+ <Show when={hasS3Config()}>
+ <Button
+ variant="gray"
+ disabled={busy() || !isActive()}
+ onClick={() => setActive.mutate("s3")}
+ >
+ Use S3
+ </Button>
+ </Show>
+ <Button
+ variant="gray"
+ disabled={busy()}
+ onClick={() => testConnection.mutate()}
+ >
+ {testConnection.isPending ? "Testing..." : "Test"}
+ </Button>
+ <Button
+ variant="destructive"
+ disabled={busy()}
+ onClick={() => disconnect.mutate()}
+ >
+ Disconnect
+ </Button>
+ </div>
+ </Show>
+ </div>
+ </div>
+ </Suspense>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/apps/desktop/src/routes/(window-chrome)/settings/integrations/index.tsx b/apps/desktop/src/routes/(window-chrome)/settings/integrations/index.tsx
index 491e2752b..41e94bfb5 100644
--- a/apps/desktop/src/routes/(window-chrome)/settings/integrations/index.tsx
+++ b/apps/desktop/src/routes/(window-chrome)/settings/integrations/index.tsx
@@ -7,6 +7,40 @@ import "@total-typescript/ts-reset/filter-boolean";
import { authStore } from "~/store";
import { commands } from "~/utils/tauri";
+const GoogleDriveIcon = (props: { class?: string }) => (
+ <svg
+ class={props.class}
+ viewBox="0 0 87.3 78"
+ xmlns="http://www.w3.org/2000/svg"
+ aria-hidden="true"
+ >
+ <path
+ d="m6.6 66.85 3.85 6.65c.8 1.4 1.95 2.5 3.3 3.3l13.75-23.8h-27.5c0 1.55.4 3.1 1.2 4.5z"
+ fill="#0066da"
+ />
+ <path
+ d="m43.65 25-13.75-23.8c-1.35.8-2.5 1.9-3.3 3.3l-25.4 44a9.06 9.06 0 0 0 -1.2 4.5h27.5z"
+ fill="#00ac47"
+ />
+ <path
+ d="m73.55 76.8c1.35-.8 2.5-1.9 3.3-3.3l1.6-2.75 7.65-13.25c.8-1.4 1.2-2.95 1.2-4.5h-27.502l5.852 11.5z"
+ fill="#ea4335"
+ />
+ <path
+ d="m43.65 25 13.75-23.8c-1.35-.8-2.9-1.2-4.5-1.2h-18.5c-1.6 0-3.15.45-4.5 1.2z"
+ fill="#00832d"
+ />
+ <path
+ d="m59.8 53h-32.3l-13.75 23.8c1.35.8 2.9 1.2 4.5 1.2h50.8c1.6 0 3.15-.45 4.5-1.2z"
+ fill="#2684fc"
+ />
+ <path
+ d="m73.4 26.5-12.7-22c-.8-1.4-1.95-2.5-3.3-3.3l-13.75 23.8 16.15 28h27.45c0-1.55-.4-3.1-1.2-4.5z"
+ fill="#ffba00"
+ />
+ </svg>
+);
+
export default function AppsTab() {
const navigate = useNavigate();
const auth = authStore.createQuery();
@@ -18,6 +52,14 @@ export default function AppsTab() {
});
const apps = [
+ {
+ name: "Google Drive",
+ description:
+ "Connect Google Drive for new shareable link uploads. Cap stores new videos in a private Cap folder in your Drive and continues serving them through Cap after normal access checks.",
+ icon: GoogleDriveIcon,
+ url: "/settings/integrations/google-drive-config",
+ pro: true,
+ },
{
name: "S3 Config",
description:
diff --git a/apps/desktop/src/routes/(window-chrome)/settings/integrations/s3-config.tsx b/apps/desktop/src/routes/(window-chrome)/settings/integrations/s3-config.tsx
index 70a6849d3..36e1fe44e 100644
--- a/apps/desktop/src/routes/(window-chrome)/settings/integrations/s3-config.tsx
+++ b/apps/desktop/src/routes/(window-chrome)/settings/integrations/s3-config.tsx
@@ -5,6 +5,7 @@ import { createResource, Suspense } from "solid-js";
import { Input } from "~/routes/editor/ui";
import { commands } from "~/utils/tauri";
import { apiClient, protectedHeaders } from "~/utils/web-api";
+import { IntegrationConfigHeader } from "./config-header";
interface S3Config {
provider: string;
@@ -74,7 +75,7 @@ export default function S3ConfigPage() {
const testConfig = useMutation(() => ({
mutationFn: async (config: S3Config) => {
const controller = new AbortController();
- const timeoutId = setTimeout(() => controller.abort(), 5500); // 5.5s timeout (slightly longer than backend)
+ const timeoutId = setTimeout(() => controller.abort(), 5500);
try {
const response = await apiClient.desktop.testS3Config({
@@ -144,6 +145,7 @@ export default function S3ConfigPage() {
return (
<div class="flex flex-col p-4 h-full">
+ <IntegrationConfigHeader title="S3 Config" />
<div class="rounded-xl border bg-gray-2 border-gray-4 custom-scroll">
<div class="flex-1">
<Suspense