mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-02-08 13:55:06 -05:00
refactor(LibrarySortMenu): Optimize component with memoization and mapping improvements
This commit is contained in:
@@ -10,11 +10,19 @@ import {
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { useMemo } from "react";
|
||||
|
||||
const SORT_LABELS: Record<LibraryAgentSortEnum, string> = {
|
||||
[LibraryAgentSortEnum.CREATED_AT]: "Creation Date",
|
||||
[LibraryAgentSortEnum.UPDATED_AT]: "Last Modified",
|
||||
[LibraryAgentSortEnum.LAST_EXECUTION]: "Last Run",
|
||||
};
|
||||
|
||||
export default function LibrarySortMenu(): React.ReactNode {
|
||||
const api = useBackendAPI();
|
||||
const { setAgentLoading, setAgents, setLibrarySort, searchTerm, librarySort } =
|
||||
useLibraryPageContext();
|
||||
|
||||
const handleSortChange = async (value: LibraryAgentSortEnum) => {
|
||||
setLibrarySort(value);
|
||||
setAgentLoading(true);
|
||||
@@ -28,18 +36,10 @@ export default function LibrarySortMenu(): React.ReactNode {
|
||||
setAgentLoading(false);
|
||||
};
|
||||
|
||||
const getPlaceholderText = () => {
|
||||
switch (librarySort) {
|
||||
case LibraryAgentSortEnum.CREATED_AT:
|
||||
return "Creation Date";
|
||||
case LibraryAgentSortEnum.UPDATED_AT:
|
||||
return "Last Modified";
|
||||
case LibraryAgentSortEnum.LAST_EXECUTION:
|
||||
return "Last Run";
|
||||
default:
|
||||
return "Last Modified";
|
||||
}
|
||||
};
|
||||
const placeholderText = useMemo(
|
||||
() => SORT_LABELS[librarySort] || SORT_LABELS[LibraryAgentSortEnum.UPDATED_AT],
|
||||
[librarySort]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="flex items-center">
|
||||
@@ -47,19 +47,15 @@ export default function LibrarySortMenu(): React.ReactNode {
|
||||
<Select onValueChange={handleSortChange} defaultValue={librarySort}>
|
||||
<SelectTrigger className="ml-1 w-fit space-x-1 border-none px-0 text-base underline underline-offset-4 shadow-none">
|
||||
<ArrowDownNarrowWideIcon className="h-4 w-4 sm:hidden" />
|
||||
<SelectValue placeholder={getPlaceholderText()} />
|
||||
<SelectValue placeholder={placeholderText} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectItem value={LibraryAgentSortEnum.CREATED_AT}>
|
||||
Creation Date
|
||||
</SelectItem>
|
||||
<SelectItem value={LibraryAgentSortEnum.UPDATED_AT}>
|
||||
Last Modified
|
||||
</SelectItem>
|
||||
<SelectItem value={LibraryAgentSortEnum.LAST_EXECUTION}>
|
||||
Last Run
|
||||
</SelectItem>
|
||||
{Object.entries(SORT_LABELS).map(([value, label]) => (
|
||||
<SelectItem key={value} value={value}>
|
||||
{label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
Reference in New Issue
Block a user