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: Add provider to allowed list #551

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ref: convert allowed list hook to context provider
  • Loading branch information
mayan-000 committed Mar 12, 2024
commit 885a760f1a85c34ee3944df6cb2c154bd85f0130
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,47 @@
/**
* External dependencies.
*/
import { useEffect, useRef, useState } from 'react';
import React, {
useEffect,
useRef,
useState,
type PropsWithChildren,
} from 'react';
import { noop } from '@ps-analysis-tool/common';
import { createContext } from 'use-context-selector';

/**
* Internal dependencies.
*/
import { getCurrentTab } from '../../../../../../utils/getCurrentTabId';
import { useCookieStore } from '../../../../stateProviders/syncCookieStore';
import setDomainsInAllowList from './setDomainsInAllowList';
import getDotPrefixedDomain from './getDotPrefixedDomain';
import { getCurrentTab } from '../../../../utils/getCurrentTabId';
import { useCookieStore } from '../syncCookieStore';
import setDomainsInAllowList from './utils/setDomainsInAllowList';
import getDotPrefixedDomain from './utils/getDotPrefixedDomain';
import useContextSelector from '../../../../utils/useContextSelector';

export interface AllowedListContext {
state: {
domainsInAllowList: Set<string>;
isIncognito: boolean;
};
actions: {
setDomainsInAllowListCallback: (list: Set<string>) => void;
};
}

const initialState: AllowedListContext = {
state: {
domainsInAllowList: new Set(),
isIncognito: false,
},
actions: {
setDomainsInAllowListCallback: noop,
},
};

export const Context = createContext<AllowedListContext>(initialState);

const useAllowedList = () => {
export const Provider = ({ children }: PropsWithChildren) => {
const { tabUrl, cookies } = useCookieStore(({ state }) => ({
tabUrl: state.tabUrl,
cookies: state.tabCookies,
Expand Down Expand Up @@ -96,11 +126,36 @@ const useAllowedList = () => {
})();
}, [cookies, tabUrl]);

return {
domainsInAllowList,
setDomainsInAllowListCallback,
isIncognito: isIncognito.current,
};
return (
<Context.Provider
value={{
state: {
domainsInAllowList,
isIncognito: isIncognito.current,
},
actions: {
setDomainsInAllowListCallback,
},
}}
>
{children}
</Context.Provider>
);
};

export default useAllowedList;
export function useAllowedList(): AllowedListContext;
export function useAllowedList<T>(
selector: (state: AllowedListContext) => T
): T;

/**
* Allowed list hook.
* @param selector Selector function to partially select state.
* @returns selected part of the state
*/
export function useAllowedList<T>(
selector: (state: AllowedListContext) => T | AllowedListContext = (state) =>
state
) {
return useContextSelector(Context, selector);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export { default as getDotPrefixedDomain } from './getDotPrefixedDomain';
export { default as isCookieDomainInAllowList } from './isCookieDomainInAllowList';
export { default as onAllowListClick } from './onAllowListClick';
export { default as removeFromAllowList } from './removeFromAllowList';
export { default as setDomainsInAllowList } from './setDomainsInAllowList';
export { default as setParentDomain } from './setParentDomain';
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Internal dependencies.
*/
import { ChromeStorage } from '../../../../../../store';
import { ChromeStorage } from '../../../../../store';
import removeFromAllowList from './removeFromAllowList';

const onAllowListClick = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
/**
* Internal dependencies.
*/
import {
ChromeStorage,
type AllowedDomainObject,
} from '../../../../../../store';
import { ChromeStorage, type AllowedDomainObject } from '../../../../../store';

/**
* Remove object from allow list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Internal dependencies.
*/
import { ChromeStorage } from '../../../../../../store';
import { ChromeStorage } from '../../../../../store';

const setParentDomain = async (
domain: string,
Expand Down