Files
twenty/packages/twenty-front/src/modules/ui/field/display/components/CurrencyDisplay.tsx
T
Charles Bochet 3bfdc2c83f chore(twenty-front): migrate command-menu, workflow, page-layout and UI modules from Emotion to Linaria (PR 4-6/10) (#18342)
## Summary

Continues the Emotion → Linaria migration (PR 4-6 from the [migration
plan](docs/emotion-to-linaria-migration-plan.md)). Migrates **311
files** across four module groups:

| Module | Files |
|---|---|
| command-menu | 53 |
| workflow | 84 |
| page-layout | 84 |
| UI (partial - first ~80 files) | ~80 |
| twenty-ui (TEXT_INPUT_STYLE) | 1 |
| misc (hooks, keyboard-shortcut-menu, file-upload) | ~9 |

### Migration patterns applied

- `import styled from '@emotion/styled'` → `import { styled } from
'@linaria/react'`
- `import { useTheme } from '@emotion/react'` → `import { useContext }
from 'react'` + `import { ThemeContext } from 'twenty-ui/theme'`
- `${({ theme }) => theme.X.Y.Z}` → `${themeCssVariables.X.Y.Z}` (static
CSS variables)
- `theme.spacing(N)` → `themeCssVariables.spacing[N]`
- `styled(motion.div)` → `motion.create(StyledBase)` (11 components)
- `styled(Component)<TypeParams>` → wrapper div approach for non-HTML
elements
- Multi-declaration interpolations split into one CSS property per
interpolation
- Interpolation return types fixed (`&&` → ternary `? : ''`)
- `TEXT_INPUT_STYLE` converted from function to static string constant
(backward compatible)
- Emotion `<Global>` replaced with `useEffect` style injection
- Complex runtime-dependent styles use CSS custom properties via
`style={}` prop

### After this PR

- **Remaining files**: ~400 (object-record: ~160, settings: ~200, UI:
~44)
- **No breaking changes**: CSS variables resolve identically to the
previous Emotion theme values
2026-03-03 16:42:03 +01:00

94 lines
3.4 KiB
TypeScript

import { useContext, useId, useState } from 'react';
import { createPortal } from 'react-dom';
import { AppTooltip, TooltipDelay, TooltipPosition } from 'twenty-ui/display';
import { useNumberFormat } from '@/localization/hooks/useNumberFormat';
import { type FieldDefinition } from '@/object-record/record-field/ui/types/FieldDefinition';
import {
type FieldCurrencyMetadata,
type FieldCurrencyValue,
} from '@/object-record/record-field/ui/types/FieldMetadata';
import { SETTINGS_FIELD_CURRENCY_CODES } from '@/settings/data-model/constants/SettingsFieldCurrencyCodes';
import { EllipsisDisplay } from '@/ui/field/display/components/EllipsisDisplay';
import { isDefined, formatToShortNumber } from 'twenty-shared/utils';
import { DEFAULT_DECIMAL_VALUE } from '~/utils/format/formatNumber';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
import { ThemeContext } from 'twenty-ui/theme';
type CurrencyDisplayProps = {
currencyValue: FieldCurrencyValue | null | undefined;
fieldDefinition: FieldDefinition<FieldCurrencyMetadata>;
};
export const CurrencyDisplay = ({
currencyValue,
fieldDefinition,
}: CurrencyDisplayProps) => {
const { theme } = useContext(ThemeContext);
const instanceId = useId();
const [shouldRenderTooltip, setShouldRenderTooltip] = useState(false);
const currencyCode = currencyValue?.currencyCode;
const currencyMetadata = isDefined(currencyCode)
? SETTINGS_FIELD_CURRENCY_CODES[currencyCode]
: null;
const CurrencyIcon = currencyMetadata?.Icon ?? null;
const amountToDisplay = isUndefinedOrNull(currencyValue?.amountMicros)
? null
: currencyValue?.amountMicros / 1000000;
const format = fieldDefinition.metadata.settings?.format;
const decimals = fieldDefinition.metadata.settings?.decimals;
const decimalsToUse = decimals ?? DEFAULT_DECIMAL_VALUE;
const { formatNumber } = useNumberFormat();
const tooltipAnchorId = `currency-icon-${instanceId.replace(/[^a-zA-Z0-9-_]/g, '-')}`;
const currencyTooltipContent = isDefined(currencyCode)
? `${currencyCode}${currencyMetadata?.label ? ` - ${currencyMetadata.label}` : ''}`
: undefined;
const shouldShowCurrencyTooltip =
isDefined(CurrencyIcon) &&
amountToDisplay !== null &&
isDefined(currencyTooltipContent);
return (
<>
<EllipsisDisplay>
{shouldShowCurrencyTooltip && (
<>
<span
id={tooltipAnchorId}
onMouseEnter={() => setShouldRenderTooltip(true)}
onMouseLeave={() => setShouldRenderTooltip(false)}
>
<CurrencyIcon
color={theme.font.color.primary}
size={theme.icon.size.md}
stroke={theme.icon.stroke.sm}
/>
</span>{' '}
</>
)}
{amountToDisplay !== null
? !isDefined(format) || format === 'short'
? formatToShortNumber(amountToDisplay)
: formatNumber(amountToDisplay, { decimals: decimalsToUse })
: null}
</EllipsisDisplay>
{shouldRenderTooltip &&
shouldShowCurrencyTooltip &&
createPortal(
<AppTooltip
anchorSelect={`#${tooltipAnchorId}`}
content={currencyTooltipContent}
delay={TooltipDelay.shortDelay}
place={TooltipPosition.Top}
positionStrategy="fixed"
/>,
document.body,
)}
</>
);
};