Skip to content

Commit

Permalink
Refactor:perfromanxce, optimization , disabled button if revoked
Browse files Browse the repository at this point in the history
  • Loading branch information
harshmittal1750 committed Jul 28, 2024
1 parent 31472cc commit 518e183
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
53 changes: 18 additions & 35 deletions src/app/_components/ManageAttestation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
TableBody,
TableCell,
} from "@/components/ui/table";
import RevokeAttestationPage from "./RevokeAttestation";

import RevokeAttestation from "./RevokeAttestation";
import { useCallback, useMemo } from "react";
import { formatKey } from "@/lib/formatKey";
import { formatTime } from "@/lib/formatTime";
const ManageAttestation: NextPage = () => {
const { attestations, loading, error } = useGetAttestations();
console.log(attestations);

const renderTableHeader = () => {
const renderTableHeader = useMemo(() => {
if (attestations && attestations.length > 0) {
return (
<TableRow>
Expand All @@ -29,16 +30,17 @@ const ManageAttestation: NextPage = () => {
</TableRow>
);
}
return null;
};
return <></>;
}, [attestations]);

const renderTableRows = () => {
const renderTableRows = useCallback(() => {
return attestations?.map((attestation: any) => (
<TableRow key={attestation.id}>
<TableCell className="text-right">
<RevokeAttestationPage
<RevokeAttestation
uid={attestation.id}
schemaId={attestation.schema.id}
isRevoked={attestation.revoked}
/>
</TableCell>
{Object.entries(attestation).map(([key, value], index) => (
Expand All @@ -59,44 +61,25 @@ const ManageAttestation: NextPage = () => {
))}
</TableRow>
));
};

const formatKey = (key: string): string => {
// Replace underscores with spaces and split camelCase words with a space
const result = key
.replace(/([A-Z])/g, " $1") // Insert space before each uppercase letter
.replace(/_/g, " ") // Replace underscores with spaces
.trim(); // Remove leading and trailing spaces
}, [attestations]);

// Capitalize the first letter of each word
return result
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
};
if (loading) {
return <p>Loading...</p>;
}

const formatTime = (timestamp: number, timeType: string) => {
if (timestamp === 0 && timeType === "revocationTime") {
return "No Previous revocation";
}
if (timestamp === 0 && timeType === "expirationTime") {
return "No Expiration";
}
const date = new Date(timestamp * 1000); // Convert from seconds to milliseconds
return date.toLocaleString(); // Format the date as a human-readable string
};
if (error) {
return <p>{error.message}</p>;
}

return (
<div>
{loading && <p>Loading...</p>}
{error && <p>{error.message}</p>}
<Table>
<TableCaption>
{attestations
? "A list of your attestations"
: "Please connect wallet"}
</TableCaption>
<TableHeader>{renderTableHeader()}</TableHeader>
<TableHeader>{renderTableHeader}</TableHeader>
<TableBody>{renderTableRows()}</TableBody>
</Table>
</div>
Expand Down
8 changes: 5 additions & 3 deletions src/app/_components/RevokeAttestation.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Button } from "@/components/ui/button";
import useRevokeAttestation from "@/hooks/useRevokeOnChainAttestation";

const RevokeAttestationPage = ({
const RevokeAttestation = ({
uid,
schemaId,
isRevoked,
}: {
uid: string;
schemaId: string;
isRevoked: boolean;
}) => {
const { revokeAttestation, loading, error, success } = useRevokeAttestation();

Expand All @@ -20,7 +22,7 @@ const RevokeAttestationPage = ({
variant={"outline"}
className="bg-red-500 text-white"
onClick={handleRevoke}
disabled={loading}
disabled={loading || isRevoked}
>
Revoke
</Button>
Expand All @@ -33,4 +35,4 @@ const RevokeAttestationPage = ({
);
};

export default RevokeAttestationPage;
export default RevokeAttestation;
6 changes: 3 additions & 3 deletions src/hooks/usePGPKeyServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { useAttestationVerification } from "./useAttestationVerification";
const EAS_CONTRACT_ADDRESS = "0xC2679fBD37d54388Ce493F1DB75320D236e1815e"; // Sepolia v0.26
const SCHEMA_REGISTRY_ADDRESS = "0x0a7E2Ff54e76B8E6659aedc9103FB21c038050D0"; // Sepolia 0.26

const graphqlFetch = async (query: string, variables: any) => {
const urqlFetch = async (query: string, variables: any) => {
//TODO: it should be an environment variable
const response = await fetch("https://sepolia.easscan.org/graphql", {
method: "POST",
Expand Down Expand Up @@ -136,7 +136,7 @@ export const usePGPKeyServer = () => {
}
`;

const result = await graphqlFetch(query, {
const result = await urqlFetch(query, {
selfSchemaId: SELF_ATTESTATION_SCHEMA_UID,
thirdPartySchemaId: THIRD_PARTY_ATTESTATION_SCHEMA_UID,
publicKeyOrFingerprint: encodeURIComponent(publicKeyOrFingerprint),
Expand Down Expand Up @@ -201,7 +201,7 @@ export const usePGPKeyServer = () => {
}
`;

const result = await graphqlFetch(query, { attester: attesterAddress });
const result = await urqlFetch(query, { attester: attesterAddress });
const attestationCount = result.data.attestationCount.aggregate.count;
return Math.min(100, attestationCount * 5); // 5 points per attestation, max 100
},
Expand Down
13 changes: 13 additions & 0 deletions src/lib/formatKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const formatKey = (key: string): string => {
// Replace underscores with spaces and split camelCase words with a space
const result = key
.replace(/([A-Z])/g, " $1") // Insert space before each uppercase letter
.replace(/_/g, " ") // Replace underscores with spaces
.trim(); // Remove leading and trailing spaces

// Capitalize the first letter of each word
return result
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
};
10 changes: 10 additions & 0 deletions src/lib/formatTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const formatTime = (timestamp: number, timeType: string) => {
if (timestamp === 0 && timeType === "revocationTime") {
return "No Previous revocation";
}
if (timestamp === 0 && timeType === "expirationTime") {
return "No Expiration";
}
const date = new Date(timestamp * 1000); // Convert from seconds to milliseconds
return date.toLocaleString(); // Format the date as a human-readable string
};
1 change: 1 addition & 0 deletions src/lib/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ATTESTATIONS_FOR_SPECIFIC_ATTESTER = gql`
revocable
revocationTime
expirationTime
revoked
schema {
id
}
Expand Down

0 comments on commit 518e183

Please sign in to comment.