Advance from KeygenStep to AccountStep

This commit is contained in:
Alex Gleason 2024-02-19 15:17:35 -06:00
parent 0a3eb6b187
commit 3157969645
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 17 additions and 6 deletions

View File

@ -154,7 +154,7 @@ const fetchAccount = (id: string) =>
const account = selectAccount(getState(), id); const account = selectAccount(getState(), id);
if (account) { if (account) {
return null; return Promise.resolve(null);
} }
dispatch(fetchAccountRequest(id)); dispatch(fetchAccountRequest(id));

View File

@ -30,7 +30,7 @@ const NostrSigninModal: React.FC<INostrSigninModal> = ({ onClose }) => {
case 'key': case 'key':
return <KeyStep setStep={setStep} onClose={handleClose} />; return <KeyStep setStep={setStep} onClose={handleClose} />;
case 'keygen': case 'keygen':
return <KeygenStep setSigner={setSigner} setStep={setStep} onClose={handleClose} />; return <KeygenStep setAccountId={setAccountId} setSigner={setSigner} setStep={setStep} onClose={handleClose} />;
case 'account': case 'account':
return <AccountStep accountId={accountId!} setStep={setStep} onClose={handleClose} />; return <AccountStep accountId={accountId!} setStep={setStep} onClose={handleClose} />;
case 'register': case 'register':

View File

@ -4,6 +4,7 @@ import { FormattedMessage } from 'react-intl';
import { useAccount } from 'soapbox/api/hooks'; import { useAccount } from 'soapbox/api/hooks';
import { Avatar, Text, Stack, Emoji, Button, Tooltip, Modal } from 'soapbox/components/ui'; import { Avatar, Text, Stack, Emoji, Button, Tooltip, Modal } from 'soapbox/components/ui';
import ModalLoading from 'soapbox/features/ui/components/modal-loading';
import { useInstance } from 'soapbox/hooks'; import { useInstance } from 'soapbox/hooks';
import { Step } from '../nostr-signin-modal'; import { Step } from '../nostr-signin-modal';
@ -24,7 +25,7 @@ const AccountStep: React.FC<IAccountStep> = ({ accountId, setStep, onClose }) =>
); );
if (!account) { if (!account) {
return null; return <ModalLoading />;
} }
return ( return (

View File

@ -1,12 +1,13 @@
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools'; import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
import { NostrSigner } from 'nspec'; import { NostrSigner } from 'nspec';
import React, { useMemo, useState } from 'react'; import React, { useEffect, useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { fetchAccount } from 'soapbox/actions/accounts';
import CopyableInput from 'soapbox/components/copyable-input'; import CopyableInput from 'soapbox/components/copyable-input';
import { Button, Stack, Modal, FormGroup, Text, Tooltip } from 'soapbox/components/ui'; import { Button, Stack, Modal, FormGroup, Text, Tooltip } from 'soapbox/components/ui';
import { NKeys } from 'soapbox/features/nostr/keys'; import { NKeys } from 'soapbox/features/nostr/keys';
import { useInstance } from 'soapbox/hooks'; import { useAppDispatch, useInstance } from 'soapbox/hooks';
import { download } from 'soapbox/utils/download'; import { download } from 'soapbox/utils/download';
import { slugify } from 'soapbox/utils/input'; import { slugify } from 'soapbox/utils/input';
@ -14,13 +15,15 @@ import EmojiGraphic from '../components/emoji-graphic';
import { Step } from '../nostr-signin-modal'; import { Step } from '../nostr-signin-modal';
interface IKeygenStep { interface IKeygenStep {
setAccountId(accountId: string): void;
setSigner(signer: NostrSigner): void; setSigner(signer: NostrSigner): void;
setStep(step: Step): void; setStep(step: Step): void;
onClose(): void; onClose(): void;
} }
const KeygenStep: React.FC<IKeygenStep> = ({ setSigner, setStep, onClose }) => { const KeygenStep: React.FC<IKeygenStep> = ({ setAccountId, setSigner, setStep, onClose }) => {
const instance = useInstance(); const instance = useInstance();
const dispatch = useAppDispatch();
const secretKey = useMemo(() => generateSecretKey(), []); const secretKey = useMemo(() => generateSecretKey(), []);
const pubkey = useMemo(() => getPublicKey(secretKey), [secretKey]); const pubkey = useMemo(() => getPublicKey(secretKey), [secretKey]);
@ -30,6 +33,11 @@ const KeygenStep: React.FC<IKeygenStep> = ({ setSigner, setStep, onClose }) => {
const [downloaded, setDownloaded] = useState(false); const [downloaded, setDownloaded] = useState(false);
useEffect(() => {
// Pre-fetch into cache.
dispatch(fetchAccount(pubkey)).catch(() => {});
}, [pubkey]);
const handleDownload = () => { const handleDownload = () => {
download(nsec, `${slugify(instance.title)}-${npub.slice(5, 9)}.nsec.txt`); download(nsec, `${slugify(instance.title)}-${npub.slice(5, 9)}.nsec.txt`);
setDownloaded(true); setDownloaded(true);
@ -40,6 +48,8 @@ const KeygenStep: React.FC<IKeygenStep> = ({ setSigner, setStep, onClose }) => {
const handleNext = () => { const handleNext = () => {
const signer = NKeys.add(secretKey); const signer = NKeys.add(secretKey);
setSigner(signer); setSigner(signer);
setAccountId(pubkey); // HACK: Ditto uses pubkeys as account IDs.
setStep('account');
}; };
return ( return (