From 5b23166fb7fbb55c20910950d86bf1c52efa6284 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 16 Mar 2024 15:06:55 -0500 Subject: [PATCH] NostrSignin: remove IdentityStep, AccountStep, other useless code --- .../nostr-signin-modal/nostr-signin-modal.tsx | 15 +-- .../nostr-signin-modal/steps/account-step.tsx | 127 ------------------ .../steps/identity-step.tsx | 95 ------------- .../nostr-signin-modal/steps/key-add-step.tsx | 11 +- .../nostr-signin-modal/steps/key-step.tsx | 2 +- .../nostr-signin-modal/steps/keygen-step.tsx | 8 +- 6 files changed, 10 insertions(+), 248 deletions(-) delete mode 100644 src/features/ui/components/modals/nostr-signin-modal/steps/account-step.tsx delete mode 100644 src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx diff --git a/src/features/ui/components/modals/nostr-signin-modal/nostr-signin-modal.tsx b/src/features/ui/components/modals/nostr-signin-modal/nostr-signin-modal.tsx index b31ac38b0..259f25348 100644 --- a/src/features/ui/components/modals/nostr-signin-modal/nostr-signin-modal.tsx +++ b/src/features/ui/components/modals/nostr-signin-modal/nostr-signin-modal.tsx @@ -1,37 +1,30 @@ import React, { useState } from 'react'; -import AccountStep from './steps/account-step'; import ExtensionStep from './steps/extension-step'; -import IdentityStep from './steps/identity-step'; import KeyAddStep from './steps/key-add-step'; import KeyStep from './steps/key-step'; import KeygenStep from './steps/keygen-step'; -type Step = 'extension' | 'identity' | 'key' | 'keygen' | 'key-add' | 'account'; +type Step = 'extension' | 'key' | 'keygen' | 'key-add'; interface INostrSigninModal { onClose: (type?: string) => void; } const NostrSigninModal: React.FC = ({ onClose }) => { - const [step, setStep] = useState(window.nostr ? 'extension' : 'identity'); - const [accountId, setAccountId] = useState(); + const [step, setStep] = useState(window.nostr ? 'extension' : 'key-add'); const handleClose = () => onClose('NOSTR_SIGNIN'); switch (step) { case 'extension': return ; - case 'identity': - return ; case 'key': return ; case 'key-add': - return ; + return ; case 'keygen': - return ; - case 'account': - return ; + return ; default: return null; } diff --git a/src/features/ui/components/modals/nostr-signin-modal/steps/account-step.tsx b/src/features/ui/components/modals/nostr-signin-modal/steps/account-step.tsx deleted file mode 100644 index 4fee21fc8..000000000 --- a/src/features/ui/components/modals/nostr-signin-modal/steps/account-step.tsx +++ /dev/null @@ -1,127 +0,0 @@ -import { NSchema as n } from '@soapbox/nspec'; -import { nip19 } from 'nostr-tools'; -import React, { useMemo, useState } from 'react'; -import { FormattedMessage } from 'react-intl'; - -import { useAccount } from 'soapbox/api/hooks'; -import { Avatar, Text, Stack, Emoji, Button, Tooltip, Modal } from 'soapbox/components/ui'; -import { useNostr } from 'soapbox/contexts/nostr-context'; -import { useNostrReq } from 'soapbox/features/nostr/hooks/useNostrReq'; -import ModalLoading from 'soapbox/features/ui/components/modal-loading'; -import { useInstance } from 'soapbox/hooks'; - -import { Step } from '../nostr-signin-modal'; - -interface IAccountStep { - accountId: string; - setStep(step: Step): void; - onClose(): void; -} - -const AccountStep: React.FC = ({ accountId, setStep, onClose }) => { - const { relay, signer } = useNostr(); - const { account } = useAccount(accountId); - const [submitting, setSubmitting] = useState(false); - const [submitted, setSubmitted] = useState(false); - const instance = useInstance(); - - const { events } = useNostrReq((instance.nostr && account?.nostr) ? [{ - kinds: [7000, 6951], - authors: [instance.nostr.pubkey], - '#p': [account.nostr.pubkey], - }] : []); - - const success = events.find((event) => event.kind === 6951); - const feedback = events.find((event) => event.kind === 7000); - - const handleJoin = async () => { - if (!relay || !signer || !instance.nostr) return; - setSubmitting(true); - - const event = await signer.signEvent({ - kind: 5951, - content: '', - tags: [ - ['i', instance.nostr.relay, 'text'], - ['p', instance.nostr.pubkey, 'text'], - ], - created_at: Math.floor(Date.now() / 1000), - }); - - await relay.event(event); - - setSubmitting(false); - setSubmitted(true); - }; - - const username = useMemo( - () => n.bech32().safeParse(account?.acct).success ? account?.acct.slice(0, 13) : account?.acct, - [account?.acct], - ); - - if (!account) { - return ; - } - - const acct = account.nostr.pubkey ? nip19.npubEncode(account.nostr.pubkey) : account.acct; - - return ( - } - onClose={onClose} - onBack={() => setStep('identity')} - > - - - - - - - - - - {username} - - - - - - {account.ditto.is_registered ? ( - - ) : ( - - - - - - {(success || feedback) ? ( - JSON.stringify(success || feedback, null, 2) - ) : ( - <>You need an account on {instance.title} to continue. - )} - - - - - - )} - - - ); -}; - -export default AccountStep; diff --git a/src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx b/src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx deleted file mode 100644 index 64f8a1814..000000000 --- a/src/features/ui/components/modals/nostr-signin-modal/steps/identity-step.tsx +++ /dev/null @@ -1,95 +0,0 @@ -import React, { useState } from 'react'; -import { FormattedMessage, defineMessages, useIntl } from 'react-intl'; - -import { accountLookup } from 'soapbox/actions/accounts'; -import { Button, Form, FormGroup, HStack, Input, Stack, Modal } from 'soapbox/components/ui'; -import { useAppDispatch } from 'soapbox/hooks'; - -import EmojiGraphic from '../components/emoji-graphic'; -import NostrExtensionIndicator from '../components/nostr-extension-indicator'; -import { Step } from '../nostr-signin-modal'; - -interface IIdentityStep { - setAccountId(accountId: string): void; - setStep(step: Step): void; - onClose(): void; -} - -const messages = defineMessages({ - notFound: { id: 'nostr_signin.identity.not_found', defaultMessage: 'Account not found' }, - nsec: { id: 'nostr_signin.identity.nsec', defaultMessage: 'Enter your public key' }, -}); - -const IdentityStep: React.FC = ({ setAccountId, setStep, onClose }) => { - const intl = useIntl(); - const dispatch = useAppDispatch(); - - const [error, setError] = useState(); - const [loading, setLoading] = useState(false); - const [username, setUsername] = useState(''); - - const handleChangeUsername: React.ChangeEventHandler = (e) => { - setError(undefined); - setUsername(e.target.value); - }; - - const handleSubmit = async () => { - setLoading(true); - - if (username.startsWith('nsec1')) { - setError(intl.formatMessage(messages.nsec)); - setLoading(false); - return; - } - - try { - const account = await dispatch(accountLookup(username)); - setAccountId(account.id); - setStep('account'); - } catch (e: any) { - if (e.response?.status === 404) { - setError(intl.formatMessage(messages.notFound)); - } - setLoading(false); - } - }; - - return ( - } onClose={onClose}> -
- -
- -
- - - - - - - - - - - - -
-
-
- ); -}; - -export default IdentityStep; diff --git a/src/features/ui/components/modals/nostr-signin-modal/steps/key-add-step.tsx b/src/features/ui/components/modals/nostr-signin-modal/steps/key-add-step.tsx index ea3e6e9ba..06ab0c2c5 100644 --- a/src/features/ui/components/modals/nostr-signin-modal/steps/key-add-step.tsx +++ b/src/features/ui/components/modals/nostr-signin-modal/steps/key-add-step.tsx @@ -1,4 +1,4 @@ -import { getPublicKey, nip19 } from 'nostr-tools'; +import { nip19 } from 'nostr-tools'; import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; @@ -7,15 +7,12 @@ import { NKeys } from 'soapbox/features/nostr/keys'; import EmojiGraphic from '../components/emoji-graphic'; import NostrExtensionIndicator from '../components/nostr-extension-indicator'; -import { Step } from '../nostr-signin-modal'; interface IKeyAddStep { - setAccountId(accountId: string): void; - setStep(step: Step): void; onClose(): void; } -const KeyAddStep: React.FC = ({ setAccountId, setStep, onClose }) => { +const KeyAddStep: React.FC = ({ onClose }) => { const [nsec, setNsec] = useState(''); const [error, setError] = useState(); @@ -29,10 +26,8 @@ const KeyAddStep: React.FC = ({ setAccountId, setStep, onClose }) = const result = nip19.decode(nsec); if (result.type === 'nsec') { const seckey = result.data; - const pubkey = getPublicKey(seckey); NKeys.add(seckey); - setAccountId(pubkey); - setStep('account'); + // TODO: log in, close modal } } catch (e) { setError('Invalid nsec'); diff --git a/src/features/ui/components/modals/nostr-signin-modal/steps/key-step.tsx b/src/features/ui/components/modals/nostr-signin-modal/steps/key-step.tsx index 7d2e6ef62..fef9bd9b1 100644 --- a/src/features/ui/components/modals/nostr-signin-modal/steps/key-step.tsx +++ b/src/features/ui/components/modals/nostr-signin-modal/steps/key-step.tsx @@ -25,7 +25,7 @@ const KeyStep: React.FC = ({ setStep, onClose }) => { Generate key - diff --git a/src/features/ui/components/modals/nostr-signin-modal/steps/keygen-step.tsx b/src/features/ui/components/modals/nostr-signin-modal/steps/keygen-step.tsx index 506a1313b..b024b2356 100644 --- a/src/features/ui/components/modals/nostr-signin-modal/steps/keygen-step.tsx +++ b/src/features/ui/components/modals/nostr-signin-modal/steps/keygen-step.tsx @@ -11,15 +11,12 @@ import { download } from 'soapbox/utils/download'; import { slugify } from 'soapbox/utils/input'; import EmojiGraphic from '../components/emoji-graphic'; -import { Step } from '../nostr-signin-modal'; interface IKeygenStep { - setAccountId(accountId: string): void; - setStep(step: Step): void; onClose(): void; } -const KeygenStep: React.FC = ({ setAccountId, setStep, onClose }) => { +const KeygenStep: React.FC = ({ onClose }) => { const instance = useInstance(); const dispatch = useAppDispatch(); @@ -45,8 +42,7 @@ const KeygenStep: React.FC = ({ setAccountId, setStep, onClose }) = const handleNext = () => { NKeys.add(secretKey); - setAccountId(pubkey); // HACK: Ditto uses pubkeys as account IDs. - setStep('account'); + // TODO: log in, close modal }; return (