-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use context to manage popup screen
- Loading branch information
1 parent
5322e83
commit 42f7e3a
Showing
5 changed files
with
438 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import React, {createContext, useReducer, useContext} from 'react'; | ||
|
||
import {Kutt} from '../Background'; | ||
|
||
export enum ExtensionSettingsActionTypes { | ||
SET_EXTENSION_SETTINGS = 'set-extension-settings', | ||
} | ||
|
||
export type HostProperties = { | ||
hostDomain: string; | ||
hostUrl: string; | ||
}; | ||
|
||
export type DomainOptionsProperties = { | ||
option: string; | ||
value: string; | ||
id: string; | ||
disabled: boolean; | ||
}; | ||
|
||
type SET_EXTENSION_SETTINGS = { | ||
type: ExtensionSettingsActionTypes.SET_EXTENSION_SETTINGS; | ||
payload: { | ||
apikey: string; | ||
domainOptions: DomainOptionsProperties[]; | ||
host: HostProperties; | ||
}; | ||
}; | ||
|
||
type Action = SET_EXTENSION_SETTINGS; | ||
|
||
type InitialValues = { | ||
apikey: string; | ||
domainOptions: DomainOptionsProperties[]; | ||
host: HostProperties; | ||
}; | ||
|
||
const initialValues: InitialValues = { | ||
apikey: '', | ||
domainOptions: [], | ||
host: Kutt, | ||
}; | ||
|
||
type State = InitialValues; | ||
type Dispatch = (action: Action) => void; | ||
|
||
const ExtensionSettingsStateContext = createContext<State | undefined>( | ||
undefined | ||
); | ||
const ExtensionSettingsDispatchContext = createContext<Dispatch | undefined>( | ||
undefined | ||
); | ||
|
||
function extensionSettingsReducer(state: State, action: Action): State { | ||
switch (action.type) { | ||
case ExtensionSettingsActionTypes.SET_EXTENSION_SETTINGS: { | ||
return {...state, ...action.payload}; | ||
} | ||
|
||
default: | ||
return state; | ||
} | ||
} | ||
|
||
function useExtensionSettingsState(): State { | ||
const context = useContext(ExtensionSettingsStateContext); | ||
|
||
if (context === undefined) { | ||
throw new Error( | ||
'useExtensionSettingsState must be used within a ExtensionSettingsProvider' | ||
); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
function useExtensionSettingsDispatch(): Dispatch { | ||
const context = useContext(ExtensionSettingsDispatchContext); | ||
|
||
if (context === undefined) { | ||
throw new Error( | ||
'useExtensionSettingsDispatch must be used within a ExtensionSettingsProvider' | ||
); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
function useExtensionSettings(): [State, Dispatch] { | ||
// To access const [state, dispatch] = useExtensionSettings() | ||
return [useExtensionSettingsState(), useExtensionSettingsDispatch()]; | ||
} | ||
|
||
type ExtensionSettingsProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const ExtensionSettingsProvider: React.FC<ExtensionSettingsProviderProps> = ({ | ||
children, | ||
}) => { | ||
const [state, dispatch] = useReducer(extensionSettingsReducer, initialValues); | ||
|
||
return ( | ||
<> | ||
<ExtensionSettingsStateContext.Provider value={state}> | ||
<ExtensionSettingsDispatchContext.Provider value={dispatch}> | ||
{children} | ||
</ExtensionSettingsDispatchContext.Provider> | ||
</ExtensionSettingsStateContext.Provider> | ||
</> | ||
); | ||
}; | ||
|
||
export {ExtensionSettingsProvider, useExtensionSettings}; |
Oops, something went wrong.