Skip to content

Commit

Permalink
Apply contract/message card component to policies
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Jan 7, 2025
1 parent cc038ae commit ad2ad03
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 140 deletions.
159 changes: 23 additions & 136 deletions packages/keychain/src/components/Policies.tsx
Original file line number Diff line number Diff line change
@@ -1,148 +1,35 @@
import {
HStack,
VStack,
Text,
AccordionButton,
AccordionItem,
Accordion,
Spacer,
AccordionPanel,
Box,
} from "@chakra-ui/react";
import { FnIcon, WedgeIcon, CopyAddress } from "@cartridge/ui-next";
import { SessionPolicies } from "@cartridge/presets";
import { ContractCard } from "./session/ContractCard";
import { useConnection } from "@/hooks/connection";
import { MessageCard } from "./session/MessageCard";

export function Policies({
title,
policies,
}: {
title?: string;
policies: SessionPolicies;
}) {
const connection = useConnection();
const parsedContracts = connection.policies?.contracts ?? {};

return (
<Box position="relative">
{title && (
<VStack
align="flex-start"
bg="solid.primary"
p={3}
position="sticky"
top={0}
borderTopRadius="base"
>
<Text
color="text.secondaryAccent"
fontSize="xs"
fontWeight="bold"
casing="uppercase"
>
{title}
</Text>
</VStack>
<div className="flex flex-col gap-4">
{Object.entries(policies.contracts ?? {}).map(([address, p]) => {
const c = parsedContracts[address];
return (
<ContractCard
key={address}
address={address}
title={c?.meta?.name || "Contract"}
icon={c?.meta?.icon}
methods={p.methods}
/>
);
})}

{policies.messages && policies.messages.length > 0 && (
<MessageCard messages={policies.messages} />
)}

<Accordion w="full" allowMultiple overflowY="auto">
{Object.entries(policies.contracts ?? {}).map(([contractAddress, p]) =>
p.methods.map((m) => (
<AccordionItem
key={`${contractAddress}${m.entrypoint}`}
// borderTopRadius={i === 0 && !title ? "base" : "none"}
// // The container already set border radius (for top & bottom), but we
// // set the bottom radius for the last item here because for certain
// // browsers' scrolling behaviour (eg Firefox) just to make it look nicer.
// borderBottomRadius={i === policies?.length - 1 ? "base" : "none"}
>
{({ isExpanded }) => (
<>
<AccordionButton
_disabled={{
cursor: "auto",
opacity: 1,
}}
>
<HStack>
<FnIcon size="sm" />
<Text>{m.entrypoint}</Text>
</HStack>

<Spacer />

<WedgeIcon
variant={isExpanded ? "down" : "right"}
className="text-muted-foreground"
/>
</AccordionButton>

<AccordionPanel>
<VStack align="flex-start" w="full" p={3}>
<CopyAddress address={contractAddress} />
{m.description && (
<Text w="full" color="inherit">
{m.description}
</Text>
)}
</VStack>
</AccordionPanel>
</>
)}
</AccordionItem>
)),
)}
{policies.messages?.map((p) => (
<AccordionItem
key={`${p.domain.name}${p.primaryType}`}
// borderTopRadius={i === 0 && !title ? "base" : "none"}
// // The container already set border radius (for top & bottom), but we
// // set the bottom radius for the last item here because for certain
// // browsers' scrolling behaviour (eg Firefox) just to make it look nicer.
// borderBottomRadius={i === policies?.length - 1 ? "base" : "none"}
>
{({ isExpanded }) => (
<>
<AccordionButton
_disabled={{
cursor: "auto",
opacity: 1,
}}
>
<HStack>
<Text>Sign Message</Text>
</HStack>

<Spacer />

<WedgeIcon
variant={isExpanded ? "down" : "right"}
className="text-muted-foreground"
/>
</AccordionButton>

<AccordionPanel>
<VStack align="flex-start" w="full" p={3}>
<Text w="full" color="inherit">
Domain: {p.domain.name}
</Text>
<Text w="full" color="inherit">
Primary Type: {p.primaryType}
</Text>
<Text w="full" color="inherit">
Types:
</Text>
{Object.keys(p.types).map((key) =>
key === "StarknetDomain" ||
key === "StarkNetDomain" ? null : (
<Text key={key} w="full" color="inherit">
{key}: {JSON.stringify(p.types[key])}
</Text>
),
)}
</VStack>
</AccordionPanel>
</>
)}
</AccordionItem>
))}
</Accordion>
</Box>
</div>
);
}
10 changes: 6 additions & 4 deletions packages/keychain/src/components/session/ContractCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export function ContractCard({
<Accordion type="multiple" defaultValue={["methods"]}>
<AccordionItem value="methods" className="flex flex-col gap-4">
<AccordionTrigger className="text-xs text-muted-foreground">
Approve{" "}
<span className="text-accent-foreground font-bold">
{methods.length} {methods.length > 1 ? "methods" : "method"}
</span>
<div>
Approve{" "}
<span className="text-accent-foreground font-bold">
{methods.length} {methods.length > 1 ? "methods" : "method"}
</span>
</div>
</AccordionTrigger>
<AccordionContent className="bg-background border border-background rounded-md gap-px">
{methods.map((method) => (
Expand Down

0 comments on commit ad2ad03

Please sign in to comment.