Skip to content

Commit

Permalink
feat(tx-builder-mode): share dark mode information with safe apps
Browse files Browse the repository at this point in the history
  • Loading branch information
clovisdasilvaneto committed Oct 8, 2024
1 parent 14c5da5 commit e74533b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/components/safe-apps/AppFrame/useAppCommunicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { SAFE_APPS_EVENTS, trackSafeAppEvent } from '@/services/analytics'
import { useAppSelector } from '@/store'
import { selectRpc } from '@/store/settingsSlice'
import { createSafeAppsWeb3Provider } from '@/hooks/wallets/web3'
import { useDarkMode } from '@/hooks/useDarkMode'

export enum CommunicatorMessages {
REJECT_TRANSACTION_MESSAGE = 'Transaction was rejected',
Expand Down Expand Up @@ -73,6 +74,7 @@ const useAppCommunicator = (
): AppCommunicator | undefined => {
const [communicator, setCommunicator] = useState<AppCommunicator | undefined>(undefined)
const customRpc = useAppSelector(selectRpc)
const isDarkMode = useDarkMode()

const safeAppWeb3Provider = useMemo(() => {
if (!chain) {
Expand Down Expand Up @@ -119,6 +121,15 @@ const useAppCommunicator = (
}
}, [app, iframeRef])

useEffect(() => {
communicator?.send(
{
darkMode: isDarkMode,
},
'tx-builder',
)
}, [communicator, isDarkMode])

// Adding communicator logic for the required SDK Methods
// We don't need to unsubscribe from the events because there can be just one subscription
// per event type and the next effect run will simply replace the handlers
Expand Down Expand Up @@ -205,7 +216,17 @@ const useAppCommunicator = (
communicator?.on(Methods.requestAddressBook, (msg) => {
return handlers.onRequestAddressBook(msg.origin)
})
}, [safeAppWeb3Provider, handlers, chain, communicator])

// TODO: it will be moved to safe-apps-sdk soon
communicator?.on('getCurrentTheme' as Methods, (msg) => {
communicator.send(
{
darkMode: isDarkMode,
},
msg.data.id,
)
})
}, [safeAppWeb3Provider, handlers, chain, communicator, isDarkMode])

return communicator
}
Expand Down
6 changes: 5 additions & 1 deletion src/services/safe-apps/AppCommunicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ class AppCommunicator {
const sentFromIframe = this.iframeRef.current?.contentWindow === msg.source
const knownMethod = Object.values(Methods).includes(msg.data.method)

return sentFromIframe && knownMethod
// TODO: move it to safe-app Methods types
const isThemeInfoMethod = (msg.data.method as string) === 'getCurrentTheme'

return sentFromIframe && (knownMethod || isThemeInfoMethod)
}

private canHandleMessage = (msg: SDKMessageEvent): boolean => {
if (!msg.data) return false

return Boolean(this.handlers.get(msg.data.method))
}

Expand Down

0 comments on commit e74533b

Please sign in to comment.