Skip to content

Commit

Permalink
Track event metrics in vercel
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Apr 11, 2024
1 parent f463e34 commit 4b428ff
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 23 deletions.
4 changes: 3 additions & 1 deletion app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useLog } from './features/editor/hooks/useLog';
import { Toolchain } from './features/editor/components/ToolchainDropdown';
import { useTranspile } from './features/editor/hooks/useTranspile';
import EditorView from './features/editor/components/EditorView';
import { Analytics } from '@vercel/analytics/react';
import { Analytics, track } from '@vercel/analytics/react';

const DRAWER_WIDTH = '40vw';

Expand Down Expand Up @@ -88,8 +88,10 @@ function App() {
);

const onCompileClick = useCallback(() => {
track('Compile Click', { toolchain });
if (showSolidity) {
// Transpile the Solidity code before compiling.
track('Transpile');
setCodeToTranspile(solidityCode);
} else {
setCodeToCompile(swayCode);
Expand Down
8 changes: 5 additions & 3 deletions app/src/features/editor/components/ExampleDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ function ExampleDropdown({
examples,
style,
}: ExampleDropdownProps) {
const [currentExample, setCurrentExample] =
React.useState<ExampleMenuItem | null>(null);
const [currentExample, setCurrentExample] = React.useState<ExampleMenuItem>({
label: '',
code: '',
});

const onChange = useCallback(
(event: any) => {
Expand All @@ -47,7 +49,7 @@ function ExampleDropdown({
label='Example'
variant='outlined'
style={{ minWidth: '110px', background: 'white' }}
value={currentExample?.label}
value={currentExample.label}
onChange={onChange}>
{examples.map(({ label }: ExampleMenuItem, index) => (
<MenuItem key={`${label}-${index}`} value={index}>
Expand Down
8 changes: 7 additions & 1 deletion app/src/features/editor/hooks/useCompile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { CopyableHex } from '../../../components/shared';
import { Toolchain } from '../components/ToolchainDropdown';
import { SERVER_URI } from '../../../constants';
import { track } from '@vercel/analytics/react';

function toResults(
prefixedBytecode: string,
Expand Down Expand Up @@ -63,6 +64,10 @@ export function useCompile(
if (response.status < 400) {
return response.json();
} else {
track('Compile Error', {
source: 'network',
status: response.status,
});
setServerError(true);
}
})
Expand Down Expand Up @@ -94,6 +99,7 @@ export function useCompile(
}
})
.catch(() => {
track('Compile Error', { source: 'network' });
setServerError(true);
});
setIsCompiled(true);
Expand All @@ -113,4 +119,4 @@ export function useCompile(
setVersion(undefined);
}
}, [setResults, version]);
}
}
6 changes: 6 additions & 0 deletions app/src/features/editor/hooks/useTranspile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import styled from '@emotion/styled';
import ansicolor from 'ansicolor';
import React, { useState, useEffect } from 'react';
import { SERVER_URI } from '../../../constants';
import { track } from '@vercel/analytics/react';

export function useTranspile(
code: string | undefined,
Expand Down Expand Up @@ -40,6 +41,10 @@ export function useTranspile(
if (response.status < 400) {
return response.json();
} else {
track('Transpile Error', {
source: 'network',
status: response.status,
});
setServerError(true);
}
})
Expand All @@ -64,6 +69,7 @@ export function useTranspile(
}
})
.catch(() => {
track('Transpile Error', { source: 'network' });
setServerError(true);
});
}, [code, setResults, onSwayCodeChange, setCodeToCompile]);
Expand Down
7 changes: 5 additions & 2 deletions app/src/features/interact/components/CallButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useCallFunction } from '../hooks/useCallFunction';
import { CallType } from '../../../utils/types';
import { CallableParamValue } from './FunctionParameters';
import SecondaryButton from '../../../components/SecondaryButton';
import { track } from '@vercel/analytics/react';
import { useCallback } from 'react';

interface CallButtonProps {
contractId: string;
Expand Down Expand Up @@ -29,10 +31,11 @@ export function CallButton({
updateLog,
});

function onFunctionClick() {
const onFunctionClick = useCallback(() => {
track('Function Call Click', { callType });
setResponse('');
functionMutation.mutate();
}
}, [callType, functionMutation, setResponse]);

return (
<SecondaryButton
Expand Down
2 changes: 2 additions & 0 deletions app/src/features/toolbar/components/DeploymentButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import SecondaryButton from '../../../components/SecondaryButton';
import { ButtonSpinner } from '../../../components/shared';
import { useConnectIfNotAlready } from '../hooks/useConnectIfNotAlready';
import { track } from '@vercel/analytics/react';

interface DeploymentButtonProps {
abi: string;
Expand Down Expand Up @@ -72,6 +73,7 @@ export function DeploymentButton({
);

const onDeployClick = useCallback(async () => {
track('Deploy Click');
if (!isConnected) {
updateLog(`Connecting to wallet...`);
}
Expand Down
43 changes: 27 additions & 16 deletions app/src/features/toolbar/hooks/useDeployContract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ContractFactory, JsonAbi, StorageSlot } from 'fuels';
import { useMutation } from '@tanstack/react-query';
import { useFuel, useWallet } from '@fuels/react';
import { useConnectors, useFuel, useWallet } from '@fuels/react';
import { track } from '@vercel/analytics/react';
import { useMemo } from 'react';

const DEPLOYMENT_TIMEOUT_MS = 120000;

Expand All @@ -19,14 +21,25 @@ export function useDeployContract(
) {
const { wallet, isLoading: walletIsLoading } = useWallet();
const { fuel } = useFuel();
const { connectors } = useConnectors();

const walletName = useMemo(() => {
const currentConnector = connectors.find(
(connector) => connector.connected
);
return !!wallet && !!currentConnector ? currentConnector.name : 'none';
}, [connectors, wallet]);

const mutation = useMutation({
// Retry once if the wallet is still loading.
retry: walletIsLoading && !wallet ? 1 : 0,
onSuccess: onSuccess,
onError: onError,
onSuccess,
onError: (error) => {
track('Deploy Error', { source: error.name, walletName });
onError(error);
},
mutationFn: async () => {
const network = await fuel.currentNetwork();
const { url: networkUrl } = await fuel.currentNetwork();
if (!wallet) {
if (walletIsLoading) {
updateLog('Connecting to wallet...');
Expand All @@ -49,26 +62,24 @@ export function useDeployContract(
});
resolve({
contractId: contract.id.toB256(),
networkUrl: network.url,
networkUrl,
});
} catch (error) {
track('Deploy Error', { source: 'sdk', networkUrl, walletName });
reject(error);
}
}
);

const timeoutPromise = new Promise((_resolve, reject) =>
setTimeout(
() =>
reject(
new Error(
`Request timed out after ${
DEPLOYMENT_TIMEOUT_MS / 1000
} seconds`
)
),
DEPLOYMENT_TIMEOUT_MS
)
setTimeout(() => {
track('Deploy Error', { source: 'timeout', networkUrl, walletName });
reject(
new Error(
`Request timed out after ${DEPLOYMENT_TIMEOUT_MS / 1000} seconds`
)
);
}, DEPLOYMENT_TIMEOUT_MS)
);

return await Promise.race([resultPromise, timeoutPromise]);
Expand Down

0 comments on commit 4b428ff

Please sign in to comment.