Skip to content

Commit

Permalink
feat: improve type truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
begonaalvarezd committed Oct 29, 2024
1 parent d3bd054 commit a6b8a21
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions apps/explorer/src/components/ui/InternalLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { formatAddress, formatDigest } from '@iota/iota-sdk/utils';
import { formatAddress, formatDigest, formatType } from '@iota/iota-sdk/utils';
import { type ReactNode } from 'react';

import { Link, type LinkProps } from '~/components/ui';
Expand Down Expand Up @@ -48,6 +48,6 @@ export const CheckpointSequenceLink = createInternalLink('checkpoint', 'sequence
export const AddressLink = createInternalLink('address', 'address', (addressOrNs) =>
formatAddress(addressOrNs),
);
export const ObjectLink = createInternalLink('object', 'objectId', formatAddress);
export const ObjectLink = createInternalLink('object', 'objectId', formatType);
export const TransactionLink = createInternalLink('txblock', 'digest', formatDigest);
export const ValidatorLink = createInternalLink('validator', 'address', formatAddress);
8 changes: 4 additions & 4 deletions apps/explorer/src/lib/ui/utils/generateObjectListColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import { TableCellBase, TableCellText } from '@iota/apps-ui-kit';
import type { ColumnDef } from '@tanstack/react-table';

import { parseObjectType, trimStdLibPrefix } from '~/lib';
import { ObjectVideoImage, ObjectLink } from '~/components';
import type { IotaObjectResponse } from '@iota/iota-sdk/client';
import { formatAddress, formatType } from '@iota/iota-sdk/utils';
import { ObjectLink, ObjectVideoImage } from '~/components';
import { useResolveVideo } from '~/hooks';
import { formatAddress } from '@iota/iota-sdk/utils';
import { parseObjectType, trimStdLibPrefix } from '~/lib';

function Asset({ object }: { object: IotaObjectResponse }) {
const video = useResolveVideo(object);
Expand Down Expand Up @@ -56,7 +56,7 @@ export function generateObjectListColumns(): ColumnDef<IotaObjectResponse>[] {
cell({ row: { original: object } }) {
const objectId = object?.data?.objectId;
if (!objectId) return null;
const type = trimStdLibPrefix(parseObjectType(object));
const type = trimStdLibPrefix(formatType(parseObjectType(object)));
return (
<TableCellBase>
<ObjectLink objectId={objectId} label={type}>
Expand Down
3 changes: 1 addition & 2 deletions apps/explorer/src/pages/object-result/views/ObjectView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ function TypeCard({ objectType }: TypeCardCardProps): JSX.Element {
};

const normalizedStructTag = normalizeStructTag(structTag);

return (
<DisplayStats
label="Type"
value={
<ObjectLink objectId={`${address}?module=${module}`}>
<ObjectLink objectId={`${address}?module=${module}`} label={normalizedStructTag}>
{normalizedStructTag}
</ObjectLink>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CheckIcon, CopyIcon } from 'lucide-react';
import { useState } from 'react';
import toast from 'react-hot-toast';

import { formatAddress } from './utils';
import { formatAddress, formatType } from './utils';

Check failure on line 10 in dapps/multisig-toolkit/src/components/preview-effects/ObjectLink.tsx

View workflow job for this annotation

GitHub Actions / turborepo / Lint, Build, and Test

'formatAddress' is defined but never used. Allowed unused vars must match /^_/u

type OwnerDisplay = string | { address: string } | { object: string };

Expand Down Expand Up @@ -43,7 +43,7 @@ export function ObjectLink({
if (ownerDisplay) {
if (typeof ownerDisplay !== 'string') {
objectId = 'address' in ownerDisplay ? ownerDisplay.address : ownerDisplay.object;
display = formatAddress(objectId);
display = formatType(objectId);
} else {
display = ownerDisplay;
}
Expand All @@ -55,18 +55,18 @@ export function ObjectLink({

if (inputObject) {
objectId = inputObject;
display = formatAddress(inputObject);
display = formatType(inputObject);
}

if (object) {
if ('objectId' in object) {
objectId = object.objectId;
display = formatAddress(objectId);
display = formatType(objectId);
}

if ('packageId' in object) {
objectId = object.packageId;
display = formatAddress(objectId);
display = formatType(objectId);
}
}

Expand Down
13 changes: 13 additions & 0 deletions sdk/typescript/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { isValidIotaAddress, isValidIotaObjectId, IOTA_ADDRESS_LENGTH } from './iota-types.js';

const ELLIPSIS = '\u{2026}';

export function formatAddress(address: string) {
Expand All @@ -18,3 +20,14 @@ export function formatDigest(digest: string) {
// Use 10 first characters
return `${digest.slice(0, 10)}${ELLIPSIS}`;
}

export function formatType(type: string) {
const objectAddressPattern = new RegExp(`0x[a-fA-F0-9]{${IOTA_ADDRESS_LENGTH * 2}}`, 'g');
const matches = type.match(objectAddressPattern) ?? [];
for (const match of matches) {
if (isValidIotaAddress(match) || isValidIotaObjectId(match)) {
type = type.replace(match, formatAddress(match));
}
}
return type;
}
2 changes: 1 addition & 1 deletion sdk/typescript/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

export { formatAddress, formatDigest } from './format.js';
export { formatAddress, formatDigest, formatType } from './format.js';
export {
isValidIotaAddress,
isValidIotaObjectId,
Expand Down

0 comments on commit a6b8a21

Please sign in to comment.