-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat: extension background storage #98
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…storage
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import browser from 'webextension-polyfill' | ||
|
||
export interface IStorageService { | ||
set(_key: string, _value: any): Promise<void> | ||
get(_key: string): Promise<any> | ||
} | ||
|
||
export interface ExtensionStorageData { | ||
amount: number | ||
amountType: { | ||
recurring: boolean | ||
} | ||
rateOfPay: number | ||
wmEnabled: boolean | ||
accessTokenQuote: string | ||
accessTokenOutgoing: string | ||
refreshToken: string | ||
manageUrl: string | ||
} | ||
|
||
export const defaultData: ExtensionStorageData = { | ||
amount: 0, | ||
amountType: { | ||
recurring: true, | ||
}, | ||
rateOfPay: 0.36, | ||
wmEnabled: true, | ||
accessTokenQuote: '', | ||
accessTokenOutgoing: '', | ||
refreshToken: '', | ||
manageUrl: '', | ||
} | ||
|
||
class StorageService { | ||
async get(key: string): Promise<ExtensionStorageData | undefined> { | ||
try { | ||
const result = await browser.storage.sync.get(key) | ||
return result[key] as ExtensionStorageData | ||
} catch (error) { | ||
throw new Error(`Error retrieving data: ${error}`) | ||
} | ||
} | ||
|
||
async set(key: string, value: ExtensionStorageData): Promise<void> { | ||
try { | ||
await browser.storage.sync.set({ [key]: value }) | ||
} catch (error) { | ||
throw new Error(`Error saving data: ${error}`) | ||
} | ||
} | ||
} | ||
|
||
export default StorageService |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Background from '@/background/Background' | ||
|
||
const getStorageData = async (background: Background) => { | ||
console.log('getStorageData background', background) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get this is a dummy example, but to show case the usage, you can invoke the background.storageService and return the result |
||
return { | ||
type: 'SUCCESS', | ||
data: { | ||
someData: 'data', | ||
}, | ||
} | ||
} | ||
|
||
export default { callback: getStorageData, type: 'GET_STORAGE_DATA' } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import React, { createContext, useState } from 'react' | ||
import React, { createContext, useEffect, useState } from 'react' | ||
|
||
import { sendMessage } from '@/utils/sendMessages' | ||
|
||
import { PopupContextValue, TPopupContext } from './providers.interface' | ||
|
||
|
@@ -27,6 +29,17 @@ export const PopupContext = createContext<PopupContextValue>({ | |
export const PopupProvider: React.FC<IProps> = ({ children }) => { | ||
const [data, setData] = useState<TPopupContext>(defaultData) | ||
|
||
const getStorageData = async () => await sendMessage({ type: 'GET_STORAGE_DATA' }) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are no dependencies, so it can be moved outside the component, maybe a utils file associated with the popup-context provider (maybe group under a folder) |
||
useEffect(() => { | ||
;(async () => { | ||
const storageData = await getStorageData() | ||
console.log('storageData xxx', storageData) | ||
// setData(storageData.data) | ||
})() | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
return <PopupContext.Provider value={{ data, setData }}>{children}</PopupContext.Provider> | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialisation of data should be done when create a new instance of storage, because it's a singleton