Skip to content
22 changes: 22 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,27 @@ export function EventEmitterProvider({
});
activeOperations.current.delete(toastId);
}

// Perform mutations on affected resources
if (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)
// e.g. /1.0/instances/foo -> /1.0/instances
// 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