Scope project refreshes and harden deploy script
This commit is contained in:
@@ -253,31 +253,60 @@ export function NativeAppBridge() {
|
||||
|
||||
export function RealtimeRefresh({
|
||||
events,
|
||||
projectId,
|
||||
}: {
|
||||
events: BossEventName[];
|
||||
projectId?: string;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
const source = new EventSource("/api/v1/events");
|
||||
const refresh = () => router.refresh();
|
||||
const listeners = [
|
||||
const listeners = Array.from(new Set([
|
||||
"conversation.context_indicator.updated",
|
||||
"project.context_risk.updated",
|
||||
...events,
|
||||
];
|
||||
]));
|
||||
const shouldRefresh = (event: Event) => {
|
||||
if (!projectId || !("data" in event) || typeof event.data !== "string" || !event.data.trim()) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
const payload = JSON.parse(event.data) as { projectId?: string };
|
||||
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
||||
return true;
|
||||
}
|
||||
if (typeof payload.projectId !== "string" || !payload.projectId.trim()) {
|
||||
return true;
|
||||
}
|
||||
return payload.projectId === projectId;
|
||||
} catch {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
const listenerMap = new Map<string, (event: Event) => void>();
|
||||
|
||||
for (const event of listeners) {
|
||||
const refresh = (nextEvent: Event) => {
|
||||
if (!shouldRefresh(nextEvent)) {
|
||||
return;
|
||||
}
|
||||
router.refresh();
|
||||
};
|
||||
listenerMap.set(event, refresh);
|
||||
source.addEventListener(event, refresh);
|
||||
}
|
||||
|
||||
return () => {
|
||||
for (const event of listeners) {
|
||||
source.removeEventListener(event, refresh);
|
||||
const refresh = listenerMap.get(event);
|
||||
if (refresh) {
|
||||
source.removeEventListener(event, refresh);
|
||||
}
|
||||
}
|
||||
source.close();
|
||||
};
|
||||
}, [events, router]);
|
||||
}, [events, projectId, router]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user