|
| 1 | +'use client' |
| 2 | + |
| 3 | +/** |
| 4 | + * MVP Proof-of-Concept: Adaptive Tier Rendering |
| 5 | + * |
| 6 | + * This page demonstrates bandwidth-adaptive tier selection. |
| 7 | + * UIProvider with `adaptive` prop auto-detects network conditions |
| 8 | + * and adjusts motion level (and eventually component weight tier). |
| 9 | + * |
| 10 | + * Test by: |
| 11 | + * 1. Open Chrome DevTools → Network → throttle to "Slow 3G" |
| 12 | + * 2. Reload page → should detect lite tier (motion 0) |
| 13 | + * 3. Remove throttle → reload → should detect premium (motion 3) |
| 14 | + */ |
| 15 | + |
| 16 | +import { useState } from 'react' |
| 17 | +import { UIProvider } from '@ui/components/ui-provider' |
| 18 | +import { useAdaptiveContext } from '@ui/core/adaptive/adaptive-context' |
| 19 | +// detectAdaptiveTier available for manual testing |
| 20 | +import { Button } from '@ui/components/button' |
| 21 | +import { Card } from '@ui/components/card' |
| 22 | +import { Badge } from '@ui/components/badge' |
| 23 | +import { MetricCard } from '@ui/domain/metric-card' |
| 24 | +import { PageShell } from '@ui/components/page-shell' |
| 25 | +import { PageHeader } from '@ui/components/page-header' |
| 26 | +import { StatsGrid } from '@ui/components/stats-grid' |
| 27 | +import { SectionHeader } from '@ui/components/section-header' |
| 28 | +import { CardGrid } from '@ui/components/card-grid' |
| 29 | +import { Accordion } from '@ui/components/accordion' |
| 30 | +import { Progress } from '@ui/components/progress' |
| 31 | +import { Tabs, TabPanel } from '@ui/components/tabs' |
| 32 | +import { css } from '@ui/core/styles/css-tag' |
| 33 | +import { useStyles } from '@ui/core/styles/use-styles' |
| 34 | + |
| 35 | +const styles = css` |
| 36 | + @layer demo { |
| 37 | + .adaptive-demo__info { |
| 38 | + padding: 1.25rem; |
| 39 | + border-radius: var(--radius-lg); |
| 40 | + background: var(--bg-surface); |
| 41 | + border: 1px solid var(--border-default); |
| 42 | + font-family: 'SF Mono', 'Fira Code', monospace; |
| 43 | + font-size: 0.8125rem; |
| 44 | + line-height: 1.6; |
| 45 | + } |
| 46 | +
|
| 47 | + .adaptive-demo__info-row { |
| 48 | + display: flex; |
| 49 | + justify-content: space-between; |
| 50 | + padding: 0.25rem 0; |
| 51 | + } |
| 52 | +
|
| 53 | + .adaptive-demo__info-label { |
| 54 | + color: var(--text-secondary); |
| 55 | + } |
| 56 | +
|
| 57 | + .adaptive-demo__info-value { |
| 58 | + color: var(--text-primary); |
| 59 | + font-weight: 600; |
| 60 | + } |
| 61 | +
|
| 62 | + .adaptive-demo__tier-badge { |
| 63 | + display: inline-flex; |
| 64 | + align-items: center; |
| 65 | + gap: 0.375rem; |
| 66 | + padding: 0.25rem 0.75rem; |
| 67 | + border-radius: var(--radius-full, 9999px); |
| 68 | + font-size: 0.75rem; |
| 69 | + font-weight: 700; |
| 70 | + text-transform: uppercase; |
| 71 | + letter-spacing: 0.05em; |
| 72 | + } |
| 73 | +
|
| 74 | + .adaptive-demo__tier-badge[data-tier="lite"] { |
| 75 | + background: oklch(40% 0.1 150 / 0.2); |
| 76 | + color: oklch(70% 0.15 150); |
| 77 | + } |
| 78 | +
|
| 79 | + .adaptive-demo__tier-badge[data-tier="standard"] { |
| 80 | + background: oklch(40% 0.1 220 / 0.2); |
| 81 | + color: oklch(70% 0.15 220); |
| 82 | + } |
| 83 | +
|
| 84 | + .adaptive-demo__tier-badge[data-tier="premium"] { |
| 85 | + background: oklch(40% 0.15 280 / 0.2); |
| 86 | + color: oklch(70% 0.2 280); |
| 87 | + } |
| 88 | +
|
| 89 | + .adaptive-demo__controls { |
| 90 | + display: flex; |
| 91 | + gap: 0.5rem; |
| 92 | + flex-wrap: wrap; |
| 93 | + } |
| 94 | + } |
| 95 | +` |
| 96 | + |
| 97 | +function AdaptiveInfoPanel() { |
| 98 | + const adaptive = useAdaptiveContext() |
| 99 | + |
| 100 | + return ( |
| 101 | + <div className="adaptive-demo__info"> |
| 102 | + <div className="adaptive-demo__info-row"> |
| 103 | + <span className="adaptive-demo__info-label">Detected Tier</span> |
| 104 | + <span className="adaptive-demo__tier-badge" data-tier={adaptive.tier}> |
| 105 | + {adaptive.tier === 'premium' ? '✨' : adaptive.tier === 'standard' ? '⚡' : '🪶'} |
| 106 | + {adaptive.tier} |
| 107 | + </span> |
| 108 | + </div> |
| 109 | + <div className="adaptive-demo__info-row"> |
| 110 | + <span className="adaptive-demo__info-label">Motion Level</span> |
| 111 | + <span className="adaptive-demo__info-value">{adaptive.motion}</span> |
| 112 | + </div> |
| 113 | + <div className="adaptive-demo__info-row"> |
| 114 | + <span className="adaptive-demo__info-label">Confidence</span> |
| 115 | + <span className="adaptive-demo__info-value">{adaptive.confidence}</span> |
| 116 | + </div> |
| 117 | + <div className="adaptive-demo__info-row"> |
| 118 | + <span className="adaptive-demo__info-label">Reason</span> |
| 119 | + <span className="adaptive-demo__info-value">{adaptive.reason}</span> |
| 120 | + </div> |
| 121 | + <div className="adaptive-demo__info-row"> |
| 122 | + <span className="adaptive-demo__info-label">Adaptive Active</span> |
| 123 | + <span className="adaptive-demo__info-value">{adaptive.isAdaptive ? 'Yes' : 'No'}</span> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ) |
| 127 | +} |
| 128 | + |
| 129 | +function DemoContent() { |
| 130 | + const adaptive = useAdaptiveContext() |
| 131 | + |
| 132 | + return ( |
| 133 | + <PageShell padding="lg" maxWidth="xl"> |
| 134 | + <PageHeader |
| 135 | + title="Adaptive Tier Demo" |
| 136 | + description={`Currently rendering at "${adaptive.tier}" tier with motion level ${adaptive.motion}. Throttle your network in DevTools and reload to see the tier change.`} |
| 137 | + actions={ |
| 138 | + <Badge color={adaptive.tier === 'premium' ? 'brand' : adaptive.tier === 'standard' ? 'info' : 'neutral'} size="lg"> |
| 139 | + {adaptive.tier.toUpperCase()} TIER |
| 140 | + </Badge> |
| 141 | + } |
| 142 | + /> |
| 143 | + |
| 144 | + <SectionHeader title="Detection Result" /> |
| 145 | + <AdaptiveInfoPanel /> |
| 146 | + |
| 147 | + <SectionHeader title="Components at Current Tier" /> |
| 148 | + <StatsGrid columns={4}> |
| 149 | + <MetricCard title="Users" value="1,284" trend="up" status="ok" /> |
| 150 | + <MetricCard title="Active" value="42" status="ok" /> |
| 151 | + <MetricCard title="Errors" value="3" status="critical" /> |
| 152 | + <MetricCard title="Uptime" value="99.9%" status="ok" /> |
| 153 | + </StatsGrid> |
| 154 | + |
| 155 | + <SectionHeader title="Interactive Elements" /> |
| 156 | + <CardGrid columns={2}> |
| 157 | + <Card padding="md"> |
| 158 | + <h3 style={{ margin: '0 0 1rem' }}>Buttons</h3> |
| 159 | + <div style={{ display: 'flex', gap: '0.5rem', flexWrap: 'wrap' }}> |
| 160 | + <Button variant="primary">Primary</Button> |
| 161 | + <Button variant="secondary">Secondary</Button> |
| 162 | + <Button variant="ghost">Ghost</Button> |
| 163 | + <Button variant="danger">Danger</Button> |
| 164 | + </div> |
| 165 | + </Card> |
| 166 | + <Card padding="md"> |
| 167 | + <h3 style={{ margin: '0 0 1rem' }}>Progress</h3> |
| 168 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '0.75rem' }}> |
| 169 | + <Progress value={75} size="sm" /> |
| 170 | + <Progress value={45} size="md" color="warning" /> |
| 171 | + <Progress value={90} size="lg" color="success" /> |
| 172 | + </div> |
| 173 | + </Card> |
| 174 | + </CardGrid> |
| 175 | + |
| 176 | + <SectionHeader title="Accordion" /> |
| 177 | + <Card padding="md"> |
| 178 | + <Accordion items={[ |
| 179 | + { id: 'what', trigger: 'What is adaptive tier?', content: 'Adaptive tier automatically adjusts the visual richness of components based on your network bandwidth. Fast connections get premium animations and effects. Slow connections get lightweight, instant-loading components.' }, |
| 180 | + { id: 'how', trigger: 'How does detection work?', content: 'The system uses the Navigator.connection API (Network Information API) to check effectiveType and downlink speed. It falls back to Performance API timing measurements. Detection happens in <50ms on page load.' }, |
| 181 | + { id: 'layout', trigger: 'Does it affect layout?', content: 'No. All tiers share the same HTML structure and box model. Only visual enhancements (animations, glows, shadows) change. The layout is identical across tiers — zero layout shift.' }, |
| 182 | + ]} /> |
| 183 | + </Card> |
| 184 | + |
| 185 | + <SectionHeader title="Tabbed Content" /> |
| 186 | + <Tabs defaultValue="react"> |
| 187 | + <TabPanel tabId="react">React component code would go here</TabPanel> |
| 188 | + <TabPanel tabId="vue">Vue component code would go here</TabPanel> |
| 189 | + <TabPanel tabId="angular">Angular component code would go here</TabPanel> |
| 190 | + </Tabs> |
| 191 | + |
| 192 | + <SectionHeader title="How to Test" /> |
| 193 | + <Card padding="md"> |
| 194 | + <ol style={{ margin: 0, paddingInlineStart: '1.5rem', display: 'flex', flexDirection: 'column', gap: '0.5rem' }}> |
| 195 | + <li>Open Chrome DevTools (F12)</li> |
| 196 | + <li>Go to Network tab</li> |
| 197 | + <li>Click the throttle dropdown (usually says "No throttling")</li> |
| 198 | + <li>Select <strong>"Slow 3G"</strong></li> |
| 199 | + <li>Reload this page — tier should switch to <strong>lite</strong> (motion 0)</li> |
| 200 | + <li>Remove throttle, reload — should be <strong>premium</strong> (motion 3)</li> |
| 201 | + <li>Select <strong>"Fast 3G"</strong> — should be <strong>standard</strong> (motion 1-2)</li> |
| 202 | + </ol> |
| 203 | + </Card> |
| 204 | + </PageShell> |
| 205 | + ) |
| 206 | +} |
| 207 | + |
| 208 | +export default function AdaptiveTierDemoPage() { |
| 209 | + useStyles('adaptive-demo', styles) |
| 210 | + const [key, setKey] = useState(0) |
| 211 | + |
| 212 | + // Allow manual re-detection |
| 213 | + const redetect = () => setKey(k => k + 1) |
| 214 | + |
| 215 | + return ( |
| 216 | + <div> |
| 217 | + <UIProvider adaptive key={key}> |
| 218 | + <DemoContent /> |
| 219 | + <div style={{ padding: '1.5rem', textAlign: 'center' }}> |
| 220 | + <Button variant="secondary" onClick={redetect}> |
| 221 | + Re-detect Bandwidth |
| 222 | + </Button> |
| 223 | + </div> |
| 224 | + </UIProvider> |
| 225 | + </div> |
| 226 | + ) |
| 227 | +} |
0 commit comments