Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tx-builder-mode): share dark mode information with safe apps #4340

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 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,17 @@ const useAppCommunicator = (
}
}, [app, iframeRef])

useEffect(() => {
const id = Math.random().toString(36).slice(2)

communicator?.send(
{
darkMode: isDarkMode,
},
id,
)
}, [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 +218,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
Loading