Skip to content

Commit

Permalink
ci: deploy widget-configurator for pr (#3265)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Oct 27, 2023
1 parent 0c25bd9 commit ac7eb7f
Show file tree
Hide file tree
Showing 24 changed files with 104 additions and 121 deletions.
1 change: 1 addition & 0 deletions apps/cowswap-frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/>

<script src="/emergency.js"></script>

<!-- Manifest, theme & colors -->
<link rel="manifest" href="manifest.json" />
<meta name="theme-color" media="(prefers-color-scheme: light)" content="white" />
Expand Down
15 changes: 14 additions & 1 deletion apps/cowswap-frontend/src/common/hooks/useAnalyticsReporter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react'
import { useEffect, useMemo } from 'react'

import {
Dimensions,
Expand All @@ -21,6 +21,8 @@ import ReactGA from 'react-ga4'
import { useLocation } from 'react-router-dom'
import { getCLS, getFCP, getFID, getLCP, Metric } from 'web-vitals'

import { useInjectedWidgetMetaData } from 'modules/injectedWidget'

import { useGetMarketDimension } from './useGetMarketDimension'

export function sendTiming(timingCategory: any, timingVar: any, timingValue: any, timingLabel: any) {
Expand Down Expand Up @@ -58,6 +60,7 @@ export function useAnalyticsReporter() {
const { connector } = useWeb3React()
const { chainId, account } = useWalletInfo()
const { walletName: _walletName } = useWalletDetails()
const injectedWidgetMetaData = useInjectedWidgetMetaData()
const prevAccount = usePrevious(account)

const marketDimension = useGetMarketDimension()
Expand All @@ -73,6 +76,11 @@ export function useAnalyticsReporter() {

const walletName = _walletName || getConnectionName(connection.type, isMetaMask)

const injectedWidgetAppId = useMemo(
() => (injectedWidgetMetaData ? `${injectedWidgetMetaData.appKey}:${injectedWidgetMetaData.url}` : ''),
[injectedWidgetMetaData]
)

useEffect(() => {
// Custom dimension 2 - walletname
googleAnalytics.setDimension(Dimensions.walletName, account ? walletName : 'Not connected')
Expand All @@ -93,6 +101,11 @@ export function useAnalyticsReporter() {
googleAnalytics.setDimension(Dimensions.market, marketDimension)
}, [marketDimension])

useEffect(() => {
// Custom dimension 6 - injected widget app id
googleAnalytics.setDimension(Dimensions.injectedWidgetAppId, injectedWidgetAppId)
}, [injectedWidgetAppId])

useEffect(() => {
googleAnalytics.pageview(`${pathname}${search}`)
}, [pathname, search])
Expand Down
6 changes: 3 additions & 3 deletions apps/cowswap-frontend/src/modules/appData/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { DEFAULT_APP_CODE, SAFE_APP_CODE } from '@cowprotocol/common-const'
import { isInjectedWidget } from '@cowprotocol/common-utils'
import { useIsSafeApp } from '@cowprotocol/wallet'

import { useInjectedWidgetMetaData } from 'modules/injectedWidget'

import { addAppDataToUploadQueueAtom, appDataHooksAtom, appDataInfoAtom } from './state/atoms'
import { AppDataInfo } from './types'

import { injectedWidgetMetaDataAtom } from '../injectedWidget/state/injectedWidgetMetaDataAtom'

const APP_CODE = process.env.REACT_APP_APP_CODE

export function useAppData(): AppDataInfo | null {
return useAtomValue(appDataInfoAtom)
}

export function useAppCode(): string | null {
const injectedWidgetMetaData = useAtomValue(injectedWidgetMetaDataAtom)
const injectedWidgetMetaData = useInjectedWidgetMetaData()
const isSafeApp = useIsSafeApp()

return useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useAtomValue } from 'jotai'

import { InjectedWidgetMetaData, injectedWidgetMetaDataAtom } from '../state/injectedWidgetMetaDataAtom'

export function useInjectedWidgetMetaData(): InjectedWidgetMetaData | null {
return useAtomValue(injectedWidgetMetaDataAtom)
}
5 changes: 3 additions & 2 deletions apps/cowswap-frontend/src/modules/injectedWidget/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './updaters/InjectedWidgetUpdater'
export * from './hooks/useInjectedWidgetParams'
export { InjectedWidgetUpdater } from './updaters/InjectedWidgetUpdater'
export { useInjectedWidgetParams } from './hooks/useInjectedWidgetParams'
export { useInjectedWidgetMetaData } from './hooks/useInjectedWidgetMetaData'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from 'react'
import { useLayoutEffect, useRef } from 'react'

import { COW_SWAP_WIDGET_EVENT_KEY } from '../consts'
import { useInjectedWidgetParams } from '../hooks/useInjectedWidgetParams'
Expand All @@ -9,7 +9,7 @@ export function IframeResizer() {
const { dynamicHeightEnabled } = useInjectedWidgetParams()
const previousHeightRef = useRef(0)

useEffect(() => {
useLayoutEffect(() => {
if (!dynamicHeightEnabled) return

// Initial height calculation and message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useAtomValue, useSetAtom } from 'jotai'
import { useEffect } from 'react'

import { isInjectedWidget } from '@cowprotocol/common-utils'

import {
LimitOrdersDerivedState,
limitOrdersDerivedStateAtom,
Expand All @@ -17,7 +19,7 @@ export function useFillLimitOrdersDerivedState() {
const rawState = useAtomValue(limitOrdersRawStateAtom)
const updateDerivedState = useSetAtom(limitOrdersDerivedStateAtom)

const isUnlocked = rawState.isUnlocked
const isUnlocked = rawState.isUnlocked || isInjectedWidget()
const derivedState = useBuildTradeDerivedState(limitOrdersRawStateAtom)

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion apps/widget-configurator/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/apps/widget-configurator"
"outputPath": "build/widget-configurator"
},
"configurations": {
"development": {
Expand Down
24 changes: 0 additions & 24 deletions apps/widget-configurator/src/app/WidgetIframe/index.tsx

This file was deleted.

25 changes: 0 additions & 25 deletions apps/widget-configurator/src/app/WidgetIframe/styled.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import EventEmitter from 'events'

const onDisconnect = new EventEmitter()

export function useProvider(): EthereumProvider | null {
export function useProvider(): EthereumProvider | undefined {
const config = useConfig()
const { isDisconnected } = useAccount()
const [provider, setProvider] = useState<EthereumProvider | null>(null)
const [provider, setProvider] = useState<EthereumProvider | undefined>(undefined)

useEffect(() => {
return config.subscribe(({ connector }) => {
Expand All @@ -38,7 +38,7 @@ function getEthereumProvider(provider: EthereumProvider, onDisconnect: EventEmit
enable(...args) {
return provider.enable(...args)
},
on(event: string, args: unknown) {
on(event: string, args: never) {
if (event === 'disconnect' || event === 'close') {
return onDisconnect.on('disconnect', args)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { useMemo } from 'react'

import { CowSwapWidgetParams, CowSwapWidgetSettings, EthereumProvider } from '@cowprotocol/widget-lib'
import { CowSwapWidgetEnv, CowSwapWidgetParams, CowSwapWidgetSettings, EthereumProvider } from '@cowprotocol/widget-lib'

import { isDev, isLocalHost, isVercel } from '../../../env'
import { ConfiguratorState } from '../types'

const getEnv = (): CowSwapWidgetEnv => {
if (isLocalHost) return 'local'
if (isDev) return 'dev'
if (isVercel) return 'pr'

return 'prod'
}

export function useWidgetParamsAndSettings(
provider: EthereumProvider | null,
widgetContainer: HTMLDivElement,
provider: EthereumProvider | undefined,
widgetContainer: HTMLDivElement | null,
configuratorState: ConfiguratorState
) {
return useMemo(() => {
if (!widgetContainer) return null

const {
chainId,
theme,
Expand All @@ -19,7 +30,7 @@ export function useWidgetParamsAndSettings(
sellTokenAmount,
buyToken,
buyTokenAmount,
isDynamicHeightEnabled,
dynamicHeightEnabled,
} = configuratorState

const params: CowSwapWidgetParams = {
Expand All @@ -34,15 +45,15 @@ export function useWidgetParamsAndSettings(
urlParams: {
theme,
chainId,
env: 'local',
env: getEnv(),
tradeType: currentTradeType,
tradeAssets: {
sell: { asset: sellToken, amount: sellTokenAmount ? sellTokenAmount.toString() : undefined },
buy: { asset: buyToken, amount: buyTokenAmount.toString() },
buy: { asset: buyToken, amount: buyTokenAmount?.toString() },
},
},
appParams: {
dynamicHeightEnabled: isDynamicHeightEnabled,
dynamicHeightEnabled,
enabledTradeTypes,
},
}
Expand Down
22 changes: 5 additions & 17 deletions apps/widget-configurator/src/app/configurator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { cowSwapWidget, EthereumProvider, TradeType, UpdateWidgetCallback } from
import WalletIcon from '@mui/icons-material/Wallet'
import LoadingButton from '@mui/lab/LoadingButton'
import Box from '@mui/material/Box'
import Checkbox from '@mui/material/Checkbox'
import Divider from '@mui/material/Divider'
import Drawer from '@mui/material/Drawer'
import FormControlLabel from '@mui/material/FormControlLabel'
import Link from '@mui/material/Link'
import Typography from '@mui/material/Typography'

Expand Down Expand Up @@ -60,8 +58,6 @@ export function Configurator({ title }: { title: string }) {
const iframeContainerRef = useRef<HTMLDivElement>(null)
const updateWidgetRef = useRef<UpdateWidgetCallback | null>(null)

const [isDynamicHeightEnabled, setDynamicHeightEnabled] = useState(true)

const provider = useProvider()
const providerRef = useRef<EthereumProvider | null>()

Expand All @@ -74,13 +70,14 @@ export function Configurator({ title }: { title: string }) {
sellTokenAmount,
buyToken,
buyTokenAmount,
isDynamicHeightEnabled,
dynamicHeightEnabled: true,
}

const { params, settings } = useWidgetParamsAndSettings(provider, iframeContainerRef.current, state)
const paramsAndSettings = useWidgetParamsAndSettings(provider, iframeContainerRef.current, state)
const { params, settings } = paramsAndSettings || {}

useEffect(() => {
if (!params.container) return
if (!params?.container || !settings) return

// Re-initialize widget when provider is changed
if (provider && providerRef.current !== provider) {
Expand Down Expand Up @@ -146,15 +143,6 @@ export function Configurator({ title }: { title: string }) {

<CurrencyInputControl label="Buy token" tokenIdState={buyTokenState} tokenAmountState={buyTokenAmountState} />

<Divider variant="middle">Advanced</Divider>

<FormControlLabel
control={
<Checkbox checked={isDynamicHeightEnabled} onChange={(e) => setDynamicHeightEnabled(e.target.checked)} />
}
label="Dynamic widget height"
/>

<Divider variant="middle" />

{/* <LoadingButton loading={false} loadingPosition="start" startIcon={<SaveIcon />} variant="contained" onClick={handleWidgetRefreshClick}>
Expand All @@ -167,7 +155,7 @@ export function Configurator({ title }: { title: string }) {
</Drawer>

<Box sx={ContentStyled}>
<EmbedDialog params={params} settings={settings} />
{params && settings && <EmbedDialog params={params} settings={settings} />}
<br />
<div ref={iframeContainerRef}></div>
</Box>
Expand Down
2 changes: 1 addition & 1 deletion apps/widget-configurator/src/app/configurator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export interface ConfiguratorState {
sellTokenAmount: number | undefined
buyToken: string
buyTokenAmount: number | undefined
isDynamicHeightEnabled: boolean
dynamicHeightEnabled: boolean
}

This file was deleted.

5 changes: 5 additions & 0 deletions apps/widget-configurator/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const isLocalHost = ['localhost', '127.0.0.1'].includes(window.location.hostname)

export const isVercel = window.location.hostname.includes('vercel.app')

export const isDev = window.location.hostname.includes('dev.swap.cow.fi')
3 changes: 3 additions & 0 deletions apps/widget-configurator/src/theme/ColorModeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export const globalStyles = (theme: Theme, mode: PaletteMode) => ({
background: mode === 'light' && theme.palette.cow.gradient,
backgroundImage: mode === 'dark' && theme.palette.cow.gradient,
},
'w3m-modal': {
zIndex: 1200,
},
})

export enum ThemeMode {
Expand Down
Loading

0 comments on commit ac7eb7f

Please sign in to comment.