Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button } from "@superset/ui/button";
import { Input } from "@superset/ui/input";
import { Tabs, TabsList, TabsTrigger } from "@superset/ui/tabs";
import { useRef } from "react";
import { useEffect, useRef, useState } from "react";
import { HiOutlineMagnifyingGlass, HiXMark } from "react-icons/hi2";
import { LuPlay } from "react-icons/lu";
import { useAppHotkey } from "renderer/stores/hotkeys";
Expand Down Expand Up @@ -54,6 +54,23 @@ export function TasksTopBar({
onClearSelection,
}: TasksTopBarProps) {
const searchInputRef = useRef<HTMLInputElement>(null);
const [localSearch, setLocalSearch] = useState(searchQuery);
const debounceRef = useRef<ReturnType<typeof setTimeout>>(undefined);

// Sync local state when parent clears the search
useEffect(() => {
if (searchQuery === "") setLocalSearch("");
}, [searchQuery]);
Comment on lines +61 to +63

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Stale debounce fires after parent clears the search.

When the parent resets searchQuery to "" (e.g., on tab change), localSearch is synced to "", but a pending debounced onSearchChange(value) from a prior keystroke can still fire ~150 ms later, re-populating the search with the old value.

Cancel the timer alongside the sync:

Proposed fix
 	useEffect(() => {
-		if (searchQuery === "") setLocalSearch("");
+		if (searchQuery === "") {
+			clearTimeout(debounceRef.current);
+			setLocalSearch("");
+		}
 	}, [searchQuery]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
if (searchQuery === "") setLocalSearch("");
}, [searchQuery]);
useEffect(() => {
if (searchQuery === "") {
clearTimeout(debounceRef.current);
setLocalSearch("");
}
}, [searchQuery]);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@apps/desktop/src/renderer/routes/_authenticated/_dashboard/tasks/components/TasksView/components/TasksTopBar/TasksTopBar.tsx`
around lines 61 - 63, The effect that syncs localSearch to an empty parent
searchQuery must also cancel any pending debounced update so a stale
onSearchChange(oldValue) doesn't fire; inside the useEffect that checks if
(searchQuery === "") { setLocalSearch("") } also clear the debounce (e.g.,
clearTimeout(searchDebounceRef.current) or call
searchDebounceRef.current.cancel() if using lodash/debounce) and reset the ref,
ensuring no pending onSearchChange(value) will run after the parent clears the
query.


const handleSearchInput = (value: string) => {
setLocalSearch(value);
clearTimeout(debounceRef.current);
debounceRef.current = setTimeout(() => onSearchChange(value), 150);
};

useEffect(() => {
return () => clearTimeout(debounceRef.current);
}, []);

useAppHotkey(
"FOCUS_TASK_SEARCH",
Expand Down Expand Up @@ -129,10 +146,12 @@ export function TasksTopBar({
ref={searchInputRef}
type="text"
placeholder="Search tasks..."
value={searchQuery}
onChange={(e) => onSearchChange(e.target.value)}
value={localSearch}
onChange={(e) => handleSearchInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Escape") {
clearTimeout(debounceRef.current);
setLocalSearch("");
onSearchChange("");
searchInputRef.current?.blur();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fuse from "fuse.js";
import { useCallback, useMemo } from "react";
import { useCallback, useRef } from "react";

interface SearchableTask {
id: string;
Expand All @@ -16,52 +16,56 @@ interface SearchResult<T extends SearchableTask> {
}

export function useHybridSearch<T extends SearchableTask>(tasks: T[]) {
const exactFuse = useMemo(
() =>
new Fuse(tasks, {
keys: [
{ name: "slug", weight: 2 },
{ name: "labels", weight: 1 },
],
threshold: 0,
includeScore: true,
ignoreLocation: true,
useExtendedSearch: false,
}),
[tasks],
);
const tasksRef = useRef(tasks);
tasksRef.current = tasks;

const fuzzyFuse = useMemo(
() =>
new Fuse(tasks, {
keys: [
{ name: "title", weight: 2 },
{ name: "description", weight: 1 },
],
threshold: 0.3,
includeScore: true,
ignoreLocation: true,
useExtendedSearch: false,
}),
[tasks],
);
const exactFuseRef = useRef<Fuse<T> | null>(null);
const fuzzyFuseRef = useRef<Fuse<T> | null>(null);
const indexedTasksRef = useRef<T[] | null>(null);

const ensureIndex = useCallback(() => {
if (indexedTasksRef.current === tasksRef.current) return;
exactFuseRef.current = new Fuse(tasksRef.current, {
keys: [
{ name: "slug", weight: 2 },
{ name: "labels", weight: 1 },
],
threshold: 0,
includeScore: true,
ignoreLocation: true,
useExtendedSearch: false,
});
fuzzyFuseRef.current = new Fuse(tasksRef.current, {
keys: [
{ name: "title", weight: 2 },
{ name: "description", weight: 1 },
],
threshold: 0.3,
includeScore: true,
ignoreLocation: true,
useExtendedSearch: false,
});
indexedTasksRef.current = tasksRef.current;
}, []);

const search = useCallback(
(query: string): SearchResult<T>[] => {
if (!query.trim()) {
return tasks.map((item) => ({
return tasksRef.current.map((item) => ({
item,
score: 1,
matchType: "exact" as const,
}));
}

const exactMatches = exactFuse.search(query);
ensureIndex();

const exactMatches = exactFuseRef.current?.search(query) ?? [];
const exactIds = new Set(exactMatches.map((m) => m.item.id));

const fuzzyMatches = fuzzyFuse
.search(query)
.filter((m) => !exactIds.has(m.item.id));
const fuzzyMatches = (fuzzyFuseRef.current?.search(query) ?? []).filter(
(m) => !exactIds.has(m.item.id),
);

return [
...exactMatches.map((m) => ({
Expand All @@ -76,7 +80,7 @@ export function useHybridSearch<T extends SearchableTask>(tasks: T[]) {
})),
];
},
[exactFuse, fuzzyFuse, tasks],
[ensureIndex],
);

return { search };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
import type { SelectUser } from "@superset/db/schema";
import { Avatar } from "@superset/ui/atoms/Avatar";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@superset/ui/dropdown-menu";
import { useLiveQuery } from "@tanstack/react-db";
import type { CellContext } from "@tanstack/react-table";
import { useMemo, useState } from "react";
import { useState } from "react";
import { HiOutlineUserCircle } from "react-icons/hi2";
import { useCollections } from "renderer/routes/_authenticated/providers/CollectionsProvider";
import type { TaskWithStatus } from "../../useTasksTable";

interface AssigneeCellProps {
info: CellContext<TaskWithStatus, string | null>;
users: SelectUser[];
}

export function AssigneeCell({ info }: AssigneeCellProps) {
export function AssigneeCell({ info, users }: AssigneeCellProps) {
const collections = useCollections();
const [open, setOpen] = useState(false);

const task = info.row.original;
const assigneeId = info.getValue();

const { data: allUsers } = useLiveQuery(
(q) => q.from({ users: collections.users }),
[collections],
);

const users = useMemo(() => allUsers || [], [allUsers]);

const handleSelectUser = (userId: string | null) => {
if (userId === assigneeId) {
setOpen(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ export function useTasksTable({
const rowSelection = useRowSelectionStore((s) => s.rowSelection);
const setRowSelection = useRowSelectionStore((s) => s.setRowSelection);

const { data: allUsers } = useLiveQuery(
(q) => q.from({ users: collections.users }),
[collections],
);

const users = useMemo(() => allUsers ?? [], [allUsers]);

const { data: allData, isLoading } = useLiveQuery(
(q) =>
q
Expand Down Expand Up @@ -299,7 +306,7 @@ export function useTasksTable({
},
cell: (info) => {
if (info.cell.getIsPlaceholder()) return null;
return <AssigneeCell info={info} />;
return <AssigneeCell info={info} users={users} />;
},
}),

Expand All @@ -317,7 +324,7 @@ export function useTasksTable({
},
}),
],
[],
[users],
);

const table = useReactTable({
Expand Down
Loading