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(pilot-app,extension): save route to extension #569

Merged
merged 5 commits into from
Jan 17, 2025
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
2 changes: 1 addition & 1 deletion deployables/app/app/routes/$avatar.$chainId/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const loader = async ({ params }: Route.LoaderArgs) => {
chainId: verifiedChainId,
})
} catch (e) {
console.log(e)
console.error(e)
return []
}
}
46 changes: 33 additions & 13 deletions deployables/app/app/routes/edit-route.$data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { render } from '@/test-utils'
import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Chain, CHAIN_NAME } from '@zodiac/chains'
import { CompanionAppMessageType } from '@zodiac/messages'
import {
encodeRoleKey,
fetchZodiacModules,
Expand All @@ -24,7 +25,6 @@ import {
randomAddress,
randomPrefixedAddress,
} from '@zodiac/test-utils'
import { chromeMock } from '@zodiac/test-utils/chrome'
import { formatPrefixedAddress, type ChainId } from 'ser-kit'
import { describe, expect, it, vi } from 'vitest'

Expand Down Expand Up @@ -64,6 +64,8 @@ const mockFetchZodiacModules = vi.mocked(fetchZodiacModules)
const mockQueryRolesV1MultiSend = vi.mocked(queryRolesV1MultiSend)
const mockQueryRolesV2MultiSend = vi.mocked(queryRolesV2MultiSend)

const mockPostMessage = vi.spyOn(window, 'postMessage')

describe('Edit route', () => {
describe('Label', () => {
it('shows the name of a route', async () => {
Expand All @@ -88,9 +90,12 @@ describe('Edit route', () => {

await userEvent.click(screen.getByRole('button', { name: 'Save' }))

expect(chromeMock.runtime.sendMessage).toHaveBeenCalledWith(
expect(mockPostMessage).toHaveBeenCalledWith(
{
type: CompanionAppMessageType.SAVE_ROUTE,
data: expect.objectContaining({ label: 'New route label' }),
},
expect.anything(),
expect.objectContaining({ label: 'New route label' }),
)
})
})
Expand Down Expand Up @@ -213,9 +218,12 @@ describe('Edit route', () => {

await userEvent.click(screen.getByRole('button', { name: 'Save' }))

expect(chromeMock.runtime.sendMessage).toHaveBeenCalledWith(
expect(mockPostMessage).toHaveBeenCalledWith(
{
type: CompanionAppMessageType.SAVE_ROUTE,
data: updateAvatar(route, { safe }),
},
expect.anything(),
updateAvatar(route, { safe }),
)
})

Expand All @@ -234,9 +242,12 @@ describe('Edit route', () => {
)
await userEvent.click(screen.getByRole('button', { name: 'Save' }))

expect(chromeMock.runtime.sendMessage).toHaveBeenCalledWith(
expect(mockPostMessage).toHaveBeenCalledWith(
{
type: CompanionAppMessageType.SAVE_ROUTE,
data: updateAvatar(route, { safe }),
},
expect.anything(),
updateAvatar(route, { safe }),
)
})

Expand All @@ -257,9 +268,12 @@ describe('Edit route', () => {
)
await userEvent.click(screen.getByRole('button', { name: 'Save' }))

expect(chromeMock.runtime.sendMessage).toHaveBeenCalledWith(
expect(mockPostMessage).toHaveBeenCalledWith(
{
type: CompanionAppMessageType.SAVE_ROUTE,
data: removeAvatar(route),
},
expect.anything(),
removeAvatar(route),
)
})
})
Expand Down Expand Up @@ -424,9 +438,12 @@ describe('Edit route', () => {

await userEvent.click(screen.getByRole('button', { name: 'Save' }))

expect(chromeMock.runtime.sendMessage).toHaveBeenCalledWith(
expect(mockPostMessage).toHaveBeenCalledWith(
{
type: CompanionAppMessageType.SAVE_ROUTE,
data: updateRoleId(route, roleId),
},
expect.anything(),
updateRoleId(route, roleId),
)
})
})
Expand Down Expand Up @@ -535,9 +552,12 @@ describe('Edit route', () => {

await userEvent.click(screen.getByRole('button', { name: 'Save' }))

expect(chromeMock.runtime.sendMessage).toHaveBeenCalledWith(
expect(mockPostMessage).toHaveBeenCalledWith(
{
type: CompanionAppMessageType.SAVE_ROUTE,
data: updateRoleId(route, encodeRoleKey('MANAGER')),
},
expect.anything(),
updateRoleId(route, encodeRoleKey('MANAGER')),
)
})
})
Expand Down
7 changes: 6 additions & 1 deletion deployables/app/app/routes/edit-route.$data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getOptionalString,
getString,
} from '@zodiac/form-data'
import { CompanionAppMessageType } from '@zodiac/messages'
import {
getRolesVersion,
queryRolesV1MultiSend,
Expand Down Expand Up @@ -94,7 +95,11 @@ export const clientAction = async ({

route = updateLabel(route, getString(data, 'label'))

chrome.runtime.sendMessage('', route)
window.postMessage(
{ type: CompanionAppMessageType.SAVE_ROUTE, data: route },
'*',
)
// chrome.runtime.sendMessage('', route)

return editRoute(request.url, route)
}
Expand Down
1 change: 1 addition & 0 deletions deployables/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@tanstack/react-query": "^5.56.2",
"@zodiac/chains": "workspace:*",
"@zodiac/form-data": "workspace:*",
"@zodiac/messages": "workspace:*",
"@zodiac/modules": "workspace:*",
"@zodiac/safe": "workspace:*",
"@zodiac/schema": "workspace:*",
Expand Down
3 changes: 3 additions & 0 deletions deployables/extension/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ esbuild
'./src/monitor/contentScript/main.ts',
'./src/monitor/injectedScript/main.ts',

// COMPANION APP TO EDIT ROUTES
'./src/companion/contentScripts/main.ts',

// SIDEPANEL APP
'./src/panel/app.tsx',
],
Expand Down
1 change: 1 addition & 0 deletions deployables/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"@headlessui/react": "^2.2.0",
"@zodiac/chains": "workspace:*",
"@zodiac/form-data": "workspace:*",
"@zodiac/messages": "workspace:*",
"@zodiac/modules": "workspace:*",
"@zodiac/safe": "workspace:*",
"@zodiac/schema": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion deployables/extension/src/background/PilotSession.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Message, PilotMessageType } from '@/messages'
import { sendMessageToTab } from '@/utils'
import { invariant } from '@epic-web/invariant'
import { PilotMessageType, type Message } from '@zodiac/messages'
import { removeCSPHeaderRule, updateCSPHeaderRule } from './cspHeaderRule'
import { addRpcRedirectRules, removeAllRpcRedirectRules } from './rpcRedirect'
import type { TrackRequestsResult } from './rpcTracking'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PilotMessageType } from '@/messages'
import { callListeners, chromeMock, createMockTab } from '@/test-utils'
import { PilotMessageType } from '@zodiac/messages'
import { describe, expect, it } from 'vitest'
import { enableExternalPanelOpen } from './enableExternalPanelOpen'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PilotMessageType } from '@/messages'
import { invariant } from '@epic-web/invariant'
import { PilotMessageType } from '@zodiac/messages'
import { resolve } from 'path'

