Skip to content

Add fuzzy matching to the tasks filter #1960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2025
Merged
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
38 changes: 0 additions & 38 deletions apps/webapp/app/hooks/useFilterTasks.ts

This file was deleted.

61 changes: 61 additions & 0 deletions apps/webapp/app/hooks/useFuzzyFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useMemo, useState } from "react";
import { matchSorter } from "match-sorter";

/**
* A hook that provides fuzzy filtering functionality for a list of objects.
* Uses match-sorter to perform the filtering across multiple object properties and
* consistently order the results by score.
*
* @param params - The parameters object
* @param params.items - Array of objects to filter
* @param params.keys - Array of object keys to perform the fuzzy search on
* @returns An object containing:
* - filterText: The current filter text
* - setFilterText: Function to update the filter text
* - filteredItems: The filtered array of items based on the current filter text
*
* @example
* ```tsx
* const users = [{ name: "John", email: "[email protected]" }];
* const { filterText, setFilterText, filteredItems } = useFuzzyFilter({
* items: users,
* keys: ["name", "email"]
* });
* ```
*/
export function useFuzzyFilter<T extends Object>({
items,
keys,
}: {
items: T[];
keys: Extract<keyof T, string>[];
}) {
const [filterText, setFilterText] = useState("");

const filteredItems = useMemo<T[]>(() => {
const filterTerms = filterText
.trim()
.split(" ")
.map((term) => term.trim())
.filter((term) => term !== "");

if (filterTerms.length === 0) {
return items;
}

// sort by the score of the first term
return filterTerms.reduceRight(
(results, term) =>
matchSorter(results, term, {
keys,
}),
items
);
}, [items, filterText]);

return {
filterText,
setFilterText,
filteredItems,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ import {
} from "~/components/runs/v3/TaskTriggerSource";
import { useEnvironment } from "~/hooks/useEnvironment";
import { useEventSource } from "~/hooks/useEventSource";
import { useFuzzyFilter } from "~/hooks/useFuzzyFilter";
import { useOrganization } from "~/hooks/useOrganizations";
import { useProject } from "~/hooks/useProject";
import { useTextFilter } from "~/hooks/useTextFilter";
import { findProjectBySlug } from "~/models/project.server";
import { findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
import {
Expand Down Expand Up @@ -169,23 +169,9 @@ export default function Page() {
const environment = useEnvironment();
const { tasks, activity, runningStats, durations, usefulLinksPreference } =
useTypedLoaderData<typeof loader>();
const { filterText, setFilterText, filteredItems } = useTextFilter<TaskListItem>({
const { filterText, setFilterText, filteredItems } = useFuzzyFilter<TaskListItem>({
items: tasks,
filter: (task, text) => {
if (task.slug.toLowerCase().includes(text.toLowerCase())) {
return true;
}

if (task.filePath.toLowerCase().includes(text.toLowerCase())) {
return true;
}

if (task.triggerSource === "SCHEDULED" && "scheduled".includes(text.toLowerCase())) {
return true;
}

return false;
},
keys: ["slug", "filePath", "triggerSource"],
});

const hasTasks = tasks.length > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "~/components/primitives/Table";
import { TaskTriggerSourceIcon } from "~/components/runs/v3/TaskTriggerSource";
import { useEnvironment } from "~/hooks/useEnvironment";
import { useFilterTasks } from "~/hooks/useFilterTasks";
import { useFuzzyFilter } from "~/hooks/useFuzzyFilter";
import { useLinkStatus } from "~/hooks/useLinkStatus";
import { useOrganization } from "~/hooks/useOrganizations";
import { useProject } from "~/hooks/useProject";
Expand Down Expand Up @@ -120,7 +120,10 @@ function TaskSelector({
tasks: TaskListItem[];
activeTaskIdentifier?: string;
}) {
const { filterText, setFilterText, filteredItems } = useFilterTasks<TaskListItem>({ tasks });
const { filterText, setFilterText, filteredItems } = useFuzzyFilter<TaskListItem>({
items: tasks,
keys: ["taskIdentifier", "friendlyId", "id", "filePath", "triggerSource"],
});
const hasTaskInEnvironment = activeTaskIdentifier
? tasks.some((t) => t.taskIdentifier === activeTaskIdentifier)
: undefined;
Expand Down
Loading