From 9b33ed36fca73ae406cf696cc8bedc84ec7dc6fc Mon Sep 17 00:00:00 2001 From: Hanssen0 Date: Thu, 17 Oct 2024 10:48:47 +0800 Subject: [PATCH] feat(connector-react): useCcc().isOpen --- .changeset/proud-jars-vanish.md | 5 +++++ packages/connector-react/src/hooks/useCcc.tsx | 20 +++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .changeset/proud-jars-vanish.md diff --git a/.changeset/proud-jars-vanish.md b/.changeset/proud-jars-vanish.md new file mode 100644 index 00000000..a6445362 --- /dev/null +++ b/.changeset/proud-jars-vanish.md @@ -0,0 +1,5 @@ +--- +"@ckb-ccc/connector-react": patch +--- + +feat(connector-react): useCcc().isOpen diff --git a/packages/connector-react/src/hooks/useCcc.tsx b/packages/connector-react/src/hooks/useCcc.tsx index 6c5a6d10..bfb769a0 100644 --- a/packages/connector-react/src/hooks/useCcc.tsx +++ b/packages/connector-react/src/hooks/useCcc.tsx @@ -15,7 +15,9 @@ import { Connector } from "../components/index.js"; const CCC_CONTEXT = createContext< | { + isOpen: boolean; open: () => unknown; + close: () => unknown; disconnect: () => unknown; setClient: (client: ccc.Client) => unknown; client: ccc.Client; @@ -81,7 +83,7 @@ export function Provider({ const [ref, setRef] = useState(null); const [isOpen, setIsOpen] = useState(false); const [_, setFlag] = useState(0); - const defautlSignersController = useRef< + const defaultSignersController = useRef< SignersControllerWithFilter | undefined >(undefined); @@ -93,6 +95,10 @@ export function Provider({ setIsOpen(true); ref?.requestUpdate(); }, [setIsOpen, ref, ref?.requestUpdate]); + const close = useCallback(() => { + setIsOpen(false); + ref?.requestUpdate(); + }, [setIsOpen, ref, ref?.requestUpdate]); const disconnect = useMemo( () => ref?.disconnect.bind(ref) ?? (() => {}), [ref, ref?.disconnect], @@ -108,19 +114,21 @@ export function Provider({ } }, [setClient]); useEffect(() => { - if (!defautlSignersController.current) { - defautlSignersController.current = new SignersControllerWithFilter( + if (!defaultSignersController.current) { + defaultSignersController.current = new SignersControllerWithFilter( signerFilter, ); } else { - defautlSignersController.current.filter = signerFilter; + defaultSignersController.current.filter = signerFilter; } }, [signerFilter]); return ( setFlag((f) => f + 1)} - onClose={() => setIsOpen(false)} + onClose={close} preferredNetworks={preferredNetworks} clientOptions={clientOptions} {...{