SaveButton.tsx raw

   1  import { Button } from '@/components/ui/button'
   2  import { createRelayListDraftEvent } from '@/lib/draft-event'
   3  import { useNostr } from '@/providers/NostrProvider'
   4  import { TMailboxRelay } from '@/types'
   5  import { CloudUpload, Loader } from 'lucide-react'
   6  import { useState } from 'react'
   7  import { useTranslation } from 'react-i18next'
   8  import { toast } from 'sonner'
   9  
  10  export default function SaveButton({
  11    mailboxRelays,
  12    hasChange,
  13    setHasChange
  14  }: {
  15    mailboxRelays: TMailboxRelay[]
  16    hasChange: boolean
  17    setHasChange: (hasChange: boolean) => void
  18  }) {
  19    const { t } = useTranslation()
  20    const { pubkey, publish, updateRelayListEvent } = useNostr()
  21    const [pushing, setPushing] = useState(false)
  22  
  23    const save = async () => {
  24      if (!pubkey) return
  25  
  26      setPushing(true)
  27      const event = createRelayListDraftEvent(mailboxRelays)
  28      const relayListEvent = await publish(event)
  29      await updateRelayListEvent(relayListEvent)
  30      toast.success('Successfully saved mailbox relays')
  31      setHasChange(false)
  32      setPushing(false)
  33    }
  34  
  35    return (
  36      <Button className="w-full" disabled={!pubkey || pushing || !hasChange} onClick={save}>
  37        {pushing ? <Loader className="animate-spin" /> : <CloudUpload />}
  38        {t('Save')}
  39      </Button>
  40    )
  41  }
  42