Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed : search bug on dialog #1747

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 13 additions & 20 deletions src/components/search/CommandMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { signOut } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import { useCallback, useEffect } from 'react';
import VideoSearchCard from './VideoSearchCard';

interface CommandMenuProps {
icon: string;
Expand All @@ -33,6 +34,7 @@ interface CommandMenuProps {
commandSearchTerm: string;
onCommandSearchTermChange: (value: string) => void;
loading: boolean;
selectedIndex: number;
searchedVideos: TSearchedVideos[] | null;
onCardClick: (videoUrl: string) => void;
onClose: () => void;
Expand All @@ -45,6 +47,7 @@ export function CommandMenu({
commandSearchTerm,
onCommandSearchTermChange,
loading,
selectedIndex,
searchedVideos,
onCardClick,
onClose,
Expand Down Expand Up @@ -99,33 +102,23 @@ export function CommandMenu({
onValueChange={onCommandSearchTermChange}
/>
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>

<CommandGroup heading="Videos">
<div className='overflow-hidden p-1 text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground'>
<div cmdk-group-heading="" aria-hidden="true" id=":r15:">videos</div>
{!loading &&
searchedVideos &&
searchedVideos.length > 0 &&
searchedVideos.map((video) => (
<CommandItem
searchedVideos.map((video, index) => (
<div
key={video.id}
onSelect={() => {
if (video.parentId && video.parent?.courses.length) {
const courseId = video.parent.courses[0].courseId;
const videoUrl = `/courses/${courseId}/${video.parentId}/${video.id}`;
onCardClick(videoUrl);
onClose();
}
}}
className={`${index === selectedIndex ? 'bg-blue-600/10 text-blue-600' : ''}`}
>
<Play className="mr-2 h-4 w-4" />
<span className="truncate">{video.title}</span>
</CommandItem>
<VideoSearchCard video={video} onCardClick={onCardClick} />
</div>
))}
{!loading && (!searchedVideos || searchedVideos.length === 0) && (
<CommandItem>No videos found</CommandItem>
<div className="relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none data-[disabled=true]:pointer-events-none data-[selected='true']:bg-accent data-[selected=true]:text-accent-foreground data-[disabled=true]:opacity-50">No videos found</div>
)}
</CommandGroup>

</div>
<CommandSeparator />

<CommandGroup heading="Suggestions">
Expand Down Expand Up @@ -191,4 +184,4 @@ export function CommandMenu({
</CommandList>
</CommandDialog>
);
}
}
4 changes: 3 additions & 1 deletion src/components/search/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function SearchBar({ onCardClick, isMobile = false }: SearchBarProps) {
throw new Error('Network response was not ok');
}
const data = await response.json();

setState((prev) => ({ ...prev, searchedVideos: data, loading: false }));
} catch (err) {
toast.error('Something went wrong while searching for videos');
Expand Down Expand Up @@ -213,9 +214,10 @@ export function SearchBar({ onCardClick, isMobile = false }: SearchBarProps) {
loading={state.loading}
searchedVideos={state.searchedVideos}
onCardClick={handleCardClick}
selectedIndex={state.selectedIndex}
onClose={() => setState((prev) => ({ ...prev, open: false }))}
/>
)}
</>
);
}
}