Skip to content

Commit

Permalink
pushes the abandoned sns to the bottom of the lists"
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabib committed Dec 4, 2024
1 parent 530149e commit f656199
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 39 deletions.
53 changes: 41 additions & 12 deletions frontend/src/lib/stores/sns-aggregator.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,29 @@ export const snsAggregatorIncludingAbortedProjectsStore =

export const snsAggregatorStore: SnsAggregatorStore = derived(
snsAggregatorIncludingAbortedProjectsStore,
(store) => ({
data: store.data
?.filter(
(sns) =>
nonNullish(sns.lifecycle) &&
sns.lifecycle.lifecycle !== SnsSwapLifecycle.Aborted
)
.map(fixBrokenSnsMetadataBasedOnId),
})
(store) => {
const data = store.data?.filter(
(sns) =>
nonNullish(sns.lifecycle) &&
sns.lifecycle.lifecycle !== SnsSwapLifecycle.Aborted
);

// TODO: Find a better way to fix broken SNS metadata. These transformations will be remove once we have a better solution.
const handledAbandonedSnsData =
data?.map(fixBrokenSnsMetadataBasedOnId) ?? [];
const sortedAbandonesSnsData = sortedListBasedOnAbandoned(
handledAbandonedSnsData
);
const cachedSnsData = sortedAbandonesSnsData.map(
({ isAbandoned: _, ...sns }) => ({ ...sns })
);

return {
data: cachedSnsData,
};
}
);

// TODO: Find a better way to fix broken SNS metadata.
const brokenSnsOverrides: Record<
string,
{ name: string; tokenSymbol: string }
Expand All @@ -60,12 +71,17 @@ const brokenSnsOverrides: Record<
},
};

const fixBrokenSnsMetadataBasedOnId = (sns: CachedSnsDto): CachedSnsDto => {
const fixBrokenSnsMetadataBasedOnId = (
sns: CachedSnsDto
): CachedSnsDto & { isAbandoned?: boolean } => {
const override = brokenSnsOverrides[sns.list_sns_canisters.root];

// Required for the tokens and stakin routes
const hiddenCharacterToPushSnsToEndOfList = "\u200B";
if (!nonNullish(override)) return sns;
const newMeta = {
...sns.meta,
name: `${sns.meta.name} (formerly ${override.name})`,
name: `${hiddenCharacterToPushSnsToEndOfList}${sns.meta.name} (formerly ${override.name})`,
};

const newIcrc1Metadata = sns.icrc1_metadata.map<
Expand All @@ -86,5 +102,18 @@ const fixBrokenSnsMetadataBasedOnId = (sns: CachedSnsDto): CachedSnsDto => {
...sns,
meta: { ...newMeta },
icrc1_metadata: [...newIcrc1Metadata],
isAbandoned: true,
};
};

// Required for the proposals route
const sortedListBasedOnAbandoned = (
list: (CachedSnsDto & { isAbandoned?: boolean })[]
) => [
...list.sort((a, b) => {
if (a.isAbandoned && !b.isAbandoned) return 1;
if (!a.isAbandoned && b.isAbandoned) return -1;

return 0;
}),
];
76 changes: 49 additions & 27 deletions frontend/src/tests/lib/stores/sns-aggregator.store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,41 +153,63 @@ describe("sns-aggregator store", () => {
},
});

it("should override information for SNS with rootCanisterId ibahq-taaaa-aaaaq-aadna-cai", () => {
const mockedSns = aggregatorMockSnsesDataDto[0];
const brokenSns = withBrokenSns({
sns: {
...mockedSns,
meta: {
...mockedSns.meta,
name: "---",
},
icrc1_metadata: [...mockedSns.icrc1_metadata].map(([name, value]) => {
if (name === "icrc1:symbol" && "Text" in value) {
return [
name,
{
Text: "---",
},
];
}
return [name, value];
}),
const mockedSns = aggregatorMockSnsesDataDto[0];
const brokenSns = withBrokenSns({
sns: {
...mockedSns,
meta: {
...mockedSns.meta,
name: "---",
},
rootCanisterId: "ibahq-taaaa-aaaaq-aadna-cai",
});
icrc1_metadata: [...mockedSns.icrc1_metadata].map(([name, value]) => {
if (name === "icrc1:symbol" && "Text" in value) {
return [
name,
{
Text: "---",
},
];
}
return [name, value];
}),
},
rootCanisterId: "ibahq-taaaa-aaaaq-aadna-cai",
});

it("should override information for SNS with rootCanisterId ibahq-taaaa-aaaaq-aadna-cai", () => {
const data = [brokenSns];
snsAggregatorIncludingAbortedProjectsStore.setData(data);
expect(get(snsAggregatorIncludingAbortedProjectsStore).data[0].meta.name).toBe(
"---"
);
expect(
get(snsAggregatorIncludingAbortedProjectsStore).data[0].icrc1_metadata[3][1]
get(snsAggregatorIncludingAbortedProjectsStore).data[0].meta.name
).toBe("---");
expect(
get(snsAggregatorIncludingAbortedProjectsStore).data[0]
.icrc1_metadata[3][1]
).toEqual({ Text: "---" });

const result = get(snsAggregatorStore).data[0];
expect(result.meta.name).toBe("--- (formerly CYCLES_TRANSFER_STATION)");
expect(result.meta.name).toBe(
"\u200B--- (formerly CYCLES_TRANSFER_STATION)"
);
expect(result.icrc1_metadata[3][1]).toEqual({ Text: "--- (CTS)" });
});

it("should sort sns by temporal isAbandoded property", () => {
const data = [brokenSns, ...aggregatorMockSnsesDataDto];
snsAggregatorIncludingAbortedProjectsStore.setData(data);
expect(
get(snsAggregatorIncludingAbortedProjectsStore).data[0].meta.name
).toBe("---");
expect(
get(snsAggregatorIncludingAbortedProjectsStore).data[0]
.icrc1_metadata[3][1]
).toEqual({ Text: "---" });

const result =
get(snsAggregatorStore).data[get(snsAggregatorStore).data.length - 1];
expect(result.meta.name).toBe(
"\u200B--- (formerly CYCLES_TRANSFER_STATION)"
);
expect(result.icrc1_metadata[3][1]).toEqual({ Text: "--- (CTS)" });
});
});
Expand Down

0 comments on commit f656199

Please sign in to comment.