Overwrite existing saved search (#14792)

* Overwrite existing saved search

* simplify
This commit is contained in:
Josh Hawkins 2024-11-04 13:50:05 -06:00 committed by GitHub
parent 553676aade
commit ac762762c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 8 deletions

View File

@ -1,4 +1,10 @@
import React, { useState, useRef, useEffect, useCallback } from "react"; import React, {
useState,
useRef,
useEffect,
useCallback,
useMemo,
} from "react";
import { import {
LuX, LuX,
LuFilter, LuFilter,
@ -88,6 +94,11 @@ export default function InputWithTags({
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
const [searchToDelete, setSearchToDelete] = useState<string | null>(null); const [searchToDelete, setSearchToDelete] = useState<string | null>(null);
const searchHistoryNames = useMemo(
() => searchHistory?.map((item) => item.name) ?? [],
[searchHistory],
);
const handleSetSearchHistory = useCallback(() => { const handleSetSearchHistory = useCallback(() => {
setIsSaveDialogOpen(true); setIsSaveDialogOpen(true);
}, []); }, []);
@ -96,12 +107,8 @@ export default function InputWithTags({
(name: string) => { (name: string) => {
if (searchHistoryLoaded) { if (searchHistoryLoaded) {
setSearchHistory([ setSearchHistory([
...(searchHistory ?? []), ...(searchHistory ?? []).filter((item) => item.name !== name),
{ { name, search, filter: filters },
name: name,
search: search,
filter: filters,
},
]); ]);
} }
}, },
@ -835,6 +842,7 @@ export default function InputWithTags({
</CommandList> </CommandList>
</Command> </Command>
<SaveSearchDialog <SaveSearchDialog
existingNames={searchHistoryNames}
isOpen={isSaveDialogOpen} isOpen={isSaveDialogOpen}
onClose={() => setIsSaveDialogOpen(false)} onClose={() => setIsSaveDialogOpen(false)}
onSave={handleSaveSearch} onSave={handleSaveSearch}

View File

@ -9,17 +9,19 @@ import {
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { useState } from "react"; import { useMemo, useState } from "react";
import { isMobile } from "react-device-detect"; import { isMobile } from "react-device-detect";
import { toast } from "sonner"; import { toast } from "sonner";
type SaveSearchDialogProps = { type SaveSearchDialogProps = {
existingNames: string[];
isOpen: boolean; isOpen: boolean;
onClose: () => void; onClose: () => void;
onSave: (name: string) => void; onSave: (name: string) => void;
}; };
export function SaveSearchDialog({ export function SaveSearchDialog({
existingNames,
isOpen, isOpen,
onClose, onClose,
onSave, onSave,
@ -37,6 +39,11 @@ export function SaveSearchDialog({
} }
}; };
const overwrite = useMemo(
() => existingNames.includes(searchName),
[existingNames, searchName],
);
return ( return (
<Dialog open={isOpen} onOpenChange={onClose}> <Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent <DialogContent
@ -58,6 +65,12 @@ export function SaveSearchDialog({
onChange={(e) => setSearchName(e.target.value)} onChange={(e) => setSearchName(e.target.value)}
placeholder="Enter a name for your search" placeholder="Enter a name for your search"
/> />
{overwrite && (
<div className="ml-1 text-sm text-danger">
{searchName} already exists. Saving will overwrite the existing
value.
</div>
)}
<DialogFooter> <DialogFooter>
<Button aria-label="Cancel" onClick={onClose}> <Button aria-label="Cancel" onClick={onClose}>
Cancel Cancel