Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program renaming feature #140

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f121167
feat: renaming the programs
Molaryy May 3, 2023
52f940b
feat: add programs to contact class
Molaryy May 3, 2023
338180d
feat: add programs to drive class constructor
Molaryy May 3, 2023
5b43894
feat: add id, encryptInfos && logs to the IPCProgram type
Molaryy May 3, 2023
65439db
feat: new button rename when right mouse is clicked on programs
Molaryy May 3, 2023
f2fcf9f
fix: yarn lint + env variables
Molaryy May 3, 2023
ad5322e
add: env variables example
Molaryy May 3, 2023
b047509
fix: compilation with yarn
Molaryy May 3, 2023
d9302dc
feat: removed deleteProgram component to start it in another branch
Molaryy May 8, 2023
c8d71da
feat: update program's name with Aleph
Molaryy May 8, 2023
e3b2b43
feat: update next version
Molaryy May 8, 2023
86237c1
feat: removed the program updateName function from contact class
Molaryy May 8, 2023
20e981d
feat: removed information in the program type that is not used
Molaryy May 8, 2023
288d0e6
feat: removed all about programs in the drive class
Molaryy May 8, 2023
798c72b
feat: now information updates information from computing class
Molaryy May 8, 2023
3c9108d
fix: removed variables that I deleted but were in the code
Molaryy May 9, 2023
d30bfeb
fix: removed variables that I deleted but were in the code
Molaryy May 9, 2023
a076667
feat: added delete component to the program options
Molaryy May 9, 2023
a48f027
feat: updated delete program function
Molaryy May 9, 2023
8d93d43
feat: added frontend for the delete program, there is a bug when tryi…
Molaryy May 9, 2023
df94e7e
feat(deleteProgram): updated delete program
Molaryy May 10, 2023
35767c4
feat(deleteProgram): need to use publishAgreate to remove the file
Molaryy May 10, 2023
96d2376
fix(deleteProgram): removed an unused variable
Molaryy May 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/components/computing/github/GithubModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,18 @@ const GithubModal = ({ isOpen, onClose }: { isOpen: boolean; onClose: () => void
});
if (result.status !== 200) throw new Error('Unable to clone repository from github');
const newProgram: IPCProgram = {
id: crypto.randomUUID(),
name: customName || result.data.name,
hash: result.data.item_hash,
createdAt: Date.now(),
entrypoint: result.data.entrypoint,
size: 0,
log: [
{
action: 'Program created',
date: Date.now(),
},
],
};
user.computing.programs.push(newProgram);
await user.computing.publishAggregate();
Expand Down
7 changes: 7 additions & 0 deletions src/components/computing/programs/ProgramModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ const ProgramModal = ({
try {
const upload = await user.computing.uploadProgram(
{
id: crypto.randomUUID(),
name: customName || filename,
hash: '',
createdAt: Date.now(),
entrypoint: customEntrypoint || user.config?.defaultEntrypoint.value || 'main:app',
size: fileEvent.target.files[0].size,
log: [
{
action: 'Program created',
date: Date.now(),
},
],
},
fileEvent.target.files[0],
!!oldProgram,
Expand Down
1 change: 1 addition & 0 deletions src/components/contact/AddContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const AddContact = (): JSX.Element => {
files: [],
folders: [],
config: undefined,
programs: []
});

toast({ title: add.message, status: add.success ? 'success' : 'error' });
Expand Down
8 changes: 7 additions & 1 deletion src/components/dashboardPage/ProgramOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ import {
} from '@chakra-ui/react';
import { useEffect } from 'react';


import { IPCProgram } from 'types/types';

import { useConfigContext } from 'contexts/config';


import DeployProgram from 'components/computing/programs/DeployProgram';
import GoToWebsiteProgram from 'components/programs/GoToWebsiteProgram';
import RenameProgram from 'components/programs/RenameProgram';
import DeleteProgram from 'components/programs/DeleteProgram';

const ProgramOptionsContent = ({ program }: { program: IPCProgram }): JSX.Element => (
const ProgramOptionsContent = ({ program }: { program: IPCProgram}): JSX.Element => (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it's still not properly linted 👀

<>
<GoToWebsiteProgram program={program} />
<DeployProgram selectedProgram={program} />
<RenameProgram program={program}/>
<DeleteProgram program={program}/>
</>
);

Expand Down
100 changes: 100 additions & 0 deletions src/components/programs/DeleteProgram.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
HStack,
Icon,
Text,
useBreakpointValue,
useDisclosure,
useColorMode,
useColorModeValue,
} from '@chakra-ui/react';
import { useState } from 'react';
import { IoTrashSharp } from 'react-icons/io5';

import { useDriveContext } from 'contexts/drive';
import { useUserContext } from 'contexts/user';

import Button from 'components/Button';
import Modal from 'components/Modal';
import type { IPCProgram } from 'types/types';
import { textColorMode } from 'config/colorMode';

type DeleteProgramProps = {
program: IPCProgram;
};

const DeleteProgram = ({ program }: DeleteProgramProps): JSX.Element => {
const { user } = useUserContext();
const { programs, setPrograms } = useDriveContext();

const [isLoading, setIsLoading] = useState(false);
const { isOpen, onOpen, onClose } = useDisclosure();

const isDrawer = useBreakpointValue({ base: true, sm: false }) || false;
const textColor = useColorModeValue(textColorMode.light, textColorMode.dark);
const { colorMode } = useColorMode();

const DeleteActualProgram = async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const DeleteActualProgram = async () => {
const deleteActualProgram = async () => {

Use PascalCase only for component names, not for functions 😉

setIsLoading(true);
console.log(`hash = ${program.hash}`);
const update = await user.computing.UpdateDeleteProgram(program.hash);
console.log(`message = ${update.message}`);
if (update.success)
setPrograms([...programs]);
setIsLoading(false);
onClose();
}

return (
<HStack
spacing={isDrawer ? '24px' : '12px'}
p="8px 12px"
borderRadius="8px"
role="group"
onClick={onOpen}
w="100%"
cursor="pointer"
id="ipc-dashboard-delete-button"
_hover={{
bg: colorMode === 'light' ? 'blue.50' : 'gray.750',
}}
>
<Icon
as={IoTrashSharp}
_groupHover={{ color: 'red.800' }}
w={isDrawer ? '24px' : '20px'}
h={isDrawer ? '24px' : '20px'}
/>
<Text
fontSize="16px"
fontWeight="400"
_groupHover={{
color: 'red.800',
fontWeight: '500',
}}
color={textColor}
>
Delete
</Text>
<Modal
isOpen={isOpen}
onClose={onClose}
title="Delete program"
CTA={
<Button
variant="primary"
size="lg"
onClick={DeleteActualProgram}
isLoading={isLoading}
id="ipc-dashboard-delete-program-button"
>
Delete
</Button>
}
>
<Text color={textColor}>Are you sure you want to delete this program?</Text>
</Modal>
</HStack>
);
};

export default DeleteProgram;
129 changes: 129 additions & 0 deletions src/components/programs/RenameProgram.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
HStack,
FormControl,
FormLabel,
Input,
useDisclosure,
Icon,
Text,
useBreakpointValue,
useColorMode,
useColorModeValue
} from '@chakra-ui/react';

import { BsPencil } from 'react-icons/bs';

import type { IPCProgram } from 'types/types';

import { textColorMode } from 'config/colorMode';

import Modal from 'components/Modal';
import Button from 'components/Button';

import { ChangeEvent, useState } from 'react';


import { useUserContext } from 'contexts/user';
import { useDriveContext } from 'contexts/drive';

type RenameProgramProps = {
program: IPCProgram;
};

const RenameProgram = ({ program }: RenameProgramProps): JSX.Element => {
const isDrawer = useBreakpointValue({ base: true, sm: false }) || false;
const { isOpen, onOpen, onClose } = useDisclosure();
const textColor = useColorModeValue(textColorMode.light, textColorMode.dark);
const [name, setName] = useState('');
const { colorMode } = useColorMode();
const [isLoading, setIsLoading] = useState(false);

const { user } = useUserContext();
const {programs, setPrograms} = useDriveContext();

const updateProgramName = async () => {
setIsLoading(true);
if (name) {
const update = await user.computing.updateProgramName(program, name);
if (update.success) {
const index = programs.indexOf(program);
if (index !== -1) {
programs[index].name = name;
programs[index].log.push({
action: `Renamed program to ${name}`,
date: Date.now(),
});
}
setPrograms([...programs]);
}
}
setName('');
setIsLoading(false);
onClose();
};

return (
<HStack
spacing={isDrawer ? '24px' : '12px'}
p="8px 12px"
borderRadius="8px"
role="group"
onClick={onOpen}
w="100%"
cursor="pointer"
id="ipc-dashboard-rename-button"
_hover={{
bg: colorMode === 'light' ? 'blue.50' : 'gray.750',
}}
>
<Icon
as={BsPencil}
_groupHover={{ color: 'red.800' }}
w={isDrawer ? '24px' : '20px'}
h={isDrawer ? '24px' : '20px'}
/>
<Text
fontSize="16px"
fontWeight="400"
_groupHover={{
color: 'red.800',
fontWeight: '500',
}}
color={textColor}
>
Rename
</Text>
<Modal
isOpen={isOpen}
onClose={onClose}
title="Rename the program"
CTA={
<Button
variant="primary"
size="lg"
onClick={updateProgramName}
isLoading={isLoading}
id="ipc-dashboard-update-programName-button"
>
OK
</Button>
}
>
<FormControl>
<FormLabel color={textColor}>New program name</FormLabel>
<Input
type="text"
w="100%"
p="10px"
my="4px"
placeholder={program.name}
onChange={(e: ChangeEvent<HTMLInputElement>) => setName(e.target.value)}
id="ipc-dashboard-input-update-programame"
/>
</FormControl>
</Modal>
</HStack>
);
}

export default RenameProgram;
35 changes: 28 additions & 7 deletions src/lib/computing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Computing {
});
}


public async load(): Promise<ResponseType> {
try {
if (this.account) {
Expand All @@ -53,16 +54,15 @@ class Computing {
}
}

public async deleteProgram(programHash: string): Promise<ResponseType> {
public async UpdateDeleteProgram(programHash: string): Promise<ResponseType> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public async UpdateDeleteProgram(programHash: string): Promise<ResponseType> {
public async deleteProgram(programHash: string): Promise<ResponseType> {

This function is only deleting a program, not updating it ?

try {
if (this.account) {
await forget.Publish({
const test = await forget.Publish({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const test = await forget.Publish({
await forget.Publish({

channel: ALEPH_CHANNEL,
hashes: [programHash],
storageEngine: ItemType.ipfs,
storageEngine: ItemType.storage,
account: this.account,
});

return { success: true, message: 'program deleted' };
}
return { success: false, message: 'Failed to load account' };
Expand All @@ -72,6 +72,29 @@ class Computing {
}
}

public async updateProgramName(
concernedProgram: IPCProgram,
newName: string,
): Promise<ResponseType> {
try {
const copy = concernedProgram;
await Promise.all(
this.programs.map(async () => {
copy.name = newName;
copy.log.push({
action: `Renamed program to ${newName}`,
date: Date.now(),
});
Comment on lines +80 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work 🤔

Maybe something like this:

Suggested change
const copy = concernedProgram;
await Promise.all(
this.programs.map(async () => {
copy.name = newName;
copy.log.push({
action: `Renamed program to ${newName}`,
date: Date.now(),
});
await Promise.all(
this.programs = this.programs.map(async (prog) => {
if (prog.id === concernedProgram.id) {
const newLog = {
action: `Renamed program to ${newName}`,
date: Date.now(),
};
return {...prog, name: newName, logs: [...prog.logs, newLog]}
}
return prog;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw you can remove the async and Promise.all here, nothing is awaited

await this.publishAggregate();
}),
);
return { success: true, message: 'Program name updated' };
} catch (err) {
console.error(err);
return { success: false, message: 'Failed to update program name' };
}
}

public async uploadProgram(
myProgram: IPCProgram,
uploadFile: File,
Expand All @@ -82,20 +105,18 @@ class Computing {
if (this.account) {
// remove old program from user's programs array
if (isRedeploy && oldProgramHash) {
const newProgramsArray: IPCProgram[] = this.programs.filter(
const newProgramsArray: IPCProgram[] = this.programs.filter (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const newProgramsArray: IPCProgram[] = this.programs.filter (
const newProgramsArray: IPCProgram[] = this.programs.filter(

(oldProgram: IPCProgram) => oldProgram !== oldProgramHash,
);
this.programs = newProgramsArray;
}

const programHashPublishProgram = await program.publish({
channel: ALEPH_CHANNEL,
account: this.account,
storageEngine: ItemType.storage,
file: uploadFile,
entrypoint: myProgram.entrypoint,
});

const newProgram: IPCProgram = {
...myProgram,
hash: programHashPublishProgram.item_hash,
Expand Down
Loading