Skip to content

Commit

Permalink
lint: ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mujahidkay committed Oct 1, 2024
1 parent 8e64332 commit 65b0ef7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
15 changes: 7 additions & 8 deletions ui/src/components/DisplayAmount.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PurseJSONState } from '@agoric/react-components';
import type { DisplayInfoForBrand } from '../store/displayInfo';
import { stringifyValue, type AssetKind } from '@agoric/web-components';
import { stringifyValue} from '@agoric/web-components';
import type { Amount } from '@agoric/ertp/src/types';
import { isCopyBagValue } from '@agoric/ertp';
import { useEffect, useRef, useState } from 'react';
Expand All @@ -10,7 +10,7 @@ export const PurseValue = ({
purse,
allowPopup,
}: {
purse: PurseJSONState<AssetKind>;
purse: PurseJSONState<'nat' | 'copyBag' | 'set'>;
allowPopup?: boolean;
}) => {
const displayInfoForBrand: DisplayInfoForBrand = {
Expand Down Expand Up @@ -65,7 +65,7 @@ export const NonNatValue = ({
petname,
allowPopup = true,
}: {
value: Amount<'set'>['value'] | Amount<'copyBag'>['value'];
value: Amount['value'];
petname: string;
allowPopup?: boolean;
}) => {
Expand All @@ -86,24 +86,23 @@ export const NonNatValue = ({

const count = isCopyBag
? String(
value.payload.reduce(
// @ts-expect-error cast
(value as Amount<'copyBag'>['value']).payload.reduce(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(total, [_, count]) => total + count,
0n,
),
)
: value.length;
: (value as Amount<'set'>['value']).length;

const items = isCopyBag
? value.payload.map((entry: [unknown, bigint]) => (
? (value as Amount<'copyBag'>['value']).payload.map((entry: [unknown, bigint]) => (
<CopyBagEntry
key={stringifyData(entry[0])}
className="mb-4"
entry={entry}
/>
))
: value.map((entry: unknown) => (
: (value as Amount<'set'>['value']).map((entry: unknown) => (
<SetEntry className="mb-4" key={stringifyData(entry)} entry={entry} />
));

Expand Down
29 changes: 15 additions & 14 deletions ui/src/components/PurseAmountInput.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { AmountInput, type PurseJSONState } from '@agoric/react-components';
import type { Amount, AssetKind } from '@agoric/web-components';
import type { AssetKind } from '@agoric/web-components';
import { useState } from 'react';
import { CopyBagEntry, PurseValue, SetEntry } from './DisplayAmount';
import { stringifyData } from '../utils/stringify';
import { makeCopyBag } from '@endo/patterns';
import { makeCopyBag, Key } from '@endo/patterns';

type Props = {
purse: PurseJSONState<AssetKind>;
onChangeInput?: (amount: {brand: globalThis.Brand, value: unknown} | null) => void;
onChange: (amount: Amount | null) => void;
};

Expand Down Expand Up @@ -35,7 +36,7 @@ const PurseAmountInput = (props: Props) => {
return <></>;
};

const SetInput = ({ purse, onChange }: Props) => {
const SetInput = ({ purse, onChangeInput }: Props) => {
const entries = purse.currentAmount.value;
const [checkedEntries, setCheckedEntries] = useState(new Set<unknown>());

Expand All @@ -48,13 +49,13 @@ const SetInput = ({ purse, onChange }: Props) => {
updated.add(entry);
}
if (!updated.size) {
onChange(null);
onChangeInput?.(null);
} else {
const newAmount = {
brand: purse.brand,
value: [...updated],
};
onChange(newAmount);
onChangeInput?.(newAmount);
}
setCheckedEntries(updated);
};
Expand Down Expand Up @@ -85,32 +86,32 @@ const SetInput = ({ purse, onChange }: Props) => {
});
};

const CopyBagInput = ({ purse, onChange }: Props) => {
const CopyBagInput = ({ purse, onChangeInput }: Props) => {
const entries = purse.currentAmount.value.payload;
const [entriesMap, setEntriesMap] = useState(
new Map<string, [unknown, bigint]>(),
new Map<string, [Key, bigint]>(),
);

const updateCount = (entry: [unknown, bigint], count: bigint) => {
const updateCount = (entry: [Key, bigint], count: bigint) => {
const updated = new Map(entriesMap);
if (count === 0n) {
updated.delete(stringifyData(entry[0]));
} else {
updated.set(stringifyData(entry[0]), [entry[0], count]);
}
if (count > entry[1]) {
onChange(null);
onChangeInput?.(null);
} else {
const newAmount = {
brand: purse.brand,
value: makeCopyBag(updated.values()),
};
onChange(newAmount);
onChangeInput?.(newAmount);
}
setEntriesMap(updated);
};

return entries.map((entry: [unknown, bigint]) => {
return entries.map((entry: [Key, bigint]) => {
const selectedAmount = entriesMap.get(stringifyData(entry[0]))?.[1] ?? null;

const onInput = (count: bigint) => {
Expand Down Expand Up @@ -143,16 +144,16 @@ const CopyBagInput = ({ purse, onChange }: Props) => {
});
};

const NatAmountInput = ({ purse, onChange }: Props) => {
const NatAmountInput = ({ purse, onChangeInput }: Props) => {
const [amount, setAmount] = useState({ brand: purse?.brand, value: 0n });
const hasError = amount.value > purse.currentAmount.value;

const onInputChange = (value: bigint) => {
const newAmount = { brand: purse?.brand, value };
if (value > purse.currentAmount.value || value === 0n) {
onChange(null);
onChangeInput?.(null);
} else {
onChange(newAmount);
onChangeInput?.(newAmount);
}
setAmount(newAmount);
};
Expand Down
6 changes: 4 additions & 2 deletions ui/src/components/swap/IncomingOffers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ const IncomingOffers = () => {
const { instances } = useContractStore();
const swaparooInstance = instances?.['swaparoo'];
const invitationPurse = usePurse('Invitation');
const swaparooInvitations = invitationPurse?.currentAmount.value.filter(
const swaparooInvitations = Array.isArray(invitationPurse?.currentAmount.value) ?
invitationPurse?.currentAmount.value.filter(
// @ts-expect-error cast
({ instance, description }: { instance: unknown; description: string }) =>
instance === swaparooInstance && description.startsWith('matchOffer'),
) as Array<unknown>[] | undefined;
): []

return (
<div className="w-80">
Expand Down
2 changes: 1 addition & 1 deletion ui/src/providers/Contract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const watchContract = (watcher: ChainStorageWatcher) => {
},
);

watcher.watchLatest<Array<[string, unknown]>>(
watcher.watchLatest<Array<[string, Brand]>>(
[Kind.Data, 'published.agoricNames.brand'],
brands => {
console.log('Got brands', brands);
Expand Down
2 changes: 1 addition & 1 deletion ui/src/store/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { create } from 'zustand';

interface ContractState {
instances?: Record<string, unknown>;
brands?: Record<string, unknown>;
brands?: Record<string, Brand>;
}

export const useContractStore = create<ContractState>(() => ({}));

0 comments on commit 65b0ef7

Please sign in to comment.