Skip to content

Commit

Permalink
Overwrite existing saved search (#14792)
Browse files Browse the repository at this point in the history
* Overwrite existing saved search

* simplify
  • Loading branch information
hawkeye217 authored Nov 4, 2024
1 parent 553676a commit ac76276
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
22 changes: 15 additions & 7 deletions web/src/components/input/InputWithTags.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useState, useRef, useEffect, useCallback } from "react";
import React, {
useState,
useRef,
useEffect,
useCallback,
useMemo,
} from "react";
import {
LuX,
LuFilter,
Expand Down Expand Up @@ -88,6 +94,11 @@ export default function InputWithTags({
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
const [searchToDelete, setSearchToDelete] = useState<string | null>(null);

const searchHistoryNames = useMemo(
() => searchHistory?.map((item) => item.name) ?? [],
[searchHistory],
);

const handleSetSearchHistory = useCallback(() => {
setIsSaveDialogOpen(true);
}, []);
Expand All @@ -96,12 +107,8 @@ export default function InputWithTags({
(name: string) => {
if (searchHistoryLoaded) {
setSearchHistory([
...(searchHistory ?? []),
{
name: name,
search: search,
filter: filters,
},
...(searchHistory ?? []).filter((item) => item.name !== name),
{ name, search, filter: filters },
]);
}
},
Expand Down Expand Up @@ -835,6 +842,7 @@ export default function InputWithTags({
</CommandList>
</Command>
<SaveSearchDialog
existingNames={searchHistoryNames}
isOpen={isSaveDialogOpen}
onClose={() => setIsSaveDialogOpen(false)}
onSave={handleSaveSearch}
Expand Down
15 changes: 14 additions & 1 deletion web/src/components/input/SaveSearchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import {

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useState } from "react";
import { useMemo, useState } from "react";
import { isMobile } from "react-device-detect";
import { toast } from "sonner";

type SaveSearchDialogProps = {
existingNames: string[];
isOpen: boolean;
onClose: () => void;
onSave: (name: string) => void;
};

export function SaveSearchDialog({
existingNames,
isOpen,
onClose,
onSave,
Expand All @@ -37,6 +39,11 @@ export function SaveSearchDialog({
}
};

const overwrite = useMemo(
() => existingNames.includes(searchName),
[existingNames, searchName],
);

return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent
Expand All @@ -58,6 +65,12 @@ export function SaveSearchDialog({
onChange={(e) => setSearchName(e.target.value)}
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>
<Button aria-label="Cancel" onClick={onClose}>
Cancel
Expand Down

0 comments on commit ac76276

Please sign in to comment.