import { Button } from '@/components/ui/button' import { Label } from '@/components/ui/label' import { Switch } from '@/components/ui/switch' import { StorageKey } from '@/constants' import { useToast } from '@/hooks/use-toast' import { createPictureNoteDraftEvent } from '@/lib/draft-event' import { cn } from '@/lib/utils' import { useNostr } from '@/providers/NostrProvider' import { ChevronDown, Loader, LoaderCircle, Plus, X } from 'lucide-react' import { Dispatch, SetStateAction, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import Image from '../Image' import TextareaWithMentions from '../TextareaWithMentions.tsx' import Mentions from './Mentions' import Uploader from './Uploader' export default function PicturePostContent({ close }: { close: () => void }) { const { t } = useTranslation() const { toast } = useToast() const { publish, checkLogin } = useNostr() const [content, setContent] = useState('') const [pictureInfos, setPictureInfos] = useState<{ url: string; tags: string[][] }[]>([]) const [posting, setPosting] = useState(false) const [showMoreOptions, setShowMoreOptions] = useState(false) const [addClientTag, setAddClientTag] = useState(false) const canPost = !!content && !posting && pictureInfos.length > 0 useEffect(() => { setAddClientTag(window.localStorage.getItem(StorageKey.ADD_CLIENT_TAG) === 'true') }, []) const post = async (e: React.MouseEvent) => { e.stopPropagation() checkLogin(async () => { if (!canPost) { close() return } setPosting(true) try { if (!pictureInfos.length) { throw new Error(t('Picture note requires images')) } const draftEvent = await createPictureNoteDraftEvent(content, pictureInfos, { addClientTag }) await publish(draftEvent) setContent('') close() } catch (error) { if (error instanceof AggregateError) { error.errors.forEach((e) => toast({ variant: 'destructive', title: t('Failed to post'), description: e.message }) ) } else if (error instanceof Error) { toast({ variant: 'destructive', title: t('Failed to post'), description: error.message }) } console.error(error) return } finally { setPosting(false) } toast({ title: t('Post successful'), description: t('Your post has been published') }) }) } const onAddClientTagChange = (checked: boolean) => { setAddClientTag(checked) window.localStorage.setItem(StorageKey.ADD_CLIENT_TAG, checked.toString()) } return (
{t('A special note for picture-first clients like Olas')}
{showMoreOptions && (
{t('Show others this was sent via Jumble')}
)}
) } function PictureUploader({ pictureInfos, setPictureInfos }: { pictureInfos: { url: string; tags: string[][] }[] setPictureInfos: Dispatch< SetStateAction< { url: string tags: string[][] }[] > > }) { const [uploading, setUploading] = useState(false) return (
{pictureInfos.map(({ url }, index) => (
))} { setPictureInfos((prev) => [...prev, { url, tags }]) }} onUploadingChange={setUploading} >
{uploading ? : }
) }