Skip to content

Commit 338ff14

Browse files
authored
Merge pull request #4115 from IntersectMBO/test
v2.0.37
2 parents a07d885 + b94fe1f commit 338ff14

File tree

8 files changed

+55
-15
lines changed

8 files changed

+55
-15
lines changed

govtool/backend/src/VVA/API.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import qualified Data.Text.Lazy.Encoding as TL
2828
import Data.Time (TimeZone, localTimeToUTC)
2929
import Data.Time.LocalTime (TimeZone, getCurrentTimeZone)
3030
import qualified Data.Vector as V
31+
import Data.Hashable (hash, hashWithSalt)
3132

3233
import Numeric.Natural (Natural)
3334

@@ -64,6 +65,7 @@ type VVAApi =
6465
:> QueryParam "sort" DRepSortMode
6566
:> QueryParam "page" Natural
6667
:> QueryParam "pageSize" Natural
68+
:> QueryParam "seed" Text
6769
:> Get '[JSON] ListDRepsResponse
6870
:<|> "drep" :> "get-voting-power" :> Capture "drepId" HexText :> Get '[JSON] Integer
6971
:<|> "drep" :> "getVotes"
@@ -175,8 +177,8 @@ delegationToResponse Types.Delegation {..} =
175177
}
176178

177179

178-
drepList :: App m => Maybe Text -> [DRepStatus] -> Maybe DRepSortMode -> Maybe Natural -> Maybe Natural -> m ListDRepsResponse
179-
drepList mSearchQuery statuses mSortMode mPage mPageSize = do
180+
drepList :: App m => Maybe Text -> [DRepStatus] -> Maybe DRepSortMode -> Maybe Natural -> Maybe Natural -> Maybe Text -> m ListDRepsResponse
181+
drepList mSearchQuery statuses mSortMode mPage mPageSize mSeed = do
180182
CacheEnv {dRepListCache} <- asks vvaCache
181183
dreps <- cacheRequest dRepListCache (fromMaybe "" mSearchQuery) (DRep.listDReps mSearchQuery)
182184

@@ -193,17 +195,22 @@ drepList mSearchQuery statuses mSortMode mPage mPageSize = do
193195
Types.DRep ->
194196
True
195197

196-
197198
let filterDRepsByStatus = case statuses of
198199
[] -> id
199200
_ -> filter $ \Types.DRepRegistration {..} ->
200201
mapDRepStatus dRepRegistrationStatus `elem` statuses
201202

202-
randomizedOrderList <- mapM (\_ -> randomRIO (0, 1 :: Double)) dreps
203+
let seedInt :: Int
204+
seedInt =
205+
maybe 0 (hash . Text.toCaseFold) mSeed
206+
207+
randomKey :: Types.DRepRegistration -> Int
208+
randomKey Types.DRepRegistration{..} =
209+
hashWithSalt seedInt dRepRegistrationDRepHash
203210

204211
let sortDReps = case mSortMode of
205212
Nothing -> id
206-
Just Random -> fmap snd . sortOn fst . Prelude.zip randomizedOrderList
213+
Just Random -> sortOn randomKey
207214
Just VotingPower -> sortOn $ \Types.DRepRegistration {..} ->
208215
Down dRepRegistrationVotingPower
209216
Just Activity -> sortOn $ \Types.DRepRegistration {..} ->

govtool/frontend/package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

govtool/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@emotion/styled": "^11.11.0",
2828
"@emurgo/cardano-serialization-lib-asmjs": "^14.1.1",
2929
"@hookform/resolvers": "^3.3.1",
30-
"@intersect.mbo/govtool-outcomes-pillar-ui": "v1.5.9",
30+
"@intersect.mbo/govtool-outcomes-pillar-ui": "1.5.10",
3131
"@intersect.mbo/intersectmbo.org-icons-set": "^1.0.8",
3232
"@intersect.mbo/pdf-ui": "1.0.15-beta",
3333
"@mui/icons-material": "^5.14.3",

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)