Open source CapCut alternative.
TypeScript
fix(site): keep a new folder on the shelf while it saves
David Lec2fa8f01ad4d
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex 58c81baf..74045b95 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -766,52 +766,50 @@ export function LibraryView() {{/* The folders filed at this level, at the top and inside any folderalike. A new one is made here and a dropped one is filed here. */}- {shownFolders.length > 0 || folderCreating ? (- <FolderShelf- folders={shownFolders}- referenceOf={(f) => libraryFolderRef(f, folders)}- mime={LIBRARY_MOVE_MIME}- folderMime={LIBRARY_FOLDER_MOVE_MIME}- creating={folderCreating}- onCreatingChange={setFolderCreating}- statOf={(id) => ({- count:- all.filter((a) => (a.folderId ?? null) === id).length +- templates.filter((t) => (t.folderId ?? null) === id).length +- childrenOf(folders, id).length +- (notes.data?.notes ?? []).filter((n) => n.libraryLocation?.folderId === id && n.libraryLocation.residency === folders.find((f) => f.id === id)?.residency).length,- })}- badgeOf={(id) => {- const r = folders.find((f) => f.id === id)?.residency;- return bothShelves && r ? (- <ShelfBadge residency={r} offline={!live(r)} />- ) : null;- }}- picked={selected}- onPick={(e, id) => pickTile(e, folderSelId(id), order)}- renaming={renamingFolder}- onRenamingChange={setRenamingFolder}- onOpen={gotoFolder}- onShare={shareFolder}- onCreate={async (name) => {- if (!live(newShelf)) return;- const f = await createLibraryFolder(name, newShelf, newParent);- patch((d) => ({ ...d, folders: [...d.folders, f] }));- }}- onRename={renameFolder}- // A folder in a pick with others takes the pick to the confirm;- // alone, itself.- onDelete={(id) => {- const f = shownFolders.find((x) => x.id === id);- if (!f) return;- if (selected.has(folderSelId(id)) && pickSize > 1) setDeleting(pick);- else setDeleting({ ...NO_PICK, folders: [f] });- }}- onDropIds={(ids, fid) => void moveItems(ids, fid)}- onDropFolders={(ids, fid) => void moveFolders(ids, fid)}- onDropFiles={(files, fid) => void upload(files, fid)}- />- ) : null}+ <FolderShelf+ folders={shownFolders}+ referenceOf={(f) => libraryFolderRef(f, folders)}+ mime={LIBRARY_MOVE_MIME}+ folderMime={LIBRARY_FOLDER_MOVE_MIME}+ creating={folderCreating}+ onCreatingChange={setFolderCreating}+ statOf={(id) => ({+ count:+ all.filter((a) => (a.folderId ?? null) === id).length ++ templates.filter((t) => (t.folderId ?? null) === id).length ++ childrenOf(folders, id).length ++ (notes.data?.notes ?? []).filter((n) => n.libraryLocation?.folderId === id && n.libraryLocation.residency === folders.find((f) => f.id === id)?.residency).length,+ })}+ badgeOf={(id) => {+ const r = folders.find((f) => f.id === id)?.residency;+ return bothShelves && r ? (+ <ShelfBadge residency={r} offline={!live(r)} />+ ) : null;+ }}+ picked={selected}+ onPick={(e, id) => pickTile(e, folderSelId(id), order)}+ renaming={renamingFolder}+ onRenamingChange={setRenamingFolder}+ onOpen={gotoFolder}+ onShare={shareFolder}+ onCreate={async (name) => {+ if (!live(newShelf)) return;+ const f = await createLibraryFolder(name, newShelf, newParent);+ patch((d) => ({ ...d, folders: [...d.folders, f] }));+ }}+ onRename={renameFolder}+ // A folder in a pick with others takes the pick to the confirm;+ // alone, itself.+ onDelete={(id) => {+ const f = shownFolders.find((x) => x.id === id);+ if (!f) return;+ if (selected.has(folderSelId(id)) && pickSize > 1) setDeleting(pick);+ else setDeleting({ ...NO_PICK, folders: [f] });+ }}+ onDropIds={(ids, fid) => void moveItems(ids, fid)}+ onDropFolders={(ids, fid) => void moveFolders(ids, fid)}+ onDropFiles={(files, fid) => void upload(files, fid)}+ /><NotesView ref={noteView} library={{ folderId: openFolder, residency: openOwner ?? target, name: trail.at(-1)?.name ?? "Library" }} />diff --git a/site/src/cut/components/NotesView.tsx b/site/src/cut/components/NotesView.tsxindex 53b3fd53..c998900c 100644--- a/site/src/cut/components/NotesView.tsx+++ b/site/src/cut/components/NotesView.tsx@@ -620,7 +620,7 @@ export function NotesView({ library, ref }: Props = {}) {{/* The folders filed at this level, at the top and inside any folderalike. A new one is made here and a dropped one is filed here. */}- {!library && (shownFolders.length > 0 || folderCreating) ? (+ {!library ? (<FolderShelffolders={shownFolders}mime={NOTES_MOVE_MIME}diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsxindex 8b99cbdc..bf0f2b3f 100644--- a/site/src/cut/components/ProjectsHome.tsx+++ b/site/src/cut/components/ProjectsHome.tsx@@ -790,7 +790,7 @@ export function ProjectsHome() {onOpen={gotoFolder}onCreate={(n) => {const r = folderCreating ?? folderTarget;- void createFolder(r, n, r === folderOwner ? openFolder : null);+ return createFolder(r, n, r === folderOwner ? openFolder : null);}}onRename={(id, n) => void renameFolder(residencyOfFolder(id), id, n)}// A folder in a pick with others takes the pick to the confirm; alone,@@ -1137,7 +1137,7 @@ export function ProjectsHome() {</p>))}- {(shownFolders.length > 0 || folderCreating !== null) && renderShelf()}+ {renderShelf()}{view === "gallery" ? renderGallery(mergedShown) : renderList(mergedShown)}</>)}diff --git a/site/src/cut/components/desktopFolders.tsx b/site/src/cut/components/desktopFolders.tsxindex 1d5594bf..146292c4 100644--- a/site/src/cut/components/desktopFolders.tsx+++ b/site/src/cut/components/desktopFolders.tsx@@ -37,6 +37,10 @@ export interface DeskFolder {// A folder sits in the grid's pick beside the items, under an id of its own// shape, so a folder and an item never collide in the one Set.const FOLDER_SEL = "folder:";+// How long a settled create waits for its folder to reach the shelf before+// the name it held lets go: a host that patches its cache on a timer lands+// within it, one that bailed out never does.+const LANDING_GRACE_MS = 1000;export const folderSelId = (id: string) => FOLDER_SEL + id;/** A pick taken apart: the folder ids and the item ids it holds. */@@ -505,6 +509,27 @@ export function FolderShelf<F extends DeskFolder>({};const closeRename = () => setEditingId(null);+ // A name sent to the host stays on the shelf as the folder it becomes until+ // that folder lands in `folders`, so a create the server answers later+ // leaves no gap. A create that fails, or settles without a new folder,+ // lets the name go.+ const [landing, setLanding] = useState<{ name: string; known: Set<string> } | null>(null);+ if (landing && folders.some((f) => !landing.known.has(f.id))) setLanding(null);+ const commitCreate = (name: string) => {+ const pending = { name, known: new Set(folders.map((f) => f.id)) };+ setLanding(pending);+ const release = () => setLanding((l) => (l === pending ? null : l));+ Promise.resolve(onCreate?.(name)).then(+ () => setTimeout(release, LANDING_GRACE_MS),+ release,+ );+ closeCreate();+ };++ // Nothing filed here and nothing being made: the shelf takes no room. The+ // host keeps it mounted, so a name held for a create outlives the field.+ if (folders.length === 0 && !creating && !landing) return null;+const editRowClass = rows? "flex items-center gap-2.5 rounded-lg px-2 py-1.5": "flex w-[92px] flex-col items-start gap-1 px-2 pt-1.5";@@ -697,6 +722,15 @@ export function FolderShelf<F extends DeskFolder>({);})}+ {landing && (+ <div className={editRowClass} data-no-marquee>+ <FolderGlyph className={cn(editGlyphClass, "drop-shadow-sm")} />+ <span className={cn("truncate text-xs font-medium leading-tight", !rows && "mt-0.5 max-w-full")}>+ {landing.name}+ </span>+ </div>+ )}+{creating && (<div className={editRowClass} data-no-marquee><FolderGlyph className={cn(editGlyphClass, "opacity-60")} />@@ -709,8 +743,7 @@ export function FolderShelf<F extends DeskFolder>({onBlur={closeCreate}onKeyDown={(e) => {if (e.key === "Enter" && draft.trim()) {- void onCreate?.(draft.trim());- closeCreate();+ commitCreate(draft.trim());} else if (e.key === "Escape") closeCreate();}}/>