Skip to content

Commit

Permalink
feat: sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Apr 8, 2024
1 parent dc29188 commit 2125a66
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 213 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 4 additions & 0 deletions manifest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export const getManifest = ({ dev }: { dev?: boolean }) =>
all_frames: true,
},
],
side_panel: {
default_path: 'src/entries/iframe/index.html',
},
icons: {
'16': `icons/icon${dev ? '-dev' : ''}@16w.png`,
'32': `icons/icon${dev ? '-dev' : ''}@32w.png`,
Expand All @@ -36,6 +39,7 @@ export const getManifest = ({ dev }: { dev?: boolean }) =>
'contextMenus',
'declarativeNetRequest',
'scripting',
'sidePanel',
'storage',
'tabs',
'unlimitedStorage',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@biomejs/biome": "1.0.0",
"@samrum/vite-plugin-web-extension": "^5.1.0",
"@types/chroma-js": "^2.4.0",
"@types/chrome": "^0.0.244",
"@types/chrome": "^0.0.266",
"@types/opentype.js": "^1.3.4",
"@types/qs": "^6.9.7",
"@types/react": "^18.2.33",
Expand Down
38 changes: 2 additions & 36 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ReactNode, useCallback, useMemo } from 'react'
import { type ReactNode, useMemo } from 'react'
import { Link } from 'react-router-dom'
import { type Hex, formatGwei } from 'viem'

Expand All @@ -24,15 +24,12 @@ import { useHost } from '~/hooks/useHost'
import { useMine } from '~/hooks/useMine'
import { useNetworkStatus } from '~/hooks/useNetworkStatus'
import { usePendingBlock } from '~/hooks/usePendingBlock'
import { getMessenger } from '~/messengers'
import { useAccountStore, useNetworkStore, useSessionsStore } from '~/zustand'

import { useRevert } from '../hooks/useRevert'
import { useSnapshot } from '../hooks/useSnapshot'
import * as styles from './Header.css'

const contentMessenger = getMessenger('wallet:contentScript')

export function Header({ isNetworkOffline }: { isNetworkOffline?: boolean }) {
const { type } = useAppMeta()
return (
Expand Down Expand Up @@ -62,12 +59,6 @@ export function Header({ isNetworkOffline }: { isNetworkOffline?: boolean }) {
<Column width="content">
<SettingsButton />
</Column>
<Column width="content">
<Separator orientation="vertical" />
</Column>
<Column width="content">
<CollapseButton />
</Column>
</>
)}
</Columns>
Expand Down Expand Up @@ -194,31 +185,6 @@ function DappConnection() {
)
}

function CollapseButton() {
const handleClose = useCallback(() => {
contentMessenger.send('toggleWallet', undefined)
}, [])

return (
<Tooltip label="Hide Wallet" height="full">
<Box
alignItems="center"
as="button"
backgroundColor={{
hover: 'surface/fill/quarternary',
}}
display="flex"
justifyContent="center"
height="full"
onClick={handleClose}
style={{ width: '28px' }}
>
<SFSymbol size="12px" symbol="chevron.right.2" weight="medium" />
</Box>
</Tooltip>
)
}

