Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout when user click close on upgrade screen #1052

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/keychain/src/components/Provider/connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type ConnectionContextValue = {
setController: (controller: Controller) => void;
closeModal: () => void;
openModal: () => void;
logout: (context: ConnectionCtx) => void;
logout: () => void;
openSettings: () => void;
};

Expand Down
7 changes: 4 additions & 3 deletions packages/keychain/src/components/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import { useState } from "react";
import { Container, Footer } from "components/layout";
import { Recovery } from "./Recovery";
import { Delegate } from "./Delegate";
import { useConnection } from "hooks/connection";

enum State {
SETTINGS,
RECOVERY,
DELEGATE,
}

export function Settings({ onLogout }: { onLogout: () => void }) {
// const { controller, context, setContext } = useConnection();
export function Settings() {
const { logout } = useConnection();
const [state, setState] = useState<State>(State.SETTINGS);
// const [delegateAccount, setDelegateAccount] = useState("");

Expand Down Expand Up @@ -159,7 +160,7 @@ export function Settings({ onLogout }: { onLogout: () => void }) {
</VStack>
</Content> */}
<Footer>
<Button w="full" onClick={onLogout}>
<Button w="full" onClick={logout}>
Log out
</Button>
</Footer>
Expand Down
24 changes: 15 additions & 9 deletions packages/keychain/src/components/connect/Logout.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { Button } from "@chakra-ui/react";
import { Container, Footer } from "components/layout";
import { LogoutDuoIcon } from "@cartridge/ui";
import { useConnection } from "hooks/connection";
import { ResponseCodes } from "@cartridge/controller";

export function Logout({
onConfirm,
onCancel,
}: {
onConfirm: () => void;
onCancel: () => void;
}) {
export function Logout() {
const { context, logout } = useConnection();
return (
<Container Icon={LogoutDuoIcon} title="Log Out" description="Are you sure?">
<Footer>
<Button colorScheme="colorful" onClick={onConfirm}>
<Button colorScheme="colorful" onClick={logout}>
Log Out
</Button>
<Button onClick={onCancel}>Cancel</Button>
<Button
onClick={() => {
context.resolve({
code: ResponseCodes.CANCELED,
message: "User cancelled logout",
});
}}
>
Cancel
</Button>
</Footer>
</Container>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { TimesIcon } from "@cartridge/ui";
import { isIframe } from "@cartridge/utils";
import { IconButton } from "@chakra-ui/react";
import { useConnection } from "hooks/connection";
import { useCallback } from "react";

export function CloseButton() {
const { closeModal } = useConnection();
const { upgrade, logout, closeModal } = useConnection();

const onClose = useCallback(() => {
if (upgrade.available) {
logout();
}
closeModal();
}, [upgrade.available, logout, closeModal]);

if (!isIframe()) {
return null;
Expand All @@ -19,7 +27,7 @@ export function CloseButton() {
opacity: 0.75,
}}
icon={<TimesIcon fontSize={24} />}
onClick={closeModal}
onClick={onClose}
/>
);
}
18 changes: 9 additions & 9 deletions packages/keychain/src/hooks/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Controller from "utils/controller";
import {
connectToController,
ConnectionCtx,
LogoutCtx,
OpenSettingsCtx,
} from "utils/connection";
import { getChainName, isIframe } from "@cartridge/utils";
Expand Down Expand Up @@ -155,14 +154,15 @@ export function useConnectionValue() {
}
}, [rpcUrl, controller]);

const logout = useCallback((context: ConnectionCtx) => {
setContext({
origin: context.origin,
type: "logout",
resolve: context.resolve,
reject: context.reject,
} as LogoutCtx);
}, []);
const logout = useCallback(() => {
window.controller?.disconnect();
setController(undefined);

context.resolve({
code: ResponseCodes.NOT_CONNECTED,
message: "User logged out",
});
}, [context, setController]);

const openSettings = useCallback(() => {
setContext({
Expand Down
46 changes: 4 additions & 42 deletions packages/keychain/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import { ResponseCodes } from "@cartridge/controller";
import { DeployController, ConfirmTransaction, SignMessage } from "components";
import { CreateController, CreateSession, Logout } from "components/connect";
import { useConnection } from "hooks/connection";
import {
DeployCtx,
LogoutCtx,
OpenSettingsCtx,
SignMessageCtx,
} from "utils/connection";
import { DeployCtx, SignMessageCtx } from "utils/connection";
import { LoginMode } from "components/connect/types";
import { ErrorPage } from "components/ErrorBoundary";
import { Settings } from "components/Settings";
Expand All @@ -19,8 +14,7 @@ import { useEffect } from "react";
import { usePostHog } from "posthog-js/react";

function Home() {
const { context, controller, setController, error, policies, upgrade } =
useConnection();
const { context, controller, error, policies, upgrade } = useConnection();
const posthog = usePostHog();

useEffect(() => {
Expand Down Expand Up @@ -85,26 +79,7 @@ function Home() {
case "logout": {
posthog?.capture("Call Logout");

const ctx = context as LogoutCtx;
return (
<Logout
onConfirm={() => {
window.controller?.disconnect();
setController(undefined);

ctx.resolve({
code: ResponseCodes.NOT_CONNECTED,
message: "User logged out",
});
}}
onCancel={() =>
ctx.resolve({
code: ResponseCodes.CANCELED,
message: "User cancelled logout",
})
}
/>
);
return <Logout />;
}
case "sign-message": {
posthog?.capture("Call Sign Message");
Expand Down Expand Up @@ -147,20 +122,7 @@ function Home() {
case "open-settings": {
posthog?.capture("Call Open Settings");

const ctx = context as OpenSettingsCtx;
return (
<Settings
onLogout={() => {
window.controller?.disconnect();
setController(undefined);

ctx.resolve({
code: ResponseCodes.NOT_CONNECTED,
message: "User logged out",
});
}}
/>
);
return <Settings />;
}
case "open-purchase-credits": {
posthog?.capture("Call Purchase Credits");
Expand Down
Loading