Skip to content

Commit

Permalink
Use chrome storage util
Browse files Browse the repository at this point in the history
  • Loading branch information
victorkirov committed Jan 29, 2024
1 parent 584aab1 commit 62ed272
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/app/hooks/useChromeLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { chromeLocalStorage } from '@utils/chromeStorage';
import { useEffect, useState } from 'react';

export const useChromeLocalStorage = <T extends unknown>(key: string, defaultValue?: T) => {
const [value, setValueState] = useState<T | undefined>(undefined);

useEffect(() => {
setValueState(undefined);
chrome.storage.local.get(key, (result) => {
const newValue = result[key] === undefined ? defaultValue : result[key];
chromeLocalStorage.getItem<T>(key).then((result) => {
const newValue = result === undefined ? defaultValue : result;
setValueState(newValue);
});
}, [key]);

const setValue = (newValue: T) => {
chrome.storage.local.set({ [key]: newValue });
chromeLocalStorage.setItem(key, newValue);
setValueState(newValue);
};

Expand Down
18 changes: 8 additions & 10 deletions src/app/utils/chromeLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { chromeLocalStorage } from './chromeStorage';

export const chromeLocalStorageKeys = {
isPriorityWallet: 'isPriorityWallet',
};

export function getIsPriorityWallet(): Promise<boolean> {
return new Promise((resolve) => {
try {
chrome.storage.local.get(chromeLocalStorageKeys.isPriorityWallet, (result) => {
resolve(result[chromeLocalStorageKeys.isPriorityWallet]);
});
} catch (e) {
resolve(false);
}
});
export async function getIsPriorityWallet(): Promise<boolean> {
const isPriorityWallet = await chromeLocalStorage.getItem<boolean>(
chromeLocalStorageKeys.isPriorityWallet,
);

return isPriorityWallet ?? true;
}
6 changes: 3 additions & 3 deletions src/app/utils/chromeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ChromeStorage {
return runtimeMap.get(this).lastError;
}

setItem(key: string, item: any): Promise<any> {
setItem(key: string, item: any): Promise<true> {
return new Promise((resolve, reject) => {
this.getDriver().set({ [key]: item }, () => {
if (this.hasError()) {
Expand All @@ -30,7 +30,7 @@ class ChromeStorage {
});
}

getItem<T = any>(key: string): Promise<T> {
getItem<T = any>(key: string): Promise<T | undefined> {
return new Promise((resolve, reject) => {
this.getDriver().get(key, (response: any) => {
if (this.hasError()) {
Expand All @@ -41,7 +41,7 @@ class ChromeStorage {
});
}

removeItem(key: string): Promise<any> {
removeItem(key: string): Promise<true> {
return new Promise((resolve, reject) => {
this.getDriver().remove(key, () => {
if (this.hasError()) {
Expand Down
14 changes: 11 additions & 3 deletions src/content-scripts/content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,18 @@ document.addEventListener(DomEventName.createRepeatInscriptionsRequest, ((
}) as EventListener);

// Inject in-page script (Stacks and Bitcoin Providers)
getIsPriorityWallet().then((isPriorityWallet) => {
const injectInPageScript = (isPriority) => {
const inpage = document.createElement('script');
inpage.src = chrome.runtime.getURL('inpage.js');
inpage.id = 'xverse-wallet-provider';
inpage.setAttribute('data-is-priority', isPriorityWallet ? 'true' : '');
inpage.setAttribute('data-is-priority', isPriority ? 'true' : '');
document.body.appendChild(inpage);
});
};

getIsPriorityWallet()
.then((isPriorityWallet) => {
injectInPageScript(isPriorityWallet);
})
.catch(() => {
injectInPageScript(false);
});

0 comments on commit 62ed272

Please sign in to comment.