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

fix: connect status is always connected #157

Merged
merged 8 commits into from
Mar 28, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { PlatformService } from '@nexus-wallet/types';
export const MOCK_PLATFORM_URL = 'internal://nexus-wallet.io';
export const MOCK_PLATFORM_PASSWORD = '12345678';

// TODO: move it to @nexus-wallet/testkit
export const mockPlatformService: PlatformService = /* istanbul ignore next */ {
/* istanbul ignore next */
getActiveSiteInfo: async () => undefined,
getRequesterAppInfo: async () => ({ url: MOCK_PLATFORM_URL }),
requestGrant: async () => {},
requestSignData: async () => ({ password: MOCK_PLATFORM_PASSWORD }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Flex, Icon, Box, FlexProps } from '@chakra-ui/react';
import { CheckCircleIcon } from '@chakra-ui/icons';
import { WhiteAlphaBox } from './WhiteAlphaBox';
import AvatarIcon from './icons/Avatar.svg';
import MinusCircle from '../Components/icons/MinusCircle.svg';

export type ConnectStatusCardProps = {
name: string;
Expand Down Expand Up @@ -30,7 +31,11 @@ export const ConnectStatusCard: FC<ConnectStatusCardProps> = ({ name, connected,
<Flex alignItems="center">
{connected !== undefined && (
<>
<CheckCircleIcon mr="4px" data-test-id="connectedStatusIndicator" w="16px" h="16px" color={'green.300'} />
{connected ? (
<CheckCircleIcon mr="4px" data-test-id="connectedStatusIndicator" w="16px" h="16px" color={'green.300'} />
) : (
<Icon as={MinusCircle} w="16px" h="16px" mr="4px" />
)}
<Box fontSize="sm" data-test-id="connectedStatusText">
{connected ? 'Connected' : 'Disconnected'}
</Box>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 26 additions & 1 deletion packages/extension-chrome/src/pages/Popup/containers/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import NetworkIcon from '../../Components/icons/Network.svg';
import { WhiteAlphaBox } from '../../Components/WhiteAlphaBox';
import { ConnectStatusCard } from '../../Components/ConnectStatusCard';
import { useConfigQuery } from '../../hooks/useConfigQuery';
import { useService } from '../../hooks/useService';
import { useQuery } from '@tanstack/react-query';

const FeedbackButton: FC<ButtonProps> = (props) => {
return (
Expand All @@ -16,9 +18,32 @@ const FeedbackButton: FC<ButtonProps> = (props) => {
);
};

const useConnectedStatus = () => {
const configService = useService('configService');
const platformService = useService('platformService');

const hasGrantedQuery = useQuery({
queryKey: ['hasGranted'],
queryFn: async () => {
// TODO: use GrantService to check if the current site is granted
const whitelist = await configService.getWhitelist();
const activeSite = await platformService.getActiveSiteInfo();
if (!activeSite?.url) {
return false;
}

const siteHost = new URL(activeSite.url).host;
return whitelist.some((item) => item.host === siteHost);
},
});

return hasGrantedQuery.data;
};

export const Home: FC = () => {
const navigate = useNavigate();
const { data: config } = useConfigQuery();
const connectedStatus = useConnectedStatus();

const entries = [
{
Expand All @@ -41,7 +66,7 @@ export const Home: FC = () => {

return (
<Flex flexDir="column" h="100%">
<ConnectStatusCard name={config?.nickname!} connected />
<ConnectStatusCard name={config?.nickname!} connected={connectedStatus} />

<WhiteAlphaBox direction="column" mt="20px">
{entries.map(({ title, icon, onClick, testId }) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const WhitelistSites: FC = () => {
return (
<Skeleton isLoaded={!!filteredSites}>
<Text as={Box} fontSize="md" mb="20px" w="100%">
Yan is connected to these sites. They can view your account address
{configQuery.data?.nickname} is connected to these sites. They can view your account address
</Text>
<InputGroup alignItems="center" h="60px" mb="20px">
<InputLeftElement
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-chrome/src/pages/hooks/useService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createGlobalState } from 'react-use';
import { createServicesFactory, Modules } from '../../services';
import { makeBrowserExtensionModulesFactory, Modules } from '../../services';

const useServiceFactory = createGlobalState(createServicesFactory());
const useServiceFactory = createGlobalState(makeBrowserExtensionModulesFactory());

export function useService<T extends keyof Modules>(name: T): Modules[T] {
const [serviceFactory] = useServiceFactory();
Expand Down
9 changes: 9 additions & 0 deletions packages/extension-chrome/src/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ export function createBrowserExtensionPlatformService(): PlatformService<Endpoin
}
return { url: tab.url };
},
getActiveSiteInfo: async () => {
const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
return (
tab && {
faviconUrl: tab.favIconUrl,
url: tab.url,
}
);
},
};
}

Expand Down
8 changes: 7 additions & 1 deletion packages/types/src/services/NotificationService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TransactionSkeletonObject } from '@ckb-lumos/helpers';
import { HexString } from '@ckb-lumos/lumos';

import { Tabs } from 'webextension-polyfill';
export interface PlatformService<Sender = unknown> {
/**
* request user to approve for signing a transaction,
Expand Down Expand Up @@ -30,6 +30,12 @@ export interface PlatformService<Sender = unknown> {
* @throws if the Chrome extension permission is not granted, e.g. favicon
*/
getRequesterAppInfo(sender: Sender): Promise<{ url: string }>;

/**
* get the active site(the active tab) information
* @throws if the Chrome extension permission is not granted
*/
getActiveSiteInfo(): Promise<Pick<Tabs.Tab, 'favIconUrl' | 'url'> | undefined>;
}

/**
Expand Down