diff --git a/apps/client/src/features/base/components/views/view-sort-config.tsx b/apps/client/src/features/base/components/views/view-sort-config.tsx index 5043213e..0a2665df 100644 --- a/apps/client/src/features/base/components/views/view-sort-config.tsx +++ b/apps/client/src/features/base/components/views/view-sort-config.tsx @@ -1,4 +1,4 @@ -import { useCallback } from "react"; +import { useCallback, useEffect, useState } from "react"; import { Popover, Stack, @@ -7,8 +7,9 @@ import { ActionIcon, Text, UnstyledButton, + Button, } from "@mantine/core"; -import { IconPlus, IconTrash, IconSortAscending } from "@tabler/icons-react"; +import { IconPlus, IconTrash } from "@tabler/icons-react"; import { IBaseProperty, ViewSortConfig, @@ -33,6 +34,12 @@ export function ViewSortConfigPopover({ children, }: ViewSortConfigProps) { const { t } = useTranslation(); + const [draft, setDraft] = useState(null); + + // Discard any half-configured draft when the popover closes. + useEffect(() => { + if (!opened) setDraft(null); + }, [opened]); const propertyOptions = properties.map((p) => ({ value: p.id, @@ -44,12 +51,22 @@ export function ViewSortConfigPopover({ { value: "desc", label: t("Descending") }, ]; - const handleAdd = useCallback(() => { + const handleStartDraft = useCallback(() => { const usedIds = new Set(sorts.map((s) => s.propertyId)); const available = properties.find((p) => !usedIds.has(p.id)); if (!available) return; - onChange([...sorts, { propertyId: available.id, direction: "asc" }]); - }, [sorts, properties, onChange]); + setDraft({ propertyId: available.id, direction: "asc" }); + }, [sorts, properties]); + + const handleSaveDraft = useCallback(() => { + if (!draft) return; + onChange([...sorts, draft]); + setDraft(null); + }, [draft, sorts, onChange]); + + const handleCancelDraft = useCallback(() => { + setDraft(null); + }, []); const handleRemove = useCallback( (index: number) => { @@ -82,6 +99,8 @@ export function ViewSortConfigPopover({ [sorts, onChange], ); + const canAddMore = properties.length > sorts.length + (draft ? 1 : 0); + return ( - {sorts.length === 0 && ( + {sorts.length === 0 && !draft && ( {t("No sorts applied")} @@ -132,20 +151,63 @@ export function ViewSortConfigPopover({ ))} - - - {t("Add sort")} - + {draft && ( + + + + val && + setDraft({ + ...draft, + direction: val as "asc" | "desc", + }) + } + w={110} + /> + + + + + + + )} + + {!draft && canAddMore && ( + + + {t("Add sort")} + + )}