diff --git a/README.md b/README.md index b8fb07b..4040ddd 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,9 @@ Social dapp that allows users to monetize 💰 their social activity 🫂. ## 👥 Phase 2 (Social Activity and Indexing) -- **PunkSociety contract:** Following users, liking, commenting and sharing posts. -- **Search**: By address or username +- ✅ **PunkSociety contract:** Social interactions. +- ✅ **Search**: By address or username +- Enable following users, liking, commenting and sharing posts on frontend - **Notification system** - **Integrate The Graph to index activity** and save RPC calls (Reference: [Bootstrap a Full Stack Modern dapp using the Scaffold-ETH CLI and Subgraph Extension](https://siddhantk08.hashnode.dev/bootstrap-a-full-stack-modern-dapp-using-the-scaffold-eth-cli-and-subgraph-extension) | [The Graph tool for creating a subgraph](https://thegraph.com/docs/en/developing/creating-a-subgraph/)) diff --git a/packages/nextjs/app/search/Search.tsx b/packages/nextjs/app/search/Search.tsx index 13ed36d..c430bfb 100644 --- a/packages/nextjs/app/search/Search.tsx +++ b/packages/nextjs/app/search/Search.tsx @@ -3,7 +3,7 @@ import { useState } from "react"; import { useRouter } from "next/navigation"; import { LoadingBars } from "../../components/punk-society/LoadingBars"; -import { AddressInput } from "~~/components/scaffold-eth"; +import { useScaffoldReadContract } from "~~/hooks/scaffold-eth"; export const Search = () => { const [searchQuery, setSearchQuery] = useState(""); @@ -11,47 +11,52 @@ export const Search = () => { const [error, setError] = useState(""); const router = useRouter(); + const { data: nameToAddress } = useScaffoldReadContract({ + contractName: "PunkProfile", + functionName: "nameToAddress", + args: [searchQuery], + watch: true, + }); + const handleSearch = async () => { if (!searchQuery) { - setError("Please enter an address."); + setError("Please enter an address or username."); return; } setLoading(true); setError(""); - const address = searchQuery; - - // If the search query is not an address, show an error - if (!/^0x[a-fA-F0-9]{40}$/.test(searchQuery)) { - setError("Invalid address format."); + try { + if (searchQuery.length > 17) { + // If the search query is longer than 17 characters, assume it's an Ethereum address + router.push(`/profile/${searchQuery}`); + } else if (nameToAddress && nameToAddress !== "0x0000000000000000000000000000000000000000") { + // If the search query resolves to a non-zero address, navigate to the resolved address profile + router.push(`/profile/${nameToAddress}`); + } else { + setError("Invalid address or username."); + } + } catch (error) { + setError("An error occurred while searching."); + } finally { setLoading(false); - return; } - - // Redirect to the user's profile page - router.push(`/profile/${address}`); }; - if (loading) { - return ; - } - return ( -
-
-
- -
- - {error &&

{error}

} - -
+
+ setSearchQuery(e.target.value.toLowerCase())} + placeholder="Enter username or address" + className="input input-bordered w-full max-w-xs" + /> + + {error &&

{error}

}
); }; diff --git a/packages/nextjs/components/punk-society/ProfileAddress.tsx b/packages/nextjs/components/punk-society/ProfileAddress.tsx index e0a8ef5..3f334f1 100644 --- a/packages/nextjs/components/punk-society/ProfileAddress.tsx +++ b/packages/nextjs/components/punk-society/ProfileAddress.tsx @@ -14,16 +14,6 @@ type AddressProps = { size?: "xs" | "sm" | "base" | "lg" | "xl" | "2xl" | "3xl"; }; -const blockieSizeMap = { - xs: 6, - sm: 7, - base: 8, - lg: 9, - xl: 10, - "2xl": 12, - "3xl": 15, -}; - /** * Displays an address (or ENS) with a Blockie image and option to copy address. */ @@ -72,17 +62,17 @@ export const ProfileAddress = ({ address, disableAddressLink, format, size = "ba Profile Picture ) : ( Profile Picture )}