-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply contract/message card component to policies
- Loading branch information
1 parent
cc038ae
commit ad2ad03
Showing
2 changed files
with
29 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters