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
2 changes: 2 additions & 0 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@
},
"expense_details": "Expense details",
"no_activity": "No activities yet",
"no_search_results": "No expenses match your search",
"search_expenses_placeholder": "Search expenses...",
"not_involved": "Not involved",
"not_set": "Not set",
"on": "on",
Expand Down
65 changes: 59 additions & 6 deletions src/pages/activity.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { SplitType } from '@prisma/client';
import { RefreshCcwDot, Search, X } from 'lucide-react';
import { type User } from 'next-auth';
import { type TFunction } from 'next-i18next';
import Head from 'next/head';
import Link from 'next/link';
import React from 'react';
import MainLayout from '~/components/Layout/MainLayout';
import { EntityAvatar } from '~/components/ui/avatar';
import { Button } from '~/components/ui/button';
import { Input } from '~/components/ui/input';
import { useTranslationWithUtils } from '~/hooks/useTranslationWithUtils';
import { type NextPageWithUser } from '~/types';
import { api } from '~/utils/api';
import { getCurrencyHelpers } from '~/utils/numbers';
import { type TFunction } from 'next-i18next';
import { useTranslationWithUtils } from '~/hooks/useTranslationWithUtils';
import { withI18nStaticProps } from '~/utils/i18n/server';
import { RefreshCcwDot } from 'lucide-react';
import { Button } from '~/components/ui/button';
import React from 'react';

function getPaymentString(
user: User,
Expand Down Expand Up @@ -52,6 +53,7 @@ function getPaymentString(
const ActivityPage: NextPageWithUser = ({ user }) => {
const { displayName, t, toUIDate, i18n } = useTranslationWithUtils();
const expensesQuery = api.expense.getAllExpenses.useQuery();
const [search, setSearch] = React.useState('');

const actions = React.useMemo(
() => (
Expand All @@ -64,6 +66,32 @@ const ActivityPage: NextPageWithUser = ({ user }) => {
[],
);

const normalizedSearch = search.trim().toLowerCase();

const filteredExpenses = React.useMemo(() => {
if (!expensesQuery.data) {
return expensesQuery.data;
}
if (!normalizedSearch) {
return expensesQuery.data;
}

return expensesQuery.data.filter((e) => {
const haystack = [
e.expense.name,
e.expense.category,
e.expense.group?.name,
e.expense.paidByUser.name,
e.expense.paidByUser.email,
]
.filter(Boolean)
.join(' ')
.toLowerCase();

return haystack.includes(normalizedSearch);
});
}, [expensesQuery.data, normalizedSearch]);

return (
<>
<Head>
Expand All @@ -76,10 +104,35 @@ const ActivityPage: NextPageWithUser = ({ user }) => {
loading={expensesQuery.isPending}
>
<div className="flex flex-col gap-4">
{!!expensesQuery.data?.length && (
<Input
type="text"
value={search}
onChange={(ev) => setSearch(ev.target.value)}
placeholder={t('ui.search_expenses_placeholder')}
rightIcon={
search ? (
<button
type="button"
onClick={() => setSearch('')}
aria-label={t('actions.close') ?? 'Clear'}
>
<X className="text-muted-foreground size-4" />
</button>
) : (
<Search className="text-muted-foreground size-4" />
)
}
/>
)}

{!expensesQuery.data?.length ? (
<div className="mt-[30vh] text-center text-gray-400">{t('ui.no_activity')}</div>
) : null}
{expensesQuery.data?.map((e) => {
{!!expensesQuery.data?.length && !filteredExpenses?.length ? (
<div className="mt-[20vh] text-center text-gray-400">{t('ui.no_search_results')}</div>
) : null}
{filteredExpenses?.map((e) => {
const { toUIString } = getCurrencyHelpers({
locale: i18n.language,
currency: e.expense.currency,
Expand Down
6 changes: 6 additions & 0 deletions src/server/api/routers/expense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ export const expenseRouter = createTRPCRouter({
id: true,
},
},
group: {
select: {
id: true,
name: true,
},
},
},
},
},
Expand Down
Loading