Commit 9657e427 authored by tom goriunov's avatar tom goriunov Committed by GitHub

Pass the file path when verifying a multi-part contract (#2208)

Fixes #2207
parent 344fbaef
...@@ -18,13 +18,14 @@ type FileTypes = '.sol' | '.yul' | '.json' | '.vy' ...@@ -18,13 +18,14 @@ type FileTypes = '.sol' | '.yul' | '.json' | '.vy'
interface Props { interface Props {
name?: 'sources' | 'interfaces'; name?: 'sources' | 'interfaces';
fileTypes: Array<FileTypes>; fileTypes: Array<FileTypes>;
fullFilePath?: boolean;
multiple?: boolean; multiple?: boolean;
required?: boolean; required?: boolean;
title: string; title: string;
hint: string | React.ReactNode; hint: string | React.ReactNode;
} }
const ContractVerificationFieldSources = ({ fileTypes, multiple, required, title, hint, name = 'sources' }: Props) => { const ContractVerificationFieldSources = ({ fileTypes, multiple, required, title, hint, name = 'sources', fullFilePath }: Props) => {
const { setValue, getValues, control, formState, clearErrors } = useFormContext<FormFields>(); const { setValue, getValues, control, formState, clearErrors } = useFormContext<FormFields>();
const error = (() => { const error = (() => {
...@@ -114,7 +115,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, required, title ...@@ -114,7 +115,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, required, title
rowGap={ 2 } rowGap={ 2 }
w="100%" w="100%"
> >
<DragAndDropArea onDrop={ onChange } p={{ base: 3, lg: 6 }} isDisabled={ formState.isSubmitting }> <DragAndDropArea onDrop={ onChange } fullFilePath={ fullFilePath } p={{ base: 3, lg: 6 }} isDisabled={ formState.isSubmitting }>
{ hasValue ? renderFiles(field.value) : renderUploadButton() } { hasValue ? renderFiles(field.value) : renderUploadButton() }
</DragAndDropArea> </DragAndDropArea>
</Flex> </Flex>
...@@ -123,7 +124,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, required, title ...@@ -123,7 +124,7 @@ const ContractVerificationFieldSources = ({ fileTypes, multiple, required, title
{ errorElement } { errorElement }
</> </>
); );
}, [ fileTypes, multiple, commonError, formState.isSubmitting, renderFiles, renderUploadButton ]); }, [ fileTypes, multiple, commonError, formState.isSubmitting, renderFiles, renderUploadButton, fullFilePath ]);
const validateFileType = React.useCallback(async(value: FieldPathValue<FormFields, typeof name>): Promise<ValidateResult> => { const validateFileType = React.useCallback(async(value: FieldPathValue<FormFields, typeof name>): Promise<ValidateResult> => {
if (Array.isArray(value)) { if (Array.isArray(value)) {
......
...@@ -18,6 +18,7 @@ const ContractVerificationMultiPartFile = () => { ...@@ -18,6 +18,7 @@ const ContractVerificationMultiPartFile = () => {
<ContractVerificationFieldSources <ContractVerificationFieldSources
fileTypes={ FILE_TYPES } fileTypes={ FILE_TYPES }
multiple multiple
fullFilePath
required required
title="Sources *.sol or *.yul files" title="Sources *.sol or *.yul files"
hint="Upload all Solidity or Yul contract source files." hint="Upload all Solidity or Yul contract source files."
......
...@@ -37,6 +37,7 @@ const ContractVerificationVyperMultiPartFile = () => { ...@@ -37,6 +37,7 @@ const ContractVerificationVyperMultiPartFile = () => {
name="interfaces" name="interfaces"
fileTypes={ INTERFACE_TYPES } fileTypes={ INTERFACE_TYPES }
multiple multiple
fullFilePath
title="Interfaces (.vy or .json)" title="Interfaces (.vy or .json)"
hint={ interfacesHint } hint={ interfacesHint }
/> />
......
...@@ -9,9 +9,10 @@ interface Props { ...@@ -9,9 +9,10 @@ interface Props {
onDrop: (files: Array<File>) => void; onDrop: (files: Array<File>) => void;
className?: string; className?: string;
isDisabled?: boolean; isDisabled?: boolean;
fullFilePath?: boolean;
} }
const DragAndDropArea = ({ onDrop, children, className, isDisabled }: Props) => { const DragAndDropArea = ({ onDrop, children, className, isDisabled, fullFilePath }: Props) => {
const [ isDragOver, setIsDragOver ] = React.useState(false); const [ isDragOver, setIsDragOver ] = React.useState(false);
const handleDrop = React.useCallback(async(event: DragEvent<HTMLDivElement>) => { const handleDrop = React.useCallback(async(event: DragEvent<HTMLDivElement>) => {
...@@ -22,11 +23,11 @@ const DragAndDropArea = ({ onDrop, children, className, isDisabled }: Props) => ...@@ -22,11 +23,11 @@ const DragAndDropArea = ({ onDrop, children, className, isDisabled }: Props) =>
} }
const fileEntries = await getAllFileEntries(event.dataTransfer.items); const fileEntries = await getAllFileEntries(event.dataTransfer.items);
const files = await Promise.all(fileEntries.map(convertFileEntryToFile)); const files = await Promise.all(fileEntries.map((fileEntry) => convertFileEntryToFile(fileEntry, fullFilePath)));
onDrop(files); onDrop(files);
setIsDragOver(false); setIsDragOver(false);
}, [ isDisabled, onDrop ]); }, [ isDisabled, onDrop, fullFilePath ]);
const handleDragOver = React.useCallback((event: DragEvent<HTMLDivElement>) => { const handleDragOver = React.useCallback((event: DragEvent<HTMLDivElement>) => {
event.preventDefault(); event.preventDefault();
......
import stripLeadingSlash from 'lib/stripLeadingSlash';
// Function to get all files in drop directory // Function to get all files in drop directory
export async function getAllFileEntries(dataTransferItemList: DataTransferItemList): Promise<Array<FileSystemFileEntry>> { export async function getAllFileEntries(dataTransferItemList: DataTransferItemList): Promise<Array<FileSystemFileEntry>> {
const fileEntries: Array<FileSystemFileEntry> = []; const fileEntries: Array<FileSystemFileEntry> = [];
...@@ -54,11 +56,13 @@ async function readEntriesPromise(directoryReader: DirectoryReader): Promise<Arr ...@@ -54,11 +56,13 @@ async function readEntriesPromise(directoryReader: DirectoryReader): Promise<Arr
} catch (err) {} } catch (err) {}
} }
export function convertFileEntryToFile(entry: FileSystemFileEntry): Promise<File> { export function convertFileEntryToFile(entry: FileSystemFileEntry, fullFilePath?: boolean): Promise<File> {
return new Promise((resolve) => { return new Promise((resolve) => {
entry.file(async(file: File) => { entry.file(async(file: File) => {
// const newFile = new File([ file ], entry.fullPath, { lastModified: file.lastModified, type: file.type }); const newFile = fullFilePath ?
resolve(file); new File([ file ], stripLeadingSlash(entry.fullPath), { lastModified: file.lastModified, type: file.type }) :
file;
resolve(newFile);
}); });
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment