Skip to content

Commit

Permalink
feat(extension-chrome): optimize status query logic
Browse files Browse the repository at this point in the history
  • Loading branch information
IronLu233 committed Mar 28, 2023
1 parent f506272 commit 5e2d396
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions packages/extension-chrome/src/pages/Popup/containers/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useMemo } from 'react';
import React, { FC } from 'react';
import { Flex, Box, Icon, Center, Link } from '@chakra-ui/react';
import { useNavigate } from 'react-router-dom';
import { Button, ButtonProps } from '@chakra-ui/react';
Expand All @@ -22,23 +22,18 @@ const useConnectedStatus = () => {
const configService = useService('configService');
const platformService = useService('platformService');

// TODO: use GrantService to check if the current site is granted
const whitelistQuery = useQuery({
queryKey: ['whitelist'],
queryFn: async () => configService.getWhitelist(),
});

const whitelistSitesSet = useMemo(
() => new Set(whitelistQuery.data?.map((item) => item.host) || []),
[whitelistQuery.data],
);

const hasGrantedQuery = useQuery({
// Set in query key is not serialized correct, so add the whitelistSitesSet as a extra dependency
queryKey: ['platformService', whitelistSitesSet, whitelistQuery.data] as const,
queryFn: async ({ queryKey: [, whitelistSitesSet] }) => {
const activeTab = await platformService.getActiveSiteInfo();
return !!activeTab?.url && whitelistSitesSet.has(new URL(activeTab.url).host);
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);
},
});

Expand Down

0 comments on commit 5e2d396

Please sign in to comment.