Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171"use client";import { useState } from "react";import useSWR from "swr";import { AtUri } from "@atproto/syntax";import { getReplyTargets } from "actions/replies";import { PubIcon } from "components/ActionBar/Publications";import { Avatar } from "components/Avatar";import { LoadingTiny } from "components/Icons/LoadingTiny";import { MoreOptionsVerticalTiny } from "components/Icons/MoreOptionsVerticalTiny";import { Menu, MenuItem } from "components/Menu";import { ButtonPrimary, ButtonTertiary } from "components/Buttons";import { ProfilePopover } from "components/ProfilePopover";import type { ReplyTarget, ReplyTargetsFor } from "src/documentReplies";
export const replyTargetsKey = ({ document, draftSubject }: ReplyTargetsFor) => document ? ["reply_targets", document] : draftSubject ? ["reply_targets", null, draftSubject] : null;
export function useReplyTargets(args: ReplyTargetsFor) { return useSWR(replyTargetsKey(args), () => getReplyTargets(args)).data;}
export function ReplyingTo(props: { targets: ReplyTarget[]; newTab?: boolean; onWithdraw?: (target: ReplyTarget) => Promise<boolean>; className?: string;}) { let [withdrawn, setWithdrawn] = useState<string[]>([]); let targets = props.targets.filter((t) => !withdrawn.includes(t.uri)); if (targets.length === 0) return null; let onWithdraw = props.onWithdraw; return ( <div className={`replyingTo flex flex-col gap-2 ${props.className ?? ""}`}> {targets.map((target) => ( <div key={target.uri} className="light-container w-full px-3 py-2 text-sm text-secondary flex items-center gap-2" > <div className="grow flex items-center gap-1 min-w-0"> <span className="shrink-0 pr-0.5">In reply to</span> <a href={target.href} target={props.newTab ? "_blank" : undefined} className="flex items-center gap-1 min-w-0 font-bold text-accent-contrast" > {target.publication ? ( <PubIcon tiny icon={target.publication.icon ?? undefined} pubName={target.publication.name} /> ) : ( target.authorAvatar && ( <Avatar size="tiny" src={target.authorAvatar} displayName={target.authorHandle} /> ) )} <span className="truncate">{target.title}</span> </a> {target.authorHandle && ( <div className="hidden sm:flex items-center gap-1 min-w-0"> <span className="shrink-0">by</span> <ProfilePopover didOrHandle={new AtUri(target.uri).host} triggerClassName="truncate hover:underline" trigger={`@${target.authorHandle}`} /> </div> )} </div> {target.pending && ( <span className="shrink-0 italic text-tertiary">Pending</span> )} {onWithdraw && ( <ReplyTargetOptions title={target.title} onWithdraw={async () => { let ok = await onWithdraw(target).catch(() => false); if (ok) setWithdrawn((w) => [...w, target.uri]); return ok; }} /> )} </div> ))} </div> );}
function ReplyTargetOptions(props: { title: string; onWithdraw: () => Promise<boolean>;}) { let [open, setOpen] = useState(false); let [confirming, setConfirming] = useState(false); let [withdrawing, setWithdrawing] = useState(false); return ( <Menu asChild align="end" open={open} onOpenChange={(o) => { if (withdrawing) return; setOpen(o); setConfirming(false); }} trigger={ <button type="button" aria-label="Reply options" className="shrink-0 text-tertiary hover:text-accent-contrast" > <MoreOptionsVerticalTiny /> </button> } > {confirming ? ( <div className="flex flex-col justify-center p-2 text-center max-w-64"> <div className="text-primary font-bold">Are you sure?</div> <div className="text-sm text-secondary"> This post will no longer be a reply to <em>{props.title}</em>. </div> <div className="flex gap-2 mx-auto items-center mt-2"> <ButtonTertiary compact className="text-sm" disabled={withdrawing} onClick={() => setConfirming(false)} > Nevermind </ButtonTertiary> <ButtonPrimary compact className="text-sm" disabled={withdrawing} onClick={async () => { setWithdrawing(true); let ok = await props.onWithdraw(); setWithdrawing(false); if (ok) setOpen(false); }} > {withdrawing ? ( <LoadingTiny className="animate-spin" /> ) : ( "Yes, withdraw" )} </ButtonPrimary> </div> </div> ) : ( <MenuItem onSelect={(e) => { e.preventDefault(); setConfirming(true); }} > Withdraw reply </MenuItem> )} </Menu> );}