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

Sui Feature Request - Minor bug with domain display in wallet #20410

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 44 additions & 6 deletions apps/wallet/src/ui/app/components/accounts/NicknameDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { useResolveSuiNSName } from '_app/hooks/useAppResolveSuinsName';
import {
Dialog,
DialogContent,
Expand All @@ -10,7 +11,7 @@ import {
DialogTrigger,
} from '_src/ui/app/shared/Dialog';
import { useZodForm } from '@mysten/core';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import toast from 'react-hot-toast';
import { z } from 'zod';

Expand All @@ -22,37 +23,59 @@ import { TextField } from '../../shared/forms/TextField';

const formSchema = z.object({
nickname: z.string().trim(),
useDomain: z.boolean().optional(),
});

interface NicknameDialogProps {
accountID: string;
trigger: JSX.Element;
}

type FormValues = {
nickname: string;
useDomain?: boolean;
};

export function NicknameDialog({ accountID, trigger }: NicknameDialogProps) {
const [open, setOpen] = useState(false);
const backgroundClient = useBackgroundClient();
const { data: accounts } = useAccounts();
const account = accounts?.find((account) => account.id === accountID);
const domainName = useResolveSuiNSName(account?.address);

const form = useZodForm({
mode: 'all',
schema: formSchema,
defaultValues: {
nickname: account?.nickname ?? '',
useDomain: false,
},
});

const {
register,
watch,
setValue,
formState: { isSubmitting, isValid },
} = form;

const onSubmit = async ({ nickname }: { nickname: string }) => {
const useDomain = watch('useDomain');

useEffect(() => {
if (useDomain) {
setValue('nickname', '');
} else {
setValue('nickname', account?.nickname ?? '');
}
}, [useDomain, account?.nickname, setValue]);

const onSubmit = async ({ nickname, useDomain }: FormValues) => {
if (account && accountID) {
try {
const finalNickname = useDomain ? null : nickname;
await backgroundClient.setAccountNickname({
id: accountID,
nickname: nickname || null,
nickname: finalNickname,
});
setOpen(false);
} catch (e) {
Expand All @@ -72,7 +95,22 @@ export function NicknameDialog({ accountID, trigger }: NicknameDialogProps) {
</DialogDescription>
</DialogHeader>
<Form className="flex flex-col gap-6 h-full" form={form} onSubmit={onSubmit}>
<TextField label="Personalize account with a nickname." {...register('nickname')} />
{domainName && (
<div className="flex items-center gap-2">
<input type="checkbox" {...register('useDomain')} id="use-domain" />
<label htmlFor="use-domain" className="text-sm flex items-center gap-1">
Use Sui Name Service:
<span className="font-medium text-hero px-2 py-0.5 bg-hero/10 rounded-lg">
{domainName}
</span>
</label>
</div>
)}
<TextField
label="Personalize account with a nickname."
{...register('nickname')}
disabled={useDomain}
/>
<div className="flex gap-2.5">
<Button variant="outline" size="tall" text="Cancel" onClick={() => setOpen(false)} />
<Button
Expand All @@ -81,11 +119,11 @@ export function NicknameDialog({ accountID, trigger }: NicknameDialogProps) {
variant="primary"
size="tall"
loading={isSubmitting}
text={'Save'}
text="Save"
/>
</div>
</Form>
</DialogContent>
</Dialog>
);
}
}
Loading