-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
291 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
Alert, | ||
AlertIcon, | ||
Button, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
Skeleton, | ||
Text, | ||
} from "@chakra-ui/react"; | ||
import { useImport } from "../hooks/useImport"; | ||
import { | ||
useSessionStoreWithoutUndo, | ||
useSessionStoreWithUndo, | ||
} from "../hooks/useSessionStore"; | ||
import { mapITransactionPreviewToITransaction } from "../models/preview-mappers"; | ||
import { DEFAULT_IMPORT } from "../models/state-default"; | ||
import { TransactionPreview } from "./palette/preview/TransactionPreview"; | ||
|
||
export const ImportModal: React.FC = () => { | ||
const [isLoading, transaction, setImport] = useSessionStoreWithoutUndo( | ||
(state) => [state.import.isLoading, state.import.transaction, state.set] | ||
); | ||
const setTransaction = useSessionStoreWithUndo((state) => state.set); | ||
|
||
useImport(); // reads the query params | ||
|
||
const onConfirm = () => { | ||
setTransaction((state) => { | ||
state.transaction = mapITransactionPreviewToITransaction(transaction!); | ||
}); | ||
onClose(); | ||
}; | ||
|
||
const onClose = () => { | ||
setImport((state) => { | ||
state.import = DEFAULT_IMPORT; | ||
}); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
size="lg" | ||
closeOnOverlayClick={false} | ||
isOpen={transaction !== undefined || isLoading} | ||
onClose={onClose} | ||
> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader>Import Transaction</ModalHeader> | ||
|
||
<ModalCloseButton /> | ||
|
||
<ModalBody> | ||
<Alert variant="left-accent" status="warning" mb="3"> | ||
<AlertIcon /> | ||
Only import and run transactions from trusted sources! | ||
</Alert> | ||
|
||
<Text mb="5">You are about to import the following transaction:</Text> | ||
|
||
{transaction === undefined ? ( | ||
<Skeleton h="200px" /> | ||
) : ( | ||
<TransactionPreview | ||
transaction={transaction!} | ||
interactive={false} | ||
/> | ||
)} | ||
</ModalBody> | ||
|
||
<ModalFooter> | ||
<Button | ||
colorScheme="teal" | ||
mr="2" | ||
isDisabled={transaction === undefined} | ||
onClick={onConfirm} | ||
> | ||
Confirm | ||
</Button> | ||
<Button onClick={onClose}>Cancel</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { useToast } from "@chakra-ui/react"; | ||
import Ajv from "ajv"; | ||
import axios from "axios"; | ||
import { useEffect, useMemo } from "react"; | ||
import { useSearchParams } from "react-router-dom"; | ||
import { JSON_SCHEMA } from "../models/external-types"; | ||
import { | ||
mapITransactionExtToITransactionPreview, | ||
mapTransactionResponseToITransactionPreview, | ||
} from "../models/preview-mappers"; | ||
import { ITransactionPreview } from "../models/preview-types"; | ||
import { useGetWeb3Transaction } from "./useGetWeb3Transaction"; | ||
import { usePersistentStore } from "./usePersistentStore"; | ||
import { useSessionStoreWithoutUndo } from "./useSessionStore"; | ||
|
||
export const useImport = () => { | ||
const [set, setIsLoading] = useSessionStoreWithoutUndo((state) => [ | ||
state.set, | ||
(value: boolean) => { | ||
state.set((state) => { | ||
state.import.isLoading = value; | ||
}); | ||
}, | ||
]); | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
|
||
const toast = useToast(); | ||
|
||
// determine network | ||
const network = searchParams.get("network"); | ||
const rpcEndpoints = usePersistentStore( | ||
(state) => state.appOptions.rpcEndpoints | ||
); | ||
const rpcEndpoint = Object.values(rpcEndpoints.map).find( | ||
(endpoint) => endpoint.network === (network || "mainnet-beta") | ||
)!; | ||
|
||
const { start } = useGetWeb3Transaction({ | ||
rpcEndpointUrl: rpcEndpoint.url, | ||
onFinalised: (response) => { | ||
set((state) => { | ||
state.import = { | ||
isLoading: false, | ||
transaction: mapTransactionResponseToITransactionPreview( | ||
response, | ||
rpcEndpoint | ||
), | ||
}; | ||
}); | ||
}, | ||
onError: (error) => { | ||
setIsLoading(false); | ||
toast({ | ||
title: "Transaction import failed", | ||
description: `Failed to fetch transcation: ${error.message}`, | ||
status: "error", | ||
duration: 15000, | ||
isClosable: true, | ||
}); | ||
}, | ||
}); | ||
|
||
const validate = useMemo(() => new Ajv().compile(JSON_SCHEMA), []); | ||
|
||
// TODO load the transaction in the preview sidebar | ||
// const preview = searchParams.has("preview"); | ||
|
||
const tx = searchParams.get("tx"); | ||
const share = searchParams.get("share"); | ||
|
||
// import from transaction ID | ||
useEffect(() => { | ||
if (!tx) return; | ||
|
||
setIsLoading(true); | ||
setSearchParams({}); | ||
|
||
start(tx, true); | ||
}, [tx, setIsLoading, start, setSearchParams]); | ||
|
||
// import from share URL | ||
useEffect(() => { | ||
if (!share) return; | ||
|
||
setIsLoading(true); | ||
setSearchParams({}); | ||
|
||
axios | ||
.get(share) | ||
.then((response) => { | ||
if (!validate(response.data)) { | ||
setIsLoading(false); | ||
// TODO why is this triggering 3 times? | ||
toast({ | ||
title: "Transaction import failed", | ||
description: `Invalid JSON from URL`, | ||
status: "error", | ||
duration: 15000, | ||
isClosable: true, | ||
}); | ||
return; | ||
} | ||
|
||
set((state) => { | ||
state.import = { | ||
isLoading: false, | ||
transaction: mapITransactionExtToITransactionPreview( | ||
response.data as ITransactionPreview, | ||
"shareUrl", | ||
share | ||
), | ||
}; | ||
}); | ||
}) | ||
.catch((err) => { | ||
setIsLoading(false); | ||
toast({ | ||
title: "Transaction import failed", | ||
description: `Cannot fetch tranasction from URL: ${err}`, | ||
status: "error", | ||
duration: 15000, | ||
isClosable: true, | ||
}); | ||
}); | ||
}, [share, set, setIsLoading, setSearchParams, toast, validate]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.