Skip to content

Commit

Permalink
updated the code to reuse the DeleteTagConfirmationDialog to improve …
Browse files Browse the repository at this point in the history
…performance and fix the tag deletion
  • Loading branch information
kamtschatka committed Oct 13, 2024
1 parent 60c0460 commit 878508a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
23 changes: 23 additions & 0 deletions apps/web/components/dashboard/tags/AllTagsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ArrowDownAZ, Combine } from "lucide-react";
import type { ZGetTagResponse } from "@hoarder/shared/types/tags";
import { useDeleteUnusedTags } from "@hoarder/shared-react/hooks/tags";

import DeleteTagConfirmationDialog from "./DeleteTagConfirmationDialog";
import { TagPill } from "./TagPill";

function DeleteAllUnusedTags({ numUnusedTags }: { numUnusedTags: number }) {
Expand Down Expand Up @@ -66,6 +67,11 @@ const byUsageSorter = (a: ZGetTagResponse, b: ZGetTagResponse) => {
const byNameSorter = (a: ZGetTagResponse, b: ZGetTagResponse) =>
a.name.localeCompare(b.name, undefined, { sensitivity: "base" });

interface Tag {
id: string;
name: string;
}

export default function AllTagsView({
initialData,
}: {
Expand All @@ -74,6 +80,14 @@ export default function AllTagsView({
const [draggingEnabled, setDraggingEnabled] = React.useState(false);
const [sortByName, setSortByName] = React.useState(false);

const [isDialogOpen, setIsDialogOpen] = React.useState(false);
const [selectedTag, setSelectedTag] = React.useState<Tag | null>(null);

const handleOpenDialog = (tag: Tag) => {
setSelectedTag(tag);
setIsDialogOpen(true);
};

function toggleSortByName(): void {
setSortByName(!sortByName);
}
Expand Down Expand Up @@ -104,8 +118,17 @@ export default function AllTagsView({
name={t.name}
count={t.count}
isDraggable={draggingEnabled}
onOpenDialog={handleOpenDialog}
/>
))}

{selectedTag && (
<DeleteTagConfirmationDialog
tag={selectedTag}
open={isDialogOpen}
setOpen={setIsDialogOpen}
/>
)}
</div>
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import { useDeleteTag } from "@hoarder/shared-react/hooks/tags";

export default function DeleteTagConfirmationDialog({
tag,
children,
open,
setOpen,
}: {
tag: { id: string; name: string };
children?: React.ReactNode;
open?: boolean;
setOpen?: (v: boolean) => void;
}) {
Expand Down Expand Up @@ -55,8 +53,6 @@ export default function DeleteTagConfirmationDialog({
Delete
</ActionButton>
)}
>
{children}
</ActionConfirmingDialog>
></ActionConfirmingDialog>
);
}
21 changes: 10 additions & 11 deletions apps/web/components/dashboard/tags/TagPill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import Draggable from "react-draggable";

import { useMergeTag } from "@hoarder/shared-react/hooks/tags";

import DeleteTagConfirmationDialog from "./DeleteTagConfirmationDialog";

export function TagPill({
id,
name,
count,
isDraggable,
onOpenDialog,
}: {
id: string;
name: string;
count: number;
isDraggable: boolean;
onOpenDialog: (tag: { id: string; name: string }) => void;
}) {
const [isHovered, setIsHovered] = useState(false);

Expand Down Expand Up @@ -86,15 +86,14 @@ export function TagPill({
</Link>

{isHovered && (
<DeleteTagConfirmationDialog tag={{ name, id }}>
<Button
size="none"
variant="secondary"
className="-translate-1/2 absolute -right-1 -top-1 hidden rounded-full group-hover:block"
>
<X className="size-3" />
</Button>
</DeleteTagConfirmationDialog>
<Button
size="none"
variant="secondary"
className="-translate-1/2 absolute -right-1 -top-1 hidden rounded-full group-hover:block"
onClick={() => onOpenDialog({ id, name })}
>
<X className="size-3" />
</Button>
)}
</div>
);
Expand Down

0 comments on commit 878508a

Please sign in to comment.