|
1 | 1 | // eslint-disable-next-line simple-import-sort/imports
|
2 | 2 | import BrowserOnly from '@docusaurus/BrowserOnly';
|
3 | 3 | import RouterLink from '@docusaurus/Link';
|
4 |
| -import { useHistory, useLocation } from '@docusaurus/router'; |
| 4 | +// import { useHistory, useLocation } from '@docusaurus/router'; |
5 | 5 | import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
6 |
| -import React, { useCallback } from 'react'; |
| 6 | +// import React, { useCallback } from 'react'; |
| 7 | +import React, { useEffect, useState } from 'react'; |
7 | 8 |
|
8 |
| -import { ApifySearch } from '@apify/docs-search-modal'; |
| 9 | +// import { ApifySearch } from '@apify/docs-search-modal'; |
9 | 10 |
|
10 | 11 | // needs to be imported as the last thing, so that it can override the default styles
|
11 | 12 | // TODO: update simple-import-sort to allow importing css as last.
|
12 | 13 | import './styles.css';
|
| 14 | +import { ControlKeyIcon, SearchIcon } from '@apify/docs-search-modal/dist/utils/icons'; |
13 | 15 |
|
14 | 16 | /**
|
15 | 17 | * Tests whether the given href is pointing to the current docusaurus instance (so we can use the router link).
|
@@ -40,37 +42,184 @@ export function Link(props) {
|
40 | 42 | return <a {...props}>{props.children}</a>;
|
41 | 43 | }
|
42 | 44 |
|
| 45 | +// export default function SearchBar({ onClick }) { |
| 46 | +// const { siteConfig } = useDocusaurusContext(); |
| 47 | +// const location = useLocation(); |
| 48 | +// const history = useHistory(); |
| 49 | +// |
| 50 | +// const navigate = useCallback((href) => { |
| 51 | +// const shortHref = href.substring('https://docs.apify.com'.length); |
| 52 | +// |
| 53 | +// if (matchesCurrentInstance(shortHref, siteConfig.baseUrl)) { |
| 54 | +// return history.push(shortHref); |
| 55 | +// } |
| 56 | +// return window.location.assign(href); |
| 57 | +// }, [history, siteConfig.baseUrl]); |
| 58 | +// |
| 59 | +// const getVersion = useCallback(() => { |
| 60 | +// const match = location.pathname.match(/\/(\d+\.\d+|next)/); |
| 61 | +// |
| 62 | +// return match ? match[1] : 'latest'; |
| 63 | +// }, [location]); |
| 64 | +// |
| 65 | +// return ( |
| 66 | +// <BrowserOnly> |
| 67 | +// {() => ( |
| 68 | +// <div onClick={onClick}> |
| 69 | +// <ApifySearch |
| 70 | +// algoliaAppId={siteConfig.themeConfig.algolia.appId} |
| 71 | +// algoliaIndexName='apify_sdk_v2' |
| 72 | +// algoliaKey={siteConfig.themeConfig.algolia.apiKey} |
| 73 | +// filters={`version:${getVersion()}`} |
| 74 | +// navigate={navigate} |
| 75 | +// /> |
| 76 | +// </div> |
| 77 | +// )} |
| 78 | +// </BrowserOnly> |
| 79 | +// ); |
| 80 | +// } |
| 81 | + |
43 | 82 | export default function SearchBar({ onClick }) {
|
44 |
| - const { siteConfig } = useDocusaurusContext(); |
45 |
| - const location = useLocation(); |
46 |
| - const history = useHistory(); |
| 83 | + const [variant, setVariant] = useState(null); |
47 | 84 |
|
48 |
| - const navigate = useCallback((href) => { |
49 |
| - const shortHref = href.substring('https://docs.apify.com'.length); |
| 85 | + useEffect(() => { |
| 86 | + const storedVariant = localStorage.getItem('search-provider'); |
| 87 | + |
| 88 | + if (storedVariant) { |
| 89 | + setVariant(storedVariant); |
| 90 | + } else { |
| 91 | + const assignedVariant = Math.random() < 0.5 ? 'inkeep' : 'kapa'; |
| 92 | + localStorage.setItem('search-provider', assignedVariant); |
| 93 | + setVariant(assignedVariant); |
| 94 | + } |
| 95 | + }, []); |
| 96 | + |
| 97 | + const inkeepApiKey = process.env.LOCALHOST || process.env.DEV |
| 98 | + ? 'bbbb9f1001a9b66f282431a80bb743a24e2bdefb85d4f1e4' // development, works with localhost |
| 99 | + : '8af30e40009f26622237f75aab8256064c26a3063717c48a'; |
| 100 | + |
| 101 | + onClick = () => { |
| 102 | + if (variant === 'kapa') { |
| 103 | + if (window.Kapa && typeof window.Kapa.open === 'function') { |
| 104 | + window.Kapa.open(); |
| 105 | + } else { |
| 106 | + console.error('Kapa.ai widget is not available.'); |
| 107 | + } |
| 108 | + return; |
| 109 | + } |
50 | 110 |
|
51 |
| - if (matchesCurrentInstance(shortHref, siteConfig.baseUrl)) { |
52 |
| - return history.push(shortHref); |
| 111 | + if (variant !== 'inkeep') { |
| 112 | + console.warn('Unknown search variant:', variant); |
| 113 | + return; |
53 | 114 | }
|
54 |
| - return window.location.assign(href); |
55 |
| - }, [history, siteConfig.baseUrl]); |
56 | 115 |
|
57 |
| - const getVersion = useCallback(() => { |
58 |
| - const match = location.pathname.match(/\/(\d+\.\d+|next)/); |
| 116 | + if (window.Inkeep) { |
| 117 | + const config = { |
| 118 | + baseSettings: { |
| 119 | + apiKey: inkeepApiKey, // production, only works on apify.com (any subdomain) |
| 120 | + organizationDisplayName: 'Apify', |
| 121 | + primaryBrandColor: '#FF9013', |
| 122 | + transformSource: (source) => { |
| 123 | + if (source.contentType === 'documentation') { |
| 124 | + return { |
| 125 | + ...source, |
| 126 | + tabs: [...(source.tabs || []), 'Docs'], |
| 127 | + }; |
| 128 | + } |
| 129 | + return source; |
| 130 | + }, |
| 131 | + trigger: { |
| 132 | + disableDefaultTrigger: true, |
| 133 | + }, |
| 134 | + theme: { |
| 135 | + styles: [ |
| 136 | + { |
| 137 | + key: 'main', |
| 138 | + type: 'link', |
| 139 | + value: '/inkeep-overrides.css', |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + }, |
| 144 | + modalSettings: { |
| 145 | + onOpenChange: handleOpenChange, |
| 146 | + }, |
| 147 | + searchSettings: { |
| 148 | + tabs: [ |
| 149 | + 'All', |
| 150 | + 'Docs', |
| 151 | + 'Publications', |
| 152 | + 'PDFs', |
| 153 | + 'GitHub', |
| 154 | + 'Forums', |
| 155 | + 'Discord', |
| 156 | + 'Slack', |
| 157 | + 'StackOverflow', |
| 158 | + ], |
| 159 | + }, |
| 160 | + aiChatSettings: { |
| 161 | + aiAssistantAvatar: 'https://intercom.help/apify/assets/favicon', |
| 162 | + chatSubjectName: 'Apify', |
| 163 | + exampleQuestions: [ |
| 164 | + 'What is an Actor?', |
| 165 | + 'How to use my own proxies?', |
| 166 | + 'How to integrate Apify Actors with GitHub?', |
| 167 | + 'How to share key-value stores between runs?', |
| 168 | + ], |
| 169 | + getHelpOptions: [ |
| 170 | + { |
| 171 | + action: { |
| 172 | + type: 'open_link', |
| 173 | + url: 'https://apify.com/contact', |
| 174 | + }, |
| 175 | + icon: { |
| 176 | + builtIn: 'IoChatbubblesOutline', |
| 177 | + }, |
| 178 | + name: 'Contact Us', |
| 179 | + }, |
| 180 | + ], |
| 181 | + }, |
| 182 | + }; |
| 183 | + const modal = window.Inkeep.ModalSearchAndChat(config); |
59 | 184 |
|
60 |
| - return match ? match[1] : 'latest'; |
61 |
| - }, [location]); |
| 185 | + function handleOpenChange(newOpen) { |
| 186 | + modal.update({ modalSettings: { isOpen: newOpen } }); |
| 187 | + } |
| 188 | + |
| 189 | + modal.update({ modalSettings: { isOpen: true } }); |
| 190 | + } else { |
| 191 | + console.error('Kapa.ai widget is not available.'); |
| 192 | + } |
| 193 | + }; |
| 194 | + |
| 195 | + const [key, setKey] = useState(null); |
| 196 | + |
| 197 | + useEffect(() => { |
| 198 | + if (typeof navigator !== 'undefined') { |
| 199 | + const isMac = /Mac|iPod|iPhone|iPad/.test(navigator.platform); |
| 200 | + setKey(isMac ? '⌘' : 'ctrl'); |
| 201 | + } |
| 202 | + }, []); |
62 | 203 |
|
63 | 204 | return (
|
64 | 205 | <BrowserOnly>
|
65 | 206 | {() => (
|
66 | 207 | <div onClick={onClick}>
|
67 |
| - <ApifySearch |
68 |
| - algoliaAppId={siteConfig.themeConfig.algolia.appId} |
69 |
| - algoliaIndexName='apify_sdk_v2' |
70 |
| - algoliaKey={siteConfig.themeConfig.algolia.apiKey} |
71 |
| - filters={`version:${getVersion()}`} |
72 |
| - navigate={navigate} |
73 |
| - /> |
| 208 | + <button type="button" className="DocSearch DocSearch-Button" aria-label="Search"> |
| 209 | + <span className="DocSearch-Button-Container"> |
| 210 | + <SearchIcon/> |
| 211 | + <span className="DocSearch-Button-Placeholder">Search</span> |
| 212 | + </span> |
| 213 | + <span className="DocSearch-Button-Keys"> |
| 214 | + {key !== null && (<> |
| 215 | + <kbd className="DocSearch-Button-Key"> |
| 216 | + {key === 'ctrl' ? <ControlKeyIcon/> : key} |
| 217 | + </kbd> |
| 218 | + <kbd className="DocSearch-Button-Key">K</kbd> |
| 219 | + </>)} |
| 220 | + </span> |
| 221 | + |
| 222 | + </button> |
74 | 223 | </div>
|
75 | 224 | )}
|
76 | 225 | </BrowserOnly>
|
|
0 commit comments