Skip to content

Commit

Permalink
feat: add widget events
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Sep 2, 2022
1 parent 48d34ce commit 7c0857e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/widget-embedded/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
switchChainAndAddToken,
} from '@lifi/wallet-management';
import type { WidgetConfig } from '@lifi/widget';
import { LiFiWidget, LiFiWidgetDrawer } from '@lifi/widget';
import {
LiFiWidget,
LiFiWidgetDrawer,
useWidgetEvents,
WidgetEvent,
} from '@lifi/widget';
import {
Box,
// Button,
Expand Down Expand Up @@ -97,6 +102,7 @@ const widgetConfig: WidgetConfig = {

export const App = () => {
const { connect, disconnect, account } = useWallet();
const widgetEvents = useWidgetEvents();
const [searchParams] = useState(() =>
Object.fromEntries(new URLSearchParams(window?.location.search)),
);
Expand Down Expand Up @@ -233,6 +239,16 @@ export const App = () => {
}
}, [darkMode, prefersDarkMode, primary, secondary, systemColor]);

useEffect(() => {
const onSwapStarted = () => {
console.log('onSwapStarted fired.');
};
widgetEvents.on(WidgetEvent.SwapStarted, onSwapStarted);
return () => {
widgetEvents.removeListener(WidgetEvent.SwapStarted, onSwapStarted);
};
}, [widgetEvents]);

return (
<ThemeProvider theme={theme}>
<Box sx={{ display: 'flex' }}>
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from './useTokenBalances';
export * from './useTokens';
export * from './useTokenSearch';
export * from './useTools';
export * from './useWidgetEvents';
8 changes: 8 additions & 0 deletions packages/widget/src/hooks/useWidgetEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EventEmitter } from 'events';
import create from 'zustand';

const emitterStore = create<EventEmitter>()(() => new EventEmitter());

export const useWidgetEvents = () => {
return emitterStore();
};
1 change: 1 addition & 0 deletions packages/widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppDrawer } from './AppDrawer';
import './fonts/inter.css';
import { configureReactI18next } from './i18n';

export { useWidgetEvents } from './hooks';
export * from './types';

configureReactI18next();
Expand Down
3 changes: 3 additions & 0 deletions packages/widget/src/types/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum WidgetEvent {
SwapStarted = 'swapStarted',
}
1 change: 1 addition & 0 deletions packages/widget/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './events';
export * from './token';
export * from './widget';

0 comments on commit 7c0857e

Please sign in to comment.