From ccd49df7f9308392cf8a7fb21f277d59f9671eef Mon Sep 17 00:00:00 2001 From: Ivanli Trading Date: Tue, 24 Nov 2026 01:56:09 -0300 Subject: [PATCH] perf(portal): lazy-load heavyweight charts behind Suspense boundaries --- .../app/charts/RequestTrendChart.tsx | 73 +++++++++++++++ .../components => app/charts}/UsageChart.tsx | 2 +- developer-portal/components/LazyChart.tsx | 90 +++++++++++++++++++ .../src/screens/DeveloperPortalScreen.tsx | 4 +- .../src/screens/UsageAnalyticsScreen.tsx | 62 +------------ 5 files changed, 168 insertions(+), 63 deletions(-) create mode 100644 developer-portal/app/charts/RequestTrendChart.tsx rename developer-portal/{src/components => app/charts}/UsageChart.tsx (98%) create mode 100644 developer-portal/components/LazyChart.tsx diff --git a/developer-portal/app/charts/RequestTrendChart.tsx b/developer-portal/app/charts/RequestTrendChart.tsx new file mode 100644 index 00000000..02261d0d --- /dev/null +++ b/developer-portal/app/charts/RequestTrendChart.tsx @@ -0,0 +1,73 @@ +/** + * Developer portal request-trend chart. + * + * Kept in its own module so it is only fetched when the analytics screen + * actually renders it (see components/LazyChart.tsx, issue #2271). + */ +import React from 'react'; +import { View, Text, StyleSheet, Dimensions } from 'react-native'; + +export interface RequestTrendChartProps { + data: Record; +} + +export const RequestTrendChart: React.FC = ({ data }) => { + const chartData = Object.entries(data).slice(-7); + const maxValue = Math.max(...chartData.map(([, value]) => value), 1); + const chartWidth = Dimensions.get('window').width - 64; + const barWidth = chartWidth / chartData.length - 8; + + return ( + + {chartData.map(([date, value]) => { + const height = (value / maxValue) * 120; + const dateObj = new Date(date); + const dayLabel = dateObj.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); + + return ( + + + + + {dayLabel} + {value} + + ); + })} + + ); +}; + +const styles = StyleSheet.create({ + chart: { + flexDirection: 'row', + alignItems: 'flex-end', + justifyContent: 'space-between', + height: 160, + }, + barContainer: { + alignItems: 'center', + flex: 1, + }, + barWrapper: { + height: 120, + justifyContent: 'flex-end', + alignItems: 'center', + }, + bar: { + backgroundColor: '#007AFF', + borderRadius: 4, + minHeight: 4, + }, + barLabel: { + fontSize: 10, + color: '#666', + marginTop: 4, + }, + barValue: { + fontSize: 10, + color: '#000', + fontWeight: '600', + marginTop: 2, + }, +}); diff --git a/developer-portal/src/components/UsageChart.tsx b/developer-portal/app/charts/UsageChart.tsx similarity index 98% rename from developer-portal/src/components/UsageChart.tsx rename to developer-portal/app/charts/UsageChart.tsx index b8a8a6ff..5a8637f1 100644 --- a/developer-portal/src/components/UsageChart.tsx +++ b/developer-portal/app/charts/UsageChart.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { View, Text, StyleSheet, Dimensions } from 'react-native'; -interface UsageChartProps { +export interface UsageChartProps { data: Record; period: string; onPeriodChange: (period: string) => void; diff --git a/developer-portal/components/LazyChart.tsx b/developer-portal/components/LazyChart.tsx new file mode 100644 index 00000000..b29ed8f1 --- /dev/null +++ b/developer-portal/components/LazyChart.tsx @@ -0,0 +1,90 @@ +/** + * Lazy Suspense boundaries for the developer portal charts (issue #2271). + * + * Charts are split out of the initial portal bundle and only requested when + * the surrounding screen renders them. A failed chunk shows a small message + * instead of taking down the whole screen. + */ +import React, { Suspense, lazy } from 'react'; +import { ActivityIndicator, StyleSheet, Text, View } from 'react-native'; +import type { UsageChartProps } from '../app/charts/UsageChart'; +import type { RequestTrendChartProps } from '../app/charts/RequestTrendChart'; + +const UsageChart = lazy(() => + import('../app/charts/UsageChart').then((module) => ({ default: module.UsageChart })) +); + +const RequestTrendChart = lazy(() => + import('../app/charts/RequestTrendChart').then((module) => ({ + default: module.RequestTrendChart, + })) +); + +interface ChartBoundaryProps { + label: string; + children: React.ReactNode; +} + +class ChartBoundary extends React.Component { + public state: { failed: boolean } = { failed: false }; + + public static getDerivedStateFromError(): { failed: boolean } { + return { failed: true }; + } + + public componentDidCatch(error: unknown): void { + console.warn(`[developer-portal] failed to load ${this.props.label}`, error); + } + + public render(): React.ReactNode { + if (this.state.failed) { + return ( + + {this.props.label} unavailable + + ); + } + return this.props.children; + } +} + +const ChartFallback: React.FC<{ label: string }> = ({ label }) => ( + + + {label} + +); + +export const LazyUsageChart: React.FC = (props) => ( + + }> + + + +); + +export const LazyRequestTrendChart: React.FC = (props) => ( + + }> + + + +); + +const styles = StyleSheet.create({ + fallback: { + minHeight: 160, + justifyContent: 'center', + alignItems: 'center', + padding: 16, + }, + fallbackText: { + marginTop: 8, + fontSize: 12, + color: '#6B7280', + }, + errorText: { + fontSize: 12, + color: '#DC2626', + }, +}); diff --git a/developer-portal/src/screens/DeveloperPortalScreen.tsx b/developer-portal/src/screens/DeveloperPortalScreen.tsx index 8e4d54d5..8fb60456 100644 --- a/developer-portal/src/screens/DeveloperPortalScreen.tsx +++ b/developer-portal/src/screens/DeveloperPortalScreen.tsx @@ -14,7 +14,7 @@ import { ApiKeyPermission } from '../../../src/types/developerPortal'; import { DashboardCard } from '../components/DashboardCard'; import { QuickActionCard } from '../components/QuickActionCard'; import { OnboardingProgress } from '../components/OnboardingProgress'; -import { UsageChart } from '../components/UsageChart'; +import { LazyUsageChart } from '../../components/LazyChart'; import { RecentActivity } from '../components/RecentActivity'; const DeveloperPortalScreen: React.FC<{ navigation: any }> = ({ navigation }) => { @@ -195,7 +195,7 @@ const DeveloperPortalScreen: React.FC<{ navigation: any }> = ({ navigation }) => {usageStats && ( API Usage Trends - console.log('Period changed:', period)} diff --git a/developer-portal/src/screens/UsageAnalyticsScreen.tsx b/developer-portal/src/screens/UsageAnalyticsScreen.tsx index 7ec8f107..e1b0ca9d 100644 --- a/developer-portal/src/screens/UsageAnalyticsScreen.tsx +++ b/developer-portal/src/screens/UsageAnalyticsScreen.tsx @@ -6,8 +6,8 @@ import { ScrollView, SafeAreaView, TouchableOpacity, - Dimensions, } from 'react-native'; +import { LazyRequestTrendChart } from '../../components/LazyChart'; import { useDeveloperPortalStore } from '../../../src/store/developerPortalStore'; const PERIOD_OPTIONS = [ @@ -108,7 +108,7 @@ const UsageAnalyticsScreen: React.FC = () => { Request Trend - + )} @@ -227,33 +227,6 @@ const UsageAnalyticsScreen: React.FC = () => { ); }; -const RequestTrendChart: React.FC<{ data: Record }> = ({ data }) => { - const chartData = Object.entries(data).slice(-7); - const maxValue = Math.max(...chartData.map(([, value]) => value), 1); - const chartWidth = Dimensions.get('window').width - 64; - const barWidth = chartWidth / chartData.length - 8; - - return ( - - {chartData.map(([date, value]) => { - const height = (value / maxValue) * 120; - const dateObj = new Date(date); - const dayLabel = dateObj.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); - - return ( - - - - - {dayLabel} - {value} - - ); - })} - - ); -}; - const styles = StyleSheet.create({ container: { flex: 1, @@ -366,37 +339,6 @@ const styles = StyleSheet.create({ shadowRadius: 2, elevation: 2, }, - chart: { - flexDirection: 'row', - alignItems: 'flex-end', - justifyContent: 'space-between', - height: 160, - }, - barContainer: { - alignItems: 'center', - flex: 1, - }, - barWrapper: { - height: 120, - justifyContent: 'flex-end', - alignItems: 'center', - }, - bar: { - backgroundColor: '#007AFF', - borderRadius: 4, - minHeight: 4, - }, - barLabel: { - fontSize: 10, - color: '#666', - marginTop: 4, - }, - barValue: { - fontSize: 10, - color: '#000', - fontWeight: '600', - marginTop: 2, - }, endpointCard: { backgroundColor: '#FFF', padding: 16,