Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Enforce canonical settings query keys in frontend (#14011)

Graham Neubig98357473c231
diff --git a/frontend/.eslintrc b/frontend/.eslintrc
index 3efd6aea69..88c0124bd2 100644
--- a/frontend/.eslintrc
+++ b/frontend/.eslintrc
@@ -39,6 +39,7 @@
"overrides": [
{
"files": ["*.ts", "*.tsx"],
+ "excludedFiles": ["src/hooks/query/query-keys.ts"],
"rules": {
// Allow state modification in reduce and Redux reducers
"no-param-reassign": [
@@ -48,8 +49,13 @@
"ignorePropertyModificationsFor": ["acc", "state"],
},
],
- // For https://stackoverflow.com/questions/55844608/stuck-with-eslint-error-i-e-separately-loops-should-be-avoided-in-favor-of-arra
- "no-restricted-syntax": "off",
+ "no-restricted-syntax": [
+ "error",
+ {
+ "selector": "Property[key.name='queryKey'] > ArrayExpression[elements.0.value='settings']",
+ "message": "Use SETTINGS_QUERY_KEYS helpers instead of raw settings query key arrays."
+ }
+ ],
"react/require-default-props": "off",
"import/prefer-default-export": "off",
"no-underscore-dangle": "off",
diff --git a/frontend/src/hooks/mutation/use-activate-llm-profile.ts b/frontend/src/hooks/mutation/use-activate-llm-profile.ts
index 786fee65af..c67f36ed77 100644
--- a/frontend/src/hooks/mutation/use-activate-llm-profile.ts
+++ b/frontend/src/hooks/mutation/use-activate-llm-profile.ts
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import ProfilesService from "#/api/settings-service/profiles-service.api";
import { LLM_PROFILES_QUERY_KEY } from "#/hooks/query/use-llm-profiles";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
export function useActivateLlmProfile() {
const queryClient = useQueryClient();
@@ -11,7 +12,7 @@ export function useActivateLlmProfile() {
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [LLM_PROFILES_QUERY_KEY] });
- queryClient.invalidateQueries({ queryKey: ["settings"] });
+ queryClient.invalidateQueries({ queryKey: SETTINGS_QUERY_KEYS.all });
},
});
}
diff --git a/frontend/src/hooks/mutation/use-add-git-providers.ts b/frontend/src/hooks/mutation/use-add-git-providers.ts
index a3b5da2521..ae457a4696 100644
--- a/frontend/src/hooks/mutation/use-add-git-providers.ts
+++ b/frontend/src/hooks/mutation/use-add-git-providers.ts
@@ -2,6 +2,7 @@ import { useMutation, useQueryClient } from "@tanstack/react-query";
import { SecretsService } from "#/api/secrets-service";
import { Provider, ProviderToken } from "#/types/settings";
import { useSelectedOrganizationId } from "#/context/use-selected-organization";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
export const useAddGitProviders = () => {
const queryClient = useQueryClient();
@@ -15,7 +16,7 @@ export const useAddGitProviders = () => {
}) => SecretsService.addGitProvider(providers),
onSuccess: async () => {
await queryClient.invalidateQueries({
- queryKey: ["settings", "personal", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
},
meta: {
diff --git a/frontend/src/hooks/mutation/use-add-mcp-server.ts b/frontend/src/hooks/mutation/use-add-mcp-server.ts
index aaa76ad651..e29a4566d6 100644
--- a/frontend/src/hooks/mutation/use-add-mcp-server.ts
+++ b/frontend/src/hooks/mutation/use-add-mcp-server.ts
@@ -9,6 +9,7 @@ import {
} from "#/types/settings";
import { parseMcpConfig, toSdkMcpConfig } from "#/utils/mcp-config";
import { useSelectedOrganizationId } from "#/context/use-selected-organization";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
type MCPServerType = "sse" | "stdio" | "shttp";
@@ -69,7 +70,7 @@ export function useAddMcpServer() {
},
onSuccess: () => {
queryClient.invalidateQueries({
- queryKey: ["settings", "personal", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
},
});
diff --git a/frontend/src/hooks/mutation/use-delete-git-providers.ts b/frontend/src/hooks/mutation/use-delete-git-providers.ts
index 3188d809e8..7e1008416a 100644
--- a/frontend/src/hooks/mutation/use-delete-git-providers.ts
+++ b/frontend/src/hooks/mutation/use-delete-git-providers.ts
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useSelectedOrganizationId } from "#/context/use-selected-organization";
import { SecretsService } from "#/api/secrets-service";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
export const useDeleteGitProviders = () => {
const queryClient = useQueryClient();
@@ -10,7 +11,7 @@ export const useDeleteGitProviders = () => {
mutationFn: () => SecretsService.deleteGitProviders(),
onSuccess: async () => {
await queryClient.invalidateQueries({
- queryKey: ["settings", "personal", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
},
meta: {
diff --git a/frontend/src/hooks/mutation/use-delete-llm-profile.ts b/frontend/src/hooks/mutation/use-delete-llm-profile.ts
index 94502a41e8..f0e2ea446e 100644
--- a/frontend/src/hooks/mutation/use-delete-llm-profile.ts
+++ b/frontend/src/hooks/mutation/use-delete-llm-profile.ts
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import ProfilesService from "#/api/settings-service/profiles-service.api";
import { LLM_PROFILES_QUERY_KEY } from "#/hooks/query/use-llm-profiles";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
export function useDeleteLlmProfile() {
const queryClient = useQueryClient();
@@ -14,7 +15,7 @@ export function useDeleteLlmProfile() {
// Deleting the active profile clears ``llm_profiles.active`` server-side;
// the settings cache must refetch or the LLM page will keep showing
// the deleted profile as in-use.
- queryClient.invalidateQueries({ queryKey: ["settings"] });
+ queryClient.invalidateQueries({ queryKey: SETTINGS_QUERY_KEYS.all });
},
});
}
diff --git a/frontend/src/hooks/mutation/use-delete-mcp-server.ts b/frontend/src/hooks/mutation/use-delete-mcp-server.ts
index 478fed462a..911099dcc8 100644
--- a/frontend/src/hooks/mutation/use-delete-mcp-server.ts
+++ b/frontend/src/hooks/mutation/use-delete-mcp-server.ts
@@ -4,6 +4,7 @@ import SettingsService from "#/api/settings-service/settings-service.api";
import { MCPConfig } from "#/types/settings";
import { parseMcpConfig, toSdkMcpConfig } from "#/utils/mcp-config";
import { useSelectedOrganizationId } from "#/context/use-selected-organization";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
export function useDeleteMcpServer() {
const queryClient = useQueryClient();
@@ -38,7 +39,7 @@ export function useDeleteMcpServer() {
},
onSuccess: () => {
queryClient.invalidateQueries({
- queryKey: ["settings", "personal", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
},
});
diff --git a/frontend/src/hooks/mutation/use-logout.ts b/frontend/src/hooks/mutation/use-logout.ts
index c0bc31f5a9..e9a16649f0 100644
--- a/frontend/src/hooks/mutation/use-logout.ts
+++ b/frontend/src/hooks/mutation/use-logout.ts
@@ -1,8 +1,9 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { usePostHog } from "posthog-js/react";
import AuthService from "#/api/auth-service/auth-service.api";
-import { useConfig } from "../query/use-config";
import { clearLoginData } from "#/utils/local-storage";
+import { SETTINGS_QUERY_KEYS } from "../query/query-keys";
+import { useConfig } from "../query/use-config";
export const useLogout = () => {
const posthog = usePostHog();
@@ -13,7 +14,7 @@ export const useLogout = () => {
mutationFn: () => AuthService.logout(config?.app_mode ?? "oss"),
onSuccess: async () => {
queryClient.removeQueries({ queryKey: ["tasks"] });
- queryClient.removeQueries({ queryKey: ["settings"] });
+ queryClient.removeQueries({ queryKey: SETTINGS_QUERY_KEYS.all });
queryClient.removeQueries({ queryKey: ["user"] });
queryClient.removeQueries({ queryKey: ["secrets"] });
diff --git a/frontend/src/hooks/mutation/use-rename-llm-profile.ts b/frontend/src/hooks/mutation/use-rename-llm-profile.ts
index 0420e17501..43c4409208 100644
--- a/frontend/src/hooks/mutation/use-rename-llm-profile.ts
+++ b/frontend/src/hooks/mutation/use-rename-llm-profile.ts
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import ProfilesService from "#/api/settings-service/profiles-service.api";
import { LLM_PROFILES_QUERY_KEY } from "#/hooks/query/use-llm-profiles";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
interface RenameLlmProfileVariables {
name: string;
@@ -19,7 +20,7 @@ export function useRenameLlmProfile() {
// Renaming the active profile changes ``llm_profiles.active`` to the
// new name; the settings cache must refetch so any UI that reads the
// active-profile name stays in sync.
- queryClient.invalidateQueries({ queryKey: ["settings"] });
+ queryClient.invalidateQueries({ queryKey: SETTINGS_QUERY_KEYS.all });
},
});
}
diff --git a/frontend/src/hooks/mutation/use-save-settings.ts b/frontend/src/hooks/mutation/use-save-settings.ts
index bfdecaafc7..580a0b2cb3 100644
--- a/frontend/src/hooks/mutation/use-save-settings.ts
+++ b/frontend/src/hooks/mutation/use-save-settings.ts
@@ -3,6 +3,7 @@ import { useSelectedOrganizationId } from "#/context/use-selected-organization";
import { organizationService } from "#/api/organization-service/organization-service.api";
import SettingsService from "#/api/settings-service/settings-service.api";
import { Settings, SettingsScope, SettingsValue } from "#/types/settings";
+import { SETTINGS_QUERY_KEYS } from "../query/query-keys";
type SettingsUpdate = Partial<Settings> & Record<string, unknown>;
@@ -79,11 +80,11 @@ export const useSaveSettings = (scope: SettingsScope = "personal") => {
},
onSuccess: async () => {
await queryClient.invalidateQueries({
- queryKey: ["settings", scope, organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.byScope(scope, organizationId),
});
if (scope === "org") {
await queryClient.invalidateQueries({
- queryKey: ["settings", "personal", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
}
},
diff --git a/frontend/src/hooks/mutation/use-submit-onboarding.ts b/frontend/src/hooks/mutation/use-submit-onboarding.ts
index 2c3ac7cce9..ec7d132ab9 100644
--- a/frontend/src/hooks/mutation/use-submit-onboarding.ts
+++ b/frontend/src/hooks/mutation/use-submit-onboarding.ts
@@ -1,6 +1,7 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "react-router";
import { openHands } from "#/api/open-hands-axios";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
type SubmitOnboardingArgs = {
@@ -25,7 +26,7 @@ export const useSubmitOnboarding = () => {
return data;
},
onSuccess: (data) => {
- queryClient.invalidateQueries({ queryKey: ["settings"] });
+ queryClient.invalidateQueries({ queryKey: SETTINGS_QUERY_KEYS.all });
queryClient.invalidateQueries({ queryKey: ["onboarding-status"] });
const finalRedirectUrl = data.redirect_url || "/";
diff --git a/frontend/src/hooks/mutation/use-switch-organization.ts b/frontend/src/hooks/mutation/use-switch-organization.ts
index 170511e8bc..d3d9ca19f9 100644
--- a/frontend/src/hooks/mutation/use-switch-organization.ts
+++ b/frontend/src/hooks/mutation/use-switch-organization.ts
@@ -32,7 +32,7 @@ export const useSwitchOrganization = () => {
queryKey: ["organizations", orgId, "me"],
});
// Update local state - this triggers automatic refetch for all org-scoped queries
- // since their query keys include organizationId (e.g., ["settings", orgId], ["secrets", orgId])
+ // since their query keys include the selected organizationId.
setOrganizationId(orgId);
// Broadcast org change to other apps (e.g. Automations) via localStorage
setSelectedOrg(orgId);
diff --git a/frontend/src/hooks/mutation/use-update-mcp-server.ts b/frontend/src/hooks/mutation/use-update-mcp-server.ts
index 15b0bb1406..33925d5ca9 100644
--- a/frontend/src/hooks/mutation/use-update-mcp-server.ts
+++ b/frontend/src/hooks/mutation/use-update-mcp-server.ts
@@ -9,6 +9,7 @@ import {
} from "#/types/settings";
import { parseMcpConfig, toSdkMcpConfig } from "#/utils/mcp-config";
import { useSelectedOrganizationId } from "#/context/use-selected-organization";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
type MCPServerType = "sse" | "stdio" | "shttp";
@@ -77,7 +78,7 @@ export function useUpdateMcpServer() {
},
onSuccess: () => {
queryClient.invalidateQueries({
- queryKey: ["settings", "personal", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
},
});
diff --git a/frontend/src/hooks/query/query-keys.test.ts b/frontend/src/hooks/query/query-keys.test.ts
new file mode 100644
index 0000000000..be5c4a5c6f
--- /dev/null
+++ b/frontend/src/hooks/query/query-keys.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it } from "vitest";
+import { SETTINGS_QUERY_KEYS } from "./query-keys";
+
+describe("SETTINGS_QUERY_KEYS", () => {
+ it("returns the canonical root settings key", () => {
+ expect(SETTINGS_QUERY_KEYS.all).toEqual(["settings"]);
+ });
+
+ it("builds scoped settings keys", () => {
+ expect(SETTINGS_QUERY_KEYS.byScope("personal", null)).toEqual([
+ "settings",
+ "personal",
+ null,
+ ]);
+ expect(SETTINGS_QUERY_KEYS.byScope("org", "org-123")).toEqual([
+ "settings",
+ "org",
+ "org-123",
+ ]);
+ });
+
+ it("builds the canonical personal settings key", () => {
+ expect(SETTINGS_QUERY_KEYS.personal("org-123")).toEqual([
+ "settings",
+ "personal",
+ "org-123",
+ ]);
+ expect(SETTINGS_QUERY_KEYS.personal(undefined)).toEqual(
+ SETTINGS_QUERY_KEYS.byScope("personal", undefined),
+ );
+ });
+});
diff --git a/frontend/src/hooks/query/query-keys.ts b/frontend/src/hooks/query/query-keys.ts
index b976849daa..ab5ceaa8c8 100644
--- a/frontend/src/hooks/query/query-keys.ts
+++ b/frontend/src/hooks/query/query-keys.ts
@@ -3,11 +3,21 @@
* Using constants ensures type safety and prevents typos.
*/
+import { SettingsScope } from "#/types/settings";
+
export const QUERY_KEYS = {
/** Web client configuration from the server */
WEB_CLIENT_CONFIG: ["web-client-config"] as const,
} as const;
+export const SETTINGS_QUERY_KEYS = {
+ all: ["settings"] as const,
+ byScope: (scope: SettingsScope, organizationId: string | null | undefined) =>
+ ["settings", scope, organizationId] as const,
+ personal: (organizationId: string | null | undefined) =>
+ ["settings", "personal", organizationId] as const,
+} as const;
+
/** Cache configuration shared across all config-related queries */
export const CONFIG_CACHE_OPTIONS = {
staleTime: 1000 * 60 * 5, // 5 minutes
diff --git a/frontend/src/hooks/query/use-settings.ts b/frontend/src/hooks/query/use-settings.ts
index 3201be60f6..b458b64c06 100644
--- a/frontend/src/hooks/query/use-settings.ts
+++ b/frontend/src/hooks/query/use-settings.ts
@@ -13,6 +13,7 @@ import {
pickNullableString,
} from "#/utils/settings-value-pickers";
import { parseMcpConfig } from "#/utils/mcp-config";
+import { SETTINGS_QUERY_KEYS } from "./query-keys";
/** Look up a value in a nested object by dotted key path. */
const lookupNested = (obj: Record<string, unknown>, key: string): unknown => {
@@ -130,7 +131,7 @@ export const useSettings = (scope: SettingsScope = "personal") => {
const isOss = config?.app_mode === "oss";
const query = useQuery({
- queryKey: ["settings", scope, organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.byScope(scope, organizationId),
queryFn: () => getSettingsQueryFn(scope, organizationId),
retry: (_, error) => error.status !== 404,
refetchOnWindowFocus: false,
diff --git a/frontend/src/routes/user-settings.tsx b/frontend/src/routes/user-settings.tsx
index 6fd4372ecf..7e34c6993a 100644
--- a/frontend/src/routes/user-settings.tsx
+++ b/frontend/src/routes/user-settings.tsx
@@ -2,6 +2,7 @@ import React, { useState, useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";
import { useQueryClient } from "@tanstack/react-query";
import { useSettings } from "#/hooks/query/use-settings";
+import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
import { openHands } from "#/api/open-hands-axios";
import { displaySuccessToast } from "#/utils/custom-toast-handlers";
import { useEmailVerification } from "#/hooks/use-email-verification";
@@ -147,7 +148,7 @@ function UserSettingsScreen() {
displaySuccessToast(t("SETTINGS$EMAIL_VERIFIED_SUCCESSFULLY"));
setTimeout(() => {
queryClient.invalidateQueries({
- queryKey: ["settings", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
}, 2000);
}
@@ -183,7 +184,7 @@ function UserSettingsScreen() {
// Display toast notification instead of setting state
displaySuccessToast(t("SETTINGS$EMAIL_SAVED_SUCCESSFULLY"));
queryClient.invalidateQueries({
- queryKey: ["settings", organizationId],
+ queryKey: SETTINGS_QUERY_KEYS.personal(organizationId),
});
} catch (error) {
// eslint-disable-next-line no-console