Add 'apps/web/' from commit '1e7174fa604b11e7c3983cd8ad01c596f6e77e96'
git-subtree-dir: apps/web git-subtree-mainline:068a51b46bgit-subtree-split:1e7174fa60
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { useMemo } from "react";
|
||||
import Fuse from "fuse.js";
|
||||
import { api } from "~/trpc/react";
|
||||
|
||||
export interface LineItemSuggestion {
|
||||
description: string;
|
||||
hours: number;
|
||||
rate: number;
|
||||
}
|
||||
|
||||
export function useLineItemSuggestions() {
|
||||
const { data: history = [] } = api.invoices.getLineItemHistory.useQuery(undefined, {
|
||||
staleTime: 5 * 60 * 1000,
|
||||
});
|
||||
|
||||
const fuse = useMemo(
|
||||
() =>
|
||||
new Fuse(history, {
|
||||
keys: ["description"],
|
||||
threshold: 0.4,
|
||||
minMatchCharLength: 2,
|
||||
}),
|
||||
[history],
|
||||
);
|
||||
|
||||
function search(query: string): LineItemSuggestion[] {
|
||||
if (!query || query.length < 2) return history.slice(0, 6);
|
||||
return fuse.search(query, { limit: 6 }).map((r) => r.item);
|
||||
}
|
||||
|
||||
return { search, hasHistory: history.length > 0 };
|
||||
}
|
||||
Reference in New Issue
Block a user