Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable numeraires in onboarding #203

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions apps/extension/src/hooks/numeraires-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ import { ChainRegistryClient } from '@penumbra-labs/registry';
import { useMemo } from 'react';

export const useNumeraires = (chainId?: string) => {
const { data, isLoading, error, isError } = useQuery({
const { data, isLoading, isError } = useQuery({
queryKey: ['registry', chainId],
queryFn: async () => {
const registryClient = new ChainRegistryClient();
return registryClient.remote.get(chainId!);
},
retry: 1,
retryDelay: 0,
Comment on lines +12 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single retry feels ok, without specifying these, react-query tends to retry indefinitely.

Copy link
Contributor

@TalDerei TalDerei Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: seems like react-query defaults to three consecutive retries?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Maybe it's the delay then that causes such long retry times. Alas, for our case, we want to fail fast given this is not a critical setting to select.

staleTime: Infinity,
enabled: Boolean(chainId),
});

const numeraires = useMemo(() => {
if (!chainId) {
if (isError) {
console.error(`Could not load numeraires for chainId: ${chainId}`);
}
return [];
Comment on lines -17 to -21
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the !chainId conditional was necessary. Removing.

if (isError) {
console.error(`Could not load numeraires for chainId: ${chainId}`);
}

return data?.numeraires.map(n => data.getMetadata(n)) ?? [];
}, [data, chainId, isError]);

return { numeraires, isLoading, error };
return { numeraires, isLoading, isError };
};
4 changes: 3 additions & 1 deletion apps/extension/src/routes/page/onboarding/set-numeraire.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { FadeTransition } from '@repo/ui/components/ui/fade-transition';
import { usePageNav } from '../../../utils/navigate';
import { PagePath } from '../paths';
import { NumeraireForm } from '../../../shared/components/numeraires-form';
import { useStore } from '../../../state';

export const SetNumerairesPage = () => {
const navigate = usePageNav();
const chainId = useStore(state => state.network.chainId);

const onSuccess = (): void => {
navigate(PagePath.ONBOARDING_SUCCESS);
Expand All @@ -22,7 +24,7 @@ export const SetNumerairesPage = () => {
</CardDescription>
</CardHeader>
<div className='mt-6'>
<NumeraireForm isOnboarding onSuccess={onSuccess} />
<NumeraireForm isOnboarding onSuccess={onSuccess} chainId={chainId} />
</div>
</Card>
</FadeTransition>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { NumerairesGradientIcon } from '../../../icons/numeraires-gradient';
import { usePopupNav } from '../../../utils/navigate';
import { PopupPath } from '../paths';
import { NumeraireForm } from '../../../shared/components/numeraires-form';
import { useChainIdQuery } from '../../../hooks/chain-id';

export const SettingsNumeraires = () => {
const navigate = usePopupNav();
const { chainId } = useChainIdQuery();

const onSuccess = () => {
navigate(PopupPath.INDEX);
};
return (
<SettingsScreen title='Price denominations' IconComponent={NumerairesGradientIcon}>
<NumeraireForm onSuccess={onSuccess} />
<NumeraireForm onSuccess={onSuccess} chainId={chainId} />
</SettingsScreen>
);
};
26 changes: 11 additions & 15 deletions apps/extension/src/shared/components/numeraires-form.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AllSlices, useStore } from '../../state';
import { useChainIdQuery } from '../../hooks/chain-id';
import { useEffect, useState } from 'react';
import { ServicesMessage } from '../../message/services';
import { SelectList } from '@repo/ui/components/ui/select';
Expand All @@ -13,39 +12,36 @@ const useNumerairesSelector = (state: AllSlices) => {
selectedNumeraires: state.numeraires.selectedNumeraires,
selectNumeraire: state.numeraires.selectNumeraire,
saveNumeraires: state.numeraires.saveNumeraires,
networkChainId: state.network.chainId,
};
};

export const NumeraireForm = ({
isOnboarding,
onSuccess,
chainId,
}: {
chainId?: string;
isOnboarding?: boolean;
onSuccess: () => void | Promise<void>;
onSuccess: () => void;
}) => {
const { chainId } = useChainIdQuery();
const { selectedNumeraires, selectNumeraire, saveNumeraires, networkChainId } =
useStore(useNumerairesSelector);

// 'chainId' from 'useChainIdQuery' is not available during onboarding,
// this forces you to use two sources to guarantee 'chainId' for both settings and onboarding
const { numeraires } = useNumeraires(chainId ?? networkChainId);
Comment on lines -28 to -33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing both (serving the settings & onboarding simultaneously internally), I changed the component so that chainId is passed in

const { selectedNumeraires, selectNumeraire, saveNumeraires } = useStore(useNumerairesSelector);
const { numeraires, isLoading, isError } = useNumeraires(chainId);

// If query errors or there aren't numeraires in the registry, can skip
useEffect(() => {
if (numeraires.length === 0) {
void onSuccess();
if (isError || (!isLoading && numeraires.length === 0)) {
onSuccess();
}
}, [numeraires]);
}, [numeraires.length, isError, isLoading]);
Comment on lines +30 to +35
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous source of the bug. During the loading state, the numeraires array was length zero. Hence it was being skipped. Now it only skips if there is an error or it's still loading.


const [loading, setLoading] = useState(false);

const handleSubmit = () => {
setLoading(true);
void (async function () {
await saveNumeraires();
await chrome.runtime.sendMessage(ServicesMessage.ChangeNumeraires);
await onSuccess();
void chrome.runtime.sendMessage(ServicesMessage.ChangeNumeraires);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not feel necessary to await the response here, hold up the UI from progressing. The fire-and-forget works.

onSuccess();
})();
};

Expand Down