// Copyright: © 2026 TWWIM UG. All rights reserved. (www.twwim.com) /** * usePluginSettings Hooks * * TanStack Query hooks for the tenant plugin widget configuration: * - `usePluginSettings(tenantId)` — read current settings. * - `useUpdatePluginSettings(tenantId)` — full-replacement mutation. * * Cache key is local to this feature (single read path, no list views). * Mutation primes the cache via `setQueryData` instead of invalidating, * since the PUT response echoes the saved state. * * @layer Presentation - TanStack Query Hooks */ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { pluginSettingsApi } from '@/infrastructure/http/api/plugin-settings/api'; import type { PluginSettingsResponse, UpdatePluginSettingsRequest, } from '@archer/api-interface'; const pluginSettingsQueryKey = (tenantId: string) => ['plugin-settings', tenantId] as const; /** * Fetches the current plugin widget configuration for a tenant. * * Disabled while `tenantId` is falsy so the route can mount before the * param is resolved without firing a request. */ export function usePluginSettings(tenantId: string) { return useQuery({ queryKey: pluginSettingsQueryKey(tenantId), queryFn: () => pluginSettingsApi.get(tenantId), enabled: !!tenantId, staleTime: 5 * 60 * 1000, }); } /** * Replaces the plugin widget configuration for a tenant. * On success primes the read cache with the echoed saved state. */ export function useUpdatePluginSettings(tenantId: string) { const queryClient = useQueryClient(); return useMutation({ mutationFn: (dto) => pluginSettingsApi.update(tenantId, dto), onSuccess: (data) => { queryClient.setQueryData(pluginSettingsQueryKey(tenantId), data); }, }); }