Add page filtering

feat/cmd-k
Kamran Ahmed 2 years ago
parent 80dac2f7f7
commit bb23f57204
  1. 13
      src/components/CommandMenu/CommandMenu.tsx
  2. 2
      src/hooks/use-outside-click.ts
  3. 4
      src/pages/pages.json.ts

@ -8,6 +8,7 @@ import VideoIcon from '../../icons/video.svg';
import { httpGet } from '../../lib/http'; import { httpGet } from '../../lib/http';
import { useKeydown } from '../../hooks/use-keydown'; import { useKeydown } from '../../hooks/use-keydown';
import { isLoggedIn } from '../../lib/jwt'; import { isLoggedIn } from '../../lib/jwt';
import { useOutsideClick } from '../../hooks/use-outside-click';
type PageType = { type PageType = {
url: string; url: string;
@ -45,6 +46,7 @@ function shouldShowPage(page: PageType) {
export function CommandMenu() { export function CommandMenu() {
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
const modalRef = useRef<HTMLInputElement>(null);
const [isActive, setIsActive] = useState(false); const [isActive, setIsActive] = useState(false);
const [allPages, setAllPages] = useState<PageType[]>([]); const [allPages, setAllPages] = useState<PageType[]>([]);
const [searchResults, setSearchResults] = useState<PageType[]>(defaultPages); const [searchResults, setSearchResults] = useState<PageType[]>(defaultPages);
@ -55,6 +57,10 @@ export function CommandMenu() {
setIsActive(true); setIsActive(true);
}); });
useOutsideClick(modalRef, () => {
setIsActive(false);
});
useEffect(() => { useEffect(() => {
if (!isActive || !inputRef.current) { if (!isActive || !inputRef.current) {
return; return;
@ -105,7 +111,7 @@ export function CommandMenu() {
return ( return (
<div className="fixed left-0 right-0 top-0 z-50 flex h-full justify-center overflow-y-auto overflow-x-hidden bg-black/50"> <div className="fixed left-0 right-0 top-0 z-50 flex h-full justify-center overflow-y-auto overflow-x-hidden bg-black/50">
<div className="relative top-0 h-full w-full max-w-lg p-2 sm:top-20 md:h-auto"> <div className="relative top-0 h-full w-full max-w-lg p-2 sm:top-20 md:h-auto">
<div className="relative rounded-lg bg-white shadow"> <div className="relative rounded-lg bg-white shadow" ref={modalRef}>
<input <input
ref={inputRef} ref={inputRef}
autofocus={true} autofocus={true}
@ -168,10 +174,7 @@ export function CommandMenu() {
<span class="mr-2 text-gray-400">{page.group}</span> <span class="mr-2 text-gray-400">{page.group}</span>
)} )}
{page.icon && ( {page.icon && (
<img <img src={page.icon} class="mr-2 h-4 w-4" />
src={page.icon}
class="mr-2 h-4 w-4"
/>
)} )}
{page.title} {page.title}
</a> </a>

@ -1,4 +1,4 @@
import { useEffect, useState } from 'preact/hooks'; import { useEffect } from 'preact/hooks';
export function useOutsideClick(ref: any, callback: any) { export function useOutsideClick(ref: any, callback: any) {
useEffect(() => { useEffect(() => {

@ -22,12 +22,12 @@ export async function get() {
group: 'Best Practices', group: 'Best Practices',
})), })),
...guides.map((guide) => ({ ...guides.map((guide) => ({
url: `/${guide.id}`, url: `/guides/${guide.id}`,
title: guide.frontmatter.title, title: guide.frontmatter.title,
group: 'Guides', group: 'Guides',
})), })),
...videos.map((guide) => ({ ...videos.map((guide) => ({
url: `/${guide.id}`, url: `/videos/${guide.id}`,
title: guide.frontmatter.title, title: guide.frontmatter.title,
group: 'Videos', group: 'Videos',
})), })),

Loading…
Cancel
Save