fix(ui): prevent render crashes on malformed backend arrays (#384)

This commit is contained in:
paisley
2026-03-10 14:43:19 +08:00
committed by GitHub
Unverified
parent 1bae8229af
commit 19b5b2d540
4 changed files with 143 additions and 138 deletions

View File

@@ -111,18 +111,20 @@ export function Channels() {
); );
} }
const safeChannels = Array.isArray(channels) ? channels : [];
return ( return (
<div className="flex flex-col -m-6 dark:bg-background h-[calc(100vh-2.5rem)] overflow-hidden"> <div className="flex flex-col -m-6 dark:bg-background h-[calc(100vh-2.5rem)] overflow-hidden">
<div className="w-full max-w-5xl mx-auto flex flex-col h-full p-10 pt-16"> <div className="w-full max-w-5xl mx-auto flex flex-col h-full p-10 pt-16">
{/* Header */} {/* Header */}
<div className="flex flex-col md:flex-row md:items-start justify-between mb-12 shrink-0 gap-4"> <div className="flex flex-col md:flex-row md:items-start justify-between mb-8 shrink-0 gap-4">
<div> <div>
<h1 className="text-5xl md:text-6xl font-serif text-foreground mb-3 font-normal tracking-tight" style={{ fontFamily: 'Georgia, Cambria, "Times New Roman", Times, serif' }}> <h1 className="text-5xl md:text-6xl font-serif text-foreground mb-3 font-normal tracking-tight" style={{ fontFamily: 'Georgia, Cambria, "Times New Roman", Times, serif' }}>
{t('title', 'Messaging Channels')} {t('title') || 'Channels'}
</h1> </h1>
<p className="text-[17px] text-foreground/80 font-medium"> <p className="text-[17px] text-foreground/80 font-medium">
{t('subtitle', 'Manage your messaging channels and connections')} {t('subtitle') || 'Connect to messaging platforms.'}
</p> </p>
</div> </div>
@@ -133,15 +135,16 @@ export function Channels() {
void fetchChannels(); void fetchChannels();
void fetchConfiguredTypes(); void fetchConfiguredTypes();
}} }}
disabled={gatewayStatus.state !== 'running'}
className="h-9 text-[13px] font-medium rounded-full px-4 border-black/10 dark:border-white/10 bg-transparent hover:bg-black/5 dark:hover:bg-white/5 shadow-none text-foreground/80 hover:text-foreground transition-colors" className="h-9 text-[13px] font-medium rounded-full px-4 border-black/10 dark:border-white/10 bg-transparent hover:bg-black/5 dark:hover:bg-white/5 shadow-none text-foreground/80 hover:text-foreground transition-colors"
> >
<RefreshCw className="h-3.5 w-3.5 mr-2" /> <RefreshCw className={cn("h-3.5 w-3.5 mr-2", loading && "animate-spin")} />
{t('refresh')} {t('refresh')}
</Button> </Button>
</div> </div>
</div> </div>
{/* Content Area */} {/* Content Area */}
<div className="flex-1 overflow-y-auto pr-2 pb-10 min-h-0 -mr-2"> <div className="flex-1 overflow-y-auto min-h-0 pr-2 -mr-2 space-y-8 pb-10">
{/* Gateway Warning */} {/* Gateway Warning */}
{gatewayStatus.state !== 'running' && ( {gatewayStatus.state !== 'running' && (
@@ -164,13 +167,13 @@ export function Channels() {
)} )}
{/* Available Channels (Configured) */} {/* Available Channels (Configured) */}
{channels.length > 0 && ( {safeChannels.length > 0 && (
<div className="mb-12"> <div className="mb-12">
<h2 className="text-3xl font-serif text-foreground mb-6 font-normal tracking-tight" style={{ fontFamily: 'Georgia, Cambria, "Times New Roman", Times, serif' }}> <h2 className="text-3xl font-serif text-foreground mb-6 font-normal tracking-tight" style={{ fontFamily: 'Georgia, Cambria, "Times New Roman", Times, serif' }}>
Available Channels Available Channels
</h2> </h2>
<div className="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-4"> <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{channels.map((channel) => ( {safeChannels.map((channel) => (
<ChannelCard <ChannelCard
key={channel.id} key={channel.id}
channel={channel} channel={channel}
@@ -190,7 +193,7 @@ export function Channels() {
<div className="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-4"> <div className="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-4">
{displayedChannelTypes.map((type) => { {displayedChannelTypes.map((type) => {
const meta = CHANNEL_META[type]; const meta = CHANNEL_META[type];
const isConfigured = channels.some(c => c.type === type) || configuredTypes.includes(type); const isConfigured = safeChannels.some(c => c.type === type) || configuredTypes.includes(type);
// Hide already configured channels from "Supported Channels" section // Hide already configured channels from "Supported Channels" section
if (isConfigured) return null; if (isConfigured) return null;

View File

@@ -516,9 +516,10 @@ export function Cron() {
}, [fetchJobs, isGatewayRunning]); }, [fetchJobs, isGatewayRunning]);
// Statistics // Statistics
const activeJobs = jobs.filter((j) => j.enabled); const safeJobs = Array.isArray(jobs) ? jobs : [];
const pausedJobs = jobs.filter((j) => !j.enabled); const activeJobs = safeJobs.filter((j) => j.enabled);
const failedJobs = jobs.filter((j) => j.lastRun && !j.lastRun.success); const pausedJobs = safeJobs.filter((j) => !j.enabled);
const failedJobs = safeJobs.filter((j) => j.lastRun && !j.lastRun.success);
const handleSave = useCallback(async (input: CronJobCreateInput) => { const handleSave = useCallback(async (input: CronJobCreateInput) => {
if (editingJob) { if (editingJob) {
@@ -615,7 +616,7 @@ export function Cron() {
</div> </div>
</div> </div>
<div className="mt-4 flex items-baseline gap-3"> <div className="mt-4 flex items-baseline gap-3">
<p className="text-[40px] leading-none font-serif text-foreground">{jobs.length}</p> <p className="text-[40px] leading-none font-serif text-foreground">{safeJobs.length}</p>
<p className="text-[14px] font-medium text-muted-foreground">{t('stats.total')}</p> <p className="text-[14px] font-medium text-muted-foreground">{t('stats.total')}</p>
</div> </div>
</div> </div>
@@ -658,7 +659,7 @@ export function Cron() {
</div> </div>
{/* Jobs List */} {/* Jobs List */}
{jobs.length === 0 ? ( {safeJobs.length === 0 ? (
<div className="flex flex-col items-center justify-center py-20 text-muted-foreground bg-black/5 dark:bg-white/5 rounded-3xl border border-transparent border-dashed"> <div className="flex flex-col items-center justify-center py-20 text-muted-foreground bg-black/5 dark:bg-white/5 rounded-3xl border border-transparent border-dashed">
<Clock className="h-10 w-10 mb-4 opacity-50" /> <Clock className="h-10 w-10 mb-4 opacity-50" />
<h3 className="text-lg font-medium mb-2 text-foreground">{t('empty.title')}</h3> <h3 className="text-lg font-medium mb-2 text-foreground">{t('empty.title')}</h3>
@@ -679,7 +680,7 @@ export function Cron() {
</div> </div>
) : ( ) : (
<div className="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-4"> <div className="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-4">
{jobs.map((job) => ( {safeJobs.map((job) => (
<CronJobCard <CronJobCard
key={job.id} key={job.id}
job={job} job={job}

View File

@@ -225,7 +225,7 @@ export function Dashboard() {
<CardTitle className="text-lg">{t('connectedChannels')}</CardTitle> <CardTitle className="text-lg">{t('connectedChannels')}</CardTitle>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
{channels.length === 0 ? ( {(!Array.isArray(channels) || channels.length === 0) ? (
<FeedbackState <FeedbackState
state="empty" state="empty"
title={t('noChannels')} title={t('noChannels')}
@@ -269,7 +269,7 @@ export function Dashboard() {
<CardTitle className="text-lg">{t('activeSkills')}</CardTitle> <CardTitle className="text-lg">{t('activeSkills')}</CardTitle>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
{skills.filter((s) => s.enabled).length === 0 ? ( {(!Array.isArray(skills) || skills.filter((s) => s.enabled).length === 0) ? (
<FeedbackState <FeedbackState
state="empty" state="empty"
title={t('noSkills')} title={t('noSkills')}

View File

@@ -380,7 +380,8 @@ export function Skills() {
}, [fetchSkills, isGatewayRunning]); }, [fetchSkills, isGatewayRunning]);
// Filter skills // Filter skills
const filteredSkills = skills.filter((skill) => { const safeSkills = Array.isArray(skills) ? skills : [];
const filteredSkills = safeSkills.filter((skill) => {
const matchesSearch = skill.name.toLowerCase().includes(searchQuery.toLowerCase()) || const matchesSearch = skill.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
skill.description.toLowerCase().includes(searchQuery.toLowerCase()); skill.description.toLowerCase().includes(searchQuery.toLowerCase());
@@ -404,9 +405,9 @@ export function Skills() {
}); });
const sourceStats = { const sourceStats = {
all: skills.length, all: safeSkills.length,
builtIn: skills.filter(s => s.isBundled).length, builtIn: safeSkills.filter(s => s.isBundled).length,
marketplace: skills.filter(s => !s.isBundled).length, marketplace: safeSkills.filter(s => !s.isBundled).length,
}; };
const bulkToggleVisible = useCallback(async (enable: boolean) => { const bulkToggleVisible = useCallback(async (enable: boolean) => {
@@ -453,7 +454,7 @@ export function Skills() {
} }
}, [enableSkill, disableSkill, t]); }, [enableSkill, disableSkill, t]);
const hasInstalledSkills = skills.some(s => !s.isBundled); const hasInstalledSkills = safeSkills.some(s => !s.isBundled);
const handleOpenSkillsFolder = useCallback(async () => { const handleOpenSkillsFolder = useCallback(async () => {
try { try {
@@ -748,7 +749,7 @@ export function Skills() {
{searchResults.length > 0 ? ( {searchResults.length > 0 ? (
searchResults.map((skill) => { searchResults.map((skill) => {
const isInstalled = skills.some(s => s.id === skill.slug || s.name === skill.name); const isInstalled = safeSkills.some(s => s.id === skill.slug || s.name === skill.name);
const isInstallLoading = !!installing[skill.slug]; const isInstallLoading = !!installing[skill.slug];
return ( return (