Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): add importMedia helper for video and image pickers

Richie McIlroy5a4909f18e86
diff --git a/apps/desktop/src/utils/importMedia.ts b/apps/desktop/src/utils/importMedia.ts
new file mode 100644
index 000000000..59401c422
--- /dev/null
+++ b/apps/desktop/src/utils/importMedia.ts
@@ -0,0 +1,97 @@
+import { invoke } from "@tauri-apps/api/core";
+import { getCurrentWindow } from "@tauri-apps/api/window";
+import * as dialog from "@tauri-apps/plugin-dialog";
+import { commands } from "~/utils/tauri";
+
+const videoExtensions = [
+ "mp4",
+ "mov",
+ "avi",
+ "mkv",
+ "webm",
+ "wmv",
+ "m4v",
+ "flv",
+];
+const imageExtensions = [
+ "png",
+ "jpg",
+ "jpeg",
+ "webp",
+ "gif",
+ "bmp",
+ "tif",
+ "tiff",
+];
+
+type ImportOptions = {
+ hideCurrentWindow?: boolean;
+};
+
+const selectedPath = (result: string | string[] | null) =>
+ typeof result === "string" ? result : null;
+
+const maybeHideCurrentWindow = async (options?: ImportOptions) => {
+ if (options?.hideCurrentWindow) await getCurrentWindow().hide();
+};
+
+export const importVideoPath = async (
+ sourcePath: string,
+ options?: ImportOptions,
+) => {
+ const projectPath = await commands.startVideoImport(sourcePath);
+ await commands.showWindow({ Editor: { project_path: projectPath } });
+ await maybeHideCurrentWindow(options);
+ return projectPath;
+};
+
+export const importImagePath = async (
+ sourcePath: string,
+ options?: ImportOptions,
+) => {
+ const imagePath = await invoke<string>("start_image_import", { sourcePath });
+ await commands.showWindow({ ScreenshotEditor: { path: imagePath } });
+ await maybeHideCurrentWindow(options);
+ return imagePath;
+};
+
+export const importVideoFromPicker = async (options?: ImportOptions) => {
+ const result = await dialog.open({
+ filters: [
+ {
+ name: "Video Files",
+ extensions: videoExtensions,
+ },
+ ],
+ multiple: false,
+ });
+ const path = selectedPath(result);
+ if (!path) return null;
+ return await importVideoPath(path, options);
+};
+
+export const importImageFromPicker = async (options?: ImportOptions) => {
+ const result = await dialog.open({
+ filters: [
+ {
+ name: "Image Files",
+ extensions: imageExtensions,
+ },
+ ],
+ multiple: false,
+ });
+ const path = selectedPath(result);
+ if (!path) return null;
+ return await importImagePath(path, options);
+};
+
+export const showImportError = async (
+ mediaType: "video" | "image",
+ error: unknown,
+) => {
+ const message = error instanceof Error ? error.message : String(error);
+ await dialog.message(`Failed to import ${mediaType}: ${message}`, {
+ title: "Import Error",
+ kind: "error",
+ });
+};