Skip to content
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
119 changes: 4 additions & 115 deletions api/src/routes/get-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,8 @@ import {
} from '@shared/config-storage-service'
import { getDefaultProfile } from '@shared/default-data'
import { AWS_PREFIX } from '@shared/defines'
import {
numberToBannerFontSize,
numberToWidgetFontSize,
PROFILE_IDS,
TOOLS,
} from '@shared/types'
import type {
BaseToolProfile,
Configuration,
ConfigVersions,
ElementConfigType,
Tool,
ToolProfile,
} from '@shared/types'
import { PROFILE_IDS, TOOLS } from '@shared/types'
import type { Configuration } from '@shared/types'
import { app } from '../app.js'
import { createHTTPException } from '../utils/utils.js'

Expand All @@ -47,21 +35,8 @@ app.get(
const storage = new ConfigStorageService({ ...env, AWS_PREFIX })

try {
let profile: ToolProfile<typeof tool> | null = null
try {
const config = await storage.getJson<Configuration>(walletAddress)
profile = config[tool]?.[profileId] ?? null
} catch (e) {
if (!isConfigStorageNotFoundError(e)) {
throw e
}
// TODO: to be removed after the completion of versioned config migration
const legacy = await storage.getJson<ConfigVersions>(
walletAddress,
true,
)
profile = convertToProfile(legacy[profileId], tool) ?? null
}
const config = await storage.getJson<Configuration>(walletAddress)
const profile = config[tool]?.[profileId] ?? null

if (!profile)
throw new ConfigStorageServiceError(
Expand All @@ -81,89 +56,3 @@ app.get(
}
},
)

// TODO: to be removed after the completion of versioned configurations
function convertToProfile<T extends Tool>(
config: ElementConfigType,
tool: T,
): ToolProfile<T> | undefined {
// means there is no profile for the given tool/profileId
if (!config) return

if (tool === 'offerwall') {
return config.offerwall as ToolProfile<T>
}

return {
$version: '0.0.1',
$name: config.versionName,
$modifiedAt: '',
...getToolProfile(config, tool),
} as ToolProfile<T>
}

/** @legacy */
function getToolProfile(profile: ElementConfigType, tool: Tool) {
if (tool === 'banner') {
return {
title: {
text: profile.bannerTitleText,
},
description: {
text: profile.bannerDescriptionText,
isVisible: profile.bannerDescriptionVisible,
},
font: {
name: profile.bannerFontName,
size: numberToBannerFontSize(profile.bannerFontSize),
},
animation: {
type: profile.bannerSlideAnimation,
},
position: profile.bannerPosition,
border: {
type: profile.bannerBorder,
},
color: {
text: profile.bannerTextColor,
background: profile.bannerBackgroundColor,
},
thumbnail: {
value: profile.bannerThumbnail,
},
} satisfies Omit<ToolProfile<'banner'>, keyof BaseToolProfile>
}
if (tool === 'widget') {
return {
title: {
text: profile.widgetTitleText,
},
description: {
text: profile.widgetDescriptionText,
isVisible: profile.widgetDescriptionVisible,
},
font: {
name: profile.widgetFontName,
size: numberToWidgetFontSize(profile.widgetFontSize),
},
position: profile.widgetPosition,
border: {
type: profile.widgetButtonBorder,
},
color: {
text: profile.widgetTextColor,
background: profile.widgetBackgroundColor,
theme: profile.widgetButtonBackgroundColor,
},
ctaPayButton: {
text: profile.widgetButtonText,
},
icon: {
value: '',
color: profile.widgetTriggerBackgroundColor,
},
} satisfies Omit<ToolProfile<'widget'>, keyof BaseToolProfile>
}

throw new Error(`Unsupported tool type: ${tool}`)
}
4 changes: 2 additions & 2 deletions components/src/banner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'lit'
import { property, state } from 'lit/decorators.js'
import defaultLogo from '@c/assets/wm_logo_animated.svg?url'
import { bannerFontSizeToNumber, BORDER_RADIUS } from '@shared/types'
import { BANNER_FONT_SIZE_MAP, BORDER_RADIUS } from '@shared/types'
import type {
FontFamilyKey,
BorderRadiusKey,
Expand Down Expand Up @@ -265,7 +265,7 @@ export class BannerController implements ReactiveController {
if (font.size) {
element.style.setProperty(
'--wm-font-size',
bannerFontSizeToNumber(font.size) + 'px',
`${BANNER_FONT_SIZE_MAP[font.size]}px`,
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/src/widget/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
import {
WIDGET_POSITION,
BORDER_RADIUS,
widgetFontSizeToNumber,
WIDGET_FONT_SIZE_MAP,
} from '@shared/types'
import type { FontFamilyKey, BorderRadiusKey } from '@shared/types'
import { applyFontFamily } from '../utils.js'
Expand Down Expand Up @@ -112,7 +112,7 @@ export class WidgetController implements ReactiveController {
if (font.size) {
element.style.setProperty(
'--wm-font-size',
`${widgetFontSizeToNumber(font.size)}px`,
`${WIDGET_FONT_SIZE_MAP[font.size]}px`,
)
}
if (border.type) {
Expand Down
15 changes: 4 additions & 11 deletions frontend/app/components/redesign/components/ToolsWalletAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { SVGRefresh, SVGSpinner } from '~/assets/svg'
import { useConnectWallet } from '~/hooks/useConnectWallet'
import { useTrackEvent } from '~/lib/analytics'
import type { ElementErrors } from '~/lib/types'
import { useUIActions } from '~/stores/uiStore'
import type { WalletActions, WalletStore } from '~/stores/wallet-store'

Expand All @@ -28,7 +27,7 @@ export const ToolsWalletAddress = ({
const { connect, disconnect } = useConnectWallet(snap, walletActions)
const uiActions = useUIActions()
const trackEvent = useTrackEvent()
const [error, setError] = useState<ElementErrors>()
const [error, setError] = useState<{ walletAddress?: string[] }>()
const [isLoading, setIsLoading] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)

Expand All @@ -48,10 +47,7 @@ export const ToolsWalletAddress = ({

const handleContinue = async () => {
if (!snap.walletAddress.trim()) {
setError({
fieldErrors: { walletAddress: ['This field is required'] },
message: [],
})
setError({ walletAddress: ['This field is required'] })
return
}

Expand All @@ -67,10 +63,7 @@ export const ToolsWalletAddress = ({
await connect()
trackEvent('wallet_connected')
} catch (error) {
setError({
fieldErrors: { walletAddress: [(error as Error).message] },
message: [],
})
setError({ walletAddress: [(error as Error).message] })
} finally {
setIsLoading(false)
}
Expand Down Expand Up @@ -162,7 +155,7 @@ export const ToolsWalletAddress = ({
onChange={handleWalletAddressChange}
disabled={snap.isWalletConnected}
readOnly={isLoading}
error={error?.fieldErrors.walletAddress}
error={error?.walletAddress}
/>
</div>
{snap.isWalletConnected && (
Expand Down
41 changes: 1 addition & 40 deletions frontend/app/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type z from 'zod'
import type { ElementConfigType, Tool, ToolProfiles } from '@shared/types'
import type {
createBannerSchema,
createButtonSchema,
createWidgetSchema,
} from '../utils/validate.server.js'
import type { Tool, ToolProfiles } from '@shared/types'

export type SaveResult = {
success?: boolean
Expand All @@ -29,39 +23,6 @@ export type GetProfilesResult<T extends Tool> = {
}
}

export type SanitizedFields = Pick<
ElementConfigType,
| 'bannerTitleText'
| 'bannerDescriptionText'
| 'widgetTitleText'
| 'widgetDescriptionText'
| 'widgetButtonText'
| 'buttonText'
| 'buttonDescriptionText'
| 'walletAddress'
| 'tag'
>

export type JSONError<T extends z.ZodTypeAny> = {
errors: z.ZodFlattenedError<z.infer<T>>
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Keys<T> = T extends any ? keyof T : never

export type ZodFieldErrors<T extends z.ZodTypeAny> = {
[P in Keys<z.TypeOf<T>>]?: string[] | undefined
}

export type ElementErrors = {
fieldErrors: ZodFieldErrors<
| typeof createButtonSchema
| typeof createBannerSchema
| typeof createWidgetSchema
>
message: string[]
}

declare global {
interface Window {
umami?: {
Expand Down
27 changes: 6 additions & 21 deletions frontend/app/routes/api.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import z from 'zod'
import { isConfigStorageNotFoundError } from '@shared/config-storage-service'
import { AWS_PREFIX } from '@shared/defines'
import {
type ConfigVersions,
PROFILE_IDS,
type Configuration,
TOOL_BANNER,
TOOL_OFFERWALL,
TOOL_WIDGET,
} from '@shared/types'
import { getWalletAddress, normalizeWalletAddress } from '@shared/utils'
import { convertToConfiguration } from '@shared/utils/profile-converter'
import { APP_BASEPATH } from '~/lib/constants.js'
import type { ApiError } from '~/lib/helpers'
import { INVALID_PAYLOAD_ERROR } from '~/lib/helpers'
Expand Down Expand Up @@ -108,32 +106,19 @@ export async function action({ request, context }: ActionFunctionArgs) {
const storage = new ConfigStorageService({ ...env, AWS_PREFIX })
const now = new Date().toISOString()

let config: Configuration | null = null
let config: Configuration
try {
config = await storage.getJson<Configuration>(walletAddressId)
} catch (e) {
if (!isConfigStorageNotFoundError(e)) {
throw e
}

try {
// TODO: to be removed after the completion of versioned config migration
const legacy = await storage.getJson<ConfigVersions>(
walletAddressId,
true,
)
config = convertToConfiguration(legacy, walletAddressId)
} catch (e) {
if (!isConfigStorageNotFoundError(e)) {
throw e
}

config = {
$walletAddress: walletAddress,
$walletAddressId: walletAddressId,
$createdAt: now,
$modifiedAt: now,
}
config = {
$walletAddress: walletAddress,
$walletAddressId: walletAddressId,
$createdAt: now,
$modifiedAt: now,
}
}

Expand Down
28 changes: 4 additions & 24 deletions frontend/app/routes/api.profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ import {
isConfigStorageNotFoundError,
} from '@shared/config-storage-service'
import { AWS_PREFIX } from '@shared/defines'
import type {
Configuration,
ConfigVersions,
Tool,
ToolProfiles,
} from '@shared/types'
import type { Configuration, Tool } from '@shared/types'
import { TOOLS } from '@shared/types'
import { getWalletAddress, normalizeWalletAddress } from '@shared/utils'
import { convertToProfiles } from '@shared/utils/profile-converter'
import { INVALID_PAYLOAD_ERROR } from '~/lib/helpers'
import type { GetProfilesResult } from '~/lib/types'
import { walletSchema } from '~/utils/validate.server'
Expand Down Expand Up @@ -54,23 +48,9 @@ export async function loader({ request, context }: Route.LoaderArgs) {
const walletAddressId = normalizeWalletAddress(walletAddressData)
const storage = new ConfigStorageService({ ...env, AWS_PREFIX })

let profiles: ToolProfiles<typeof tool> | null = null
try {
const config = await storage.getJson<Configuration>(walletAddressId)
// config can exist but not have tool specific profiles,
profiles = config[tool]
} catch (e) {
if (!isConfigStorageNotFoundError(e)) {
throw e
}

// TODO: to be removed after the completion of versioned config migration
const legacy = await storage.getJson<ConfigVersions>(
walletAddressId,
true,
)
profiles = convertToProfiles(legacy, tool)
}
// config can exist but not have tool specific profiles
const config = await storage.getJson<Configuration>(walletAddressId)
const profiles = config[tool]

if (!profiles) {
throw new ConfigStorageServiceError(
Expand Down
Loading
Loading