Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions components/AlgoliaSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface ISearchContext {
onOpen: (indexName?: string) => void;
onClose: () => void;
onInput?: (e: React.KeyboardEvent) => void;
searchHistory: string[];
setSearchHistory: React.Dispatch<React.SetStateAction<string[]>>;
}

const SearchContext = createContext<ISearchContext>({} as ISearchContext);
Expand All @@ -37,6 +39,8 @@ interface AlgoliaModalProps {
onClose: (event?: React.MouseEvent) => void;
initialQuery: string;
indexName: string;
searchHistory: string[];
setSearchHistory: React.Dispatch<React.SetStateAction<string[]>>;
}

interface IUseDocSearchKeyboardEvents {
Expand Down Expand Up @@ -110,7 +114,7 @@ function Hit({ hit, children }: IHitProps) {
* @description The Algolia modal used for searching the website
* @param {IAlgoliaModalProps} props - The props of the Algolia modal
*/
function AlgoliaModal({ onClose, initialQuery, indexName }: AlgoliaModalProps) {
function AlgoliaModal({ onClose, initialQuery, indexName, searchHistory, setSearchHistory }: AlgoliaModalProps) {
const router = useRouter();

return createPortal(
Expand All @@ -132,7 +136,11 @@ function AlgoliaModal({ onClose, initialQuery, indexName }: AlgoliaModalProps) {
}
}}
hitComponent={Hit}
transformItems={transformItems}
transformItems={(items) => {
const transformedItems = transformItems(items);
setSearchHistory((prevHistory) => [...new Set([...prevHistory, ...transformedItems.map((item) => item.query)])]);
return transformedItems;
}}
getMissingResultsUrl={({ query }) => {
return `https://github.com/asyncapi/website/issues/new?title=Cannot%20search%20given%20query:%20${query}`;
}}
Expand Down Expand Up @@ -235,6 +243,7 @@ export default function AlgoliaSearch({ children }: { children: React.ReactNode
const [isOpen, setIsOpen] = useState(false);
const [indexName, setIndexName] = useState<string>(INDEX_NAME);
const [initialQuery, setInitialQuery] = useState<string>();
const [searchHistory, setSearchHistory] = useState<string[]>([]);

const onOpen = useCallback(
(_indexName?: string) => {
Expand Down Expand Up @@ -270,8 +279,18 @@ export default function AlgoliaSearch({ children }: { children: React.ReactNode
<Head>
<link rel='preconnect' href={`https://${APP_ID}-dsn.algolia.net`} crossOrigin='anonymous' />
</Head>
<SearchContext.Provider value={{ isOpen, onOpen, onClose, onInput }}>{children}</SearchContext.Provider>
{isOpen && <AlgoliaModal initialQuery={initialQuery ?? ''} onClose={onClose} indexName={indexName} />}
<SearchContext.Provider value={{ isOpen, onOpen, onClose, onInput, searchHistory, setSearchHistory }}>
{children}
</SearchContext.Provider>
{isOpen && (
<AlgoliaModal
initialQuery={initialQuery ?? ''}
onClose={onClose}
indexName={indexName}
searchHistory={searchHistory}
setSearchHistory={setSearchHistory}
/>
)}
</>
);
}
Expand All @@ -282,7 +301,7 @@ export default function AlgoliaSearch({ children }: { children: React.ReactNode
* @param {ISearchButtonProps} props - The props of the search button
*/
export function SearchButton({ children, indexName = INDEX_NAME, ...props }: ISearchButtonProps) {
const { onOpen, onInput } = useContext(SearchContext);
const { onOpen, onInput, searchHistory, setSearchHistory } = useContext(SearchContext);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove unused props in SearchButton component.

The searchHistory and setSearchHistory props are destructured but never used in the component.

Apply this diff to fix the unused variables:

-  const { onOpen, onInput, searchHistory, setSearchHistory } = useContext(SearchContext);
+  const { onOpen, onInput } = useContext(SearchContext);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { onOpen, onInput, searchHistory, setSearchHistory } = useContext(SearchContext);
const { onOpen, onInput } = useContext(SearchContext);
🧰 Tools
🪛 ESLint

[error] 304-304: 'searchHistory' is assigned a value but never used.

(unused-imports/no-unused-vars)


[error] 304-304: 'searchHistory' is assigned a value but never used.

(no-unused-vars)


[error] 304-304: 'setSearchHistory' is assigned a value but never used.

(unused-imports/no-unused-vars)


[error] 304-304: 'setSearchHistory' is assigned a value but never used.

(no-unused-vars)

🪛 GitHub Actions: PR testing - if Node project

[error] 304-304: 'searchHistory' is assigned a value but never used.


[error] 304-304: 'setSearchHistory' is assigned a value but never used.

const [Children, setChildren] = useState<string | React.ReactNode>('');
const searchButtonRef = useRef<HTMLButtonElement>(null);
const actionKey = getActionKey();
Expand Down
37 changes: 17 additions & 20 deletions components/Hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HeadingLevel, HeadingTypeStyle } from '@/types/typography/Heading';
import { ParagraphTypeStyle } from '@/types/typography/Paragraph';

import { useTranslation } from '../utils/i18n';
import AlgoliaSearch, { SearchButton } from './AlgoliaSearch'; // Import AlgoliaSearch component
import { SearchButton } from './AlgoliaSearch'; // Import SearchButton component
import Button from './buttons/Button';
import AnnouncementHero from './campaigns/AnnouncementHero';
import DemoAnimation from './DemoAnimation';
Expand Down Expand Up @@ -52,25 +52,22 @@ export default function Hero({ className = '' }: HeroProps) {
icon={<ArrowRight className='-mb-1 size-5' />}
data-testid='Hero-Button'
/>
{/* Wrap SearchButton with AlgoliaSearch component */}
<AlgoliaSearch>
<SearchButton className='flex w-full items-center space-x-3 rounded-md border border-secondary-500 bg-secondary-100 px-4 py-3 text-left text-secondary-500 shadow-md transition-all duration-500 ease-in-out hover:bg-secondary-500 hover:text-white md:w-auto'>
{({ actionKey }) => (
<>
<IconLoupe />
<span className='flex-auto'>{t('main.search_btn')}</span>
{actionKey && (
<kbd className='font-sans font-semibold'>
<abbr title={actionKey.key} className='no-underline'>
{actionKey.shortKey}
</abbr>{' '}
K
</kbd>
)}
</>
)}
</SearchButton>
</AlgoliaSearch>
<SearchButton className='flex w-full items-center space-x-3 rounded-md border border-secondary-500 bg-secondary-100 px-4 py-3 text-left text-secondary-500 shadow-md transition-all duration-500 ease-in-out hover:bg-secondary-500 hover:text-white md:w-auto'>
{({ actionKey }) => (
<>
<IconLoupe />
<span className='flex-auto'>{t('main.search_btn')}</span>
{actionKey && (
<kbd className='font-sans font-semibold'>
<abbr title={actionKey.key} className='no-underline'>
{actionKey.shortKey}
</abbr>{' '}
K
</kbd>
)}
</>
)}
</SearchButton>
</div>
<Paragraph typeStyle={ParagraphTypeStyle.sm} className='mt-4' textColor='text-gray-500'>
{t('main.slogan_text')}{' '}
Expand Down
16 changes: 14 additions & 2 deletions components/navigation/DocsMobileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ export default function DocsMobileMenu({ post, navigation, onClickClose = () =>
className='flex w-full items-center space-x-3 rounded-md border border-gray-300 bg-white px-3 py-1.5 text-left text-sm text-gray-700 shadow-sm transition-all duration-500 ease-in-out hover:border-secondary-500 hover:bg-secondary-100 hover:text-secondary-500'
indexName={DOCS_INDEX_NAME}
>
<IconLoupe />
<span className='flex-auto'>Search docs...</span>
{({ actionKey }) => (
<>
<IconLoupe />
<span className='flex-auto'>Search docs...</span>
{actionKey && (
<kbd className='font-sans font-semibold'>
<abbr title={actionKey.key} className='no-underline'>
{actionKey.shortKey}
</abbr>{' '}
K
</kbd>
)}
</>
)}
</SearchButton>
</div>
<nav className='mb-4 mt-5 px-2'>
Expand Down
Loading