import { Button } from '@/components/ui/button' import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' import { extractMentions } from '@/lib/event' import { useNostr } from '@/providers/NostrProvider' import { Check } from 'lucide-react' import { Event } from 'nostr-tools' import { HTMLAttributes, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { SimpleUserAvatar } from '../UserAvatar' import { SimpleUsername } from '../Username' import { useMuteList } from '@/providers/MuteListProvider' export default function Mentions({ content, mentions, setMentions, parentEvent }: { content: string mentions: string[] setMentions: (mentions: string[]) => void parentEvent?: Event }) { const { t } = useTranslation() const { pubkey } = useNostr() const { mutePubkeys } = useMuteList() const [potentialMentions, setPotentialMentions] = useState([]) const [parentEventPubkey, setParentEventPubkey] = useState() const [removedPubkeys, setRemovedPubkeys] = useState([]) useEffect(() => { extractMentions(content, parentEvent).then(({ pubkeys, relatedPubkeys, parentEventPubkey }) => { const _parentEventPubkey = parentEventPubkey !== pubkey ? parentEventPubkey : undefined setParentEventPubkey(_parentEventPubkey) const potentialMentions = [...pubkeys, ...relatedPubkeys].filter((p) => p !== pubkey) if (_parentEventPubkey) { potentialMentions.push(_parentEventPubkey) } setPotentialMentions(potentialMentions) setRemovedPubkeys((pubkeys) => { return Array.from( new Set( pubkeys .filter((p) => potentialMentions.includes(p)) .concat( potentialMentions.filter((p) => mutePubkeys.includes(p) && p !== _parentEventPubkey) ) ) ) }) }) }, [content, parentEvent, pubkey]) useEffect(() => { const newMentions = potentialMentions.filter((pubkey) => !removedPubkeys.includes(pubkey)) setMentions(newMentions) }, [potentialMentions, removedPubkeys]) return (
{potentialMentions.map((_, index) => { const pubkey = potentialMentions[potentialMentions.length - 1 - index] const isParentPubkey = pubkey === parentEventPubkey return ( { if (isParentPubkey) { return } if (checked) { setRemovedPubkeys((pubkeys) => pubkeys.filter((p) => p !== pubkey)) } else { setRemovedPubkeys((pubkeys) => [...pubkeys, pubkey]) } }} disabled={isParentPubkey} > ) })}
) } function PopoverCheckboxItem({ children, checked, onCheckedChange, disabled, ...props }: HTMLAttributes & { disabled?: boolean checked: boolean onCheckedChange?: (checked: boolean) => void }) { return (
) }