"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; onCreate: (input: CreateStandardReaderListInput) => Promise; }; 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([]); const [creatorInput, setCreatorInput] = useState(""); const [creators, setCreators] = useState([]); const [pending, setPending] = useState(false); const [error, setError] = useState(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 ( Create List Choose publications and creator accounts for your list.

Lists and their members are public and may be copied by others. Creating a list does not subscribe you to its members.

{ event.preventDefault(); void create(); }} >