[Fix] UI - Policies: Fix unhandled promise rejections and isPending semantics

- Wrap mutateAsync calls in try/catch to swallow re-thrown errors
  (notifications already handled by onError in mutation hooks)
- Use isLoading instead of isPending for version loading state —
  isPending is true when query is disabled with no cache, isLoading
  is only true during active fetches (matches original behavior)
- Add isLoading assertions to disabled-state tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yuneng-jiang 2026-03-20 23:04:25 -07:00
parent 79aea5ddcf
commit 8aa1ebfb07
2 changed files with 29 additions and 13 deletions

View file

@ -96,6 +96,8 @@ describe("usePolicyVersions", () => {
);
expect(result.current.fetchStatus).toBe("idle");
// isLoading (not isPending) must be false when query is disabled
expect(result.current.isLoading).toBe(false);
expect(mockListPolicyVersions).not.toHaveBeenCalled();
});
@ -106,6 +108,8 @@ describe("usePolicyVersions", () => {
);
expect(result.current.fetchStatus).toBe("idle");
// isLoading (not isPending) must be false when query is disabled
expect(result.current.isLoading).toBe(false);
expect(mockListPolicyVersions).not.toHaveBeenCalled();
});

View file

@ -1305,7 +1305,7 @@ export const FlowBuilderPage: React.FC<FlowBuilderPageProps> = ({
const {
data: versionsData,
isPending: isVersionsLoading,
isLoading: isVersionsLoading,
} = usePolicyVersions({
policyName: editingPolicy?.policy_name,
enabled: showVersionsSidebar,
@ -1316,8 +1316,12 @@ export const FlowBuilderPage: React.FC<FlowBuilderPageProps> = ({
const updateStatusMutation = useUpdatePolicyVersionStatus(editingPolicy?.policy_name);
const handleNewVersion = async () => {
const newPolicy = await createVersionMutation.mutateAsync();
onVersionCreated?.(newPolicy);
try {
const newPolicy = await createVersionMutation.mutateAsync();
onVersionCreated?.(newPolicy);
} catch {
// Notification already shown by onError in the mutation hook
}
};
const handleSelectVersion = (policy: Policy) => {
@ -1326,20 +1330,28 @@ export const FlowBuilderPage: React.FC<FlowBuilderPageProps> = ({
const handlePublishVersion = async () => {
if (!editingPolicy?.policy_id) return;
const updated = await updateStatusMutation.mutateAsync({
policyId: editingPolicy.policy_id,
status: "published",
});
onVersionStatusUpdated?.(updated);
try {
const updated = await updateStatusMutation.mutateAsync({
policyId: editingPolicy.policy_id,
status: "published",
});
onVersionStatusUpdated?.(updated);
} catch {
// Notification already shown by onError in the mutation hook
}
};
const handlePromoteToProduction = async () => {
if (!editingPolicy?.policy_id) return;
const updated = await updateStatusMutation.mutateAsync({
policyId: editingPolicy.policy_id,
status: "production",
});
onVersionStatusUpdated?.(updated);
try {
const updated = await updateStatusMutation.mutateAsync({
policyId: editingPolicy.policy_id,
status: "production",
});
onVersionStatusUpdated?.(updated);
} catch {
// Notification already shown by onError in the mutation hook
}
};
const handleSave = async () => {