Fix ActivityFeed fetch TypeError: add proper error handling and type safety
This commit is contained in:
@@ -77,42 +77,85 @@ export default function ActivityFeed() {
|
|||||||
|
|
||||||
// Fetch data every 30 seconds (optimized to match server cache)
|
// Fetch data every 30 seconds (optimized to match server cache)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Don't fetch if tracking is disabled
|
// Don't fetch if tracking is disabled or during SSR
|
||||||
if (!isTrackingEnabled) {
|
if (!isTrackingEnabled || typeof window === 'undefined') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
try {
|
try {
|
||||||
|
// Check if fetch is available (should be, but safety check)
|
||||||
|
if (typeof fetch === 'undefined') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Add timestamp to prevent aggressive caching but respect server cache
|
// Add timestamp to prevent aggressive caching but respect server cache
|
||||||
const res = await fetch("/api/n8n/status", {
|
const res = await fetch("/api/n8n/status", {
|
||||||
cache: "default",
|
cache: "default",
|
||||||
|
}).catch((fetchError) => {
|
||||||
|
// Handle network errors gracefully
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
console.warn('ActivityFeed: Fetch failed:', fetchError);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
});
|
});
|
||||||
if (!res.ok) return;
|
|
||||||
let json = await res.json();
|
|
||||||
|
|
||||||
|
if (!res || !res.ok) {
|
||||||
|
if (process.env.NODE_ENV === 'development' && res) {
|
||||||
|
console.warn('ActivityFeed: API returned non-OK status:', res.status);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let json: unknown;
|
||||||
|
try {
|
||||||
|
json = await res.json();
|
||||||
|
} catch (parseError) {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
console.warn('ActivityFeed: Failed to parse JSON response:', parseError);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
console.log("ActivityFeed data (raw):", json);
|
console.log("ActivityFeed data (raw):", json);
|
||||||
|
}
|
||||||
|
|
||||||
// Handle array response if API returns it wrapped
|
// Handle array response if API returns it wrapped
|
||||||
if (Array.isArray(json)) {
|
if (Array.isArray(json)) {
|
||||||
json = json[0] || null;
|
json = json[0] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
console.log("ActivityFeed data (processed):", json);
|
console.log("ActivityFeed data (processed):", json);
|
||||||
|
}
|
||||||
|
|
||||||
setData(json);
|
if (!json || typeof json !== 'object') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Type assertion - API should return StatusData format
|
||||||
|
const activityData = json as StatusData;
|
||||||
|
setData(activityData);
|
||||||
|
|
||||||
// Check if there's any active activity
|
// Check if there's any active activity
|
||||||
const hasActiveActivity =
|
const coding = activityData.coding;
|
||||||
json.coding?.isActive ||
|
const gaming = activityData.gaming;
|
||||||
json.gaming?.isPlaying ||
|
const music = activityData.music;
|
||||||
json.music?.isPlaying;
|
|
||||||
|
|
||||||
|
const hasActiveActivity = Boolean(
|
||||||
|
coding?.isActive ||
|
||||||
|
gaming?.isPlaying ||
|
||||||
|
music?.isPlaying
|
||||||
|
);
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
console.log("Has activity:", hasActiveActivity, {
|
console.log("Has activity:", hasActiveActivity, {
|
||||||
coding: json.coding?.isActive,
|
coding: coding?.isActive,
|
||||||
gaming: json.gaming?.isPlaying,
|
gaming: gaming?.isPlaying,
|
||||||
music: json.music?.isPlaying,
|
music: music?.isPlaying,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
setHasActivity(hasActiveActivity);
|
setHasActivity(hasActiveActivity);
|
||||||
|
|
||||||
@@ -120,8 +163,12 @@ export default function ActivityFeed() {
|
|||||||
if (hasActiveActivity && !isMinimized) {
|
if (hasActiveActivity && !isMinimized) {
|
||||||
setIsExpanded(true);
|
setIsExpanded(true);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error("Failed to fetch activity", e);
|
// Silently fail - activity feed is not critical
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
console.error("Failed to fetch activity:", error);
|
||||||
|
}
|
||||||
|
// Don't set error state - just fail silently
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user