Skip to content

Commit 9664275

Browse files
committed
Only render SearchOrAskAI bar if the user has access to the api endpoint
1 parent 1afb37e commit 9664275

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/SearchOrAskAiButton.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { css } from '@emotion/react'
1616
import * as React from 'react'
1717
import { useEffect, Suspense, lazy } from 'react'
18+
import { useQuery } from '@tanstack/react-query'
1819

1920
// Lazy load the modal component
2021
const SearchOrAskAiModal = lazy(() =>
@@ -28,6 +29,16 @@ export const SearchOrAskAiButton = () => {
2829
const { clearSearchTerm } = useSearchActions()
2930
const isModalOpen = useModalIsOpen()
3031
const { openModal, closeModal, toggleModal } = useModalActions()
32+
33+
const { data: isApiAvailable } = useQuery({
34+
queryKey: ['api-health'],
35+
queryFn: async () => {
36+
const response = await fetch('/docs/_api/v1/', { method: 'HEAD' })
37+
return response.ok
38+
},
39+
staleTime: 5 * 60 * 1000, // 5 minutes
40+
retry: false
41+
})
3142

3243
const positionCss = css`
3344
position: absolute;
@@ -61,6 +72,10 @@ export const SearchOrAskAiButton = () => {
6172
window.removeEventListener('keydown', handleKeydown)
6273
}
6374
}, [])
75+
76+
if (!isApiAvailable) {
77+
return null
78+
}
6479

6580
return (
6681
<>

src/api/Elastic.Documentation.Api.Infrastructure/MappingsExstension.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static class MappingsExtension
1515
{
1616
public static void MapElasticDocsApiEndpoints(this IEndpointRouteBuilder group)
1717
{
18+
_ = group.MapMethods("/", [HttpMethods.Head], () => Results.Empty);
1819
MapAskAiEndpoint(group);
1920
MapSearchEndpoint(group);
2021
}

0 commit comments

Comments
 (0)