Skip to content

Commit

Permalink
internet identity added and canister improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgayayci committed Feb 14, 2024
1 parent f6f94e4 commit a9df532
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 120 deletions.
7 changes: 6 additions & 1 deletion main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ const schema = {
type: "object",
properties: {
name: { type: "string" },
principal: { type: "string" },
isInternetIdentity: { type: "boolean" },
internetIdentity: {
type: "string",
},
},
},
},
};

const store = new Store({ schema });

store.set("identities", []);

async function handleFileOpen() {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
Expand Down
28 changes: 15 additions & 13 deletions main/helpers/manage-identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ export function handleIdentities(store, action, identity, newIdentity?) {

switch (action) {
case "add":
// Ensure identity has a name and does not already exist.
if (!identity.name || identities.some((i) => i.name === identity.name)) {
throw new Error("Identity already exists or name is missing");
}

// For adding an internetIdentity, ensure the structure is correct.
if (
identity.isInternetIdentity &&
(!identity.internetIdentity ||
typeof identity.internetIdentity !== "string")
) {
throw new Error("Invalid internetIdentity object");
}

identities.push(identity);
break;

Expand All @@ -17,31 +27,23 @@ export function handleIdentities(store, action, identity, newIdentity?) {
if (existingIdentityIndex === -1) {
throw new Error("Identity to rename not found");
}
if (identities.some((i) => i.name === newIdentity)) {
if (identities.some((i) => i.name === newIdentity.name)) {
throw new Error("New identity name already exists");
}
identities[existingIdentityIndex] = {
...identities[existingIdentityIndex],
name: newIdentity,
name: newIdentity.name,
};
break;

case "delete":
const keyToDelete = identity.isInternetIdentity
? "internetIdentityPrincipal"
: "name";
identities = identities.filter(
(i) => i[keyToDelete] !== identity[keyToDelete]
);
identities = identities.filter((i) => i.name !== identity.name);
break;

case "get":
if (identity) {
const keyToFind = identity.isInternetIdentity
? "internetIdentityPrincipal"
: "name";
if (identity.name) {
const requestedIdentity = identities.find(
(i) => i[keyToFind] === identity[keyToFind]
(i) => i.name === identity.name
);
return requestedIdentity || null;
}
Expand Down
3 changes: 1 addition & 2 deletions renderer/components/canisters/canister-detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ export default function CanisterDetail({
if (err.message) {
errorMessage += err.message;
} else {
// Stringify the error object or parts of it
errorMessage += JSON.stringify(err, null, 2); // Pretty print the error
errorMessage += JSON.stringify(err, null, 2);
}
setErrorMessage(errorMessage);
}
Expand Down
66 changes: 56 additions & 10 deletions renderer/components/canisters/canister-status-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
} from "@components/ui/accordion";
import { Button } from "@components/ui/button";
import { Alert, AlertDescription, AlertTitle } from "@components/ui/alert";
import { AlertCircle, ThumbsUpIcon } from "lucide-react";
import { AlertCircle, ThumbsUpIcon, ThumbsDownIcon } from "lucide-react";
import { ScrollArea, ScrollBar } from "@components/ui/scroll-area";

const ReactJson = dynamic(() => import("react-json-view"), {
ssr: false, // This will only import 'ReactJson' on the client-side
ssr: false,
});

export default function CanisterStatusConfig({
Expand Down Expand Up @@ -75,18 +75,45 @@ export default function CanisterStatusConfig({
}

async function checkCanisterStatus() {
// Here we call the exposed method from preload.js
try {
const result = await window.awesomeApi.runDfxCommand(
// Fetching the status of the canister
const statusResult = await window.awesomeApi.runDfxCommand(
"canister",
"status",
[canister.name],
[],
projectPath
);

const parsedResult = parseCliOutput(result);
setCanisterStatus(parsedResult);
const parsedResult = parseCliOutput(statusResult);
console.log("Parsed result:", parsedResult);

// Fetching the principal ID of the active identity
const identityResult = await window.awesomeApi.runDfxCommand(
"identity",
"get-principal"
);

//@ts-ignore
const controllersString = parsedResult?.Controllers;
const controllersArray = controllersString.split(" ");
controllersArray.pop(); // Remove the last word
const controllers = controllersArray.join(" ");

// Comparing the active identity principal with the canister controllers
if (controllers.includes(identityResult)) {
console.log("Active Identity is controller of the canister.");
setCanisterStatus({
...parsedResult,
isActiveIdentityController: true,
});
} else {
console.log("Active Identity is not controller of the canister.");
setCanisterStatus({
...parsedResult,
isActiveIdentityController: false,
});
}
} catch (error) {
console.log("Error invoking remote method:", error);
}
Expand All @@ -111,10 +138,29 @@ export default function CanisterStatusConfig({
</AccordionTrigger>
<AccordionContent>
{canisterStatus ? (
<ReactJson
name={canister.name + "_status"}
src={canisterStatus}
/>
<div>
{canisterStatus.isActiveIdentityController ? (
<Alert variant="success" className="mb-4">
<ThumbsUpIcon className="h-4 w-4 text-green-600" />
<AlertTitle>Canister Status</AlertTitle>
<AlertDescription>
Active Identity is controller of the canister.
</AlertDescription>
</Alert>
) : (
<Alert className="mb-4">
<ThumbsDownIcon className="h-4 w-4 text-red-600" />
<AlertTitle>Canister Status</AlertTitle>
<AlertDescription>
Active Identity is not controller of the canister.
</AlertDescription>
</Alert>
)}
<ReactJson
name={canister.name + "_status"}
src={canisterStatus}
/>
</div>
) : (
<Alert variant="warning">
<AlertCircle className="h-4 w-4" />
Expand Down

0 comments on commit a9df532

Please sign in to comment.