Skip to content

Commit

Permalink
fix(web): Add optimistic rendering for the tags editor
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Sep 1, 2024
1 parent 67729c1 commit 300f3c5
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions apps/web/components/dashboard/bookmarks/TagsEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ActionMeta } from "react-select";
import { useState } from "react";
import { useClientConfig } from "@/lib/clientConfig";
import { api } from "@/lib/trpc";
import { cn } from "@/lib/utils";
Expand All @@ -17,7 +18,7 @@ interface EditableTag {
}

export function TagsEditor({
tags,
tags: _tags,
onAttach,
onDetach,
}: {
Expand All @@ -27,6 +28,8 @@ export function TagsEditor({
}) {
const demoMode = !!useClientConfig().demoMode;

const [optimisticTags, setOptimisticTags] = useState<ZBookmarkTags[]>(_tags);

const { data: existingTags, isLoading: isExistingTagsLoading } =
api.tags.list.useQuery();

Expand All @@ -40,6 +43,9 @@ export function TagsEditor({
case "pop-value":
case "remove-value": {
if (actionMeta.removedValue.value) {
setOptimisticTags((prev) =>
prev.filter((t) => t.id != actionMeta.removedValue.value),
);
onDetach({
tagId: actionMeta.removedValue.value,
tagName: actionMeta.removedValue.label,
Expand All @@ -48,11 +54,27 @@ export function TagsEditor({
break;
}
case "create-option": {
setOptimisticTags((prev) => [
...prev,
{
id: "",
name: actionMeta.option.label,
attachedBy: "human" as const,
},
]);
onAttach({ tagName: actionMeta.option.label });
break;
}
case "select-option": {
if (actionMeta.option) {
setOptimisticTags((prev) => [
...prev,
{
id: actionMeta.option?.value ?? "",
name: actionMeta.option!.label,
attachedBy: "human" as const,
},
]);
onAttach({
tagName: actionMeta.option.label,
tagId: actionMeta.option?.value,
Expand All @@ -74,7 +96,7 @@ export function TagsEditor({
attachedBy: "human" as const,
})) ?? []
}
value={tags.map((t) => ({
value={optimisticTags.map((t) => ({
label: t.name,
value: t.id,
attachedBy: t.attachedBy,
Expand Down

0 comments on commit 300f3c5

Please sign in to comment.