import { useSuspenseQuery } from '@tanstack/react-query' import { ApiResource } from '../types' import type { ChatConfigurationResource } from '../types/resources/chat_configuration_resource' import { getChatConfigurationRequestArgs, getChatConfigurationQueryKey, } from '../utils/request/get_chat_configuration' import { ONE_HOUR } from '../utils/time' import { FALLBACK_ALLOWED_FILE_EXTENSIONS, FALLBACK_ALLOWED_MIME_TYPES, FALLBACK_MAX_ATTACHMENTS_PER_MESSAGE, FALLBACK_MAX_FILE_SIZE_IN_BYTES, } from './attachments/fallback_chat_configuration' import { useApiClient } from './use_api_client' // Client-side mirror of ChatConfiguration (server is source of truth). // Returns fallback values if the API request fails so that the attachment // UX keeps working during transient server issues. The server still // enforces its own limits — this hook only drives pre-upload UX. export function useChatConfiguration() { const apiClient = useApiClient() const requestArgs = getChatConfigurationRequestArgs() const { data } = useSuspenseQuery({ queryKey: getChatConfigurationQueryKey(), queryFn: () => { return apiClient.chat .get>(requestArgs) .catch(() => stableFallbackConfiguration) }, staleTime: ONE_HOUR, }) const attrs = data.data return { allowedFileExtensions: attrs.allowedFileExtensions, // During the server-side rollout the API may omit allowedMimeTypes. // The static fallback covers the same set of file kinds as the server's // hardcoded ALLOWED_FILE_EXTENSIONS, so the picker filter and the // post-pick extension validator agree. Once the server always returns // allowedMimeTypes, the resource type can tighten from optional to // required and this nullish-coalesce becomes pure defense-in-depth. allowedMimeTypes: attrs.allowedMimeTypes ?? FALLBACK_ALLOWED_MIME_TYPES, maxFileSizeInBytes: attrs.maxFileSizeInBytes, maxAttachmentsPerMessage: attrs.maxAttachmentsPerMessage, } } // Shape matches what consumers see after transform_response flattens // attributes and camelCases keys. const stableFallbackConfiguration: ApiResource = { data: { type: 'ChatConfiguration', id: 'current', allowedFileExtensions: FALLBACK_ALLOWED_FILE_EXTENSIONS, allowedMimeTypes: FALLBACK_ALLOWED_MIME_TYPES, maxFileSizeInBytes: FALLBACK_MAX_FILE_SIZE_IN_BYTES, maxAttachmentsPerMessage: FALLBACK_MAX_ATTACHMENTS_PER_MESSAGE, }, links: {}, meta: {}, }