Something went wrong. Try again.
The Social Wire — an ATProto publication reader. thesocialwire.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269import { COLLECTION_FOLDER } from "@/lib/pdsClient";import type { PublicationSidebarProjection, SidebarPublicationRow,} from "@/lib/publicationProjectionClient";
export const OPTIMISTIC_FOLDER_RKEY_PREFIX = "optimistic-folder-";
export function createOptimisticFolderRkey(): string { return `${OPTIMISTIC_FOLDER_RKEY_PREFIX}${crypto.randomUUID()}`;}
export function isOptimisticFolderRkey(rkey: string): boolean { return rkey.startsWith(OPTIMISTIC_FOLDER_RKEY_PREFIX);}
export function buildSidebarFolderEntry(args: { viewerDid: string; uri: string; rkey: string; name: string; icon?: string; iconImage?: string; sortOrder?: number; createdAt?: string;}): PublicationSidebarProjection["folders"][number] { const createdAt = args.createdAt ?? new Date().toISOString(); return { uri: args.uri, rkey: args.rkey, value: { $type: COLLECTION_FOLDER, name: args.name, sortOrder: args.sortOrder ?? 0, ...(args.icon ? { icon: args.icon } : {}), ...(args.iconImage ? { iconImage: args.iconImage } : {}), createdAt, }, };}
function nextFolderSortOrder( projection: PublicationSidebarProjection): number { let max = -1; for (const folder of projection.folders) { const sortOrder = folder.value.sortOrder; if (typeof sortOrder === "number" && sortOrder > max) { max = sortOrder; } } return max + 1;}
export function addOptimisticFolderToProjection( projection: PublicationSidebarProjection | undefined, args: { viewerDid: string; rkey: string; name: string; icon?: string; iconImage?: string; }): PublicationSidebarProjection | undefined { if (!projection) return undefined;
const folder = buildSidebarFolderEntry({ viewerDid: args.viewerDid, uri: `at://${args.viewerDid}/${COLLECTION_FOLDER}/${args.rkey}`, rkey: args.rkey, name: args.name, icon: args.icon, iconImage: args.iconImage, sortOrder: nextFolderSortOrder(projection), });
const folders = [...projection.folders, folder]; const folderSections = projection.folderSections ? [ ...projection.folderSections, { folderRkey: folder.rkey, folderUri: folder.uri, publications: [], }, ] : projection.folderSections;
return { ...projection, folders, folderSections, };}
export function replaceOptimisticFolderInProjection( projection: PublicationSidebarProjection | undefined, optimisticRkey: string, created: { uri: string; rkey: string }, params: { name: string; icon?: string; iconImage?: string }): PublicationSidebarProjection | undefined { if (!projection) return undefined;
const index = projection.folders.findIndex( (folder) => folder.rkey === optimisticRkey ); if (index < 0) return projection;
const existing = projection.folders[index]; const createdAt = typeof existing.value.createdAt === "string" ? existing.value.createdAt : new Date().toISOString(); const sortOrder = typeof existing.value.sortOrder === "number" ? existing.value.sortOrder : 0;
const folder = buildSidebarFolderEntry({ viewerDid: projection.viewerDid, uri: created.uri, rkey: created.rkey, name: params.name, icon: params.icon, iconImage: params.iconImage, sortOrder, createdAt, });
const folders = [...projection.folders]; folders[index] = folder;
const folderSections = projection.folderSections?.map((section) => section.folderRkey === optimisticRkey ? { ...section, folderRkey: created.rkey, folderUri: created.uri, } : section );
const publicationPrefs = projection.publicationPrefs.map((pref) => { const folderId = typeof pref.value.folderId === "string" ? pref.value.folderId : undefined; if (folderId !== optimisticRkey) return pref; return { ...pref, value: { ...pref.value, folderId: created.rkey, }, }; });
return { ...projection, folders, folderSections, publicationPrefs, };}
export function updateFolderInSidebarProjection( projection: PublicationSidebarProjection | undefined, folderRkey: string, updates: { name?: string; icon?: string; iconImage?: string; sortOrder?: number }): PublicationSidebarProjection | undefined { if (!projection) return undefined;
const folders = projection.folders.map((folder) => { if (folder.rkey !== folderRkey) return folder; const value = { ...folder.value }; if (updates.name !== undefined) value.name = updates.name; if (updates.sortOrder !== undefined) value.sortOrder = updates.sortOrder; if (updates.icon !== undefined) { if (updates.icon.trim()) value.icon = updates.icon; else delete value.icon; } if (updates.iconImage !== undefined) { if (updates.iconImage.trim()) value.iconImage = updates.iconImage; else delete value.iconImage; } return { ...folder, value }; });
return { ...projection, folders, };}
export function removeOptimisticFolderFromProjection( projection: PublicationSidebarProjection | undefined, optimisticRkey: string): PublicationSidebarProjection | undefined { if (!projection) return undefined; return removeFolderFromSidebarProjection(projection, optimisticRkey);}
function publicationsInFolderSection( projection: PublicationSidebarProjection, folderRkey: string): SidebarPublicationRow[] { const section = projection.folderSections?.find( (entry) => entry.folderRkey === folderRkey ); if (section) return section.publications;
const excluded = new Set([ ...projection.myPublications.map((row) => row.publicationId), ...projection.followingTabPublications.map((row) => row.publicationId), ]); const rows: SidebarPublicationRow[] = [];
for (const pref of projection.publicationPrefs) { const folderId = typeof pref.value.folderId === "string" ? pref.value.folderId : undefined; if (folderId !== folderRkey) continue; const row = projection.allPublicationRows.find( (entry) => entry.publicationId === pref.publicationId ); if (!row || excluded.has(row.publicationId)) continue; rows.push(row); }
return rows;}
export function removeFolderFromSidebarProjection( projection: PublicationSidebarProjection | undefined, folderRkey: string): PublicationSidebarProjection | undefined { if (!projection) return undefined;
const restoredPublications = publicationsInFolderSection( projection, folderRkey ); const restoredIds = new Set( restoredPublications.map((row) => row.publicationId) ); const subscribedUnfoldered = [ ...projection.subscribedUnfoldered.filter( (row) => !restoredIds.has(row.publicationId) ), ...restoredPublications, ];
const publicationPrefs = projection.publicationPrefs.map((pref) => { const folderId = typeof pref.value.folderId === "string" ? pref.value.folderId : undefined; if (folderId !== folderRkey) return pref; const nextValue = { ...pref.value }; delete nextValue.folderId; return { ...pref, value: nextValue }; });
return { ...projection, folders: projection.folders.filter((folder) => folder.rkey !== folderRkey), folderSections: projection.folderSections?.filter( (section) => section.folderRkey !== folderRkey ), subscribedUnfoldered, publicationPrefs, };}