Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
web: natural-sort folders and expose createdAt
Richie McIlroy60a6548c550d
diff --git a/apps/web/__tests__/unit/folder-sort.test.ts b/apps/web/__tests__/unit/folder-sort.test.tsnew file mode 100644index 000000000..94260a2e4--- /dev/null+++ b/apps/web/__tests__/unit/folder-sort.test.ts@@ -0,0 +1,103 @@+import { describe, expect, it } from "vitest";+import { isFolderSort, sortFolders } from "@/lib/folder-sort";++const folder = (id: string, name: string, createdAt: string) => ({+ id,+ name,+ createdAt: new Date(createdAt),+});++// The exact shape from the customer report: numbered folders that came back+// in primary-key order because nothing sorted them.+const numbered = [+ folder("k", "14_Account & Permissions", "2026-08-01T00:00:14Z"),+ folder("a", "23_Support & Contact", "2026-08-01T00:00:23Z"),+ folder("q", "7_Hot/Warm Leads", "2026-08-01T00:00:07Z"),+ folder("m", "1_Getting Started", "2026-08-01T00:00:01Z"),+ folder("c", "2_Command Center", "2026-08-01T00:00:02Z"),+ folder("z", "10_Team Conversations", "2026-08-01T00:00:10Z"),+];++describe("sortFolders", () => {+ it("orders numbered names numerically, not lexically", () => {+ expect(sortFolders(numbered, "name-asc").map((f) => f.name)).toEqual([+ "1_Getting Started",+ "2_Command Center",+ "7_Hot/Warm Leads",+ "10_Team Conversations",+ "14_Account & Permissions",+ "23_Support & Contact",+ ]);+ });++ it("reverses the natural order for name-desc", () => {+ expect(sortFolders(numbered, "name-desc").map((f) => f.name)).toEqual([+ "23_Support & Contact",+ "14_Account & Permissions",+ "10_Team Conversations",+ "7_Hot/Warm Leads",+ "2_Command Center",+ "1_Getting Started",+ ]);+ });++ it("sorts by creation time for newest/oldest", () => {+ const oldest = sortFolders(numbered, "oldest").map((f) => f.id);+ expect(oldest).toEqual(["m", "c", "q", "z", "k", "a"]);+ expect(sortFolders(numbered, "newest").map((f) => f.id)).toEqual(+ [...oldest].reverse(),+ );+ });++ it("ignores case and surrounding whitespace when comparing names", () => {+ const names = sortFolders(+ [+ folder("1", "beta", "2026-01-01"),+ folder("2", " Alpha", "2026-01-01"),+ folder("3", "Gamma", "2026-01-01"),+ ],+ "name-asc",+ ).map((f) => f.name);+ expect(names).toEqual([" Alpha", "beta", "Gamma"]);+ });++ it("breaks name ties by creation time, then id, so order is stable", () => {+ const ids = sortFolders(+ [+ folder("b", "Same", "2026-01-02"),+ folder("a", "Same", "2026-01-02"),+ folder("c", "Same", "2026-01-01"),+ ],+ "name-asc",+ ).map((f) => f.id);+ expect(ids).toEqual(["c", "a", "b"]);+ });++ it("tolerates missing or malformed createdAt values", () => {+ const ids = sortFolders(+ [+ { id: "x", name: "B", createdAt: "not a date" },+ { id: "y", name: "A", createdAt: null },+ { id: "z", name: "C" },+ ],+ "newest",+ ).map((f) => f.id);+ // All collapse to time 0, so the name tiebreak decides (reversed for newest).+ expect(ids).toEqual(["z", "x", "y"]);+ });++ it("does not mutate the input array", () => {+ const input = [...numbered];+ sortFolders(input, "name-asc");+ expect(input.map((f) => f.id)).toEqual(numbered.map((f) => f.id));+ });+});++describe("isFolderSort", () => {+ it("accepts known values and rejects anything else", () => {+ expect(isFolderSort("name-asc")).toBe(true);+ expect(isFolderSort("newest")).toBe(true);+ expect(isFolderSort("created")).toBe(false);+ expect(isFolderSort(null)).toBe(false);+ });+});diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsxindex 055a3b46e..964da696d 100644--- a/apps/web/app/(org)/dashboard/caps/page.tsx+++ b/apps/web/app/(org)/dashboard/caps/page.tsx@@ -229,6 +229,7 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {color: folders.color,public: folders.public,parentId: folders.parentId,+ createdAt: folders.createdAt,videoCount: sql<number>`(SELECT COUNT(*) FROM videos WHERE videos.folderId = folders.id)`,diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsxindex 56926078d..f267ce433 100644--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx@@ -62,6 +62,7 @@ async function fetchFolders(public: folders.public,parentId: folders.parentId,spaceId: folders.spaceId,+ createdAt: folders.createdAt,videoCount: sql<number>`(SELECT COUNT(*) FROM ${table} WHERE ${table}.folderId = folders.id)`,diff --git a/apps/web/lib/folder-sort.ts b/apps/web/lib/folder-sort.tsnew file mode 100644index 000000000..dadd4e449--- /dev/null+++ b/apps/web/lib/folder-sort.ts@@ -0,0 +1,68 @@+export type FolderSort = "name-asc" | "name-desc" | "newest" | "oldest";++export const FOLDER_SORT_OPTIONS: ReadonlyArray<{+ value: FolderSort;+ label: string;+}> = [+ { value: "name-asc", label: "Name (A to Z)" },+ { value: "name-desc", label: "Name (Z to A)" },+ { value: "newest", label: "Newest first" },+ { value: "oldest", label: "Oldest first" },+];++export const DEFAULT_FOLDER_SORT: FolderSort = "name-asc";++export const isFolderSort = (value: unknown): value is FolderSort =>+ FOLDER_SORT_OPTIONS.some((option) => option.value === value);++type SortableFolder = {+ id: string;+ name: string;+ createdAt?: Date | string | null;+};++// Natural ordering: "2_Foo" sorts before "10_Bar", and case/accents don't+// split otherwise-equal names apart. Users number their folders, so plain+// lexical order ("1, 10, 11, 2") is the exact complaint this exists to fix.+const collator = new Intl.Collator(undefined, {+ numeric: true,+ sensitivity: "base",+});++const compareName = (a: SortableFolder, b: SortableFolder) =>+ collator.compare(a.name.trim(), b.name.trim()) ||+ // Identical names: keep the earlier-created one first so the order is stable+ // across renders and matches what a "created order" reader expects.+ compareCreated(a, b) ||+ a.id.localeCompare(b.id);++const toTime = (value: SortableFolder["createdAt"]) => {+ if (!value) return 0;+ const time = value instanceof Date ? value.getTime() : Date.parse(value);+ return Number.isNaN(time) ? 0 : time;+};++const compareCreated = (a: SortableFolder, b: SortableFolder) =>+ toTime(a.createdAt) - toTime(b.createdAt);++const compareCreatedThenName = (a: SortableFolder, b: SortableFolder) =>+ compareCreated(a, b) ||+ collator.compare(a.name.trim(), b.name.trim()) ||+ a.id.localeCompare(b.id);++export function sortFolders<T extends SortableFolder>(+ folders: readonly T[],+ sort: FolderSort = DEFAULT_FOLDER_SORT,+): T[] {+ const sorted = [...folders];+ switch (sort) {+ case "name-asc":+ return sorted.sort(compareName);+ case "name-desc":+ return sorted.sort((a, b) => compareName(b, a));+ case "oldest":+ return sorted.sort(compareCreatedThenName);+ case "newest":+ return sorted.sort((a, b) => compareCreatedThenName(b, a));+ }+}diff --git a/apps/web/lib/folder.ts b/apps/web/lib/folder.tsindex 1932622f8..ddf88f441 100644--- a/apps/web/lib/folder.ts+++ b/apps/web/lib/folder.ts@@ -392,6 +392,7 @@ export const getChildFolders = Effect.fn(function* (public: folders.public,parentId: folders.parentId,organizationId: folders.organizationId,+ createdAt: folders.createdAt,videoCount,}).from(folders)diff --git a/apps/web/lib/public-collections.ts b/apps/web/lib/public-collections.tsindex 9738e87fb..a7c126645 100644--- a/apps/web/lib/public-collections.ts+++ b/apps/web/lib/public-collections.ts@@ -27,6 +27,7 @@ import {import { and, desc, eq, inArray, isNull, type SQL, sql } from "drizzle-orm";import { Effect } from "effect";import { cache } from "react";+import { sortFolders } from "@/lib/folder-sort";import { getVerifiedPasswordHashes } from "@/lib/password-cookie";import {PUBLIC_COLLECTION_PAGE_SIZE,@@ -664,17 +665,21 @@ async function getPublicChildFolders(isNull(organizations.tombstoneAt),);- const childFolders = await db()- .select({- id: folders.id,- name: folders.name,- color: folders.color,- parentId: folders.parentId,- })- .from(folders)- .innerJoin(organizations, eq(folders.organizationId, organizations.id))- .where(where)- .orderBy(folders.name);+ // Sorted in JS rather than ORDER BY so numbered names ("2_", "10_") come+ // out in numeric order, matching the dashboard's default folder sort.+ const childFolders = sortFolders(+ await db()+ .select({+ id: folders.id,+ name: folders.name,+ color: folders.color,+ parentId: folders.parentId,+ createdAt: folders.createdAt,+ })+ .from(folders)+ .innerJoin(organizations, eq(folders.organizationId, organizations.id))+ .where(where),+ );if (childFolders.length === 0) return [];