feat: inline chrome-extension-toolkit (#744)
* feat(build): inline chrome-extension-toolkit fix: tsconfig docs: add chrome-extension-toolkit README.md chore: update imports fix: stores fix: chrome-extension-toolkit ForegroundMessenger fix: calendarBackgroundHandler fix: format and lint fix: path alias fix: add jsdom env and fix imports Co-authored-by: Sriram Hariharan <sghsri@gmail.com> * build: vite storybook config crx toolkit line --------- Co-authored-by: Sriram Hariharan <sghsri@gmail.com> Co-authored-by: Derek <derex1987@gmail.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import type { Message, MessageData } from '../types';
|
||||
|
||||
/**
|
||||
* A helper function to create a hook that can listen for messages coming through chrome.runtime.onMessage
|
||||
* with e2e type safety
|
||||
* @returns a hook that can be used to listen for messages from the background script.
|
||||
*/
|
||||
export function createUseMessage<M>() {
|
||||
return function useMessage<N extends keyof M, D extends MessageData<M, N>>(
|
||||
name: N,
|
||||
callback: (data: D) => void
|
||||
): void {
|
||||
useEffect(() => {
|
||||
const onMessage = (message: Message<M>) => {
|
||||
if (message.name === name) {
|
||||
callback(message.data);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
chrome.runtime.onMessage.addListener(onMessage);
|
||||
|
||||
return () => {
|
||||
chrome.runtime.onMessage.removeListener(onMessage);
|
||||
};
|
||||
}, [name, callback]);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user