Skip to content

Commit

Permalink
NNS1-3486: clean up (#6030)
Browse files Browse the repository at this point in the history
# Motivation

Follow up on #6028 to tidy things up.

# Changes

- Moves types `TransactionEntity` , `TransactionResults` and
`TransactionsDateRange` to `lib/types/reporting.ts`.
- Renames `ReportingDateRange` into `ReportingPeriod`
- Renames `selectedRange` to `period` in `ReportingDateRangeSelector`
- Adds a comment to `filterTransactionsByRange`

# Tests
- Not necessary

# Todos

- [ ] Add entry to changelog (if necessary).
Not necessary
  • Loading branch information
yhabib authored Dec 18, 2024
1 parent 8b06266 commit 5152fa6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<script lang="ts">
import { i18n } from "$lib/stores/i18n";
import type { ReportingDateRange } from "$lib/types/reporting";
import type { ReportingPeriod } from "$lib/types/reporting";
let selectedRange: ReportingDateRange = "all";
let period: ReportingPeriod = "all";
const options: Array<{
value: ReportingDateRange;
value: ReportingPeriod;
label: string;
}> = [
{ value: "all", label: $i18n.reporting.range_filter_all },
{ value: "last-year", label: $i18n.reporting.range_last_year },
{ value: "year-to-date", label: $i18n.reporting.range_year_to_date },
];
function handleChange(value: ReportingDateRange) {
selectedRange = value;
function handleChange(value: ReportingPeriod) {
period = value;
}
</script>

Expand All @@ -28,8 +28,8 @@
type="radio"
name="dateRange"
value={option.value}
checked={selectedRange === option.value}
aria-checked={selectedRange === option.value}
checked={period === option.value}
aria-checked={period === option.value}
on:change={() => handleChange(option.value)}
/>
<span class="label">{option.label}</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script lang="ts">
import { i18n } from "$lib/stores/i18n";
import type { ReportingDateRange } from "$lib/types/reporting";
import type { ReportingPeriod } from "$lib/types/reporting";
import ReportingTransactionsButton from "./ReportingTransactionsButton.svelte";
// TODO: This will be hooked up in a follow up
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let selectedRange: ReportingDateRange = "all";
let selectedRange: ReportingPeriod = "all";
</script>

<div class="wrapper">
Expand Down
34 changes: 12 additions & 22 deletions frontend/src/lib/services/reporting.services.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { getTransactions } from "$lib/api/icp-index.api";
import type { Account } from "$lib/types/account";
import type { TransactionsDateRange } from "$lib/types/reporting";
import type {
TransactionEntity,
TransactionResults,
TransactionsDateRange,
} from "$lib/types/reporting";
import { neuronStake } from "$lib/utils/neuron.utils";
import { SignIdentity } from "@dfinity/agent";
import type { TransactionWithId } from "@dfinity/ledger-icp";
import type { NeuronInfo } from "@dfinity/nns";
import { isNullish, nonNullish } from "@dfinity/utils";

type TransactionEntity =
| {
identifier: string;
balance: bigint;
type: "account";
originalData: Account;
}
| {
identifier: string;
balance: bigint;
type: "neuron";
originalData: NeuronInfo;
};

const accountToTransactionEntity = (account: Account): TransactionEntity => {
return {
identifier: account.identifier,
Expand All @@ -46,12 +36,6 @@ export const mapAccountOrNeuronToTransactionEntity = (
return accountToTransactionEntity(entity);
};

export type TransactionResults = {
entity: TransactionEntity;
transactions: TransactionWithId[];
error?: string;
}[];

export const getAccountTransactionsConcurrently = async ({
entities,
identity,
Expand Down Expand Up @@ -171,7 +155,13 @@ export const getAllTransactionsFromAccountAndIdentity = async ({
}
};

// Helper function to filter transactions by date range
/**
* Filters an array of transactions based on a timestamp range.
*
* @param transactions - Array of transactions to filter
* @param range - Optional time range with inclusive 'from' and exclusive 'to' timestamps in nanoseconds
* @returns Filtered array of transactions that fall within the specified range
*/
const filterTransactionsByRange = (
transactions: TransactionWithId[],
range?: TransactionsDateRange
Expand Down
26 changes: 25 additions & 1 deletion frontend/src/lib/types/reporting.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
export type ReportingDateRange = "all" | "last-year" | "year-to-date";
import type { Account } from "$lib/types/account";
import type { TransactionWithId } from "@dfinity/ledger-icp";
import type { NeuronInfo } from "@dfinity/nns";

export type ReportingPeriod = "all" | "last-year" | "year-to-date";

export type TransactionsDateRange = {
/** Start of the date range (inclusive) - timestamp in nanoseconds */
from?: bigint;
/** End of the date range (exclusive) - timestamp in nanoseconds */
to?: bigint;
};

export type TransactionEntity =
| {
identifier: string;
balance: bigint;
type: "account";
originalData: Account;
}
| {
identifier: string;
balance: bigint;
type: "neuron";
originalData: NeuronInfo;
};

export type TransactionResults = {
entity: TransactionEntity;
transactions: TransactionWithId[];
error?: string;
}[];
6 changes: 3 additions & 3 deletions frontend/src/lib/utils/reporting.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TransactionResults } from "$lib/services/reporting.services";
import type {
ReportingDateRange,
ReportingPeriod,
TransactionResults,
TransactionsDateRange,
} from "$lib/types/reporting";
import {
Expand Down Expand Up @@ -465,7 +465,7 @@ export const buildNeuronsDatasets = ({
};

export const convertPeriodToNanosecondRange = (
period: ReportingDateRange
period: ReportingPeriod
): TransactionsDateRange => {
const now = new Date();
const currentYear = now.getFullYear();
Expand Down

0 comments on commit 5152fa6

Please sign in to comment.