Skip to content

Commit 29a2c7d

Browse files
committed
feat: auto close controller (placeholder)
1 parent 0308476 commit 29a2c7d

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

client/src/components/ui/marketplace/token-detail/TokenFooterActions.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OrderModel } from "@cartridge/arcade";
33
import { useAtomValue } from "@effect-atom/atom-react";
44
import { orderWithUsdAtom } from "@/effect/atoms/marketplace";
55
import { PriceFooter } from "../../modules/price-footer";
6+
import { useControllerCloseAfterToast } from "@/hooks/controller";
67

78
interface TokenFooterActionsProps {
89
isOwner: boolean;
@@ -76,6 +77,7 @@ export function TokenFooterActions({
7677
handleSend,
7778
}: TokenFooterActionsProps) {
7879
const orderWithUsd = useAtomValue(orderWithUsdAtom(order));
80+
useControllerCloseAfterToast();
7981

8082
if (!isOwner && !isListed) {
8183
return null;

client/src/hooks/controller.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use client";
2+
3+
import { useEffect, useState } from "react";
4+
import { useAccount } from "@starknet-react/core";
5+
import {
6+
CONTROLLER_TOAST_MESSAGE_TYPE,
7+
type ToastOptions,
8+
} from "@cartridge/ui";
9+
import type ControllerConnector from "@cartridge/connector/controller";
10+
11+
export function useControllerUsername(): string | undefined {
12+
const { account, connector } = useAccount();
13+
const [username, setUSername] = useState<string>();
14+
useEffect(() => {
15+
if (account && connector) {
16+
(connector as ControllerConnector)
17+
.username()
18+
?.then((u) => setUSername(u));
19+
}
20+
}, [account, connector]);
21+
return username;
22+
}
23+
24+
export function useControllerCloseAfterToast() {
25+
const { connector } = useAccount();
26+
useEffect(() => {
27+
if (!connector) return;
28+
29+
const eventHandler = (event: any) => {
30+
const options =
31+
event.data.type === CONTROLLER_TOAST_MESSAGE_TYPE
32+
? (event.data.options as ToastOptions)
33+
: undefined;
34+
35+
if (options?.safeToClose) {
36+
// TODO: enable when controller is updated
37+
console.log("Can safely close Controller...");
38+
// (connector as ControllerConnector).close();
39+
}
40+
};
41+
42+
window.addEventListener("message", eventHandler);
43+
return () => {
44+
window.removeEventListener("message", eventHandler);
45+
};
46+
}, [connector]);
47+
}

client/src/hooks/marketplace-actions-handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useConnectionViewModel } from "@/features/connection";
99
import { useTokenContracts } from "@/effect/hooks/tokens";
1010
import { CollectionType } from "@/effect/atoms/tokens";
1111
import type { OrderModel } from "@cartridge/arcade";
12-
import { useUsername } from "@/hooks/username";
12+
import { useControllerUsername } from "@/hooks/controller";
1313
import { useArcade } from "./arcade";
1414

1515
export type ActionHandlerParams = {
@@ -258,7 +258,7 @@ function useControllerPathBuilder(
258258
): (params: MakeControllerViewPathParams) => string | undefined {
259259
const { trackEvent, events } = useAnalytics();
260260

261-
const username = useUsername();
261+
const username = useControllerUsername();
262262

263263
const collections = useTokenContracts();
264264

client/src/hooks/username.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +0,0 @@
1-
import { useEffect, useState } from "react";
2-
import { useAccount } from "@starknet-react/core";
3-
import type ControllerConnector from "@cartridge/connector/controller";
4-
5-
export function useUsername(): string | undefined {
6-
const { account, connector } = useAccount();
7-
const [username, setUSername] = useState<string>();
8-
useEffect(() => {
9-
if (account && connector) {
10-
(connector as ControllerConnector)
11-
.username()
12-
?.then((u) => setUSername(u));
13-
}
14-
}, [account, connector]);
15-
16-
return username;
17-
}

0 commit comments

Comments
 (0)