Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinmuenster committed Mar 22, 2024
1 parent af730ad commit 54bb998
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/chrome-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {
project: ['./tsconfig.json', './tsconfig.node.json'],
tsconfigRootDir: __dirname,
},
}
};
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
Expand Down
20 changes: 10 additions & 10 deletions apps/chrome-extension/public/_locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extensionDescription": {
"description": "Extension description",
"message": "Chrome extension boilerplate developed with Vite, React and Typescript"
},
"extensionName": {
"description": "Extension name",
"message": "Chrome extension boilerplate"
}
}
{
"extensionDescription": {
"description": "Extension description",
"message": "Chrome extension boilerplate developed with Vite, React and Typescript"
},
"extensionName": {
"description": "Extension name",
"message": "Chrome extension boilerplate"
}
}
2 changes: 1 addition & 1 deletion apps/chrome-extension/src/shared/hoc/withErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ErrorBoundary extends Component<

export default function withErrorBoundary<T extends Record<string, unknown>>(
Component: ComponentType<T>,
ErrorComponent: ReactElement,
ErrorComponent: ReactElement
) {
return function WithErrorBoundary(props: T) {
return (
Expand Down
2 changes: 1 addition & 1 deletion apps/chrome-extension/src/shared/hoc/withSuspense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComponentType, ReactElement, Suspense } from 'react';

export default function withSuspense<T extends Record<string, unknown>>(
Component: ComponentType<T>,
SuspenseComponent: ReactElement,
SuspenseComponent: ReactElement
) {
return function WithSuspense(props: T) {
return (
Expand Down
7 changes: 5 additions & 2 deletions apps/chrome-extension/src/shared/hooks/useStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export default function useStorage<
Storage extends BaseStorage<Data>,
Data = Storage extends BaseStorage<infer Data> ? Data : unknown,
>(storage: Storage) {
const _data = useSyncExternalStore<Data | null>(storage.subscribe, storage.getSnapshot);
const _data = useSyncExternalStore<Data | null>(
storage.subscribe,
storage.getSnapshot
);

if (!storageMap.has(storage)) {
storageMap.set(storage, wrapPromise(storage.get()));
Expand All @@ -31,7 +34,7 @@ function wrapPromise<R>(promise: Promise<R>) {
e => {
status = 'error';
result = e;
},
}
);

return {
Expand Down
34 changes: 26 additions & 8 deletions apps/chrome-extension/src/shared/storages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,21 @@ type StorageConfig = {
/**
* Sets or updates an arbitrary cache with a new value or the result of an update function.
*/
async function updateCache<D>(valueOrUpdate: ValueOrUpdate<D>, cache: D | null): Promise<D> {
async function updateCache<D>(
valueOrUpdate: ValueOrUpdate<D>,
cache: D | null
): Promise<D> {
// Type guard to check if our value or update is a function
function isFunction<D>(value: ValueOrUpdate<D>): value is (prev: D) => D | Promise<D> {
function isFunction<D>(
value: ValueOrUpdate<D>
): value is (prev: D) => D | Promise<D> {
return typeof value === 'function';
}

// Type guard to check in case of a function, if its a Promise
function returnsPromise<D>(func: (prev: D) => D | Promise<D>): func is (prev: D) => Promise<D> {
function returnsPromise<D>(
func: (prev: D) => D | Promise<D>
): func is (prev: D) => Promise<D> {
// Use ReturnType to infer the return type of the function and check if it's a Promise
return (func as (prev: D) => Promise<D>) instanceof Promise;
}
Expand All @@ -102,21 +109,28 @@ async function updateCache<D>(valueOrUpdate: ValueOrUpdate<D>, cache: D | null):
* If one session storage needs access from content scripts, we need to enable it globally.
* @default false
*/
let globalSessionAccessLevelFlag: StorageConfig['sessionAccessForContentScripts'] = false;
let globalSessionAccessLevelFlag: StorageConfig['sessionAccessForContentScripts'] =
false;

/**
* Checks if the storage permission is granted in the manifest.json.
*/
function checkStoragePermission(storageType: StorageType): void {
if (chrome.storage[storageType] === undefined) {
throw new Error(`Check your storage permission in manifest.json: ${storageType} is not defined`);
throw new Error(
`Check your storage permission in manifest.json: ${storageType} is not defined`
);
}
}

/**
* Creates a storage area for persisting and exchanging data.
*/
export function createStorage<D>(key: string, fallback: D, config?: StorageConfig): BaseStorage<D> {
export function createStorage<D>(
key: string,
fallback: D,
config?: StorageConfig
): BaseStorage<D> {
let cache: D | null = null;
let listeners: Array<() => void> = [];
const storageType = config?.storageType ?? StorageType.Local;
Expand Down Expand Up @@ -170,7 +184,9 @@ export function createStorage<D>(key: string, fallback: D, config?: StorageConfi
});

// Listener for live updates from the browser
async function _updateFromStorageOnChanged(changes: { [key: string]: chrome.storage.StorageChange }) {
async function _updateFromStorageOnChanged(changes: {
[key: string]: chrome.storage.StorageChange;
}) {
// Check if the key we are listening for is in the changes object
if (changes[key] === undefined) return;

Expand All @@ -185,7 +201,9 @@ export function createStorage<D>(key: string, fallback: D, config?: StorageConfi

// Register listener for live updates for our storage area
if (liveUpdate) {
chrome.storage[storageType].onChanged.addListener(_updateFromStorageOnChanged);
chrome.storage[storageType].onChanged.addListener(
_updateFromStorageOnChanged
);
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BaseStorage, createStorage, StorageType } from '@src/shared/storages/base';
import {
BaseStorage,
createStorage,
StorageType,
} from '@src/shared/storages/base';

type Theme = 'light' | 'dark';

Expand Down
5 changes: 4 additions & 1 deletion apps/chrome-extension/utils/plugins/add-hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type { PluginOption } from 'vite';
const DUMMY_CODE = `export default function(){};`;

function getInjectionCode(fileName: string): string {
return readFileSync(path.resolve(__dirname, '..', 'reload', 'injections', fileName), { encoding: 'utf8' });
return readFileSync(
path.resolve(__dirname, '..', 'reload', 'injections', fileName),
{ encoding: 'utf8' }
);
}

type Config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default function inlineVitePreloadScript(): PluginOption {
return null;
}
if (!__vitePreload) {
const chunkName: string | undefined = Object.keys(meta.chunks).find(key => /preload/.test(key));
const chunkName: string | undefined = Object.keys(meta.chunks).find(
key => /preload/.test(key)
);
const modules = meta.chunks?.[chunkName]?.modules;
__vitePreload = modules?.[Object.keys(modules)?.[0]]?.code;
__vitePreload = __vitePreload?.replaceAll('const ', 'var ');
Expand Down
4 changes: 3 additions & 1 deletion apps/chrome-extension/utils/plugins/watch-rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { WebSocket } from 'ws';
import MessageInterpreter from '../reload/interpreter';
import { LOCAL_RELOAD_SOCKET_URL } from '../reload/constant';

export default function watchRebuild(config: { afterWriteBundle: () => void }): PluginOption {
export default function watchRebuild(config: {
afterWriteBundle: () => void;
}): PluginOption {
const ws = new WebSocket(LOCAL_RELOAD_SOCKET_URL);
return {
name: 'watch-rebuild',
Expand Down
2 changes: 1 addition & 1 deletion apps/chrome-extension/utils/reload/initReloadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function initReloadClient({

socket.onclose = () => {
console.log(
`Reload server disconnected.\nPlease check if the WebSocket server is running properly on ${LOCAL_RELOAD_SOCKET_URL}. This feature detects changes in the code and helps the browser to reload the extension or refresh the current tab.`,
`Reload server disconnected.\nPlease check if the WebSocket server is running properly on ${LOCAL_RELOAD_SOCKET_URL}. This feature detects changes in the code and helps the browser to reload the extension or refresh the current tab.`
);
setTimeout(() => {
initReloadClient({ watchPath, onUpdate });
Expand Down
5 changes: 4 additions & 1 deletion apps/chrome-extension/utils/reload/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { clearTimeout } from 'timers';

export function debounce<A extends unknown[]>(callback: (...args: A) => void, delay: number) {
export function debounce<A extends unknown[]>(
callback: (...args: A) => void,
delay: number
) {
let timer: NodeJS.Timeout;
return function (...args: A) {
clearTimeout(timer);
Expand Down

0 comments on commit 54bb998

Please sign in to comment.