Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions developer-portal/app/charts/RequestTrendChart.tsx
Original file line number Diff line number Diff line change
@@ -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<string, number>;
}

export const RequestTrendChart: React.FC<RequestTrendChartProps> = ({ 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 (
<View style={styles.chart}>
{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 (
<View key={date} style={styles.barContainer}>
<View style={styles.barWrapper}>
<View style={[styles.bar, { height, width: barWidth }]} />
</View>
<Text style={styles.barLabel}>{dayLabel}</Text>
<Text style={styles.barValue}>{value}</Text>
</View>
);
})}
</View>
);
};

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,
},
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';

interface UsageChartProps {
export interface UsageChartProps {
data: Record<string, number>;
period: string;
onPeriodChange: (period: string) => void;
Expand Down
90 changes: 90 additions & 0 deletions developer-portal/components/LazyChart.tsx
Original file line number Diff line number Diff line change
@@ -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<ChartBoundaryProps, { failed: boolean }> {
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 (
<View accessibilityRole="alert" style={styles.fallback}>
<Text style={styles.errorText}>{this.props.label} unavailable</Text>
</View>
);
}
return this.props.children;
}
}

const ChartFallback: React.FC<{ label: string }> = ({ label }) => (
<View style={styles.fallback} accessibilityRole="progressbar" accessibilityLabel={label}>
<ActivityIndicator size="small" color="#007AFF" />
<Text style={styles.fallbackText}>{label}</Text>
</View>
);

export const LazyUsageChart: React.FC<UsageChartProps> = (props) => (
<ChartBoundary label="Usage chart">
<Suspense fallback={<ChartFallback label="Loading usage chart" />}>
<UsageChart {...props} />
</Suspense>
</ChartBoundary>
);

export const LazyRequestTrendChart: React.FC<RequestTrendChartProps> = (props) => (
<ChartBoundary label="Request trend chart">
<Suspense fallback={<ChartFallback label="Loading request trend chart" />}>
<RequestTrendChart {...props} />
</Suspense>
</ChartBoundary>
);

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',
},
});
4 changes: 2 additions & 2 deletions developer-portal/src/screens/DeveloperPortalScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -195,7 +195,7 @@ const DeveloperPortalScreen: React.FC<{ navigation: any }> = ({ navigation }) =>
{usageStats && (
<View style={styles.section}>
<Text style={styles.sectionTitle}>API Usage Trends</Text>
<UsageChart
<LazyUsageChart
data={usageStats.requestsByDay}
period="30d"
onPeriodChange={(period) => console.log('Period changed:', period)}
Expand Down
62 changes: 2 additions & 60 deletions developer-portal/src/screens/UsageAnalyticsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -108,7 +108,7 @@ const UsageAnalyticsScreen: React.FC = () => {
<View style={styles.section}>
<Text style={styles.sectionTitle}>Request Trend</Text>
<View style={styles.chartCard}>
<RequestTrendChart data={usageStats.requestsByDay} />
<LazyRequestTrendChart data={usageStats.requestsByDay} />
</View>
</View>
)}
Expand Down Expand Up @@ -227,33 +227,6 @@ const UsageAnalyticsScreen: React.FC = () => {
);
};

const RequestTrendChart: React.FC<{ data: Record<string, number> }> = ({ 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 (
<View style={styles.chart}>
{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 (
<View key={date} style={styles.barContainer}>
<View style={styles.barWrapper}>
<View style={[styles.bar, { height, width: barWidth }]} />
</View>
<Text style={styles.barLabel}>{dayLabel}</Text>
<Text style={styles.barValue}>{value}</Text>
</View>
);
})}
</View>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
Expand Down Expand Up @@ -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,
Expand Down
Loading