Skip to content

Commit 8493da9

Browse files
author
Adam Tomaszczyk
committed
Reintroduce “Random” Sorting in DRep Directory with Pagination Support - frontend
1 parent 51c183a commit 8493da9

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

govtool/backend/src/VVA/API.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ drepList mSearchQuery statuses mSortMode mPage mPageSize mSeed = do
210210

211211
let sortDReps = case mSortMode of
212212
Nothing -> id
213-
Just Random -> sortOn randomKey
213+
Just Random -> sortOn randomKey
214214
Just VotingPower -> sortOn $ \Types.DRepRegistration {..} ->
215215
Down dRepRegistrationVotingPower
216216
Just Activity -> sortOn $ \Types.DRepRegistration {..} ->

govtool/frontend/src/consts/dRepDirectory/sorting.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ export const DREP_DIRECTORY_SORTING = [
1515
key: "Status",
1616
label: "Status",
1717
},
18+
{
19+
key: "Random",
20+
label: "Random",
21+
},
1822
];

govtool/frontend/src/hooks/queries/useGetDRepListQuery.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Args = GetDRepListArguments & {
2424
};
2525

2626
export function useGetDRepListPaginatedQuery(
27-
{ page, pageSize = 10, filters = [], searchPhrase, sorting, status }: Args,
27+
{ page, pageSize = 10, filters = [], searchPhrase, sorting, status, sortingSeed }: Args,
2828
options?: UseQueryOptions<Infinite<DRepData>>,
2929
): PaginatedResult {
3030
const { pendingTransaction } = useCardano();
@@ -47,6 +47,7 @@ export function useGetDRepListPaginatedQuery(
4747
searchPhrase ?? "",
4848
sorting ?? "",
4949
status?.length ? status : "",
50+
sortingSeed ?? ""
5051
];
5152

5253
const baselineKey = useMemo(
@@ -64,6 +65,7 @@ export function useGetDRepListPaginatedQuery(
6465
searchPhrase,
6566
sorting,
6667
status,
68+
sortingSeed,
6769
}),
6870
{
6971
keepPreviousData: true,
@@ -87,6 +89,7 @@ export function useGetDRepListPaginatedQuery(
8789
searchPhrase: "",
8890
sorting,
8991
status,
92+
sortingSeed
9093
}),
9194
{
9295
initialData: () =>

govtool/frontend/src/models/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export enum DRepListSort {
148148
VotingPower = "VotingPower",
149149
RegistrationDate = "RegistrationDate",
150150
Status = "Status",
151+
Random = "Random",
151152
}
152153

153154
type Reference = {

govtool/frontend/src/pages/DRepDirectoryContent.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
6161
...dataActionsBarProps
6262
} = useDataActionsBar();
6363

64+
const SEED_STORAGE_KEY = "drep_directory_sorting_seed";
65+
66+
const makeSeed = () =>
67+
(globalThis.crypto?.randomUUID?.() as string | undefined) ??
68+
Math.random().toString(36).slice(2);
69+
70+
const getStoredSeed = () => {
71+
if (typeof window === "undefined") return "";
72+
return sessionStorage.getItem(SEED_STORAGE_KEY) || "";
73+
};
74+
75+
const [sortingSeed, setSortingSeed] = useState<string>(() => getStoredSeed());
76+
77+
useEffect(() => {
78+
if (lastPath && !lastPath.includes("drep_directory")) {
79+
const newSeed = makeSeed();
80+
setSortingSeed(newSeed);
81+
sessionStorage.setItem(SEED_STORAGE_KEY, newSeed);
82+
}
83+
}, [sortingSeed]);
84+
6485
const { page, pageSize, setPage, setPageSize } = usePagination();
6586

6687
const { chosenFilters, chosenSorting, setChosenFilters, setChosenSorting } =
@@ -108,6 +129,7 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({
108129
searchPhrase: debouncedSearchText,
109130
sorting: chosenSorting as DRepListSort,
110131
status: chosenFilters as DRepStatus[],
132+
sortingSeed
111133
},
112134
{ enabled: !!chosenSorting },
113135
);

govtool/frontend/src/services/requests/getDRepList.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type GetDRepListArguments = {
1414
sorting?: DRepListSort;
1515
status?: DRepStatus[];
1616
searchPhrase?: string;
17+
sortingSeed?: string;
1718
};
1819

1920
export const getDRepList = async ({
@@ -23,6 +24,7 @@ export const getDRepList = async ({
2324
pageSize = 10,
2425
searchPhrase: rawSearchPhrase = "",
2526
status = [],
27+
sortingSeed = ""
2628
}: GetDRepListArguments): Promise<Infinite<DRepData>> => {
2729
const searchPhrase = await dRepSearchPhraseProcessor(rawSearchPhrase);
2830

@@ -34,6 +36,7 @@ export const getDRepList = async ({
3436
...(filters.length && { type: filters }),
3537
...(sorting && { sort: sorting }),
3638
...(status.length && { status }),
39+
...(sortingSeed && { seed: sortingSeed }),
3740
},
3841
});
3942

0 commit comments

Comments
 (0)