Files
supabase/apps/www/app/state-of-startups/components/SurveyChart.tsx
Brian-AnandDanny White f1082e28c7 chore: migrate Framer Motion imports to Motion (#51520)
## Problem

Closes #31221.
The applications and shared UI packages still import the former
`framer-motion` package instead of Motion's React entry point.

## Solution

Replace runtime imports, type imports, and three test mocks with
`motion/react` across the consuming workspaces.
Replace the seven direct dependency declarations with `motion:
^11.18.2`.
Preserve the existing animation engine version and all other dependency
resolutions, including the asynchronous `domMax` feature loader.
Historical blog examples remain as published; `framer-motion` remains a
transitive implementation dependency of Motion.

## Review instructions

1. Run `pnpm install --frozen-lockfile` and check the seven dependency
replacements in the lockfile.
2. Open the WWW Auth page and Docs homepage at desktop and mobile
widths, with normal and reduced motion preferences.
3. Open the mobile navigation, expand Product on WWW, and close the menu
using Escape and the close button.
4. Check the existing animation components, shared form validation, and
Studio tests for unchanged behavior.

Validation:

- Studio: 731 test files, 8,334 passing tests and one expected failure,
with `--maxWorkers=4`.
- Shared UI, UI patterns, WWW, and UI library: 615 passing tests.
- `pnpm run typecheck --concurrency=1`: all 16 repository tasks pass.
- All six application source lint scripts and lint ratchets pass;
generated Docs example copies are excluded from local lint.
- Repository Prettier source check, lockfile formatting, and case hazard
check pass.
- WWW: 10 existing end-to-end checks across nine pages.
- Docs: 5 existing guide link and accessibility checks across Database
and Auth.
- WWW and Docs mobile navigation pass at desktop/mobile sizes with
normal/reduced motion; SQL-to-REST translation tabs exercise the
asynchronous feature loader without browser errors.
- Frozen-lockfile installation passes; all existing package and snapshot
resolutions are unchanged.
- Learn and UI library command palettes open, filter results, and close
with Escape without browser errors.
- Independent code review finds no actionable defects.
- `npm run build -- --filter=www --filter=docs --filter=studio
--filter=learn --filter=library --concurrency=1 --continue=always`: all
five affected applications build successfully (9 tasks including
prerequisites); asset uploads disabled for local validation.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
(no docs topics changed)

---------

Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
2026-10-14 07:16:00 +00:00

163 lines
5.8 KiB
TypeScript

'use client'
import CodeBlock from '~/components/CodeBlock/CodeBlock'
import { motion } from 'motion/react'
import { useMemo, useState } from 'react'
import { Button } from 'ui'
import type { Aggregation, SurveyFilters } from '../lib/survey-key'
import { distributionToBars, getDistribution, mergeFilters } from '../lib/survey-keys'
import { buildSurveySql } from '../lib/survey-sql'
import { SurveyChartShell } from './SurveyChartShell'
import TwoOptionToggle from './TwoOptionToggle'
import { useYear } from './year-context'
interface SurveyChartProps {
title: string
/** Smaller parenthetical under the title clarifying how the question was asked. */
note?: string
column: string
aggregation: Aggregation
/** Static filters baked into the chart config (e.g. location = North America). */
filters?: SurveyFilters
/** Section-level cohort toggle filter, merged on top of `filters`. */
cohortFilter?: SurveyFilters
maxBars?: number
}
const accentBarFg = 'bg-brand-default'
const accentBarText = 'text-primary'
export function SurveyChart({
title,
note,
column,
aggregation,
filters,
cohortFilter,
maxBars,
}: SurveyChartProps) {
const { year } = useYear()
const effectiveFilters = useMemo(
() => mergeFilters(filters, cohortFilter),
[filters, cohortFilter]
)
const chartData = useMemo(() => {
const dist = getDistribution(year, column, aggregation, effectiveFilters)
return distributionToBars(dist, maxBars)
}, [year, column, aggregation, effectiveFilters, maxBars])
const [view, setView] = useState<'chart' | 'sql'>('chart')
const [isExpanded, setIsExpanded] = useState(false)
const handleViewChange = (newView: 'chart' | 'sql') => {
setView(newView)
setIsExpanded(true)
}
const maxValue = chartData.length > 0 ? Math.max(...chartData.map((item) => item.value)) : 0
const FIXED_HEIGHT = 300 // px
const BUTTON_AREA_HEIGHT = 40 // px
const CHART_HEIGHT = FIXED_HEIGHT - BUTTON_AREA_HEIGHT // px
const sql = useMemo(
() => buildSurveySql(year, column, aggregation, effectiveFilters, maxBars),
[year, column, aggregation, effectiveFilters, maxBars]
)
return (
<SurveyChartShell eyebrow="Q&A" title={title} note={note} isEmpty={chartData.length === 0}>
<div className="flex flex-row flex-wrap gap-6 px-8 pb-4 justify-end">
<div className="hidden xs:block">
<TwoOptionToggle
options={['SQL', 'chart']}
activeOption={view}
onClickOption={handleViewChange}
borderOverride="border-overlay"
/>
</div>
</div>
<motion.div
key={view}
className="overflow-hidden relative"
initial={false}
animate={{ height: isExpanded ? 'auto' : `${FIXED_HEIGHT}px` }}
transition={{ duration: 0.2, ease: 'easeInOut' }}
>
{view === 'chart' ? (
<div className="flex flex-col h-full w-full justify-between px-8 pt-4 pb-12 min-h-[300px]">
<div
className="flex flex-col gap-10"
style={{ height: isExpanded ? 'auto' : `${CHART_HEIGHT}px` }}
>
{chartData.map((item, index) => (
<div key={`${column}-${index}-${item.label}`} className="flex flex-col">
{/* Text above the bar */}
<div
className={`mb-2 flex flex-row justify-between text-sm font-mono uppercase tracking-widest tabular-nums transition-colors duration-300 ${
item.value === maxValue ? accentBarText : 'text-foreground'
}`}
>
<span>{item.label}</span>
<span>{item.value < 1 ? '<1%' : `${item.value}%`}</span>
</div>
{/* Progress bar */}
<div
className="h-4 relative overflow-hidden"
style={
{
'--bar-value': item.value,
'--reference': maxValue,
} as React.CSSProperties
}
>
<div
className="absolute inset-0 pointer-events-none bg-foreground-muted/60"
style={{
maskImage: 'url("/images/state-of-startups/pattern-stipple.svg")',
maskSize: '4px',
maskRepeat: 'repeat',
maskPosition: 'center',
}}
/>
<div
className="h-full relative bg-surface-100"
style={{ width: `calc(max(0.5%, (var(--bar-value) / 100) * 100%))` }}
>
<div
className={`absolute inset-0 pointer-events-none ${item.value === maxValue ? accentBarFg : 'bg-foreground-light'}`}
style={{
maskImage: 'url("/images/state-of-startups/pattern-checker.svg")',
maskSize: '4px',
maskRepeat: 'repeat',
maskPosition: 'top left',
}}
/>
</div>
</div>
</div>
))}
</div>
</div>
) : (
<div className="px-8 pt-4 pb-8">
<CodeBlock lang="sql">{sql}</CodeBlock>
</div>
)}
{view === 'chart' && !isExpanded && chartData.length > 3 && (
<div className="absolute bottom-0 left-0 right-0 flex items-center justify-center py-4 bg-linear-to-b from-transparent to-background">
<Button size="tiny" onClick={() => setIsExpanded(true)} className="shadow-xs">
Show more
</Button>
</div>
)}
</motion.div>
</SurveyChartShell>
)
}