mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-08 09:54:26 +00:00
fix: harden telemetry parsing and null node fallbacks
This commit is contained in:
@@ -48,6 +48,16 @@ export function NodeProvider({ children }: { children: React.ReactNode }) {
|
||||
const updatedActive = data.find((n: Node) => n.id === activeNode.id);
|
||||
if (updatedActive) {
|
||||
setActiveNodeState(updatedActive);
|
||||
} else {
|
||||
// The active node was deleted! Fallback to default
|
||||
const defaultNode = data.find((n: Node) => n.is_default);
|
||||
if (defaultNode) {
|
||||
setActiveNodeState(defaultNode);
|
||||
} else if (data.length > 0) {
|
||||
setActiveNodeState(data[0]);
|
||||
} else {
|
||||
setActiveNodeState(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,7 +75,15 @@ export function NodeProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
useEffect(() => {
|
||||
refreshNodes();
|
||||
}, []);
|
||||
|
||||
const handleNodeNotFound = () => {
|
||||
console.warn('[NodeContext] Active node is unreachable or deleted. Forcing sync...');
|
||||
refreshNodes();
|
||||
};
|
||||
|
||||
window.addEventListener('node-not-found', handleNodeNotFound);
|
||||
return () => window.removeEventListener('node-not-found', handleNodeNotFound);
|
||||
}, [refreshNodes]);
|
||||
|
||||
return (
|
||||
<NodeContext.Provider value={{ nodes, activeNode, setActiveNode, refreshNodes, isLoading }}>
|
||||
|
||||
@@ -24,6 +24,19 @@ export async function apiFetch(
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
|
||||
// Intercept 404 Node Not Found responses and force context refresh
|
||||
if (response.status === 404) {
|
||||
try {
|
||||
const clone = response.clone();
|
||||
const errData = await clone.json();
|
||||
if (errData.error && errData.error.includes('not found') && errData.error.includes('Node')) {
|
||||
window.dispatchEvent(new Event('node-not-found'));
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore JSON parse errors, caller handles standard 404s
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user