Skip to content

Commit dd05c76

Browse files
committed
Refactor Header component and useHats hook
1 parent 5dbe38d commit dd05c76

File tree

2 files changed

+66
-69
lines changed

2 files changed

+66
-69
lines changed

pkgs/frontend/app/components/Header.tsx

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -34,45 +34,37 @@ enum HeaderType {
3434
// ネットワーク切り替えコンポーネントを作成
3535
const NetworkSwitcher = ({
3636
switchToChain,
37-
connectedChainId,
3837
}: {
3938
switchToChain: (chain: Chain) => Promise<boolean>;
40-
connectedChainId?: number;
4139
}) => {
42-
const isWrongNetwork =
43-
connectedChainId && connectedChainId !== currentChain.id;
44-
4540
return (
4641
<MenuItem value="network" display="block">
4742
<Text fontWeight="bold" mb={2}>
4843
Network
4944
</Text>
50-
{isWrongNetwork ? (
51-
<>
52-
<Text color="red.500" fontSize="sm" mb={2}>
53-
⚠️ 異なるネットワークに接続されています
54-
</Text>
55-
<CommonButton
56-
key={currentChain.id}
57-
onClick={() => switchToChain(currentChain)}
58-
w="100%"
59-
mb={1}
60-
colorScheme="red"
61-
>
62-
{currentChain.name}に切り替える
63-
</CommonButton>
64-
</>
65-
) : (
45+
{/* チェーン切り替えボタンを表示 */}
46+
{/* {supportedChains.map((chain) => (
6647
<CommonButton
67-
key={currentChain.id}
68-
onClick={() => switchToChain(currentChain)}
48+
key={chain.id}
49+
onClick={() => switchToChain(chain)}
6950
w="100%"
7051
mb={1}
71-
variant="solid"
52+
variant={chain.id === currentChain.id ? "solid" : "outline"}
7253
>
73-
{currentChain.name}
54+
{chain.name}
7455
</CommonButton>
75-
)}
56+
))} */}
57+
58+
{/* 現在のチェーンを表示 */}
59+
<CommonButton
60+
key={currentChain.id}
61+
onClick={() => switchToChain(currentChain)}
62+
w="100%"
63+
mb={1}
64+
variant={"solid"}
65+
>
66+
{currentChain.name}
67+
</CommonButton>
7668
</MenuItem>
7769
);
7870
};
@@ -145,10 +137,6 @@ export const Header = () => {
145137

146138
const { switchToChain } = useSwitchNetwork();
147139

148-
const connectedChainId = wallets[0]
149-
? Number(wallets[0].chainId.split(":")[1])
150-
: undefined;
151-
152140
return headerType !== HeaderType.NonHeader ? (
153141
<Flex justifyContent="space-between" w="100%" py={3}>
154142
<Box display="flex" height="48px" flex="1" alignItems="center">
@@ -189,10 +177,7 @@ export const Header = () => {
189177
</Text>
190178
<Text fontSize="xs">{abbreviateAddress(identity.address)}</Text>
191179
</MenuItem>
192-
<NetworkSwitcher
193-
switchToChain={switchToChain}
194-
connectedChainId={connectedChainId}
195-
/>
180+
<NetworkSwitcher switchToChain={switchToChain} />
196181
<MenuItem value="logout" onClick={handleLogout}>
197182
Logout
198183
</MenuItem>
@@ -201,25 +186,12 @@ export const Header = () => {
201186
) : (
202187
<MenuRoot closeOnSelect={false}>
203188
<MenuTrigger asChild>
204-
<CommonButton
205-
w="auto"
206-
my="auto"
207-
colorScheme={
208-
connectedChainId && connectedChainId !== currentChain.id
209-
? "red"
210-
: undefined
211-
}
212-
>
213-
{connectedChainId && connectedChainId !== currentChain.id
214-
? "⚠️ ネットワークを切り替えてください"
215-
: currentChain.name}
189+
<CommonButton w="auto" my="auto">
190+
{currentChain.name}
216191
</CommonButton>
217192
</MenuTrigger>
218193
<MenuContent>
219-
<NetworkSwitcher
220-
switchToChain={switchToChain}
221-
connectedChainId={connectedChainId}
222-
/>
194+
<NetworkSwitcher switchToChain={switchToChain} />
223195
<MenuItem value="logout" onClick={handleLogout}>
224196
Logout
225197
</MenuItem>

pkgs/frontend/hooks/useHats.ts

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "@hatsprotocol/sdk-v1-subgraph";
77
import { HATS_ABI } from "abi/hats";
88
import { useCallback, useEffect, useState } from "react";
9+
import useSWR from "swr";
910
import { ipfs2https, ipfs2httpsJson } from "utils/ipfs";
1011
import { type Address, parseEventLogs } from "viem";
1112
import { base, optimism, sepolia } from "viem/chains";
@@ -35,28 +36,52 @@ export const hatsSubgraphClient = new HatsSubgraphClient({
3536
},
3637
});
3738

38-
export const useTreeInfo = (treeId: number) => {
39-
const [treeInfo, setTreeInfo] = useState<Tree>();
40-
41-
const { getTreeInfo } = useHats();
42-
43-
useEffect(() => {
44-
const fetch = async () => {
45-
setTreeInfo(undefined);
46-
if (!treeId) return;
47-
48-
const tree = await getTreeInfo({
49-
treeId: treeId,
50-
});
51-
if (!tree) return;
39+
export const useTreeInfo = (treeId?: number) => {
40+
const { data, error, isLoading } = useSWR(
41+
treeId ? ["treeInfo", treeId] : null,
42+
async ([_, treeId]) => {
43+
try {
44+
const response = await getTreeInfo(treeId);
45+
return response;
46+
} catch (error) {
47+
console.error("Error fetching tree info:", error);
48+
return null;
49+
}
50+
},
51+
);
5252

53-
setTreeInfo(tree);
54-
};
53+
return {
54+
treeInfo: data,
55+
isLoading,
56+
error,
57+
};
58+
};
5559

56-
fetch();
57-
}, [treeId, getTreeInfo]);
60+
export const useWearerInfo = (address?: string) => {
61+
const { data, error, isLoading } = useSWR(
62+
address ? ["wearerInfo", address] : null,
63+
async ([_, address]) => {
64+
try {
65+
const response = await getWearerInfo(address);
66+
return response;
67+
} catch (error) {
68+
if (error instanceof SubgraphWearerNotExistError) {
69+
// ユーザーがまだHatsを持っていない場合は空の配列を返す
70+
return {
71+
wearerStatic: [],
72+
wearerDynamic: [],
73+
};
74+
}
75+
throw error;
76+
}
77+
},
78+
);
5879

59-
return treeInfo;
80+
return {
81+
wearerInfo: data,
82+
isLoading,
83+
error,
84+
};
6085
};
6186

6287
/**

0 commit comments

Comments
 (0)