Something went wrong. Try again.
The Social Wire — an ATProto publication reader. thesocialwire.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253"use client";
import { useEffect, useRef, useState } from "react";import { LoaderCircle, Plus } from "lucide-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 type { StandardReaderList } from "@/lib/standardReaderListsClient";
type Props = { open: boolean; onOpenChange: (open: boolean) => void; lists: StandardReaderList[]; creatorResults: StandardReaderList[]; searching: boolean; saving: boolean; onSearchCreator: (creator: string) => Promise<unknown>; onAdd: (input: string) => Promise<unknown>; onCreate?: () => void;};const inputClass = "h-10 w-full min-w-0 rounded-md border border-input bg-background px-3 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring";export function AddStandardReaderListDialog({ open, onOpenChange, lists, creatorResults, searching, saving, onSearchCreator, onAdd, onCreate,}: Props) { const [input, setInput] = useState(""); const [creator, setCreator] = useState(""); const [searched, setSearched] = useState(false); const [error, setError] = useState<string | null>(null); const [pending, setPending] = useState<"search" | "add" | null>(null); const epoch = useRef(0); useEffect( () => () => { epoch.current++; }, [], ); const changeOpen = (next: boolean) => { if (!next) epoch.current++; onOpenChange(next); }; const add = async (value: string) => { if (saving || pending || !value.trim()) return; const captured = ++epoch.current; setError(null); setPending("add"); try { await onAdd(value.trim()); if (epoch.current === captured) changeOpen(false); } catch (failure) { if (epoch.current === captured) setError( failure instanceof Error ? failure.message : "Couldn't Add This List. Try Again.", ); } finally { if (epoch.current === captured) setPending(null); } }; const search = async () => { if (searching || pending || !creator.trim()) return; const captured = ++epoch.current; setError(null); setSearched(false); setPending("search"); try { await onSearchCreator(creator.trim()); if (epoch.current === captured) setSearched(true); } catch (failure) { if (epoch.current === captured) setError( failure instanceof Error ? failure.message : "Couldn't Find This Creator's Lists. Try Again.", ); } finally { if (epoch.current === captured) setPending(null); } }; const saved = new Set(lists.map((list) => list.uri)); return ( <Dialog open={open} onOpenChange={changeOpen}> <DialogContent className="max-h-[min(85svh,44rem)] overflow-y-auto sm:max-w-lg"> <DialogHeader> <DialogTitle>Add List</DialogTitle> <DialogDescription> Paste a Standard Reader list URL or AT URI, or find a creator's public lists. </DialogDescription> </DialogHeader> <p className="text-xs text-muted-foreground"> Saved lists are public. Saving a list does not subscribe you to its members. </p> <form className="space-y-3" onSubmit={(event) => { event.preventDefault(); void add(input); }} > <label className="grid gap-1.5 text-sm font-medium"> List URL Or AT URI <input aria-label="List URL Or AT URI" placeholder="https://standard-reader.app/l/… or at://…" value={input} onChange={(event) => setInput(event.target.value)} className={inputClass} required disabled={saving || pending !== null} /> </label> <Button type="submit" disabled={saving || pending !== null || !input.trim()} className="w-full" > {saving || pending === "add" ? "Adding…" : "Add List"} </Button> </form> <div className="border-t pt-4"> <form className="space-y-3" onSubmit={(event) => { event.preventDefault(); void search(); }} > <div className="grid gap-1.5 text-sm font-medium"> <label htmlFor="reader-list-creator">List Creator</label> <ActorTypeaheadInput id="reader-list-creator" placeholder="Creator Handle Or DID" value={creator} onValueChange={(value) => { setCreator(value); setSearched(false); }} className={inputClass} required disabled={saving || pending !== null} /> </div> <Button type="submit" variant="outline" disabled={ searching || saving || pending !== null || !creator.trim() } className="w-full" > {searching || pending === "search" ? ( <LoaderCircle aria-hidden="true" className="size-4 animate-spin" /> ) : null} Find Creator's Lists </Button> </form> {searched && !searching && pending !== "search" ? ( creatorResults.length ? ( <ul className="mt-3 space-y-2"> {creatorResults.map((list) => ( <li key={list.uri} className="flex min-w-0 items-center gap-2 rounded-md border p-2" > <div className="min-w-0 flex-1"> <p className="break-words text-sm font-medium"> {list.name} </p> {list.description ? ( <p className="break-words text-xs text-muted-foreground"> {list.description} </p> ) : null} </div> <Button type="button" variant="outline" size="sm" aria-label={`Add ${list.name}`} disabled={ saved.has(list.uri) || saving || pending !== null } onClick={() => void add(list.uri)} > {saved.has(list.uri) ? ( "Added" ) : ( <> <Plus aria-hidden="true" className="size-3.5" /> Add </> )} </Button> </li> ))} </ul> ) : ( <p role="status" className="mt-3 text-sm text-muted-foreground"> No Public Lists Found For This Creator. </p> ) ) : null} </div> {error ? ( <p role="alert" className="text-sm text-destructive"> {error} </p> ) : null} <DialogFooter> {onCreate ? ( <Button type="button" variant="outline" disabled={saving || pending !== null} onClick={() => { changeOpen(false); onCreate(); }} > Create List </Button> ) : null} <DialogClose render={<Button type="button" variant="outline" />}> Cancel </DialogClose> </DialogFooter> </DialogContent> </Dialog> );}