Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
web: add FoldersSection with sort, layout and collapse controls
Richie McIlroy5d871e1f532f
diff --git a/apps/web/app/(org)/dashboard/caps/components/Folder.tsx b/apps/web/app/(org)/dashboard/caps/components/Folder.tsxindex 74953741c..19df5267b 100644--- a/apps/web/app/(org)/dashboard/caps/components/Folder.tsx+++ b/apps/web/app/(org)/dashboard/caps/components/Folder.tsx@@ -18,16 +18,22 @@ import { registerDropTarget } from "../../folder/[id]/components/ClientCapCard";import { FoldersDropdown } from "./FoldersDropdown";import { MoveItemsDialog } from "./MoveItemsDialog";+export type FolderLayout = "grid" | "list";+export type FolderDataType = {name: string;id: Folder.FolderId;color: "normal" | "blue" | "red" | "yellow";public: boolean;videoCount: number;+ // Drives "Newest/Oldest first" sorting. Optional so older callers that only+ // need the card for a move menu keep working; missing dates sort as oldest.+ createdAt?: Date | string | null;spaceId?: Space.SpaceIdOrOrganisationId | null;parentId: Folder.FolderId | null;canMove?: boolean;moveRootLabel?: string;+ layout?: FolderLayout;};const FolderCard = ({@@ -40,7 +46,9 @@ const FolderCard = ({spaceId,canMove,moveRootLabel,+ layout = "grid",}: FolderDataType) => {+ const isList = layout === "list";const router = useRouter();const { theme } = useTheme();const [confirmDeleteFolderOpen, setConfirmDeleteFolderOpen] = useState(false);@@ -335,11 +343,23 @@ const FolderCard = ({onDragLeave={handleDragLeave}onDrop={handleDrop}className={clsx(- "flex justify-between items-center px-4 py-4 w-full h-auto min-w-0 rounded-lg border transition-all duration-200 bg-gray-3 hover:bg-gray-4 hover:border-gray-6",+ "flex relative justify-between items-center w-full h-auto min-w-0 rounded-lg border transition-all duration-200 bg-gray-3 hover:bg-gray-4 hover:border-gray-6",+ isList ? "gap-3 px-3 py-2" : "px-4 py-4",isDragOver ? "border-blue-10 bg-gray-4" : "border-gray-5",isMovingVideo && "opacity-70",)}>+ {/* Stretched link: the whole card opens the folder, including the icon,+ the name, and the "x videos" line. Controls that must not navigate+ (menu button, rename field) sit above it with z-10. */}+ {!isRenaming && (+ <Link+ href={folderHref}+ prefetch={false}+ aria-label={`Open folder ${updateName}`}+ className="absolute inset-0 rounded-lg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-9 focus-visible:ring-offset-1"+ />+ )}{moveEnabled && (<MoveItemsDialogopen={isMoveDialogOpen}@@ -353,14 +373,19 @@ const FolderCard = ({}}/>)}- <div className="flex flex-1 gap-3 items-center">- <Link href={folderHref} prefetch={false} className="shrink-0">+ <div className="flex flex-1 gap-3 items-center min-w-0">+ <div className="shrink-0 pointer-events-none"><FolderRivekey={`${theme}folder${id}`}- className="w-[50px] h-[50px]"+ className={isList ? "size-9" : "w-[50px] h-[50px]"}/>- </Link>- <div className="flex flex-col justify-center h-10">+ </div>+ <div+ className={clsx(+ "flex flex-1 min-w-0",+ isList ? "gap-3 items-center" : "flex-col justify-center",+ )}+ >{isRenaming ? (<textarearef={nameRef}@@ -389,25 +414,53 @@ const FolderCard = ({});}}}- className="w-full resize-none bg-transparent border-none focus:outline-none- focus:ring-0 focus:border-none text-gray-12 text-[15px] max-w-[116px] truncate p-0 m-0 h-[22px] leading-[22px] overflow-hidden font-normal tracking-normal"+ className={clsx(+ "relative z-10 flex-1 w-full min-w-0 resize-none bg-transparent border-none focus:outline-none focus:ring-0 focus:border-none text-gray-12 truncate p-0 m-0 h-[22px] leading-[22px] overflow-hidden font-normal tracking-normal",+ isList ? "text-[14px]" : "text-[15px]",+ )}/>) : (- <Link- href={folderHref}- prefetch={false}- className="block text-left"+ <div+ title={updateName}+ className={clsx(+ "block min-w-0 text-left",+ isList ? "flex-1" : "w-full",+ )}>- <span className="block text-[15px] truncate text-gray-12 w-full max-w-[116px] m-0 p-0 h-[22px] leading-[22px] font-normal tracking-normal">+ <span+ className={clsx(+ "text-gray-12 m-0 p-0 font-normal tracking-normal",+ isList+ ? "block text-[14px] truncate h-[22px] leading-[22px]"+ : // Two lines before truncating: numbered names like+ // "14_Account & Permissions" usually fit without an ellipsis.+ // line-clamp sets its own display, so no `block` here.+ "text-[15px] leading-[22px] line-clamp-2 break-words",+ )}+ >{updateName}</span>- </Link>+ </div>)}- <div className="flex gap-2 items-center">- <p className="text-sm truncate text-gray-10 w-fit">{`${videoCount} ${- videoCount === 1 ? "video" : "videos"- }`}</p>- {publicEnabled && (+ <div+ className={clsx(+ "flex gap-2 items-center",+ isList && "shrink-0 justify-end w-32",+ )}+ >+ {publicEnabled && isList && (+ <span className="inline-flex gap-1 items-center text-[11px] font-medium text-blue-9">+ <FontAwesomeIcon icon={faGlobe} className="size-2.5" />+ Public+ </span>+ )}+ <p+ className={clsx(+ "text-sm truncate text-gray-10 w-fit",+ isList && "tabular-nums",+ )}+ >{`${videoCount} ${videoCount === 1 ? "video" : "videos"}`}</p>+ {publicEnabled && !isList && (<span className="inline-flex gap-1 items-center text-[11px] font-medium text-blue-9"><FontAwesomeIcon icon={faGlobe} className="size-2.5" />Public@@ -426,31 +479,33 @@ const FolderCard = ({title="Delete Folder"description={`Are you sure you want to delete the folder "${name}"? This action cannot be undone.`}/>- <FoldersDropdown- id={id}- parentId={parentId}- public={publicEnabled}- setIsRenaming={setIsRenaming}- setConfirmDeleteFolderOpen={setConfirmDeleteFolderOpen}- nameRef={nameRef}- onPublicToggle={() => {- const nextPublic = !publicEnabled;- if (nextPublic && !ownerIsPro) {- setUpgradeModalOpen(true);- return;- }- setPublicEnabled(nextPublic);- updateFolder.mutate({- id,- public: nextPublic,- });- }}- onCopyPublicLink={async () => {- await copyPublicLink();- }}- canMove={moveEnabled}- onMove={() => setIsMoveDialogOpen(true)}- />+ <div className="relative z-10 shrink-0">+ <FoldersDropdown+ id={id}+ parentId={parentId}+ public={publicEnabled}+ setIsRenaming={setIsRenaming}+ setConfirmDeleteFolderOpen={setConfirmDeleteFolderOpen}+ nameRef={nameRef}+ onPublicToggle={() => {+ const nextPublic = !publicEnabled;+ if (nextPublic && !ownerIsPro) {+ setUpgradeModalOpen(true);+ return;+ }+ setPublicEnabled(nextPublic);+ updateFolder.mutate({+ id,+ public: nextPublic,+ });+ }}+ onCopyPublicLink={async () => {+ await copyPublicLink();+ }}+ canMove={moveEnabled}+ onMove={() => setIsMoveDialogOpen(true)}+ />+ </div></fieldset>);};diff --git a/apps/web/app/(org)/dashboard/caps/components/FoldersSection.tsx b/apps/web/app/(org)/dashboard/caps/components/FoldersSection.tsxnew file mode 100644index 000000000..10cae4868--- /dev/null+++ b/apps/web/app/(org)/dashboard/caps/components/FoldersSection.tsx@@ -0,0 +1,266 @@+"use client";++import {+ DropdownMenu,+ DropdownMenuContent,+ DropdownMenuItem,+ DropdownMenuTrigger,+} from "@cap/ui";+import {+ faArrowDownAZ,+ faArrowDownWideShort,+ faArrowUpWideShort,+ faArrowUpZA,+ faCheck,+ faChevronDown,+ faGrip,+ faList,+} from "@fortawesome/free-solid-svg-icons";+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";+import clsx from "clsx";+import { useEffect, useMemo, useState } from "react";+import {+ DEFAULT_FOLDER_SORT,+ FOLDER_SORT_OPTIONS,+ type FolderSort,+ isFolderSort,+ sortFolders,+} from "@/lib/folder-sort";+import FolderCard, { type FolderDataType, type FolderLayout } from "./Folder";++const SORT_STORAGE_KEY = "cap-dashboard-folder-sort";+const LAYOUT_STORAGE_KEY = "cap-dashboard-folder-layout";+const COLLAPSED_STORAGE_KEY = "cap-dashboard-folders-collapsed";++const SORT_ICONS: Record<FolderSort, typeof faArrowDownAZ> = {+ "name-asc": faArrowDownAZ,+ "name-desc": faArrowUpZA,+ newest: faArrowDownWideShort,+ oldest: faArrowUpWideShort,+};++const isFolderLayout = (value: unknown): value is FolderLayout =>+ value === "grid" || value === "list";++const isBooleanString = (value: unknown): value is "true" | "false" =>+ value === "true" || value === "false";++const readStored = <T,>(key: string, guard: (v: unknown) => v is T) => {+ try {+ const raw = window.localStorage.getItem(key);+ return guard(raw) ? raw : null;+ } catch {+ return null;+ }+};++const writeStored = (key: string, value: string) => {+ try {+ window.localStorage.setItem(key, value);+ } catch {+ // Storage may be unavailable (private mode, quota); the choice still+ // applies for this page view.+ }+};++// Preferences are remembered per location ("personal", or one space), so a+// numbered-folder scheme in My Caps doesn't force its sort onto every space.+// Subfolder pages share their container's setting.+const useFolderViewPreferences = (scope: string) => {+ const sortKey = `${SORT_STORAGE_KEY}:${scope}`;+ const layoutKey = `${LAYOUT_STORAGE_KEY}:${scope}`;+ const collapsedKey = `${COLLAPSED_STORAGE_KEY}:${scope}`;+ const [sort, setSort] = useState<FolderSort>(DEFAULT_FOLDER_SORT);+ const [layout, setLayout] = useState<FolderLayout>("grid");+ const [collapsed, setCollapsed] = useState(false);++ useEffect(() => {+ setSort(readStored(sortKey, isFolderSort) ?? DEFAULT_FOLDER_SORT);+ setLayout(readStored(layoutKey, isFolderLayout) ?? "grid");+ setCollapsed(readStored(collapsedKey, isBooleanString) === "true");+ }, [sortKey, layoutKey, collapsedKey]);++ return {+ sort,+ layout,+ collapsed,+ toggleCollapsed: () => {+ setCollapsed((current) => {+ writeStored(collapsedKey, String(!current));+ return !current;+ });+ },+ updateSort: (next: FolderSort) => {+ setSort(next);+ writeStored(sortKey, next);+ },+ updateLayout: (next: FolderLayout) => {+ setLayout(next);+ writeStored(layoutKey, next);+ },+ };+};++const controlClass =+ "flex items-center justify-center h-8 text-[13px] font-medium rounded-lg border transition-colors duration-200 text-gray-11 bg-gray-3 border-gray-5 hover:bg-gray-4 hover:border-gray-6 hover:text-gray-12";++type FolderItem = Omit<FolderDataType, "layout" | "canMove" | "moveRootLabel">;++interface FoldersSectionProps {+ title: string;+ /** Preference scope: "personal" for My Caps and its subfolders, or a space id. */+ scope: string;+ folders: FolderItem[];+ canMove?: boolean;+ moveRootLabel?: string;+ headingSize?: "lg" | "md";+}++export const FoldersSection = ({+ title,+ scope,+ folders,+ canMove,+ moveRootLabel,+ headingSize = "lg",+}: FoldersSectionProps) => {+ const { sort, layout, collapsed, toggleCollapsed, updateSort, updateLayout } =+ useFolderViewPreferences(scope);++ const sorted = useMemo(() => sortFolders(folders, sort), [folders, sort]);+ const activeSort =+ FOLDER_SORT_OPTIONS.find((option) => option.value === sort) ??+ FOLDER_SORT_OPTIONS[0];++ if (folders.length === 0) return null;++ return (+ <section className={collapsed ? "mb-6" : "mb-10"}>+ <div+ className={clsx(+ "flex flex-wrap gap-3 justify-between items-center w-full",+ !collapsed && "mb-6",+ )}+ >+ <button+ type="button"+ onClick={toggleCollapsed}+ aria-expanded={!collapsed}+ className="flex gap-2.5 items-center min-w-0 text-left rounded-lg group -ml-1 pl-1 pr-2 py-0.5 transition-colors hover:bg-gray-3"+ >+ <h1+ className={clsx(+ "font-medium truncate text-gray-12",+ headingSize === "lg" ? "text-2xl" : "text-xl",+ )}+ >+ {title}+ </h1>+ <span className="text-sm tabular-nums text-gray-10">+ {folders.length}+ </span>+ <FontAwesomeIcon+ icon={faChevronDown}+ className={clsx(+ "size-3 text-gray-9 transition-transform duration-200 group-hover:text-gray-11",+ collapsed && "-rotate-90",+ )}+ />+ </button>+ {!collapsed && (+ <div className="flex gap-2 items-center">+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <button+ type="button"+ aria-label={`Sort folders: ${activeSort?.label}`}+ className={clsx(controlClass, "gap-2 px-3")}+ >+ <FontAwesomeIcon+ icon={SORT_ICONS[sort]}+ className="size-3.5 text-gray-10"+ />+ <span>{activeSort?.label}</span>+ <FontAwesomeIcon+ icon={faChevronDown}+ className="size-2.5 text-gray-9"+ />+ </button>+ </DropdownMenuTrigger>+ <DropdownMenuContent align="end" className="min-w-44">+ {FOLDER_SORT_OPTIONS.map((option) => (+ <DropdownMenuItem+ key={option.value}+ onClick={() => updateSort(option.value)}+ className={clsx(+ "gap-2 rounded-lg",+ option.value === sort && "text-gray-12",+ )}+ >+ <FontAwesomeIcon+ icon={SORT_ICONS[option.value]}+ className="size-3 text-gray-10"+ />+ <span className="flex-1">{option.label}</span>+ {option.value === sort && (+ <FontAwesomeIcon+ icon={faCheck}+ className="size-3 text-gray-12"+ />+ )}+ </DropdownMenuItem>+ ))}+ </DropdownMenuContent>+ </DropdownMenu>+ <fieldset className="flex overflow-hidden rounded-lg border border-gray-5 bg-gray-3">+ <legend className="sr-only">Folder layout</legend>+ {(+ [+ { value: "grid", icon: faGrip, label: "Grid" },+ { value: "list", icon: faList, label: "List" },+ ] as const+ ).map((option) => (+ <button+ key={option.value}+ type="button"+ aria-label={`${option.label} layout`}+ aria-pressed={layout === option.value}+ onClick={() => updateLayout(option.value)}+ className={clsx(+ "flex justify-center items-center w-8 h-8 transition-colors duration-200",+ layout === option.value+ ? "bg-gray-5 text-gray-12"+ : "text-gray-10 hover:bg-gray-4 hover:text-gray-12",+ )}+ >+ <FontAwesomeIcon icon={option.icon} className="size-3.5" />+ </button>+ ))}+ </fieldset>+ </div>+ )}+ </div>+ {!collapsed && (+ <div+ className={clsx(+ layout === "grid"+ ? "grid grid-cols-[repeat(auto-fill,minmax(280px,1fr))] gap-4"+ : "flex flex-col gap-2",+ )}+ >+ {sorted.map((folder) => (+ <FolderCard+ key={folder.id}+ {...folder}+ layout={layout}+ canMove={canMove}+ moveRootLabel={moveRootLabel}+ />+ ))}+ </div>+ )}+ </section>+ );+};++export default FoldersSection;