From 4927a321dfff2b995fcf89432a816116337b8b76 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 9 Jun 2024 18:34:40 -0500 Subject: [PATCH] Rework identity request to match the new API --- src/features/edit-identity/index.tsx | 194 ++++++++++++++++++--------- src/locales/en.json | 4 +- src/schemas/admin-account.ts | 27 ++++ 3 files changed, 164 insertions(+), 61 deletions(-) create mode 100644 src/schemas/admin-account.ts diff --git a/src/features/edit-identity/index.tsx b/src/features/edit-identity/index.tsx index 2d65bd205..0b96ed3e3 100644 --- a/src/features/edit-identity/index.tsx +++ b/src/features/edit-identity/index.tsx @@ -1,12 +1,12 @@ +import { useMutation, useQuery } from '@tanstack/react-query'; import React, { useState } from 'react'; -import { defineMessages, useIntl } from 'react-intl'; +import { FormattedMessage, defineMessages, useIntl } from 'react-intl'; import { patchMe } from 'soapbox/actions/me'; import List, { ListItem } from 'soapbox/components/list'; -import { Button, Column, Emoji, HStack, Icon, Input, Tooltip } from 'soapbox/components/ui'; -import { useNostr } from 'soapbox/contexts/nostr-context'; -import { useNostrReq } from 'soapbox/features/nostr/hooks/useNostrReq'; -import { useAppDispatch, useInstance, useOwnAccount } from 'soapbox/hooks'; +import { Button, CardHeader, CardTitle, Column, Emoji, Form, HStack, Icon, Input, Textarea, Tooltip } from 'soapbox/components/ui'; +import { useApi, useAppDispatch, useInstance, useOwnAccount } from 'soapbox/hooks'; +import { adminAccountSchema } from 'soapbox/schemas/admin-account'; import toast from 'soapbox/toast'; interface IEditIdentity { @@ -18,6 +18,7 @@ const messages = defineMessages({ unverified: { id: 'edit_profile.fields.nip05_unverified', defaultMessage: 'Name could not be verified and won\'t be used.' }, success: { id: 'edit_profile.success', defaultMessage: 'Your profile has been successfully saved!' }, error: { id: 'edit_profile.error', defaultMessage: 'Profile update failed' }, + placeholder: { id: 'edit_identity.reason_placeholder', defaultMessage: 'Why do you want this name?' }, }); /** EditIdentity component. */ @@ -26,77 +27,111 @@ const EditIdentity: React.FC = () => { const instance = useInstance(); const dispatch = useAppDispatch(); const { account } = useOwnAccount(); - const { relay, signer } = useNostr(); + const { mutate } = useRequestName(); - const admin = instance.nostr?.pubkey; - const pubkey = account?.nostr?.pubkey; - const [username, setUsername] = useState(''); + const { data: approvedNames } = useNames(); + const { data: pendingNames } = usePendingNames(); - const { events: labels } = useNostrReq( - (admin && pubkey) - ? [{ kinds: [1985], authors: [admin], '#L': ['nip05'], '#p': [pubkey] }] - : [], - ); + const [username, setUsername] = useState(''); + const [reason, setReason] = useState(''); if (!account) return null; - const updateNip05 = async (nip05: string): Promise => { - if (account.source?.nostr?.nip05 === nip05) return; + const updateName = async (name: string): Promise => { + if (account.source?.nostr?.nip05 === name) return; try { - await dispatch(patchMe({ nip05 })); + await dispatch(patchMe({ nip05: name })); toast.success(intl.formatMessage(messages.success)); } catch (e) { toast.error(intl.formatMessage(messages.error)); } }; - const submit = async () => { - if (!admin || !signer || !relay) return; - - const event = await signer.signEvent({ - kind: 5950, - content: '', - tags: [ - ['i', `${username}@${instance.domain}`, 'text'], - ['p', admin], - ], - created_at: Math.floor(Date.now() / 1000), - }); - - await relay.event(event); + const submit = () => { + const name = `${username}@${instance.domain}`; + mutate({ name, reason }); }; return ( - - {labels.map((label) => { - const identifier = label.tags.find(([name]) => name === 'l')?.[1]; - if (!identifier) return null; - - return ( - - {identifier} - {(account.source?.nostr?.nip05 === identifier && account.acct !== identifier) && ( - -
- -
-
- )} - - } - isSelected={account.source?.nostr?.nip05 === identifier} - onSelect={() => updateNip05(identifier)} - /> - ); - })} - setUsername(e.target.value)} />}> - - -
+
+
+ setUsername(e.target.value)} /> +