From 5e2aa7d28308ed729c7888e54218a9c72eeabd7e Mon Sep 17 00:00:00 2001 From: Vega Date: Mon, 24 Feb 2025 12:50:03 -0800 Subject: [PATCH] coveo search integration test --- docusaurus.config.js | 2 + src/components/CoveoSearch/CoveoSearch.js | 87 +++++++++++++++++++++++ src/config/CoveoSearchHeadTags.js | 25 +++++++ src/pages/quickstarts.js | 3 + 4 files changed, 117 insertions(+) create mode 100644 src/components/CoveoSearch/CoveoSearch.js create mode 100644 src/config/CoveoSearchHeadTags.js diff --git a/docusaurus.config.js b/docusaurus.config.js index 5d079a5e63..ef0aee1fec 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -11,6 +11,7 @@ import fs from 'fs'; import footerItems from './src/config/footer.navItems.js'; import baseHeadTags from './src/config/baseHeadTags.js'; import { sidebarItemsGenerator } from './custom-sidebar.js'; +import headTags from './src/config/CoveoSearchHeadTags.js'; const baseUrl = ''; const projectName = 'ai-unlimited-docs'; @@ -82,6 +83,7 @@ const config = { headTags: [ ...baseHeadTags, ...trackingScripts, + ...headTags ], // Even if you don't use internationalization, you can use this field to set diff --git a/src/components/CoveoSearch/CoveoSearch.js b/src/components/CoveoSearch/CoveoSearch.js new file mode 100644 index 0000000000..c1555f0641 --- /dev/null +++ b/src/components/CoveoSearch/CoveoSearch.js @@ -0,0 +1,87 @@ +import { useEffect } from 'react'; + +function CoveoSearch() { + useEffect(() => { + const hspApiKey = 'xx4ac4bcbd-1c8b-4482-b60b-bc0ebdc55e01'; + const siApiKey = 'xx116e6fc7-3430-4f29-9115-fde53bc5cd24'; + const orgId = 'teradataxrkwxj74'; + const siPageId = 'cda56b93-b731-4e51-9a46-6a2498b40cb0'; + const orgEndpont = `https://${orgId}.admin.org.coveo.com`; + const redirectionUrl = '/coveo-search'; + + function isSearchPage() { + return window.location.href.indexOf('/coveo-search') !== -1; + } + + function injectExternalSearchbox(searchboxID) { + const box = document.createElement('atomic-search-box'); + box.setAttribute('clear-filters', 'false'); + const wrapper = document.createElement('atomic-external'); + wrapper.appendChild(box); + document.getElementById(searchboxID)?.appendChild(wrapper); + } + + async function initSearchBox() { + const searchInterface = document.querySelector("atomic-search-interface#searchbox"); + if (searchInterface) { + await searchInterface.initialize({ + accessToken: siApiKey, + organizationId: orgId, + organizationEndpoints: { admin: orgEndpont }, + }); + + searchInterface.querySelector('atomic-search-box').redirectionUrl = redirectionUrl; + } + } + + async function initSearchPage() { + const hsp = document.querySelector('atomic-hosted-page#searchPage'); + if (hsp) { + await hsp.initialize({ + accessToken: hspApiKey, + organizationId: orgId, + organizationEndpoints: { admin: orgEndpont }, + pageId: siPageId + }); + } + + await customElements.whenDefined("atomic-search-interface"); + + const searchInterface = document.querySelector("atomic-search-interface#search"); + injectExternalSearchbox('atomicExternalSearchBox'); + + if (searchInterface) { + await searchInterface.initialize({ + accessToken: siApiKey, + organizationId: orgId, + organizationEndpoints: { admin: orgEndpont } + }); + + searchInterface.executeFirstSearch(); + } + } + + if (isSearchPage()) { + initSearchPage(); + } else { + initSearchBox(); + } + }, []); + + return ( + <> + {/* Search Box */} +
+
+ + + +
+ + {/* Search Page */} + + + ); +} + +export default CoveoSearch; diff --git a/src/config/CoveoSearchHeadTags.js b/src/config/CoveoSearchHeadTags.js new file mode 100644 index 0000000000..c4b3ac6414 --- /dev/null +++ b/src/config/CoveoSearchHeadTags.js @@ -0,0 +1,25 @@ +const headTags = [ + { + tagName: 'link', + attributes: { + rel: 'stylesheet', + href: 'https://static.cloud.coveo.com/atomic/v2/themes/coveo.css', + }, + }, + { + tagName: 'script', + attributes: { + src: 'https://static.cloud.coveo.com/atomic/v2/atomic.esm.js', + type: 'module', + }, + }, + { + tagName: 'script', + attributes: { + src: 'https://static.cloud.coveo.com/atomic-hosted-page/v0/atomic-hosted-page/atomic-hosted-page.esm.js', + type: 'module', + }, + }, + ]; + + export default headTags; \ No newline at end of file diff --git a/src/pages/quickstarts.js b/src/pages/quickstarts.js index f79b499d25..b682f5e140 100644 --- a/src/pages/quickstarts.js +++ b/src/pages/quickstarts.js @@ -7,6 +7,7 @@ import HeroImageUrl from '@site/static/img/quickstarts-hero.webp'; import { translate } from '@docusaurus/Translate'; import styles from './index.module.css'; import { Typography } from '@teradata-web/react-components'; +import CoveoSearch from '../components/CoveoSearch/CoveoSearch'; function HomepageHeader() { return ( @@ -71,6 +72,8 @@ export default function Home() { +
TEst
+