File prefixed logs

This commit is contained in:
muzam
2022-02-01 18:42:19 +05:30
parent e6f613ae0b
commit 4f042ef3b7
3 changed files with 34 additions and 14 deletions

View File

@@ -31,6 +31,7 @@ interface Props {
children: ReactElement<TabProps>[]; children: ReactElement<TabProps>[];
keepAllAlive?: boolean; keepAllAlive?: boolean;
defaultExtension?: string; defaultExtension?: string;
forceDefaultExtension?: boolean;
onCreateNewTab?: (name: string) => any; onCreateNewTab?: (name: string) => any;
onCloseTab?: (index: number, header?: string) => any; onCloseTab?: (index: number, header?: string) => any;
} }
@@ -46,6 +47,7 @@ export const Tabs = ({
onCreateNewTab, onCreateNewTab,
onCloseTab, onCloseTab,
defaultExtension = "", defaultExtension = "",
forceDefaultExtension,
}: Props) => { }: Props) => {
const [active, setActive] = useState(activeIndex || 0); const [active, setActive] = useState(activeIndex || 0);
const tabs: TabProps[] = children.map(elem => elem.props); const tabs: TabProps[] = children.map(elem => elem.props);
@@ -83,6 +85,10 @@ export const Tabs = ({
const handleCreateTab = useCallback(() => { const handleCreateTab = useCallback(() => {
// add default extension in case omitted // add default extension in case omitted
let _tabname = tabname.includes(".") ? tabname : tabname + defaultExtension; let _tabname = tabname.includes(".") ? tabname : tabname + defaultExtension;
if (forceDefaultExtension && !_tabname.endsWith(defaultExtension)) {
_tabname = _tabname + defaultExtension;
}
const chk = validateTabname(_tabname); const chk = validateTabname(_tabname);
if (chk.error) { if (chk.error) {
setNewtabError(`Error: ${chk.error}`); setNewtabError(`Error: ${chk.error}`);

View File

@@ -18,7 +18,11 @@ const Accounts = dynamic(() => import("../../components/Accounts"), {
type TxFields = Omit<typeof transactionsData[0], "Account" | "Sequence" | "TransactionType">; type TxFields = Omit<typeof transactionsData[0], "Account" | "Sequence" | "TransactionType">;
type OtherFields = (keyof Omit<TxFields, "Destination">)[]; type OtherFields = (keyof Omit<TxFields, "Destination">)[];
const Transaction: FC = props => { interface Props {
header?: string;
}
const Transaction: FC<Props> = ({ header, ...props }) => {
const snap = useSnapshot(state); const snap = useSnapshot(state);
const transactionsOptions = transactionsData.map(tx => ({ const transactionsOptions = transactionsData.map(tx => ({
@@ -118,10 +122,15 @@ const Transaction: FC = props => {
delete options[field]; delete options[field];
} }
}); });
await sendTransaction(account, { const logPrefix = header ? `${header.split(".")[0]}: ` : undefined;
await sendTransaction(
account,
{
TransactionType, TransactionType,
...options, ...options,
}); },
{ logPrefix }
);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
if (error instanceof Error) { if (error instanceof Error) {
@@ -130,9 +139,10 @@ const Transaction: FC = props => {
} }
setTxIsLoading(false); setTxIsLoading(false);
}, [ }, [
selectedAccount, header,
selectedDestAccount, selectedAccount?.value,
selectedTransaction, selectedDestAccount?.value,
selectedTransaction?.value,
snap.accounts, snap.accounts,
txFields, txFields,
txIsDisabled, txIsDisabled,
@@ -291,13 +301,14 @@ const Test = () => {
<Box css={{ width: "60%", px: "$2", maxWidth: "800px", height: "100%", overflow: "auto" }}> <Box css={{ width: "60%", px: "$2", maxWidth: "800px", height: "100%", overflow: "auto" }}>
<Tabs <Tabs
keepAllAlive keepAllAlive
forceDefaultExtension
defaultExtension=".json" defaultExtension=".json"
onCreateNewTab={name => setTabHeaders(tabHeaders.concat(name))} onCreateNewTab={name => setTabHeaders(tabHeaders.concat(name))}
onCloseTab={index => setTabHeaders(tabHeaders.filter((_, idx) => idx !== index))} onCloseTab={index => setTabHeaders(tabHeaders.filter((_, idx) => idx !== index))}
> >
{tabHeaders.map(header => ( {tabHeaders.map(header => (
<Tab key={header} header={header}> <Tab key={header} header={header}>
<Transaction /> <Transaction header={header} />
</Tab> </Tab>
))} ))}
</Tabs> </Tabs>

View File

@@ -10,8 +10,11 @@ interface TransactionOptions {
Destination?: string Destination?: string
[index: string]: any [index: string]: any
} }
interface OtherOptions {
logPrefix?: string
}
export const sendTransaction = async (account: IAccount, txOptions: TransactionOptions) => { export const sendTransaction = async (account: IAccount, txOptions: TransactionOptions, options?: OtherOptions) => {
if (!state.client) throw Error('XRPL client not initalized') if (!state.client) throw Error('XRPL client not initalized')
const { Fee = "1000", ...opts } = txOptions const { Fee = "1000", ...opts } = txOptions
@@ -21,7 +24,7 @@ export const sendTransaction = async (account: IAccount, txOptions: TransactionO
Fee, // TODO auto-fillable Fee, // TODO auto-fillable
...opts ...opts
}; };
console.log({ tx }); const { logPrefix = '' } = options || {}
try { try {
const signedAccount = derive.familySeed(account.secret); const signedAccount = derive.familySeed(account.secret);
const { signedTransaction } = sign(tx, signedAccount); const { signedTransaction } = sign(tx, signedAccount);
@@ -32,19 +35,19 @@ export const sendTransaction = async (account: IAccount, txOptions: TransactionO
if (response.engine_result === "tesSUCCESS") { if (response.engine_result === "tesSUCCESS") {
state.transactionLogs.push({ state.transactionLogs.push({
type: 'success', type: 'success',
message: `Transaction success [${response.engine_result}]: ${response.engine_result_message}` message: `${logPrefix}[${response.engine_result}] ${response.engine_result_message}`
}) })
} else { } else {
state.transactionLogs.push({ state.transactionLogs.push({
type: "error", type: "error",
message: `[${response.error || response.engine_result}] ${response.error_exception || response.engine_result_message}`, message: `${logPrefix}[${response.error || response.engine_result}] ${response.error_exception || response.engine_result_message}`,
}); });
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
state.transactionLogs.push({ state.transactionLogs.push({
type: "error", type: "error",
message: err instanceof Error ? `Error: ${err.message}` : 'Something went wrong, try again later', message: err instanceof Error ? `${logPrefix}Error: ${err.message}` : `${logPrefix}Something went wrong, try again later`,
}); });
} }
}; };