From 96642753d611c3236ffce22a65b0b739f43c4c69 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 2 Sep 2025 16:49:23 +0200 Subject: [PATCH 1/2] Only render SearchOrAskAI bar if the user has access to the api endpoint --- .../SearchOrAskAi/SearchOrAskAiButton.tsx | 15 +++++++++++++++ .../MappingsExstension.cs | 1 + 2 files changed, 16 insertions(+) diff --git a/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx b/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx index 23ab5b61c..8570658ed 100644 --- a/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx +++ b/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx @@ -15,6 +15,7 @@ import { import { css } from '@emotion/react' import * as React from 'react' import { useEffect, Suspense, lazy } from 'react' +import { useQuery } from '@tanstack/react-query' // Lazy load the modal component const SearchOrAskAiModal = lazy(() => @@ -28,6 +29,16 @@ export const SearchOrAskAiButton = () => { const { clearSearchTerm } = useSearchActions() const isModalOpen = useModalIsOpen() const { openModal, closeModal, toggleModal } = useModalActions() + + const { data: isApiAvailable } = useQuery({ + queryKey: ['api-health'], + queryFn: async () => { + const response = await fetch('/docs/_api/v1/', { method: 'HEAD' }) + return response.ok + }, + staleTime: 5 * 60 * 1000, // 5 minutes + retry: false + }) const positionCss = css` position: absolute; @@ -61,6 +72,10 @@ export const SearchOrAskAiButton = () => { window.removeEventListener('keydown', handleKeydown) } }, []) + + if (!isApiAvailable) { + return null + } return ( <> diff --git a/src/api/Elastic.Documentation.Api.Infrastructure/MappingsExstension.cs b/src/api/Elastic.Documentation.Api.Infrastructure/MappingsExstension.cs index 1dcede6d9..dbbdff5b1 100644 --- a/src/api/Elastic.Documentation.Api.Infrastructure/MappingsExstension.cs +++ b/src/api/Elastic.Documentation.Api.Infrastructure/MappingsExstension.cs @@ -15,6 +15,7 @@ public static class MappingsExtension { public static void MapElasticDocsApiEndpoints(this IEndpointRouteBuilder group) { + _ = group.MapMethods("/", [HttpMethods.Head], () => Results.Empty); MapAskAiEndpoint(group); MapSearchEndpoint(group); } From 8eb5764a1afa7ccf9796a83dd91bdf85dd38a674 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Tue, 2 Sep 2025 16:52:06 +0200 Subject: [PATCH 2/2] Run prettier --- .../SearchOrAskAi/SearchOrAskAiButton.tsx | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx b/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx index 8570658ed..3ca8cdc1c 100644 --- a/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx +++ b/src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx @@ -13,9 +13,9 @@ import { EuiLoadingSpinner, } from '@elastic/eui' import { css } from '@emotion/react' +import { useQuery } from '@tanstack/react-query' import * as React from 'react' import { useEffect, Suspense, lazy } from 'react' -import { useQuery } from '@tanstack/react-query' // Lazy load the modal component const SearchOrAskAiModal = lazy(() => @@ -29,16 +29,16 @@ export const SearchOrAskAiButton = () => { const { clearSearchTerm } = useSearchActions() const isModalOpen = useModalIsOpen() const { openModal, closeModal, toggleModal } = useModalActions() - + const { data: isApiAvailable } = useQuery({ - queryKey: ['api-health'], - queryFn: async () => { - const response = await fetch('/docs/_api/v1/', { method: 'HEAD' }) - return response.ok - }, - staleTime: 5 * 60 * 1000, // 5 minutes - retry: false - }) + queryKey: ['api-health'], + queryFn: async () => { + const response = await fetch('/docs/_api/v1/', { method: 'HEAD' }) + return response.ok + }, + staleTime: 5 * 60 * 1000, // 5 minutes + retry: false, + }) const positionCss = css` position: absolute; @@ -72,7 +72,7 @@ export const SearchOrAskAiButton = () => { window.removeEventListener('keydown', handleKeydown) } }, []) - + if (!isApiAvailable) { return null }