Skip to content
24 changes: 24 additions & 0 deletions app/_context/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React, { createContext, useEffect, useState, useRef } from 'react';
import { toast } from 'sonner';
import { mutate } from 'swr';

type EventType = 'operation' | 'logging' | 'lifecycle';

Expand Down Expand Up @@ -151,6 +152,29 @@ export function EventEmitterProvider({
});
activeOperations.current.delete(toastId);
}

// Perform mutations on affected resources only for terminal states
if (['Success', 'Failure', 'Cancelled'].includes(op.status) && op.resources) {
const uniqueResources = new Set<string>();
Object.values(op.resources).forEach((resourceList) => {
resourceList.forEach((resource) => {
uniqueResources.add(resource);

// Also mutate the parent collection if applicable (simple heuristic).
// The threshold `parts.length > 3` assumes paths follow the pattern `/version/collection/resource`
// (e.g., `/1.0/instances/foo`), where the first part is empty due to the leading slash.
// Thus, length > 3 means there is a resource within a collection, and we can derive the parent collection path.
// Normalize path to handle trailing slashes and query params
const normalizedResource = resource.replace(/\/$/, '').split('?')[0];
const parts = normalizedResource.split('/');
if (parts.length > 3) {
const collection = parts.slice(0, parts.length - 1).join('/');
uniqueResources.add(collection);
}
});
});
uniqueResources.forEach((path) => mutate(path));
}
};

connect();
Expand Down
Loading