Repository navigation
feat: search and filter expenses in groups and with friends #36
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
Open
KMKoushik
wants to merge
1
commit into
main
Choose a base branch
from
hq/work-next-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import React, { useState } from 'react'; | ||
| import { SearchIcon, X } from 'lucide-react'; | ||
| import { cn } from '~/lib/utils'; | ||
| import { Input } from '~/components/ui/input'; | ||
| import { Button } from '~/components/ui/button'; | ||
| import { | ||
| type ExpenseFilter, | ||
| type SearchableExpense, | ||
| filterExpenses, | ||
| getFilterOptions, | ||
| } from '~/utils/expenseSearch'; | ||
|
|
||
| /** Search and category filter state for an expense list, kept on the page while it is open. */ | ||
| export const useExpenseSearch = <T extends SearchableExpense>( | ||
| expenses: T[] | undefined, | ||
| getOtherText?: (expense: T) => (string | null | undefined)[], | ||
| ) => { | ||
| const [filter, setFilter] = useState<ExpenseFilter>({ query: '', category: null }); | ||
|
|
||
| return { | ||
| filter, | ||
| setFilter, | ||
| options: getFilterOptions(expenses ?? []), | ||
| filtered: expenses ? filterExpenses(expenses, filter, getOtherText) : undefined, | ||
| isFiltering: '' !== filter.query.trim() || null !== filter.category, | ||
| }; | ||
| }; | ||
|
|
||
| export const ExpenseSearch: React.FC<{ | ||
| filter: ExpenseFilter; | ||
| onChange: (filter: ExpenseFilter) => void; | ||
| options: { key: string; label: string }[]; | ||
| className?: string; | ||
| }> = ({ filter, onChange, options, className }) => ( | ||
| <div className={cn('flex flex-col gap-2', className)}> | ||
| <div className="relative"> | ||
| <SearchIcon className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-gray-500" /> | ||
| <Input | ||
| type="search" | ||
| inputMode="search" | ||
| placeholder="Search expenses" | ||
| aria-label="Search expenses" | ||
| value={filter.query} | ||
| onChange={(e) => onChange({ ...filter, query: e.target.value })} | ||
| className="h-9 pl-9 pr-9 text-base [&::-webkit-search-cancel-button]:hidden" | ||
| /> | ||
| {filter.query ? ( | ||
| <button | ||
| type="button" | ||
| aria-label="Clear search" | ||
| onClick={() => onChange({ ...filter, query: '' })} | ||
| className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500" | ||
| > | ||
| <X className="h-4 w-4" /> | ||
| </button> | ||
| ) : null} | ||
| </div> | ||
| {1 < options.length ? ( | ||
| <div className="-mx-1 flex gap-2 overflow-x-auto px-1 pb-1"> | ||
| {options.map((option) => { | ||
| const isSelected = option.key === filter.category; | ||
| return ( | ||
| <button | ||
| key={option.key} | ||
| type="button" | ||
| aria-pressed={isSelected} | ||
| onClick={() => onChange({ ...filter, category: isSelected ? null : option.key })} | ||
| className={cn( | ||
| 'shrink-0 rounded-full border px-3 py-1 text-xs', | ||
| isSelected ? 'border-primary bg-primary text-primary-foreground' : 'text-gray-400', | ||
| )} | ||
| > | ||
| {option.label} | ||
| </button> | ||
| ); | ||
| })} | ||
| </div> | ||
| ) : null} | ||
| </div> | ||
| ); | ||
|
|
||
| export const NoMatchingExpenses: React.FC<{ onClear: () => void }> = ({ onClear }) => ( | ||
| <div className="mt-10 flex flex-col items-center gap-2 text-sm text-gray-400"> | ||
| No expenses match your search | ||
| <Button size="sm" variant="secondary" onClick={onClear}> | ||
| Clear search | ||
| </Button> | ||
| </div> | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** Expense categories as picked when adding an expense. `other` stores the group key itself. */ | ||
| export const CATEGORIES = { | ||
| entertainment: { | ||
| name: 'Entertainment', | ||
| items: [ | ||
| { | ||
| games: 'Games', | ||
| movies: 'Movies', | ||
| music: 'Music', | ||
| sports: 'Sports', | ||
| other: 'Entertainment', | ||
| }, | ||
| ], | ||
| }, | ||
| food: { | ||
| name: 'Food & Drinks', | ||
| items: [ | ||
| { | ||
| diningOut: 'Dining Out', | ||
| groceries: 'Groceries', | ||
| liquor: 'Liquor', | ||
| other: 'Food & Drinks', | ||
| }, | ||
| ], | ||
| }, | ||
| home: { | ||
| name: 'Home', | ||
| items: [ | ||
| { | ||
| electronics: 'Electronics', | ||
| furniture: 'Furniture', | ||
| supplies: 'Supplies', | ||
| maintenance: 'Maintenance', | ||
| mortgage: 'Mortgage', | ||
| pets: 'Pets', | ||
| rent: 'Rent', | ||
| services: 'Services', | ||
| other: 'Home', | ||
| }, | ||
| ], | ||
| }, | ||
| life: { | ||
| name: 'Life', | ||
| items: [ | ||
| { | ||
| childcare: 'Childcare', | ||
| clothing: 'Clothing', | ||
| education: 'Education', | ||
| gifts: 'Gifts', | ||
| medical: 'Medical', | ||
| taxes: 'Taxes', | ||
| other: 'Life', | ||
| }, | ||
| ], | ||
| }, | ||
| travel: { | ||
| name: 'Travel', | ||
| items: [ | ||
| { | ||
| bus: 'Bus', | ||
| train: 'Train', | ||
| car: 'Car', | ||
| fuel: 'Fuel', | ||
| parking: 'Parking', | ||
| plane: 'Plane', | ||
| taxi: 'Taxi', | ||
| other: 'Travel', | ||
| }, | ||
| ], | ||
| }, | ||
| utilities: { | ||
| name: 'Utilities', | ||
| items: [ | ||
| { | ||
| cleaning: 'Cleaning', | ||
| electricity: 'Electricity', | ||
| gas: 'Gas', | ||
| internet: 'Internet', | ||
| trash: 'Trash', | ||
| phone: 'Phone', | ||
| water: 'Water', | ||
| other: 'Utilities', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| const CATEGORY_LABELS: Record<string, string> = { | ||
| general: 'General', | ||
| ...Object.fromEntries( | ||
| Object.entries(CATEGORIES).flatMap(([categoryName, categoryDetails]) => [ | ||
| [categoryName, categoryDetails.name], | ||
| ...categoryDetails.items.flatMap((item) => | ||
| Object.entries(item).filter(([key]) => 'other' !== key), | ||
| ), | ||
| ]), | ||
| ), | ||
| }; | ||
|
|
||
| /** The name shown for a stored category, e.g. "diningOut" is "Dining Out". */ | ||
| export const getCategoryLabel = (category: string) => CATEGORY_LABELS[category] ?? category; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Index all relevant people in each expense list. The page callbacks omit people that users can reasonably search for. As a result, matching expenses disappear from the filtered list.
src/pages/groups/[groupId].tsx#L69-L72: includeYouwhen the current user paid, to match the displayed payer label.src/pages/balances/[friendId].tsx#L56-L59: include the friend's name on user-paid expenses, not only when the friend paid.📍 Affects 2 files
src/pages/groups/[groupId].tsx#L69-L72(this comment)src/pages/balances/[friendId].tsx#L56-L59🤖 Prompt for AI Agents