Skip to content

Commit

Permalink
refactor: decrease SnsProposalActionableData usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrasinskis committed Mar 26, 2024
1 parent 9057099 commit 17c7c0c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import type {
SnsNervousSystemFunction,
SnsNeuronId,
SnsProposalData,
SnsProposalId,
} from "@dfinity/sns";
import { subaccountToHexString } from "$lib/utils/sns-neuron.utils";
import type { UniversalProposalStatus } from "$lib/types/proposals";
import type { SnsProposalActionableData } from "$lib/derived/sns/sns-filtered-actionable-proposals.derived";
export let proposalData: SnsProposalActionableData;
export let proposalData: SnsProposalData;
export let nsFunctions: SnsNervousSystemFunction[] | undefined;
export let actionable = false;
export let hidden = false;
let id: SnsProposalId | undefined;
Expand Down Expand Up @@ -49,7 +50,7 @@
<ProposalCard
{status}
{hidden}
actionable={proposalData.isActionable}
{actionable}
{href}
id={id?.id}
{title}
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/lib/components/sns-proposals/SnsProposalsList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
disabled={disableInfiniteScroll}
>
{#each proposals as proposalData (fromNullable(proposalData.id)?.id)}
<SnsProposalCard {proposalData} {nsFunctions} />
<SnsProposalCard
actionable={proposalData.isActionable}
{proposalData}
{nsFunctions}
/>
{/each}
</InfiniteScroll>
</ListLoader>
Expand All @@ -63,7 +67,11 @@
{:else}
<InfiniteScroll layout="grid" disabled>
{#each proposals as proposalData (fromNullable(proposalData.id)?.id)}
<SnsProposalCard {proposalData} {nsFunctions} />
<SnsProposalCard
actionable={proposalData.isActionable}
{proposalData}
{nsFunctions}
/>
{/each}
</InfiniteScroll>
{/if}
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/lib/pages/SnsProposals.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@
$: actionableProposalsData = nonNullish(currentProjectCanisterId)
? $actionableSnsProposalsStore[currentProjectCanisterId.toText()]
: undefined;
let actionableProposals: SnsProposalActionableData[] | undefined;
$: actionableProposals = actionableProposalsData?.proposals.map(
(proposal) =>
({
...proposal,
isActionable: true,
}) as SnsProposalActionableData
);
let actionableSelected: boolean;
$: actionableSelected =
Expand All @@ -131,7 +139,7 @@
$: proposals = nonNullish(currentProjectCanisterId)
? sortSnsProposalsById(
actionableSelected
? actionableProposalsData?.proposals
? actionableProposals
: $snsFilteredActionableProposalsStore[
currentProjectCanisterId.toText()
]?.proposals
Expand Down
8 changes: 2 additions & 6 deletions frontend/src/lib/stores/actionable-sns-proposals.store.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { SnsProposalActionableData } from "$lib/derived/sns/sns-filtered-actionable-proposals.derived";
import { removeKeys } from "$lib/utils/utils";
import type { Principal } from "@dfinity/principal";
import type { SnsProposalData } from "@dfinity/sns";
import { writable, type Readable } from "svelte/store";

export interface ActionableSnsProposalsData {
proposals: SnsProposalActionableData[];
proposals: SnsProposalData[];
includeBallotsByCaller: boolean;
}
export interface ActionableSnsProposalsStoreData {
Expand Down Expand Up @@ -48,10 +47,7 @@ const initActionableSnsProposalsStore = (): ActionableSnsProposalsStore => {
update((currentState: ActionableSnsProposalsStoreData) => ({
...currentState,
[rootCanisterId.toText()]: {
proposals: proposals.map((proposal) => ({
...proposal,
isActionable: true,
})),
proposals,
includeBallotsByCaller,
},
}));
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/lib/utils/sns-proposals.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ export type SnsProposalDataMap = {
};

// TODO: Return also a type and the type description that for now maps to the topic
export const mapProposalInfo = <T extends SnsProposalData>({
export const mapProposalInfo = ({
proposalData,
nsFunctions,
}: {
proposalData: T;
proposalData: SnsProposalData;
nsFunctions: SnsNervousSystemFunction[] | undefined;
}): SnsProposalDataMap => {
const {
Expand Down Expand Up @@ -313,8 +313,8 @@ export const snsRewardStatus = ({
return SnsProposalRewardStatus.PROPOSAL_REWARD_STATUS_SETTLED;
};

export const lastProposalId = <T extends SnsProposalData>(
proposals: T[]
export const lastProposalId = (
proposals: SnsProposalData[]
): SnsProposalId | undefined => {
const last = sortSnsProposalsById(proposals)?.[proposals.length - 1];
return fromNullable(last?.id ?? []);
Expand All @@ -328,9 +328,9 @@ export const lastProposalId = <T extends SnsProposalData>(
* @param {SnsProposalData[]} proposals
* @returns {SnsProposalData[]}
*/
export const sortSnsProposalsById = <T extends SnsProposalData>(
proposals: T[] | undefined
): T[] | undefined =>
export const sortSnsProposalsById = <P extends SnsProposalData>(
proposals: P[] | undefined
): P[] | undefined =>
proposals === undefined
? undefined
: [...proposals].sort(({ id: idA }, { id: idB }) =>
Expand Down

0 comments on commit 17c7c0c

Please sign in to comment.