geo.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { COUNTRIES } from '@/components/interfaces/Organization/BillingSettings/BillingCustomerData/BillingAddress.constants'
  2. import { COUNTRY_LAT_LON } from '@/components/interfaces/ProjectCreation/ProjectCreation.constants'
  3. export type CountryCountRow = { country: string | null; count: number | string }
  4. export interface MapChartTheme {
  5. zeroFill: string // fill for countries with zero (or when max=0)
  6. brandFill: string // base fill color (same for all, vary by opacity)
  7. opacityScale: [number, number, number, number, number] // low -> high opacities
  8. boundaryStroke: string
  9. boundaryStrokeHover: string
  10. markerFill: string
  11. oceanFill: string
  12. }
  13. export const MAP_CHART_THEME: { light: MapChartTheme; dark: MapChartTheme } = {
  14. light: {
  15. zeroFill: 'hsl(var(--background-surface-400))',
  16. brandFill: 'hsl(var(--brand-default))',
  17. opacityScale: [0.18, 0.32, 0.5, 0.68, 0.86],
  18. boundaryStroke: 'hsla(var(--brand-300), 0.6)',
  19. boundaryStrokeHover: 'hsl(var(--brand-500))',
  20. markerFill: 'hsl(var(--brand-default))',
  21. oceanFill: 'transparent',
  22. },
  23. dark: {
  24. zeroFill: 'hsl(var(--background-selection))',
  25. brandFill: 'hsl(var(--brand-default))',
  26. opacityScale: [0.18, 0.32, 0.5, 0.68, 0.86],
  27. boundaryStroke: 'hsla(var(--brand-300), 0.6)',
  28. boundaryStrokeHover: 'hsl(var(--brand-500))',
  29. markerFill: 'hsl(var(--brand-default))',
  30. oceanFill: 'transparent',
  31. },
  32. }
  33. export const buildCountsByIso2 = (rows: CountryCountRow[]): Record<string, number> => {
  34. const counts: Record<string, number> = {}
  35. for (const row of rows) {
  36. if (!row.country) continue
  37. const code = row.country.toUpperCase()
  38. const numeric = typeof row.count === 'number' ? row.count : Number(row.count)
  39. if (!Number.isFinite(numeric)) continue
  40. counts[code] = (counts[code] || 0) + numeric
  41. }
  42. return counts
  43. }
  44. export const getFillColor = (
  45. value: number,
  46. max: number,
  47. theme: MapChartTheme = MAP_CHART_THEME.dark
  48. ): string => {
  49. if (max <= 0 || !value) return theme.zeroFill
  50. return theme.brandFill
  51. }
  52. export const getFillOpacity = (
  53. value: number,
  54. max: number,
  55. theme: MapChartTheme = MAP_CHART_THEME.dark
  56. ): number => {
  57. if (max <= 0 || !value) return 1
  58. const ratio = value / max
  59. if (ratio > 0.8) return theme.opacityScale[4]
  60. if (ratio > 0.6) return theme.opacityScale[3]
  61. if (ratio > 0.4) return theme.opacityScale[2]
  62. if (ratio > 0.2) return theme.opacityScale[1]
  63. return theme.opacityScale[0]
  64. }
  65. const MICRO_COUNTRIES = new Set([
  66. 'Singapore',
  67. 'Monaco',
  68. 'Andorra',
  69. 'Liechtenstein',
  70. 'San Marino',
  71. 'Vatican',
  72. 'Vatican City',
  73. 'Luxembourg',
  74. 'Malta',
  75. 'Bahrain',
  76. 'Brunei',
  77. 'Qatar',
  78. 'Kuwait',
  79. 'Hong Kong',
  80. 'Macau',
  81. ])
  82. export const isMicroCountry = (name: string): boolean => MICRO_COUNTRIES.has(name)
  83. export const isKnownCountryCode = (code: string): code is keyof typeof COUNTRY_LAT_LON => {
  84. return Object.prototype.hasOwnProperty.call(COUNTRY_LAT_LON, code)
  85. }
  86. export const computeMarkerRadius = (value: number, max: number): number => {
  87. if (max <= 0) return 2
  88. return Math.max(1.5, Math.min(4, (value / max) * 4))
  89. }
  90. // Best-effort extraction of ISO2 code from feature properties, with name fallback
  91. export const extractIso2FromFeatureProps = (
  92. props?: Record<string, unknown>
  93. ): string | undefined => {
  94. if (!props) return undefined
  95. const candidates = [
  96. 'ISO_A2_EH',
  97. 'ISO_A2',
  98. 'iso_a2',
  99. 'ADMIN_ISO_A2',
  100. 'WB_A2',
  101. 'ADM0_A3_IS',
  102. 'ADM0_A3',
  103. 'ISO_N3',
  104. 'id',
  105. ]
  106. for (const key of candidates) {
  107. const v = props[key] as unknown
  108. if (typeof v === 'string' && v.length === 2) return v.toUpperCase()
  109. }
  110. const name =
  111. (props['name'] as string | undefined) || (props['NAME'] as string | undefined) || undefined
  112. if (!name) return undefined
  113. const entry = COUNTRIES.find((c) => c.name === name)
  114. return entry?.code
  115. }
  116. export const iso2ToCountryName = (iso2: string): string => {
  117. const code = iso2.toUpperCase()
  118. const entry = COUNTRIES.find((c) => c.code === code)
  119. return entry?.name ?? code
  120. }