function SettingsButton() {
return (
<Tooltip label="Settings" height="full">
Expand All @@ -232,7 +198,7 @@ function SettingsButton() {
display="flex"
justifyContent="center"
height="full"
style={{ width: '28px' }}
style={{ width: '32px' }}
>
<SFSymbol size="14px" symbol="gear" weight="medium" />
</Box>
Expand Down
13 changes: 9 additions & 4 deletions src/design-system/components/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,15 @@ export const TextTruncated = forwardRef<HTMLDivElement, TextTruncatedProps>(
}, [])

useLayoutEffect(() => {
setWidth(
((wrapperRef.current as any).getBoundingClientRect() as DOMRectReadOnly)
.width,
)
setTimeout(() => {
setWidth(
(
(
wrapperRef.current as any
).getBoundingClientRect() as DOMRectReadOnly
).width,
)
}, 16)
}, [])
useResizeObserver(wrapperRef, (entry) =>
mounted ? setWidth(entry.contentRect.width) : undefined,
Expand Down
10 changes: 3 additions & 7 deletions src/entries/background/context-menu.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { getMessenger } from '~/messengers'

const inpageMessenger = getMessenger('background:inpage')

export function setupContextMenu() {
chrome.action.onClicked.addListener(() => {
inpageMessenger.send('toggleWallet', undefined)
})
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error))

// TODO: Only create context menu if selected text is "openable" in Rivet.
// chrome.contextMenus.create({
Expand Down
2 changes: 2 additions & 0 deletions src/entries/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { setupExtensionId } from './extension-id'
import { setupInpage } from './inpage'
import { interceptJsonRpcRequests } from './intercept-requests'
import { setupRpcHandler } from './rpc'
import { setupWalletSidebarHandler } from './wallet-sidebar'

const contentMessenger = getMessenger('background:contentScript')
const inpageMessenger = getMessenger('background:inpage')
Expand All @@ -21,4 +22,5 @@ setupExtensionId()
setupInpage()
setupRpcHandler({ messenger: inpageMessenger })
setupRpcHandler({ messenger: walletMessenger })
setupWalletSidebarHandler()
syncStores()
6 changes: 1 addition & 5 deletions src/entries/background/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function getRpcClient({
export function setupRpcHandler({ messenger }: { messenger: Messenger }) {
messenger.reply('request', async ({ request, rpcUrl }, meta) => {
const isInpage =
meta.sender.tab &&
!meta.sender.tab?.url?.includes('extension://') &&
(!meta.sender.frameId || meta.sender.frameId === 0)
const rpcClient = getRpcClient({ rpcUrl })
Expand Down Expand Up @@ -104,8 +105,6 @@ export function setupRpcHandler({ messenger }: { messenger: Messenger }) {

addPendingRequest({ ...request, sender: meta.sender })

inpageMessenger.send('toggleWallet', { open: true })

try {
const response = await new Promise((resolve, reject) => {
walletMessenger.reply(
Expand Down Expand Up @@ -137,7 +136,6 @@ export function setupRpcHandler({ messenger }: { messenger: Messenger }) {
})
return response as RpcResponse
} finally {
inpageMessenger.send('toggleWallet', { useStorage: true })
removePendingRequest(request.id)
}
}
Expand Down Expand Up @@ -176,8 +174,6 @@ export function setupRpcHandler({ messenger }: { messenger: Messenger }) {

addPendingRequest({ ...request, sender: meta.sender })

inpageMessenger.send('toggleWallet', { open: true })

try {
const response = await new Promise((resolve) => {
walletMessenger.reply(
Expand Down
20 changes: 20 additions & 0 deletions src/entries/background/wallet-sidebar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { settingsStore } from '../../zustand'

export function setupWalletSidebarHandler() {
chrome.runtime.onMessage.addListener((message, sender) => {
if (message.type === 'openWallet') {
const { bypassSignatureAuth, bypassTransactionAuth } =
settingsStore.getState()
const { method } = message.payload
if (
method === 'eth_requestAccounts' ||
(method === 'eth_sendTransaction' && !bypassTransactionAuth) ||
(method === 'eth_sign' && !bypassSignatureAuth) ||
(method === 'eth_signTypedData_v4' && !bypassSignatureAuth) ||
(method === 'personal_sign' && !bypassSignatureAuth)
) {
chrome.sidePanel.open({ tabId: sender.tab!.id! })
}
}
})
}
14 changes: 11 additions & 3 deletions src/entries/content/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { getMessenger } from '~/messengers'
import { setupBridgeTransportRelay } from '~/messengers/transports/bridge'

import { injectWallet } from './injectWallet'

setupBridgeTransportRelay()
injectWallet()

const backgroundMessenger = getMessenger('background:contentScript')
backgroundMessenger.send('ping', undefined)
setInterval(() => {
backgroundMessenger.send('ping', undefined)
}, 5000)

window.addEventListener('message', ({ data }) => {
if (data.type === 'openWallet') chrome.runtime.sendMessage(data)
})
148 changes: 0 additions & 148 deletions src/entries/content/injectWallet.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/messengers/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,5 @@ export type Schema = {
response: RpcResponse,
]
toggleTheme: [payload: void, response: void]
toggleWallet: [
payload:
| ({ route?: string } & (
| { open: boolean; useStorage?: never }
| { open?: never; useStorage?: true }
))
| undefined,
response: void,
]
transactionExecuted: [payload: void, response: void]
}
Loading

0 comments on commit 2125a66

Please sign in to comment.