Skip to content

Commit

Permalink
Merge pull request #205 from hackdays-io/feature/wallet
Browse files Browse the repository at this point in the history
update wallet client and get identity
  • Loading branch information
yu23ki14 authored Dec 5, 2024
2 parents caf5b9f + b4213ff commit 8983f2b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
12 changes: 10 additions & 2 deletions pkgs/frontend/app/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useState, useEffect } from "react";
import { useState, useEffect, useMemo } from "react";
import { Box, Flex, Text } from "@chakra-ui/react";
import { WorkspaceIcon } from "./icon/WorkspaceIcon";
import { UserIcon } from "./icon/UserIcon";
import { useLocation } from "@remix-run/react";
import { useActiveWalletIdentity } from "hooks/useENS";
import { ipfs2https } from "utils/ipfs";

const NO_HEADER_PATHS: string[] = ["/login", "/signup"]; // 適宜ヘッダーが不要なページのパスを追加
const WORKSPACES_PATHS: string[] = ["/workspaces"]; // 適宜ワークスペースが未選択な状態のページのパスを追加
Expand Down Expand Up @@ -34,7 +36,6 @@ export const Header = () => {
const isWorkspaceSelected = true;

// ToDo: ユーザーやワークスペースごとの各種データを取得するロジックを実装する
const userImageUrl: string | undefined = undefined;
const workspaceName: string | undefined = "Workspace Name";
const workspaceImageUrl: string | undefined = undefined;

Expand Down Expand Up @@ -62,6 +63,13 @@ export const Header = () => {
workspaceName,
]);

const { identity } = useActiveWalletIdentity();

const userImageUrl = useMemo(() => {
const avatar = identity?.text_records?.["avatar"];
return avatar ? ipfs2https(avatar) : undefined;
}, [identity]);

return headerType !== HeaderType.NonHeader ? (
<Flex justifyContent="space-between" w="100%">
<Box display="flex" height={HEADER_SIZE} flex="1">
Expand Down
46 changes: 24 additions & 22 deletions pkgs/frontend/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ export const Layout = withEmotionCache((props: LayoutProps, cache) => {
/>
</head>
<body>
{children}

<ScrollRestoration />
<Scripts />
</body>
</html>
);
});

export default function App() {
return (
<PrivyProvider
appId={import.meta.env.VITE_PRIVY_APP_ID}
config={{
embeddedWallets: {
createOnLogin: "users-without-wallets",
},
}}
>
<ChakraProvider>
{/* DarkMode 切り替えの実装の可能性に備え、ThemeProvider を残しておいてあります */}
{/* <ThemeProvider disableTransitionOnChange attribute="class"> */}

<Box
bg="gray.50"
width="100%"
Expand Down Expand Up @@ -60,31 +83,10 @@ export const Layout = withEmotionCache((props: LayoutProps, cache) => {
flexDirection="column"
position="relative"
>
{children}
<Outlet />
</Box>
</Container>
</Box>
<ScrollRestoration />
<Scripts />
</body>
</html>
);
});

export default function App() {
return (
<PrivyProvider
appId={import.meta.env.VITE_PRIVY_APP_ID}
config={{
embeddedWallets: {
createOnLogin: "users-without-wallets",
},
}}
>
<ChakraProvider>
{/* DarkMode 切り替えの実装の可能性に備え、ThemeProvider を残しておいてあります */}
{/* <ThemeProvider disableTransitionOnChange attribute="class"> */}
<Outlet />
{/* </ThemeProvider> */}
</ChakraProvider>
</PrivyProvider>
Expand Down
19 changes: 18 additions & 1 deletion pkgs/frontend/hooks/useENS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { NameData, TextRecords } from "namestone-sdk";
import axios from "axios";
import { useActiveWallet } from "./useWallet";

export const useActiveWalletIdentity = () => {
const { wallet } = useActiveWallet();

const address = useMemo(() => {
return [wallet?.account?.address!];
}, [wallet]);
const { names } = useNamesByAddresses(address);

const identity = useMemo(() => {
if (!names || names.length === 0) return;
return names[0][0];
}, [names]);

return { identity };
};

export const useNamesByAddresses = (addresses?: string[]) => {
const [names, setNames] = useState<NameData[][]>([]);
Expand Down
3 changes: 2 additions & 1 deletion pkgs/frontend/hooks/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createSmartAccountClient, SmartAccountClient } from "permissionless";
import { toSimpleSmartAccount } from "permissionless/accounts";
import { createPimlicoClient } from "permissionless/clients/pimlico";
import { useCallback, useEffect, useMemo, useState } from "react";
import { createWalletClient, custom, http, WalletClient } from "viem";
import { Address, createWalletClient, custom, http, WalletClient } from "viem";
import { entryPoint07Address } from "viem/account-abstraction";
import { currentChain, publicClient } from "./useViem";

Expand Down Expand Up @@ -86,6 +86,7 @@ export const useAccountClient = () => {
const walletClient = createWalletClient({
chain: currentChain,
transport: custom(provider),
account: wallet.address as Address,
});

setClient(walletClient);
Expand Down
6 changes: 6 additions & 0 deletions pkgs/frontend/utils/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ export const ipfsUploadFile = async (file: File) => {
throw error;
}
};

export const ipfs2https = (ipfsUri: string) => {
const { pinataGateway } = getPinataConfig();
const cid = ipfsUri.replace("ipfs://", "");
return `${pinataGateway}/ipfs/${cid}`;
};

0 comments on commit 8983f2b

Please sign in to comment.