export const enableExternalPanelOpen = () => {
Expand Down
2 changes: 1 addition & 1 deletion deployables/extension/src/background/rpcTracking.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RpcMessageType } from '@/messages'
import { sendMessageToTab } from '@/utils'
import { RpcMessageType } from '@zodiac/messages'
import type { ChainId } from 'ser-kit'
import { createEventListener } from './createEventListener'
import { hasJsonRpcBody } from './hasJsonRpcBody'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PilotMessageType } from '@/messages'
import {
callListeners,
chromeMock,
createMockTab,
mockActiveTab,
startPilotSession,
} from '@/test-utils'
import { PilotMessageType } from '@zodiac/messages'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { trackRequests } from './rpcTracking'
import { trackSessions } from './sessionTracking'
Expand Down
4 changes: 2 additions & 2 deletions deployables/extension/src/background/sessionTracking.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { type Message, PilotMessageType } from '@/messages'
import { PILOT_PANEL_PORT } from '@/port-handling'
import { isValidTab, reloadActiveTab, reloadTab } from '@/utils'
import { PilotMessageType, type Message } from '@zodiac/messages'
import type { RefObject } from 'react'
import { createEventListener } from './createEventListener'
import { getPilotSession } from './getPilotSession'
import { PilotSession, type Sessions } from './PilotSession'
import type { TrackRequestsResult } from './rpcTracking'
import type { Event } from './types'
import { type CallbackFn, withPilotSession } from './withPilotSession'
import { withPilotSession, type CallbackFn } from './withPilotSession'

type SessionDeletedEventListener = (windowId: number) => void

Expand Down
5 changes: 4 additions & 1 deletion deployables/extension/src/background/simulationTracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { PilotSimulationMessageType, type SimulationMessage } from '@/messages'
import {
PilotSimulationMessageType,
type SimulationMessage,
} from '@zodiac/messages'
import type { TrackSessionsResult } from './sessionTracking'
import { updateBadge } from './updateBadge'

Expand Down
15 changes: 15 additions & 0 deletions deployables/extension/src/companion/contentScripts/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
CompanionAppMessageType,
type CompanionAppMessage,
} from '@zodiac/messages'

