|
| 1 | +import * as React from 'react' |
| 2 | +import { type LayoutChangeEvent, StyleSheet, View } from 'react-native' |
| 3 | +import Svg, { Polygon } from 'react-native-svg' |
| 4 | + |
| 5 | +import { lstrings } from '../../locales/strings' |
| 6 | +import { styled } from '../hoc/styled' |
| 7 | +import { useTheme } from '../services/ThemeContext' |
| 8 | +import { EdgeText } from '../themed/EdgeText' |
| 9 | + |
| 10 | +// TODO: Render a badge icon |
| 11 | +export const BestRateBadge = () => { |
| 12 | + const theme = useTheme() |
| 13 | + |
| 14 | + const [dimensions, setDimensions] = React.useState({ width: 70, height: 100 }) |
| 15 | + const { width, height } = dimensions |
| 16 | + |
| 17 | + const handleLayout = (event: LayoutChangeEvent) => { |
| 18 | + const { width, height } = event.nativeEvent.layout |
| 19 | + setDimensions({ width, height }) |
| 20 | + } |
| 21 | + |
| 22 | + // Compute a 5-point star sized around the text box |
| 23 | + const { svgWidth, svgHeight, points } = React.useMemo(() => { |
| 24 | + const padding = theme.rem(0.75) |
| 25 | + const svgWidth = width + padding * 2 |
| 26 | + const svgHeight = height + padding * 2 |
| 27 | + |
| 28 | + const centerX = svgWidth / 2 |
| 29 | + const centerY = svgHeight / 2 |
| 30 | + const outerRadius = Math.max(width, height) / 2 + padding * 0.9 |
| 31 | + const innerRadius = outerRadius * 0.75 |
| 32 | + |
| 33 | + const numPoints = 14 |
| 34 | + const totalVertices = numPoints * 2 // outer+inner alternating |
| 35 | + const startAngle = -Math.PI / 2 // Point up |
| 36 | + |
| 37 | + const pts: Array<{ x: number; y: number }> = [] |
| 38 | + for (let i = 0; i < totalVertices; i++) { |
| 39 | + const isOuter = i % 2 === 0 |
| 40 | + const radius = isOuter ? outerRadius : innerRadius |
| 41 | + const angle = startAngle + (i * Math.PI) / numPoints |
| 42 | + const x = centerX + radius * Math.cos(angle) |
| 43 | + const y = centerY + radius * Math.sin(angle) |
| 44 | + pts.push({ x, y }) |
| 45 | + } |
| 46 | + |
| 47 | + const points = pts.map(p => `${p.x},${p.y}`).join(' ') |
| 48 | + return { svgWidth, svgHeight, points } |
| 49 | + }, [height, theme, width]) |
| 50 | + |
| 51 | + return ( |
| 52 | + <BestRateBadgeContainer onLayout={handleLayout}> |
| 53 | + <Svg |
| 54 | + width={svgWidth} |
| 55 | + height={svgHeight} |
| 56 | + viewBox={`0 0 ${svgWidth} ${svgHeight}`} |
| 57 | + style={[ |
| 58 | + StyleSheet.absoluteFillObject, |
| 59 | + { top: -theme.rem(0.75), left: -theme.rem(0.75) } |
| 60 | + ]} |
| 61 | + > |
| 62 | + <Polygon |
| 63 | + points={points} |
| 64 | + fill="none" |
| 65 | + stroke={theme.iconTappable} |
| 66 | + strokeWidth={2} |
| 67 | + /> |
| 68 | + </Svg> |
| 69 | + <BestRateText>{lstrings.string_best_rate_badge_text}</BestRateText> |
| 70 | + </BestRateBadgeContainer> |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +const BestRateBadgeContainer = styled(View)(theme => ({ |
| 75 | + alignItems: 'center', |
| 76 | + justifyContent: 'center' |
| 77 | +})) |
| 78 | + |
| 79 | +const BestRateText = styled(EdgeText)(theme => ({ |
| 80 | + fontSize: theme.rem(0.5), |
| 81 | + fontWeight: 'bold', |
| 82 | + color: theme.primaryText, |
| 83 | + textAlign: 'center', |
| 84 | + letterSpacing: 0, |
| 85 | + zIndex: 1 |
| 86 | +})) |
0 commit comments