From 2d64492887f9f696e30b46b8aeb1b8f667c66090 Mon Sep 17 00:00:00 2001 From: yu23ki14 Date: Sat, 11 Jan 2025 18:46:18 +0900 Subject: [PATCH 1/2] add error existing name --- pkgs/frontend/app/routes/api.namestone.$action.tsx | 13 +++++++++++++ pkgs/frontend/app/routes/login.tsx | 2 +- pkgs/frontend/app/routes/signup.tsx | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkgs/frontend/app/routes/api.namestone.$action.tsx b/pkgs/frontend/app/routes/api.namestone.$action.tsx index 660c611..a77f08d 100644 --- a/pkgs/frontend/app/routes/api.namestone.$action.tsx +++ b/pkgs/frontend/app/routes/api.namestone.$action.tsx @@ -52,6 +52,19 @@ export const action: ActionFunction = async ({ request, params }) => { switch (action) { case "set-name": { const { name, address, text_records } = await request.json(); + + // Check if name is already taken + const existingNames = await ns.searchNames({ + domain, + name, + exact_match: true, + }); + + // If name exists and is owned by a different address, throw error + if (existingNames.length > 0) { + throw data({ message: "Name is already taken" }, 409); + } + await ns.setName({ domain, name, address, text_records }); return { message: "OK" }; } diff --git a/pkgs/frontend/app/routes/login.tsx b/pkgs/frontend/app/routes/login.tsx index cfee441..c90102d 100644 --- a/pkgs/frontend/app/routes/login.tsx +++ b/pkgs/frontend/app/routes/login.tsx @@ -43,7 +43,7 @@ const Login: FC = () => { }, [wallet, navigate, fetchNames]); return ( - + diff --git a/pkgs/frontend/app/routes/signup.tsx b/pkgs/frontend/app/routes/signup.tsx index 6b3a4ff..71bdd6a 100644 --- a/pkgs/frontend/app/routes/signup.tsx +++ b/pkgs/frontend/app/routes/signup.tsx @@ -57,7 +57,7 @@ const Login: FC = () => { }; return ( - + Date: Sat, 11 Jan 2025 18:57:30 +0900 Subject: [PATCH 2/2] check name exist --- .../app/routes/$treeId_.splits.new.tsx | 2 +- pkgs/frontend/app/routes/signup.tsx | 51 +++++++++++-------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/pkgs/frontend/app/routes/$treeId_.splits.new.tsx b/pkgs/frontend/app/routes/$treeId_.splits.new.tsx index 63bb426..d44e227 100644 --- a/pkgs/frontend/app/routes/$treeId_.splits.new.tsx +++ b/pkgs/frontend/app/routes/$treeId_.splits.new.tsx @@ -221,7 +221,7 @@ const SplitterNew: FC = () => { const availableName = useMemo(() => { if (!splitterName) return false; - return addresses?.[0]?.length === 0; + return addresses[0].length === 0; }, [splitterName, addresses]); const { createSplits, previewSplits, isLoading } = useSplitsCreator( diff --git a/pkgs/frontend/app/routes/signup.tsx b/pkgs/frontend/app/routes/signup.tsx index 71bdd6a..3b4699d 100644 --- a/pkgs/frontend/app/routes/signup.tsx +++ b/pkgs/frontend/app/routes/signup.tsx @@ -3,7 +3,7 @@ import { useAddressesByNames, useSetName } from "hooks/useENS"; import { useUploadImageFileToIpfs } from "hooks/useIpfs"; import { useActiveWallet } from "hooks/useWallet"; import type { TextRecords } from "namestone-sdk"; -import { type FC, useMemo, useState } from "react"; +import { type FC, useCallback, useMemo, useState } from "react"; import { BasicButton } from "~/components/BasicButton"; import { CommonInput } from "~/components/common/CommonInput"; import { UserIcon } from "~/components/icon/UserIcon"; @@ -30,31 +30,42 @@ const Login: FC = () => { const availableName = useMemo(() => { if (!userName) return false; - return addresses?.[0]?.length === 0; + return addresses[0].length === 0; }, [userName, addresses]); - const handleSubmit = async () => { + const handleSubmit = useCallback(async () => { if (!wallet || !availableName) return; - const params: { - name: string; - address: string; - text_records: TextRecords; - } = { - name: userName, - address: wallet.account?.address, - text_records: {}, - }; + try { + const params: { + name: string; + address: string; + text_records: TextRecords; + } = { + name: userName, + address: wallet.account?.address, + text_records: {}, + }; - if (imageFile) { - const res = await uploadImageFileToIpfs(); - if (res) params.text_records.avatar = res.ipfsUri; - } - - await setName(params); + if (imageFile) { + const res = await uploadImageFileToIpfs(); + if (res) params.text_records.avatar = res.ipfsUri; + } - window.location.href = "/workspace"; - }; + await setName(params); + } catch (error) { + console.error(error); + } finally { + window.location.href = "/workspace"; + } + }, [ + availableName, + imageFile, + setName, + uploadImageFileToIpfs, + userName, + wallet, + ]); return (