Skip to content

Commit

Permalink
fix: Refactoring and useless states removed
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Jul 22, 2022
1 parent 8d57699 commit 30c7e38
Show file tree
Hide file tree
Showing 6 changed files with 1,396 additions and 1,421 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/core": "^7.18.5",
"@babel/core": "^7.18.9",
"@chakra-ui/icons": "^1.1.7",
"@chakra-ui/react": "^1.6.6",
"@chakra-ui/system": "^1.6.6",
Expand All @@ -16,7 +16,7 @@
"ethers": "^5.6.9",
"framer-motion": "^4.1.17",
"js-file-download": "^0.4.12",
"next": "^12.1.6",
"next": "^12.2.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-icons": "^4.4.0",
Expand Down Expand Up @@ -45,22 +45,22 @@
]
},
"devDependencies": {
"@next/eslint-plugin-next": "^12.1.6",
"@types/crypto-js": "^4.0.1",
"@next/eslint-plugin-next": "^12.2.3",
"@types/crypto-js": "^4.1.1",
"@types/node": "^12.20.15",
"@types/react": "^17.0.11",
"@types/react-dom": "^17.0.8",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"cypress": "^10.2.0",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"cypress": "^10.3.1",
"cypress-file-upload": "^5.0.8",
"eslint": "^8.18.0",
"eslint": "^8.20.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.6.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "^2.7.1"
Expand Down
51 changes: 22 additions & 29 deletions pages/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ const Dashboard = (): JSX.Element => {
const [isUpdateLoading, setIsUpdateLoading] = useState(false);
const [isCreateFolderLoading, setIsCreateFolderLoading] = useState(false);
const [fileEvent, setFileEvent] = useState<ChangeEvent<HTMLInputElement> | undefined>(undefined);
const [contactsNameEvent, setContactNameEvent] = useState<ChangeEvent<HTMLInputElement> | undefined>(undefined);
const [fileNameEvent, setFileNameEvent] = useState<ChangeEvent<HTMLInputElement> | undefined>(undefined);
const [folderNameEvent, setFolderNameEvent] = useState<ChangeEvent<HTMLInputElement> | undefined>(undefined);
const [newPath, setNewPath] = useState('');
const [nameEvent, setNameEvent] = useState('');
const [contactsPublicKeyEvent, setContactPublicKeyEvent] = useState<ChangeEvent<HTMLInputElement> | undefined>(
undefined,
);
Expand Down Expand Up @@ -208,8 +205,8 @@ const Dashboard = (): JSX.Element => {
};

const updateFileName = async () => {
if (fileNameEvent) {
const filename = fileNameEvent.target.value;
if (nameEvent) {
const filename = nameEvent;
const update = await user.contact.updateFileName(selectedFile, filename);
toast({ title: update.message, status: update.success ? 'success' : 'error' });
if (update.success) {
Expand All @@ -218,27 +215,28 @@ const Dashboard = (): JSX.Element => {
if (index !== -1) files[index].name = filename;
setFiles(files);
}
onCloseUpdateFileName();
}
setNameEvent('');
onCloseUpdateFileName();
};

const moveFile = async () => {
const formattedPath = formatPath(newPath);
const formattedPath = formatPath(nameEvent);

if (!isValidFolderPath(formattedPath, user.drive.folders)) {
toast({ title: 'Invalid path', status: 'error' });
return;
}

const moved = await user.contact.moveFile(selectedFile, formattedPath);

toast({ title: moved.message, status: moved.success ? 'success' : 'error' });

const index = files.indexOf(selectedFile);
if (index !== -1) {
files[index].path = formattedPath;
setFiles(files);
}
setNameEvent('');
onCloseMoveFile();
};

Expand All @@ -252,12 +250,9 @@ const Dashboard = (): JSX.Element => {
if (!fileContent) return;

const newFile: IPCFile = {
name: oldFile.name,
...oldFile,
hash: fileContent,
size: oldFile.size,
createdAt: oldFile.createdAt,
key: { iv: '', ephemPublicKey: '', ciphertext: '', mac: '' },
path: oldFile.path,
};
setIsUpdateLoading(true);
const upload = await user.drive.upload(newFile, key);
Expand Down Expand Up @@ -294,9 +289,9 @@ const Dashboard = (): JSX.Element => {
};

const addContact = async () => {
if (contactsNameEvent && contactsPublicKeyEvent) {
if (nameEvent && contactsPublicKeyEvent) {
const add = await user.contact.add({
name: contactsNameEvent.target.value,
name: nameEvent,
address: EthCrypto.publicKey.toAddress(contactsPublicKeyEvent.target.value.slice(2)),
publicKey: contactsPublicKeyEvent.target.value,
files: [],
Expand All @@ -308,20 +303,19 @@ const Dashboard = (): JSX.Element => {
} else {
toast({ title: 'Bad contact infos', status: 'error' });
}
setNameEvent('');
onCloseContactAdd();
};

const updateContact = async () => {
if (contactsPublicKeyEvent) {
const update = await user.contact.update(
contactInfos.address,
contactsNameEvent ? contactsNameEvent.target.value : contactInfos.name,
);
const update = await user.contact.update(contactInfos.address, nameEvent || contactInfos.name);
toast({ title: update.message, status: update.success ? 'success' : 'error' });
setContacts(user.contact.contacts);
} else {
toast({ title: 'Invalid address', status: 'error' });
}
setNameEvent('');
onCloseContactUpdate();
};

Expand All @@ -340,11 +334,9 @@ const Dashboard = (): JSX.Element => {

const createFolder = async () => {
setIsCreateFolderLoading(true);
if (folderNameEvent) {
const name = folderNameEvent.target.value;

if (nameEvent) {
const folder: IPCFolder = {
name,
name: nameEvent,
path,
createdAt: Date.now(),
};
Expand All @@ -354,9 +346,10 @@ const Dashboard = (): JSX.Element => {
if (created.success) {
setFolders([...folders, folder]);
}
onCloseCreateFolder();
}
setIsCreateFolderLoading(false);
setNameEvent('');
onCloseCreateFolder();
};

return (
Expand Down Expand Up @@ -478,7 +471,7 @@ const Dashboard = (): JSX.Element => {
w="100%"
p="10px"
my="4px"
onChange={(e: ChangeEvent<HTMLInputElement>) => setFolderNameEvent(e)}
onChange={(e: ChangeEvent<HTMLInputElement>) => setNameEvent(e.target.value)}
id="ipc-dashboard-input-folder-name"
/>
</FormControl>
Expand Down Expand Up @@ -507,7 +500,7 @@ const Dashboard = (): JSX.Element => {
p="10px"
my="4px"
placeholder="Name"
onChange={(e: ChangeEvent<HTMLInputElement>) => setContactNameEvent(e)}
onChange={(e: ChangeEvent<HTMLInputElement>) => setNameEvent(e.target.value)}
id="ipc-dashboard-input-contact-name"
/>
<Input
Expand Down Expand Up @@ -546,7 +539,7 @@ const Dashboard = (): JSX.Element => {
p="10px"
my="4px"
placeholder={contactInfos.name}
onChange={(e: ChangeEvent<HTMLInputElement>) => setContactNameEvent(e)}
onChange={(e: ChangeEvent<HTMLInputElement>) => setNameEvent(e.target.value)}
id="ipc-dashboard-input-contact-name"
/>
</FormControl>
Expand Down Expand Up @@ -576,7 +569,7 @@ const Dashboard = (): JSX.Element => {
p="10px"
my="4px"
placeholder={selectedFile.name}
onChange={(e: ChangeEvent<HTMLInputElement>) => setFileNameEvent(e)}
onChange={(e: ChangeEvent<HTMLInputElement>) => setNameEvent(e.target.value)}
id="ipc-dashboard-input-update-filename"
/>
</FormControl>
Expand Down Expand Up @@ -618,7 +611,7 @@ const Dashboard = (): JSX.Element => {
p="10px"
my="4px"
placeholder={`Current: '${selectedFile.path}'`}
onChange={(e: ChangeEvent<HTMLInputElement>) => setNewPath(e.target.value)}
onChange={(e: ChangeEvent<HTMLInputElement>) => setNameEvent(e.target.value)}
id="ipc-dashboard-input-move-file"
/>
</FormControl>
Expand Down
10 changes: 3 additions & 7 deletions src/lib/computing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,8 @@ class Computing {
): Promise<ResponseType> {
try {
if (this.account) {
if (oldProgram && oldProgram.hash) {
const newProgramsArray: IPCProgram[] = this.programs.filter(
(prog: IPCProgram) => prog.hash !== oldProgram.hash,
);
this.programs = newProgramsArray;
if (oldProgram) {
this.programs = this.programs.filter((prog) => prog.hash !== oldProgram.hash);
await this.deleteProgram(oldProgram.hash);
}

Expand All @@ -104,9 +101,8 @@ class Computing {
});

const newProgram: IPCProgram = {
name: myProgram.name,
...myProgram,
hash: programHashPublishProgram.item_hash,
createdAt: myProgram.createdAt,
};

this.programs.push(newProgram);
Expand Down
109 changes: 40 additions & 69 deletions src/lib/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,18 @@ class Contact {
public async updateFileContent(newFile: IPCFile, oldHash: string): Promise<ResponseType> {
try {
if (this.account) {
await Promise.all(
this.contacts.map(async (contact, i) => {
const file = this.contacts[i].files.find((f) => f.hash === oldHash);

if (file) {
file.hash = newFile.hash;
file.key = await encryptWithPublicKey(
contact.publicKey.slice(2),
await decryptWithPrivateKey(this.private_key, newFile.key),
);
await this.publishAggregate();
}
}),
);
this.contacts.forEach(async (contact, i) => {
const file = this.contacts[i].files.find((f) => f.hash === oldHash);

if (file) {
file.hash = newFile.hash;
file.key = await encryptWithPublicKey(
contact.publicKey.slice(2),
await decryptWithPrivateKey(this.private_key, newFile.key),
);
await this.publishAggregate();
}
});
return { success: true, message: 'File content updated' };
}
return { success: false, message: 'Failed to load account' };
Expand Down Expand Up @@ -170,28 +168,15 @@ class Contact {

public async updateFileName(concernedFile: IPCFile, newName: string): Promise<ResponseType> {
try {
for (let i = 0; this.contacts[i] != null; i += 1) {
this.updateOneFileName(concernedFile.hash, newName, i);
}
return { success: true, message: 'Filename updated' };
} catch (err) {
console.error(err);
return { success: false, message: 'Failed to update this filename' };
}
}
this.contacts.forEach(async (contact) => {
const file = contact.files.find((f) => f.hash === concernedFile.hash);

private async updateOneFileName(fileHash: string, newName: string, contactIndex: number): Promise<ResponseType> {
try {
if (this.account) {
const file = this.contacts[contactIndex].files.find((f) => f.hash === fileHash);
if (file) {
file.name = newName;
await this.publishAggregate();
return { success: true, message: 'Filename updated' };
}
return { success: false, message: 'File does not exist' };
}
return { success: false, message: 'Failed to load account' };
});
return { success: true, message: 'Filename updated' };
} catch (err) {
console.error(err);
return { success: false, message: 'Failed to update this filename' };
Expand All @@ -201,32 +186,24 @@ class Contact {
public async addFileToContact(contactAddress: string, mainFile: IPCFile): Promise<ResponseType> {
try {
if (this.account) {
if (
await Promise.all(
this.contacts.map(async (contact, index) => {
if (contact.address === contactAddress) {
if (this.contacts[index].files.find((file) => file.hash === mainFile.hash)) {
return { success: false, message: 'The file is already shared' };
}
this.contacts[index].files.push({
hash: mainFile.hash,
key: await encryptWithPublicKey(
contact.publicKey.slice(2),
await decryptWithPrivateKey(this.private_key, mainFile.key),
),
createdAt: mainFile.createdAt,
name: mainFile.name,
path: mainFile.path,
size: mainFile.size,
});
await this.publishAggregate();
return true;
}
return false;
}),
)
)
const index = this.contacts.findIndex((contact) => contact.address === contactAddress);

if (index !== -1) {
if (this.contacts[index].files.find((file) => file.hash === mainFile.hash)) {
return { success: false, message: 'The file is already shared' };
}
const newFile: IPCFile = {
...mainFile,
key: await encryptWithPublicKey(
this.contacts[index].publicKey.slice(2),
await decryptWithPrivateKey(this.private_key, mainFile.key),
),
};

this.contacts[index].files.push(newFile);
await this.publishAggregate();
return { success: true, message: 'File shared with the contact' };
}
return { success: false, message: 'Contact does not exist' };
}
return { success: false, message: 'Failed to load account' };
Expand All @@ -239,20 +216,14 @@ class Contact {
public async removeFileFromContact(contactAddress: string, file: IPCFile): Promise<ResponseType> {
try {
if (this.account) {
if (
await Promise.all(
this.contacts.map(async (contact, index) => {
if (contact.address === contactAddress) {
this.contacts[index].files = this.contacts[index].files.filter((f) => f.hash !== file.hash);

await this.publishAggregate();
return true;
}
return false;
}),
)
)
const index = this.contacts.findIndex((contact) => contact.address === contactAddress);

if (index !== -1) {
this.contacts[index].files = this.contacts[index].files.filter((f) => f.hash !== file.hash);

await this.publishAggregate();
return { success: true, message: 'File deleted from the contact' };
}
return { success: false, message: 'Contact does not exist' };
}
return { success: false, message: 'Failed to load account' };
Expand Down
Loading

0 comments on commit 30c7e38

Please sign in to comment.