window.addEventListener(
'message',
(event: MessageEvent<CompanionAppMessage>) => {
if (event.data.type !== CompanionAppMessageType.SAVE_ROUTE) {
return
}

chrome.runtime.sendMessage(event.data)
},
)
8 changes: 4 additions & 4 deletions deployables/extension/src/connect/contentScripts/dApp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
type ConnectedWalletMessage,
ConnectedWalletMessageType,
} from '@/messages'
import { chromeMock, createMockPort } from '@/test-utils'
import { waitFor } from '@testing-library/react'
import {
ConnectedWalletMessageType,
type ConnectedWalletMessage,
} from '@zodiac/messages'
import { describe, expect, it } from 'vitest'

describe('Connect Pilot to DApp', () => {
Expand Down
6 changes: 3 additions & 3 deletions deployables/extension/src/connect/contentScripts/dApp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { invariant } from '@epic-web/invariant'
import {
type ConnectedWalletMessage,
ConnectedWalletMessageType,
} from '@/messages'
import { invariant } from '@epic-web/invariant'
type ConnectedWalletMessage,
} from '@zodiac/messages'

const CONNECT_IFRAME_URL = process.env.CONNECT_IFRAME_URL

Expand Down
6 changes: 3 additions & 3 deletions deployables/extension/src/connect/injectedScript/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Eip1193Provider } from '@/types'
import {
type ConnectedWalletMessage,
ConnectedWalletMessageType,
} from '@/messages'
import type { Eip1193Provider } from '@/types'
type ConnectedWalletMessage,
} from '@zodiac/messages'

declare global {
interface Window {
Expand Down
2 changes: 1 addition & 1 deletion deployables/extension/src/inject/bridge/useBridgeError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
InjectedProviderMessageTyp,
type InjectedProviderMessage,
} from '@/messages'
} from '@zodiac/messages'
import { errorToast } from '@zodiac/ui'
import { useEffect, useId } from 'react'
import { useWindowId } from './BridgeContext'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { InjectedProviderMessageTyp } from '@/messages'
import {
chromeMock,
createMockTab,
Expand All @@ -10,6 +9,7 @@ import {
import type { Eip1193Provider } from '@/types'
import { cleanup, waitFor } from '@testing-library/react'
import { ZERO_ADDRESS } from '@zodiac/chains'
import { InjectedProviderMessageTyp } from '@zodiac/messages'
import { toQuantity } from 'ethers'
import type { PropsWithChildren } from 'react'
import type { ChainId } from 'ser-kit'
Expand Down
9 changes: 4 additions & 5 deletions deployables/extension/src/inject/bridge/useProviderBridge.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// this will be bundled in the panel app
import {
type InjectedProviderMessage,
InjectedProviderMessageTyp,
} from '@/messages'
import type { Eip1193Provider } from '@/types'
import { getActiveTab, sendMessageToTab } from '@/utils'
import { invariant } from '@epic-web/invariant'
import {
InjectedProviderMessageTyp,
type InjectedProviderMessage,
} from '@zodiac/messages'
import { toQuantity } from 'ethers'
import { useCallback, useEffect, useRef } from 'react'
import type { ChainId } from 'ser-kit'
Expand Down
10 changes: 5 additions & 5 deletions deployables/extension/src/inject/contentScript/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { injectScript, isValidTab } from '@/utils'
import {
type InjectedProviderMessage,
InjectedProviderMessageTyp,
PilotMessageType,
RpcMessageType,
type InjectedProviderMessage,
type InjectedProviderResponse,
type Message,
PilotMessageType,
type RpcMessage,
RpcMessageType,
} from '@/messages'
import { injectScript, isValidTab } from '@/utils'
} from '@zodiac/messages'
import { probeChainId } from './probeChainId'

// The content script is injected on tab update events, which can be triggered multiple times for the same page load.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
type InjectedProviderMessage,
InjectedProviderMessageTyp,
} from '@/messages'
} from '@zodiac/messages'
import { EventEmitter } from 'events'
import { nanoid } from 'nanoid'

Expand Down
9 changes: 6 additions & 3 deletions deployables/extension/src/manifest.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@
"content_scripts": [
{
"matches": ["<all_urls>"],
"exclude_globs": ["<CONNECT_IFRAME_URL>", "about:*", "chrome:*"],
"exclude_globs": ["<CONNECT_IFRAME_URL>*", "about:*", "chrome:*"],
"run_at": "document_start",
"js": ["build/connect/contentScripts/dApp.js"]
},
{
"matches": ["<CONNECT_IFRAME_URL>"],
"matches": ["<CONNECT_IFRAME_URL>*"],
"run_at": "document_start",
"all_frames": true,
"js": ["build/connect/contentScripts/connectIframe.js"]
"js": [
"build/connect/contentScripts/connectIframe.js",
"build/companion/contentScripts/main.js"
]
},
{
"matches": ["<all_urls>"],
Expand Down
Loading
Loading