import React from 'react' import { ZodError, ZodIssue } from 'zod' import { JSONValue } from 'superjson/dist/types' import { JSONPrimitive, ParsedActionReturnData, } from '@interval/sdk/dist/ioSchema' import { TransactionResultStatus } from '@prisma/client' import KeyValueTable from '~/components/KeyValueTable' import RenderValue from '~/components/RenderValue' import { pluralize } from '~/utils/text' import { IORenderInstruction } from '~/components/RenderIOCall' import IconExternalLink from '~/icons/compiled/ExternalLink' import useCopyToClipboard from '~/utils/useCopyToClipboard' import { notify } from '~/components/NotificationCenter' import ObjectViewer from '~/components/ObjectViewer' export default function ResultRenderer({ status, result, shareLink, }: { status: TransactionResultStatus | null result: JSONValue shareLink?: string }) { const { onCopyClick } = useCopyToClipboard() if (result == null) return null return (

Result:

{status === 'FAILURE' ? (
) : typeof result === 'object' ? ( Array.isArray(result) ? (
) : ( } /> ) ) : (
)} {shareLink && (
{ onCopyClick(shareLink || '') notify.success('Link copied to clipboard.') }} className="cursor-pointer inline-flex text-sm items-center text-primary-400 hover:opacity-60 " > Share these results
)}
) } interface ErrorLike { error?: any message: string cause?: string } function getError(result: JSONValue): ErrorLike | null { if (!result || typeof result !== 'object') return null if ('message' in result && typeof result.message === 'string') { let cause: string | undefined if ('cause' in result && typeof result.cause === 'string') { cause = result.cause } return { ...result, cause, } as ErrorLike } return null } export function TransactionError({ result }: { result: JSONValue }) { const errorResult = getError(result) if (!errorResult) { return

An unknown error occurred.

} const { error, message, cause } = errorResult if (error === 'ZodError') { try { const deserialized = JSON.parse(message) if (Array.isArray(deserialized)) { return ( ) } } catch (err) { // Probably not a ZodError, just render it normally below } } return (

{error ?? 'Error'} :{message}

{cause && (

Caused by:

{cause}

)}

Please check your host logs for more information.

) } export function ComponentError({ component, error, }: { component?: IORenderInstruction error: Pick }) { return (
{component && (

Label: {component.label ?? No label}

)}

This {component ? 'field' : 'action'} contains the following{' '} {pluralize(error.issues.length, 'error')}:

    {error.issues.map((err, idx) => (
  • {pathToString(err.path)}:{' '} {err.message}
  • ))}

Please correct your action code and try again.

) } function pathToString(path: (string | number)[]) { if (path.length === 0) return '' return ( path[0] + path .slice(1) .map(p => `[${p}]`) .join('') ) }