Skip to content
Open
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
107 changes: 53 additions & 54 deletions apps/api/src/trpc/routers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,63 +62,62 @@ export const searchRouter = createTRPCRouter({
.query(async ({ input, ctx: { db, teamId } }) => {
const { q, transactionId, limit = 30 } = input;

const [inboxResults, invoiceResults] = await Promise.all([
getInboxSearch(db, {
teamId: teamId!,
q: q ?? undefined,
transactionId: transactionId ?? undefined,
limit: limit,
}),
getInvoices(db, {
teamId: teamId!,
q: q ?? undefined,
statuses: ["unpaid", "overdue", "paid"],
pageSize: limit,
sort: null,
}),
]);
const inboxResults = await getInboxSearch(db, {
teamId: teamId!,
q: q ?? undefined,
transactionId: transactionId ?? undefined,
limit,
minConfidence: !q && transactionId ? 0.9 : undefined,
});

const inboxItems = inboxResults.map((item) => ({
type: "inbox" as const,
id: item!.id,
fileName: item!.fileName ?? null,
filePath: item!.filePath ?? [],
displayName: item!.displayName ?? null,
amount: item!.amount ?? null,
currency: item!.currency ?? null,
contentType: item!.contentType ?? null,
date: item!.date ?? null,
size: item!.size ?? null,
description: item!.description ?? null,
status: item!.status ?? null,
website: item!.website ?? null,
baseAmount: item!.baseAmount ?? null,
baseCurrency: item!.baseCurrency ?? null,
taxAmount: item!.taxAmount ?? null,
taxRate: item!.taxRate ?? null,
taxType: item!.taxType ?? null,
createdAt: item!.createdAt,
}));

// Transform inbox results
const inboxItems =
inboxResults.map((item) => ({
type: "inbox" as const,
id: item.id,
fileName: item.fileName ?? null,
filePath: item.filePath ?? [],
displayName: item.displayName ?? null,
amount: item.amount ?? null,
currency: item.currency ?? null,
contentType: item.contentType ?? null,
date: item.date ?? null,
size: item.size ?? null,
description: item.description ?? null,
status: item.status ?? null,
website: item.website ?? null,
baseAmount: item.baseAmount ?? null,
baseCurrency: item.baseCurrency ?? null,
taxAmount: item.taxAmount ?? null,
taxRate: item.taxRate ?? null,
taxType: item.taxType ?? null,
createdAt: item.createdAt,
})) ?? [];
if (!q && transactionId) {
return inboxItems;
}

const invoiceResults = await getInvoices(db, {
teamId: teamId!,
q: q ?? undefined,
statuses: ["unpaid", "overdue", "paid"],
pageSize: limit,
sort: null,
});

// Transform invoice results
const invoices =
invoiceResults.data.map((invoice) => ({
type: "invoice" as const,
id: invoice.id,
invoiceNumber: invoice.invoiceNumber ?? null,
customerName: invoice.customerName ?? null,
amount: invoice.amount ?? null,
currency: invoice.currency ?? null,
filePath: invoice.filePath ?? [],
dueDate: invoice.dueDate ?? null,
status: invoice.status,
size: invoice.fileSize ?? null,
createdAt: invoice.createdAt,
})) ?? [];
const invoices = invoiceResults.data.map((invoice) => ({
type: "invoice" as const,
id: invoice.id,
invoiceNumber: invoice.invoiceNumber ?? null,
customerName: invoice.customerName ?? null,
amount: invoice.amount ?? null,
currency: invoice.currency ?? null,
filePath: invoice.filePath ?? [],
dueDate: invoice.dueDate ?? null,
status: invoice.status,
size: invoice.fileSize ?? null,
createdAt: invoice.createdAt,
}));

// Combine and return results
return [...inboxItems, ...invoices];
}),
});
15 changes: 9 additions & 6 deletions apps/dashboard/src/components/select-attachment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ export function SelectAttachment({
}: Props) {
const [debouncedValue, setDebouncedValue] = useDebounceValue("", 200);
const [isOpen, setIsOpen] = useState(false);
const [hasFocused, setHasFocused] = useState(false);
const { data: user } = useUserQuery();
const { setParams } = useDocumentParams();

const trpc = useTRPC();

// Only fetch suggestions when user is actively searching (not just on focus)
const isSearching = debouncedValue.length > 0;

const { data: items, isLoading } = useQuery({
...trpc.search.attachments.queryOptions({
q: debouncedValue.length > 0 ? debouncedValue : undefined,
q: isSearching ? debouncedValue : undefined,
transactionId,
limit: debouncedValue.length > 0 ? 30 : 3,
limit: isSearching ? 30 : 1,
}),
enabled: Boolean(debouncedValue.length > 0 || transactionId), // Enable for search OR suggestions
enabled: Boolean(isSearching || (transactionId && hasFocused)),
});

const handleOnSelect = (item: Attachment) => {
Expand Down Expand Up @@ -104,7 +106,7 @@ export function SelectAttachment({
}

const showBestMatch =
!!transactionId && index === 0 && items?.length > 1;
!!transactionId && index === 0 && items.length > 1;

return {
id: item.id,
Expand Down Expand Up @@ -166,7 +168,8 @@ export function SelectAttachment({
: [];

const handleFocus = () => {
if (!isOpen && !debouncedValue) {
setHasFocused(true);
if (!isOpen) {
setIsOpen(true);
}
};
Expand Down
Loading
Loading