fixed Auth bug on analyze and gather rules page

This commit is contained in:
Oliver Kieselbach
2026-03-06 18:29:19 +01:00
parent 5350f03878
commit c5a01280d2
3 changed files with 19 additions and 14 deletions
@@ -62,7 +62,7 @@ export default function GatherRulesPage() {
const fetchRules = useCallback(async () => {
await fetchRulesExec(
`${API_BASE_URL}/api/gather-rules?tenantId=${tenantId}`,
`${API_BASE_URL}/api/rules/gather?tenantId=${tenantId}`,
undefined,
{ transform: (d) => (d as { rules?: GatherRule[] }).rules || [] }
);
@@ -77,9 +77,9 @@ export default function GatherRulesPage() {
const handleToggleRule = async (rule: GatherRule) => {
setTogglingRule(rule.ruleId);
const result = await mutate(
`${API_BASE_URL}/api/gather-rules/${rule.ruleId}?tenantId=${tenantId}`,
`${API_BASE_URL}/api/rules/gather/${rule.ruleId}?tenantId=${tenantId}`,
{
method: "PATCH",
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ enabled: !rule.enabled }),
}
@@ -100,7 +100,7 @@ export default function GatherRulesPage() {
setDeletingRule(rule.ruleId);
const result = await mutate(
`${API_BASE_URL}/api/gather-rules/${rule.ruleId}?tenantId=${tenantId}`,
`${API_BASE_URL}/api/rules/gather/${rule.ruleId}?tenantId=${tenantId}`,
{ method: "DELETE" }
);
if (result !== null) {
@@ -158,7 +158,7 @@ export default function GatherRulesPage() {
};
const result = await mutate(
`${API_BASE_URL}/api/gather-rules?tenantId=${tenantId}`,
`${API_BASE_URL}/api/rules/gather?tenantId=${tenantId}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -228,9 +228,9 @@ export default function GatherRulesPage() {
};
const result = await mutate(
`${API_BASE_URL}/api/gather-rules/${rule.ruleId}?tenantId=${tenantId}`,
`${API_BASE_URL}/api/rules/gather/${rule.ruleId}?tenantId=${tenantId}`,
{
method: "PATCH",
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
}
@@ -1,6 +1,6 @@
"use client";
import { useState, useCallback } from "react";
import { useState, useCallback, useRef } from "react";
import { useAuth } from "@/contexts/AuthContext";
import { useNotifications } from "@/contexts/NotificationContext";
import {
@@ -46,6 +46,10 @@ export function useAuthenticatedFetch<T = unknown>(
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
// Stabilize callback refs so `execute` identity doesn't change on every render
const optionsRef = useRef(options);
optionsRef.current = options;
const clearError = useCallback(() => setError(null), []);
const execute = useCallback(
@@ -85,9 +89,10 @@ export function useAuthenticatedFetch<T = unknown>(
setData(result);
return result;
} catch (err) {
const opts = optionsRef.current;
if (err instanceof TokenExpiredError) {
if (options?.onTokenExpired) {
options.onTokenExpired(err);
if (opts?.onTokenExpired) {
opts.onTokenExpired(err);
} else {
addNotification(
"error",
@@ -96,8 +101,8 @@ export function useAuthenticatedFetch<T = unknown>(
"session-expired",
);
}
} else if (options?.onError) {
options.onError(
} else if (opts?.onError) {
opts.onError(
err instanceof Error ? err : new Error(String(err)),
);
}
@@ -112,7 +117,7 @@ export function useAuthenticatedFetch<T = unknown>(
}
}
},
[getAccessToken, addNotification, options?.onError, options?.onTokenExpired],
[getAccessToken, addNotification],
);
return { data, loading, error, execute, clearError, setData };
File diff suppressed because one or more lines are too long