Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix: resolve comments

AbhinRustagib801c1ccea42
diff --git a/src/lib/userPreferences.test.ts b/src/lib/userPreferences.test.ts
new file mode 100644
index 000000000..5ba9fceb1
--- /dev/null
+++ b/src/lib/userPreferences.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from "vitest";
+import { parentDirectoryOf } from "./userPreferences";
+
+describe("parentDirectoryOf", () => {
+ it("returns the directory for a POSIX path", () => {
+ expect(parentDirectoryOf("/Users/me/Movies/clip.mp4")).toBe("/Users/me/Movies");
+ });
+
+ it("returns the directory for a Windows path", () => {
+ expect(parentDirectoryOf("C:\\Users\\me\\Movies\\clip.mp4")).toBe("C:\\Users\\me\\Movies");
+ });
+
+ it("preserves the POSIX root when the file is at /", () => {
+ expect(parentDirectoryOf("/video.mp4")).toBe("/");
+ });
+
+ it("preserves the Windows drive root with its trailing separator", () => {
+ expect(parentDirectoryOf("C:\\video.mp4")).toBe("C:\\");
+ expect(parentDirectoryOf("D:/video.mp4")).toBe("D:/");
+ });
+
+ it("returns null when no separator is present", () => {
+ expect(parentDirectoryOf("video.mp4")).toBeNull();
+ expect(parentDirectoryOf("")).toBeNull();
+ });
+});
diff --git a/src/lib/userPreferences.ts b/src/lib/userPreferences.ts
index 6947da5ef..2c9db6f94 100644
--- a/src/lib/userPreferences.ts
+++ b/src/lib/userPreferences.ts
@@ -89,11 +89,26 @@ export function loadUserPreferences(): UserPreferences {
/**
* Extracts the parent directory from a saved file path. Handles both POSIX
* and Windows separators since the path comes from the OS save dialog.
+ *
+ * Root directories are preserved with their trailing separator so that the
+ * value is still a valid directory path:
+ * "/video.mp4" -> "/"
+ * "C:\\video.mp4" -> "C:\\"
+ *
* Returns null if no separator is found.
*/
export function parentDirectoryOf(filePath: string): string | null {
const lastSep = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
- if (lastSep <= 0) return null;
+ if (lastSep < 0) return null;
+
+ // POSIX root, e.g. "/video.mp4" -> "/"
+ if (lastSep === 0) return filePath[0];
+
+ // Windows drive root, e.g. "C:\\video.mp4" -> "C:\\"
+ if (lastSep === 2 && /^[A-Za-z]:[/\\]/.test(filePath)) {
+ return filePath.slice(0, lastSep + 1);
+ }
+
return filePath.slice(0, lastSep);
}