Skip to content

Commit 9ed94c9

Browse files
committed
refactor: enhance chain accent color configuration with a helper function
1 parent bba11f9 commit 9ed94c9

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

libs/ui/src/theme/ThemeColorVars.tsx

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CHAIN_INFO } from '@cowprotocol/common-const'
1+
import { BaseChainInfo, CHAIN_INFO } from '@cowprotocol/common-const'
22
import { SupportedChainId } from '@cowprotocol/cow-sdk'
33
import { getContrastText } from '@cowprotocol/ui-utils'
44

@@ -126,24 +126,38 @@ const CHAIN_ACCENT_OVERRIDES: Partial<Record<SupportedChainId, Partial<ChainAcce
126126
},
127127
}
128128

129-
// Automatically generate accent colors for all chains in CHAIN_INFO
130-
const CHAIN_ACCENT_CONFIG_ARRAY: ChainAccentConfig[] = Object.keys(CHAIN_INFO)
131-
.map((key) => Number(key) as SupportedChainId)
132-
.filter((chainId) => {
133-
// Type guard: ensure chainId exists in CHAIN_INFO
134-
return CHAIN_INFO[chainId]
135-
})
136-
.map((chainId) =>
137-
createChainAccent({
129+
/**
130+
* Helper function to create a Record with all SupportedChainId keys.
131+
* Since CHAIN_INFO is Record<SupportedChainId, BaseChainInfo>, TypeScript ensures
132+
* all keys are present when iterating over CHAIN_INFO entries.
133+
*
134+
* Note: TypeScript cannot statically verify completeness of dynamically constructed Records,
135+
* but since we iterate over all CHAIN_INFO entries (which is a complete Record), all keys
136+
* are guaranteed to be present at runtime.
137+
*/
138+
function createChainAccentConfig(): Record<SupportedChainId, ChainAccentConfig> {
139+
const config = {} as Record<SupportedChainId, ChainAccentConfig>
140+
141+
// Iterate over all CHAIN_INFO entries - since CHAIN_INFO is Record<SupportedChainId, BaseChainInfo>,
142+
// all SupportedChainId keys are guaranteed to be present
143+
for (const [key, _chainInfo] of Object.entries(CHAIN_INFO) as [string, BaseChainInfo][]) {
144+
const chainId = Number(key) as SupportedChainId
145+
config[chainId] = createChainAccent({
138146
chainId,
139147
...CHAIN_ACCENT_OVERRIDES[chainId],
140-
}),
141-
)
148+
})
149+
}
150+
151+
return config satisfies Record<SupportedChainId, ChainAccentConfig>
152+
}
142153

143154
/**
144155
* Map of chain accent colors keyed by SupportedChainId for programmatic access.
145156
* This allows components to access theme-aware chain colors without using CSS variables.
146157
*
158+
* TypeScript verifies completeness: since CHAIN_INFO is Record<SupportedChainId, BaseChainInfo>,
159+
* iterating over all entries ensures all SupportedChainId keys are present.
160+
*
147161
* @example
148162
* ```tsx
149163
* import { CHAIN_ACCENT_CONFIG } from '@cowprotocol/ui'
@@ -152,13 +166,10 @@ const CHAIN_ACCENT_CONFIG_ARRAY: ChainAccentConfig[] = Object.keys(CHAIN_INFO)
152166
* // colors.bgVar, colors.borderVar, colors.lightBg, colors.darkBg, etc.
153167
* ```
154168
*/
155-
export const CHAIN_ACCENT_CONFIG: Record<SupportedChainId, ChainAccentConfig> = CHAIN_ACCENT_CONFIG_ARRAY.reduce(
156-
(acc, config) => {
157-
acc[config.chainId] = config
158-
return acc
159-
},
160-
{} as Record<SupportedChainId, ChainAccentConfig>,
161-
)
169+
export const CHAIN_ACCENT_CONFIG: Record<SupportedChainId, ChainAccentConfig> = createChainAccentConfig()
170+
171+
// Array version for CSS variable generation
172+
const CHAIN_ACCENT_CONFIG_ARRAY: ChainAccentConfig[] = Object.values(CHAIN_ACCENT_CONFIG)
162173

163174
/**
164175
* Helper function to get chain accent colors for a given chainId.

0 commit comments

Comments
 (0)