-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: Support additional filters #6213
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { useState } from 'react' | ||
| import { useTranslation } from '@/i18n/react-i18next-compat' | ||
| import { Switch } from '@/components/ui/switch' | ||
| import { | ||
| DropdownMenu, | ||
| DropdownMenuContent, | ||
| DropdownMenuItem, | ||
| DropdownMenuTrigger, | ||
| DropdownMenuSeparator, | ||
| DropdownMenuLabel, | ||
| } from '@/components/ui/dropdown-menu' | ||
| import { IconFilter, IconChevronDown } from '@tabler/icons-react' | ||
| import { cn } from '@/lib/utils' | ||
|
|
||
| export interface ModelFilterOptions { | ||
| showOnlyDownloaded: boolean | ||
| toolCallingOnly: boolean | ||
| visionOnly: boolean | ||
| // Future filters can be added here | ||
| // embeddingsOnly: boolean | ||
| // imageGenerationOnly: boolean | ||
| } | ||
|
|
||
| interface ModelFiltersProps { | ||
| filters: ModelFilterOptions | ||
| onFiltersChange: (filters: ModelFilterOptions) => void | ||
| className?: string | ||
| } | ||
|
|
||
| export function ModelFilters({ | ||
| filters, | ||
| onFiltersChange, | ||
| className, | ||
| }: ModelFiltersProps) { | ||
| const { t } = useTranslation() | ||
| const [isOpen, setIsOpen] = useState(false) | ||
|
|
||
| const handleFilterChange = (key: keyof ModelFilterOptions, value: boolean) => { | ||
| onFiltersChange({ | ||
| ...filters, | ||
| [key]: value, | ||
| }) | ||
| } | ||
|
|
||
| const activeFiltersCount = Object.values(filters).filter(Boolean).length | ||
|
|
||
| return ( | ||
| <DropdownMenu open={isOpen} onOpenChange={setIsOpen}> | ||
| <DropdownMenuTrigger asChild> | ||
| <button | ||
| type="button" | ||
| aria-label={activeFiltersCount > 0 ? `${t('hub:filters')} (${activeFiltersCount} active)` : t('hub:filters')} | ||
| aria-expanded={isOpen} | ||
| aria-haspopup="menu" | ||
| className={cn( | ||
| 'flex cursor-pointer items-center gap-2 px-2 py-1 rounded-sm text-sm outline-none text-main-view-fg font-medium relative bg-main-view-fg/15', | ||
| activeFiltersCount > 0 && 'bg-main-view-fg/20 ring-1 ring-main-view-fg/20', | ||
| className | ||
| )} | ||
| > | ||
| <IconFilter size={14} /> | ||
| <span className="hidden sm:inline">{t('hub:filters')}</span> | ||
| {activeFiltersCount > 0 && ( | ||
| <span className="pointer-events-none absolute top-0 right-0 translate-x-1/4 -translate-y-1/4 bg-primary text-primary-fg text-xs rounded-full w-5 h-5 flex items-center justify-center"> | ||
| {activeFiltersCount} | ||
| </span> | ||
| )} | ||
| <IconChevronDown | ||
| size={14} | ||
| className={cn( | ||
| 'transition-transform duration-200', | ||
| isOpen && 'rotate-180' | ||
| )} | ||
| /> | ||
| </button> | ||
| </DropdownMenuTrigger> | ||
| <DropdownMenuContent align="end" className="w-56"> | ||
| <DropdownMenuLabel>{t('hub:filterBy')}</DropdownMenuLabel> | ||
| <DropdownMenuSeparator /> | ||
|
|
||
| {/* Downloaded Models Filter */} | ||
| <DropdownMenuItem | ||
| className="flex items-center justify-between p-3 cursor-pointer" | ||
| onClick={(e) => { | ||
| e.preventDefault() | ||
| handleFilterChange('showOnlyDownloaded', !filters.showOnlyDownloaded) | ||
| }} | ||
| > | ||
| <span className="text-sm">{t('hub:downloaded')}</span> | ||
| <Switch | ||
| checked={filters.showOnlyDownloaded} | ||
| onCheckedChange={(checked) => | ||
| handleFilterChange('showOnlyDownloaded', checked) | ||
| } | ||
| onClick={(e) => e.stopPropagation()} | ||
| /> | ||
| </DropdownMenuItem> | ||
|
|
||
| {/* Tool Calling Filter */} | ||
| <DropdownMenuItem | ||
| className="flex items-center justify-between p-3 cursor-pointer" | ||
| onClick={(e) => { | ||
| e.preventDefault() | ||
| handleFilterChange('toolCallingOnly', !filters.toolCallingOnly) | ||
| }} | ||
| > | ||
| <div className="flex flex-col"> | ||
| <span className="text-sm">{t('hub:toolCalling')}</span> | ||
| </div> | ||
| <Switch | ||
| checked={filters.toolCallingOnly} | ||
| onCheckedChange={(checked) => | ||
| handleFilterChange('toolCallingOnly', checked) | ||
| } | ||
| onClick={(e) => e.stopPropagation()} | ||
| /> | ||
| </DropdownMenuItem> | ||
|
|
||
| {/* Vision Filter */} | ||
| <DropdownMenuItem | ||
| className="flex items-center justify-between p-3 cursor-pointer" | ||
| onClick={(e) => { | ||
| e.preventDefault() | ||
| handleFilterChange('visionOnly', !filters.visionOnly) | ||
| }} | ||
| > | ||
| <div className="flex flex-col"> | ||
| <span className="text-sm">{t('hub:vision')}</span> | ||
| </div> | ||
|
Comment on lines
+127
to
+129
|
||
| <Switch | ||
| checked={filters.visionOnly} | ||
| onCheckedChange={(checked) => | ||
| handleFilterChange('visionOnly', checked) | ||
| } | ||
| onClick={(e) => e.stopPropagation()} | ||
| /> | ||
| </DropdownMenuItem> | ||
|
|
||
| {/* Clear Filters */} | ||
| {activeFiltersCount > 0 && ( | ||
| <> | ||
| <DropdownMenuSeparator /> | ||
| <DropdownMenuItem | ||
| className="p-3 cursor-pointer text-center justify-center" | ||
| onClick={() => | ||
| onFiltersChange({ | ||
| showOnlyDownloaded: false, | ||
| toolCallingOnly: false, | ||
| visionOnly: false, | ||
| }) | ||
| } | ||
| > | ||
| <span className="text-sm text-muted-foreground"> | ||
| {t('hub:clearFilters')} | ||
| </span> | ||
| </DropdownMenuItem> | ||
| </> | ||
| )} | ||
| </DropdownMenuContent> | ||
| </DropdownMenu> | ||
| ) | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
web-app/src/components/filters/__tests__/ModelFilters.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react' | ||
| import userEvent from '@testing-library/user-event' | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { ModelFilters, type ModelFilterOptions } from '../../filters/ModelFilters' | ||
|
|
||
| vi.mock('@/i18n/react-i18next-compat', () => ({ | ||
| useTranslation: () => ({ | ||
| t: (key: string) => key, | ||
| }), | ||
| })) | ||
|
|
||
| // Radix portals attach to document.body; ensure JSDOM is ready | ||
| const setup = (initial: ModelFilterOptions = { showOnlyDownloaded: false, toolCallingOnly: false, visionOnly: false }) => { | ||
| let filters = { ...initial } | ||
| let rerenderFn: ReturnType<typeof render>['rerender'] | ||
|
|
||
| const onFiltersChange = vi.fn((next: ModelFilterOptions) => { | ||
| filters = next | ||
| rerenderFn(<ModelFilters filters={filters} onFiltersChange={onFiltersChange} />) | ||
| }) | ||
|
|
||
| const { rerender } = render( | ||
| <ModelFilters filters={filters} onFiltersChange={onFiltersChange} /> | ||
| ) | ||
| rerenderFn = rerender | ||
|
|
||
| return { onFiltersChange, rerender } | ||
| } | ||
|
|
||
| describe('ModelFilters', () => { | ||
| it('renders the trigger and opens the menu', async () => { | ||
| setup() | ||
| const trigger = screen.getByText('hub:filters') | ||
| expect(trigger).toBeInTheDocument() | ||
|
|
||
| const user = userEvent.setup() | ||
| await user.click(trigger) | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText('hub:filterBy')).toBeInTheDocument() | ||
| }) | ||
| }) | ||
|
|
||
| it('toggles Downloaded filter and calls onFiltersChange', async () => { | ||
| const { onFiltersChange } = setup() | ||
| const user = userEvent.setup() | ||
|
|
||
| const trigger = screen.getByText('hub:filters') | ||
| await user.click(trigger) | ||
|
|
||
| const downloadedItem = screen.getByText('hub:downloaded') | ||
| await user.click(downloadedItem) | ||
|
|
||
| expect(onFiltersChange).toHaveBeenCalled() | ||
| const lastCall = onFiltersChange.mock.lastCall?.[0] as ModelFilterOptions | ||
| expect(lastCall.showOnlyDownloaded).toBe(true) | ||
| }) | ||
|
|
||
| it('toggles Tool Calling filter and calls onFiltersChange', async () => { | ||
| const { onFiltersChange } = setup() | ||
| const user = userEvent.setup() | ||
|
|
||
| await user.click(screen.getByText('hub:filters')) | ||
| await user.click(screen.getByText('hub:toolCalling')) | ||
|
|
||
| expect(onFiltersChange).toHaveBeenCalled() | ||
| const lastCall = onFiltersChange.mock.lastCall?.[0] as ModelFilterOptions | ||
| expect(lastCall.toolCallingOnly).toBe(true) | ||
| }) | ||
|
|
||
| it('toggles Vision filter and calls onFiltersChange', async () => { | ||
| const { onFiltersChange } = setup() | ||
| const user = userEvent.setup() | ||
|
|
||
| await user.click(screen.getByText('hub:filters')) | ||
| await user.click(screen.getByText('hub:vision')) | ||
|
|
||
| expect(onFiltersChange).toHaveBeenCalled() | ||
| const lastCall = onFiltersChange.mock.lastCall?.[0] as ModelFilterOptions | ||
| expect(lastCall.visionOnly).toBe(true) | ||
| }) | ||
|
|
||
| it('shows active filter count badge', async () => { | ||
| setup({ showOnlyDownloaded: true, toolCallingOnly: true, visionOnly: false }) | ||
| const badge = document.querySelector('[data-slot="dropdown-menu-trigger"] .w-5.h-5') as HTMLElement | ||
| expect(badge).toBeInTheDocument() | ||
| expect(badge.textContent).toBe('2') | ||
| }) | ||
|
|
||
| it('clears filters via Clear all filters', async () => { | ||
| const { onFiltersChange } = setup({ showOnlyDownloaded: true, toolCallingOnly: true, visionOnly: false }) | ||
| const user = userEvent.setup() | ||
|
|
||
| await user.click(screen.getByText('hub:filters')) | ||
| await user.click(screen.getByText('hub:clearFilters')) | ||
|
|
||
| expect(onFiltersChange).toHaveBeenCalled() | ||
| const lastCall = onFiltersChange.mock.lastCall?.[0] as ModelFilterOptions | ||
| expect(lastCall).toEqual({ showOnlyDownloaded: false, toolCallingOnly: false, visionOnly: false }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
divwrapper withflex flex-colis unnecessary since it only contains a singlespanelement. The wrapper can be removed and the span can be used directly.