Skip to content

Commit

Permalink
feat(connector-react): useCcc().isOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Oct 17, 2024
1 parent e88a666 commit 9b33ed3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-jars-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-ccc/connector-react": patch
---

feat(connector-react): useCcc().isOpen
20 changes: 14 additions & 6 deletions packages/connector-react/src/hooks/useCcc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,7 +83,7 @@ export function Provider({
const [ref, setRef] = useState<ccc.WebComponentConnector | null>(null);
const [isOpen, setIsOpen] = useState(false);
const [_, setFlag] = useState(0);
const defautlSignersController = useRef<
const defaultSignersController = useRef<
SignersControllerWithFilter | undefined
>(undefined);

Expand All @@ -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],
Expand All @@ -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 (
<CCC_CONTEXT.Provider
value={{
isOpen,
open,
close,
disconnect,
setClient,

Expand All @@ -134,11 +142,11 @@ export function Provider({
name={name}
icon={icon}
signersController={
signersController ?? defautlSignersController.current
signersController ?? defaultSignersController.current
}
ref={setRef}
onWillUpdate={() => setFlag((f) => f + 1)}
onClose={() => setIsOpen(false)}
onClose={close}
preferredNetworks={preferredNetworks}
clientOptions={clientOptions}
{...{
Expand Down

0 comments on commit 9b33ed3

Please sign in to comment.