Skip to content
Closed
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
5 changes: 2 additions & 3 deletions components/Funding/ProposalSortAndFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { FC } from 'react';
import { cn } from '@/utils/styles';
import { SortMenu } from '@/components/ui/SortMenu';
import { useFundraises } from '@/contexts/FundraiseContext';
import { SORT_OPTIONS } from './lib/proposalSortAndFilterConfig';

interface ProposalSortAndFiltersProps {
className?: string;
}

export const ProposalSortAndFilters: FC<ProposalSortAndFiltersProps> = ({ className }) => {
const { entries, isLoading, sortBy, setSortBy } = useFundraises();
const { entries, isLoading, sortBy, setSortBy, sortOptions } = useFundraises();

if (!isLoading && entries.length === 0) {
return null;
Expand All @@ -20,7 +19,7 @@ export const ProposalSortAndFilters: FC<ProposalSortAndFiltersProps> = ({ classN
return (
<div className={cn('flex items-center justify-end', className)}>
<SortMenu
options={SORT_OPTIONS}
options={sortOptions}
value={sortBy}
onChange={setSortBy}
triggerProps={{ 'data-testid': 'feed-sort-trigger' }}
Expand Down
10 changes: 10 additions & 0 deletions components/Funding/lib/proposalSortAndFilterConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type ProposalStatusFilter = 'all' | 'open' | 'completed';
export type ProposalSortOption =
| 'recommended'
| 'best'
| 'newest'
| 'upvotes'
Expand All @@ -21,3 +22,12 @@ export const SORT_OPTIONS: { label: string; value: ProposalSortOption }[] = [
{ label: 'Raised', value: 'amount_raised' },
{ label: 'Completed', value: 'completed' },
];

/**
* Sort options for one RFP's proposals. `recommended` ranks within a single
* grant, so it is offered only when the feed is scoped to one.
*/
export const GRANT_SORT_OPTIONS: { label: string; value: ProposalSortOption }[] = [
{ label: 'Recommended', value: 'recommended' },
...SORT_OPTIONS,
];
18 changes: 13 additions & 5 deletions contexts/FundraiseContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import {
STATUS_OPTIONS,
SORT_OPTIONS,
GRANT_SORT_OPTIONS,
type ProposalStatusFilter,
type ProposalSortOption,
} from '@/components/Funding/lib/proposalSortAndFilterConfig';
Expand All @@ -40,6 +41,7 @@
setTaxDeductible: (value: boolean) => void;
sortBy: ProposalSortOption;
setSortBy: (value: ProposalSortOption) => void;
sortOptions: { label: string; value: ProposalSortOption }[];
isGrantScoped: boolean;

/** Call once from the consuming component to trigger the initial fetch. */
Expand All @@ -55,6 +57,7 @@
const PAGE_SIZE = 20;
const DEFAULT_STATUS: ProposalStatusFilter = 'all';
const DEFAULT_SORT: ProposalSortOption = 'best';
const DEFAULT_GRANT_SORT: ProposalSortOption = 'recommended';
const FundraiseContext = createContext<FundraiseContextValue | null>(null);

function resolveRestoredStatus(value: string | undefined): ProposalStatusFilter {
Expand All @@ -64,11 +67,12 @@
return DEFAULT_STATUS;
}

function resolveRestoredSort(value: string | undefined): ProposalSortOption {
if (SORT_OPTIONS.some((option) => option.value === value)) {
function resolveSort(value: string | undefined, isGrantScoped: boolean): ProposalSortOption {
const options = isGrantScoped ? GRANT_SORT_OPTIONS : SORT_OPTIONS;
if (options.some((option) => option.value === value)) {
return value as ProposalSortOption;
}
return DEFAULT_SORT;
return isGrantScoped ? DEFAULT_GRANT_SORT : DEFAULT_SORT;
}

interface FundraiseProviderProps {
Expand Down Expand Up @@ -96,7 +100,7 @@
const initialHasMore = restoredState?.hasMore ?? false;
const initialPage = restoredState?.page ?? 1;
const initialStatusFilter = resolveRestoredStatus(restoredState?.filters?.statusFilter);
const initialSortBy = resolveRestoredSort(restoredState?.filters?.sortBy);
const initialSortBy = resolveSort(restoredState?.filters?.sortBy, isGrantScoped);

const [entries, setEntries] = useState<FeedEntry[]>(initialEntries);
const [totalCount, setTotalCount] = useState(initialEntries.length);
Expand All @@ -108,7 +112,10 @@

const [statusFilter, setStatusFilter] = useState<ProposalStatusFilter>(initialStatusFilter);
const [taxDeductible, setTaxDeductible] = useState(false);
const [sortBy, setSortBy] = useState<ProposalSortOption>(initialSortBy);
const [selectedSortBy, setSortBy] = useState<ProposalSortOption>(initialSortBy);

Check warning on line 115 in contexts/FundraiseContext.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

useState call is not destructured into value + setter pair

See more on https://sonarcloud.io/project/issues?id=ResearchHub_web&issues=AaH-HUaS0xKWwBM58aTh&open=AaH-HUaS0xKWwBM58aTh&pullRequest=1321
// `recommended` is only valid with a grant_id (without one the backend ranks
// every proposal uncached), so fall back if the provider loses its grant.
const sortBy = resolveSort(selectedSortBy, isGrantScoped);

const feedParams = useMemo(() => {
const isStatusCompleted = statusFilter === 'completed';
Expand Down Expand Up @@ -212,6 +219,7 @@
setTaxDeductible,
sortBy,
setSortBy,
sortOptions: isGrantScoped ? GRANT_SORT_OPTIONS : SORT_OPTIONS,
isGrantScoped,
activate,
refresh: fetchProposals,
Expand Down