import React, { useState } from "react";
import { Card, Container, Tabs, Tab } from "react-bootstrap";
import { TransactionDetails } from "src/typings/api";
import DefaultTab from "./DefaultTab";
import EventsTab from "./EventsTab";
import OverviewTab from "./OverviewTab";
import TransitionsTab from "./TransitionsTab";
import CodeTab from "./CodeTab";
import "./InfoTabs.css";
export interface ReceiptTabs {
tabHeaders: string[];
tabTitles: string[];
tabContents: React.ReactNode[];
}
interface IProps {
tabs: ReceiptTabs;
}
export const generateTabsFromTxnDetails = (
data: TransactionDetails
): ReceiptTabs => {
const tabs: ReceiptTabs = {
tabHeaders: [],
tabTitles: [],
tabContents: [],
};
if (!data.txn.txParams.receipt) return tabs;
const receipt = data.txn.txParams.receipt;
if (receipt.success === undefined || data.contractAddr) {
if (data.txn.txParams.data) {
if (data.txn.txParams.data) {
tabs.tabHeaders.push("params");
tabs.tabTitles.push(`Params`);
tabs.tabContents.push();
}
}
if (data.txn.txParams.code) {
if (data.txn.txParams.code) {
tabs.tabHeaders.push("code");
tabs.tabTitles.push(`Code`);
tabs.tabContents.push();
}
}
} else {
if (data.txn.txParams.data) {
tabs.tabHeaders.push("overview");
tabs.tabTitles.push(`Overview`);
tabs.tabContents.push();
}
}
if (receipt.event_logs) {
tabs.tabHeaders.push("eventLog");
tabs.tabTitles.push(`Event Log (${receipt.event_logs.length})`);
tabs.tabContents.push();
}
if (receipt.transitions) {
tabs.tabHeaders.push("transitions");
tabs.tabTitles.push(`Transitions (${receipt.transitions.length})`);
tabs.tabContents.push();
}
if (receipt.exceptions && receipt.exceptions.length > 0) {
tabs.tabHeaders.push("exceptions");
tabs.tabTitles.push(`Exceptions (${receipt.exceptions.length})`);
tabs.tabContents.push();
}
if (receipt.errors && Object.keys(receipt.errors).length > 0) {
tabs.tabHeaders.push("errors");
tabs.tabTitles.push("Errors");
tabs.tabContents.push();
}
return tabs;
};
const InfoTabs: React.FC = ({ tabs }) => {
const { tabHeaders, tabTitles, tabContents } = tabs;
const [currTab, setCurrTab] = useState(tabHeaders[0]);
return (
<>
{tabHeaders.length > 0 ? (
k && setCurrTab(k)}
>
{tabHeaders.map((tabHeader: string, index: number) => (
))}
{tabContents[tabHeaders.indexOf(currTab)]}
) : null}
>
);
};
export default InfoTabs;