Something went wrong. Try again.
The Social Wire — an ATProto publication reader. thesocialwire.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322"use client";
import { useEffect, useRef, useState } from "react";import { Button } from "@/components/ui/button";import { ActorTypeaheadInput } from "@/components/ActorTypeaheadInput";import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle,} from "@/components/ui/dialog";import { standardReaderListDisplayHandle } from "@/lib/standardReaderListDisplayHandle";import type { StandardReaderListPublication } from "@/lib/standardReaderListsClient";import { standardReaderListRecord, type CreateStandardReaderListInput,} from "@/lib/standardReaderList";
export type StandardReaderListCreator = { did: string; handle?: string };type Props = { open: boolean; onOpenChange: (open: boolean) => void; publications: StandardReaderListPublication[]; saving: boolean; onResolveCreator: (input: string) => Promise<StandardReaderListCreator>; onCreate: (input: CreateStandardReaderListInput) => Promise<unknown>;};const inputClass = "w-full min-w-0 rounded-md border border-input bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring";
export function CreateStandardReaderListDialog({ open, onOpenChange, publications, saving, onResolveCreator, onCreate,}: Props) { const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [publicationFilter, setPublicationFilter] = useState(""); const [publicationIds, setPublicationIds] = useState<string[]>([]); const [creatorInput, setCreatorInput] = useState(""); const [creators, setCreators] = useState<StandardReaderListCreator[]>([]); const [pending, setPending] = useState(false); const [error, setError] = useState<string | null>(null); const epoch = useRef(0); useEffect( () => () => { epoch.current++; }, [], ); const changeOpen = (next: boolean) => { if (!next) epoch.current++; onOpenChange(next); }; const addCreator = async () => { if (saving || pending || !creatorInput.trim() || creators.length >= 500) return; const captured = ++epoch.current; setPending(true); setError(null); try { const creator = await onResolveCreator(creatorInput.trim()); if (captured !== epoch.current) return; setCreators((previous) => previous.some((item) => item.did === creator.did) ? previous : [...previous, creator], ); setCreatorInput(""); } catch (failure) { if (captured === epoch.current) setError( failure instanceof Error ? failure.message : "Couldn't Find This Creator. Try Again.", ); } finally { if (captured === epoch.current) setPending(false); } }; const create = async () => { if (saving || pending || !name.trim()) return; const captured = ++epoch.current; setPending(true); setError(null); try { const input = { name: name.trim(), ...(description.trim() ? { description: description.trim() } : {}), publications: publicationIds, users: creators.map((creator) => creator.did), }; standardReaderListRecord(input); await onCreate(input); if (captured === epoch.current) changeOpen(false); } catch (failure) { if (captured === epoch.current) setError( failure instanceof Error ? failure.message : "Couldn't Create This List. Try Again.", ); } finally { if (captured === epoch.current) setPending(false); } }; const filter = publicationFilter.trim().toLowerCase(); const filteredPublications = publications.filter((publication) => `${publication.title} ${standardReaderListDisplayHandle(publication.authorHandle) || ""}` .toLowerCase() .includes(filter), ); return ( <Dialog open={open} onOpenChange={changeOpen}> <DialogContent className="max-h-[min(85svh,44rem)] overflow-y-auto sm:max-w-lg"> <DialogHeader> <DialogTitle>Create List</DialogTitle> <DialogDescription> Choose publications and creator accounts for your list. </DialogDescription> </DialogHeader> <p className="text-xs text-muted-foreground"> Lists and their members are public and may be copied by others. Creating a list does not subscribe you to its members. </p> <form id="create-reader-list" className="space-y-3" onSubmit={(event) => { event.preventDefault(); void create(); }} > <label className="grid gap-1.5 text-sm font-medium"> List Name <input aria-label="List Name" className={inputClass} value={name} onChange={(event) => setName(event.target.value)} required disabled={saving || pending} /> </label> <label className="grid gap-1.5 text-sm font-medium"> Description <textarea aria-label="Description" className={inputClass} value={description} onChange={(event) => setDescription(event.target.value)} rows={2} disabled={saving || pending} /> </label> <fieldset disabled={saving || pending} className="space-y-2"> <legend className="mb-2 text-sm font-medium">Publications</legend> {publications.length ? ( <> <label className="grid gap-1.5 text-sm font-medium"> Filter Publications <input aria-label="Filter Publications" type="search" value={publicationFilter} onChange={(event) => setPublicationFilter(event.target.value) } onKeyDown={(event) => { if (event.key === "Enter") event.preventDefault(); }} className={inputClass} placeholder="Publication Name Or Creator Handle" /> </label> <p className="text-xs text-muted-foreground"> {publicationIds.length} Selected </p> <div className="max-h-40 space-y-2 overflow-y-auto rounded-md border p-3"> {filteredPublications.map((publication) => ( <label key={publication.publicationId} className="flex items-start gap-2 text-sm" > <input type="checkbox" checked={publicationIds.includes( publication.publicationId, )} disabled={ publicationIds.length >= 500 && !publicationIds.includes(publication.publicationId) } onChange={(event) => setPublicationIds((previous) => event.target.checked ? [...previous, publication.publicationId] : previous.filter( (id) => id !== publication.publicationId, ), ) } className="mt-1" /> <span className="min-w-0 break-words"> {publication.title} {standardReaderListDisplayHandle( publication.authorHandle, ) ? ( <span className="block text-xs text-muted-foreground"> @ {standardReaderListDisplayHandle( publication.authorHandle, )} </span> ) : null} </span> </label> ))} {!filteredPublications.length ? ( <p role="status" className="text-sm text-muted-foreground"> No Matching Publications. </p> ) : null} </div> </> ) : ( <p className="text-sm text-muted-foreground"> No Available Publications. You Can Add Creator Accounts Below. </p> )} </fieldset> </form> <form className="space-y-2" onSubmit={(event) => { event.preventDefault(); void addCreator(); }} > <div className="grid gap-1.5 text-sm font-medium"> <label htmlFor="reader-list-creator-account">Creator Account</label> <ActorTypeaheadInput id="reader-list-creator-account" placeholder="Creator Handle Or DID" className={inputClass} value={creatorInput} onValueChange={setCreatorInput} disabled={saving || pending} /> </div> <Button type="submit" variant="outline" disabled={ saving || pending || !creatorInput.trim() || creators.length >= 500 } > Add Creator </Button> </form> {creators.length ? ( <ul className="space-y-2"> {creators.map((creator) => ( <li key={creator.did} className="flex min-w-0 items-center justify-between gap-2 text-sm" > <span className="min-w-0 break-words"> {standardReaderListDisplayHandle(creator.handle) ? `@${standardReaderListDisplayHandle(creator.handle)}` : creator.did} </span> <Button type="button" variant="ghost" size="sm" disabled={saving || pending} aria-label={`Remove ${creator.handle || creator.did}`} onClick={() => setCreators((previous) => previous.filter((item) => item.did !== creator.did), ) } > Remove </Button> </li> ))} </ul> ) : null} {error ? ( <p role="alert" className="text-sm text-destructive"> {error} </p> ) : null} <DialogFooter className="sticky bottom-0 z-10 bg-popover"> <DialogClose render={<Button type="button" variant="outline" />}> Cancel </DialogClose> <Button type="submit" form="create-reader-list" disabled={saving || pending || !name.trim()} > {saving || pending ? "Working…" : "Create List"} </Button> </DialogFooter> </DialogContent> </Dialog> );}