Skip to content

Commit

Permalink
feat(connect): eject device [wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed Jan 7, 2024
1 parent 6d05b57 commit 11b240b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
21 changes: 20 additions & 1 deletion packages/connect-ui/src/components/InfoPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { ReactNode } from 'react';

import styled from 'styled-components';

import { Button } from '@trezor/components';

import { State } from '../types';

const Aside = styled.aside`
display: flex;
flex-flow: column;
Expand Down Expand Up @@ -70,13 +74,28 @@ interface InfoPanelProps {
origin?: string;
hostLabel?: string;
topSlot?: ReactNode;
preferredDevice?: State['preferredDevice'];
onEjectDevice?: () => void;
}

export const InfoPanel = ({ method, origin, hostLabel, topSlot }: InfoPanelProps) => (
export const InfoPanel = ({
method,
origin,
topSlot,
hostLabel,
preferredDevice,
onEjectDevice,
}: InfoPanelProps) => (
<>
<Aside data-test="@info-panel">
{/* notifications appear hear */}
{topSlot && topSlot}
{preferredDevice && onEjectDevice && (
<>
<div>zpreferovany device: {JSON.stringify({ preferredDevice })}</div>
<Button onClick={onEjectDevice}>eject</Button>
</>
)}

<MainSlot>
<Header>
Expand Down
7 changes: 7 additions & 0 deletions packages/connect-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UI_REQUEST,
POPUP,
createPopupMessage,
createUiResponse,
} from '@trezor/connect';
import { storage, OriginBoundState } from '@trezor/connect-common';

Expand Down Expand Up @@ -162,6 +163,12 @@ export const ConnectUI = ({ postMessage, clearLegacyView }: ConnectUIProps) => {
origin={state?.settings?.origin}
hostLabel={state?.settings?.hostLabel}
topSlot={Object.values(Notifications)}
preferredDevice={state?.preferredDevice}
onEjectDevice={() => {
postMessage(createUiResponse(UI.EJECT_DEVICE));
postMessage({ type: POPUP.CLOSE_WINDOW });
window.close();
}}
/>
{Component && (
<div
Expand Down
13 changes: 13 additions & 0 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ export const handleMessage = (message: CoreMessage) => {
}
break;
}
// todo: not sure about name UI group
case UI.EJECT_DEVICE: {
const origin = DataManager.getSettings('origin');
if (!origin) {
return;
}
storage.save(data => {
delete data.origin[origin]?.preferredDevice;
return data;
});

break;
}

// message from index
case IFRAME.CALL:
Expand Down
9 changes: 8 additions & 1 deletion packages/connect/src/events/ui-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const UI_RESPONSE = {
INVALID_PASSPHRASE_ACTION: 'ui-invalid_passphrase_action',
CHANGE_SETTINGS: 'ui-change_settings',
LOGIN_CHALLENGE_RESPONSE: 'ui-login_challenge_response',
EJECT_DEVICE: 'ui-eject_device',
} as const;

export interface UiResponsePopupHandshake {
Expand Down Expand Up @@ -100,6 +101,11 @@ export interface UiResponseLoginChallenge {
};
}

export interface UiResponseEjectDevice {
type: typeof UI_RESPONSE.EJECT_DEVICE;
payload: undefined;
}

export type UiResponseEvent =
| UiResponsePopupHandshake
| UiResponsePermission
Expand All @@ -111,7 +117,8 @@ export type UiResponseEvent =
| UiResponsePassphraseAction
| UiResponseAccount
| UiResponseFee
| UiResponseLoginChallenge;
| UiResponseLoginChallenge
| UiResponseEjectDevice;

export type UiResponseMessage = UiResponseEvent & { event: typeof UI_EVENT };

Expand Down

0 comments on commit 11b240b

Please sign in to comment.