Skip to content

Commit

Permalink
Set expiresAt based on selected duration
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Dec 5, 2024
1 parent 23ad742 commit e7a2a70
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
56 changes: 37 additions & 19 deletions packages/keychain/src/components/connect/CreateSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import { useConnection } from "hooks/connection";
import { ControllerErrorAlert } from "components/ErrorAlert";
import { SessionConsent } from "components/connect";
import { DEFAULT_SESSION_DURATION, SESSION_EXPIRATION } from "const";
import { DEFAULT_SESSION_DURATION } from "const";
import { Upgrade } from "./Upgrade";
import { ErrorCode } from "@cartridge/account-wasm";
import { SessionSummary } from "components/SessionSummary";
import { TypedDataPolicy } from "@cartridge/presets";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@cartridge/ui-next";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@cartridge/ui-next";

export function CreateSession({
onConnect,
Expand All @@ -24,9 +30,10 @@ export function CreateSession({
const [isConnecting, setIsConnecting] = useState(false);
const [isDisabled, setIsDisabled] = useState(false);
const [duration, setDuration] = useState<bigint>(DEFAULT_SESSION_DURATION);
const expiresAt = useMemo(() => {
return SESSION_EXPIRATION
}, [])
const expiresAt = useMemo(
() => duration + BigInt(Math.floor(Date.now() / 1000)),
[duration],
);
const [maxFee] = useState<BigNumberish>();
const [error, setError] = useState<ControllerError | Error>();

Expand All @@ -35,7 +42,7 @@ export function CreateSession({
const normalizedChainId = normalizeChainId(chainId);

const violatingPolicy = policies.messages?.find(
(policy) =>
(policy: TypedDataPolicy) =>
"domain" in policy &&
(!policy.domain.chainId ||
normalizeChainId(policy.domain.chainId) !== normalizedChainId),
Expand All @@ -44,9 +51,11 @@ export function CreateSession({
if (violatingPolicy) {
setError({
code: ErrorCode.PolicyChainIdMismatch,
message: `Policy for ${(violatingPolicy as TypedDataPolicy).domain.name
}.${(violatingPolicy as TypedDataPolicy).primaryType
} has mismatched chain ID.`,
message: `Policy for ${
(violatingPolicy as TypedDataPolicy).domain.name
}.${
(violatingPolicy as TypedDataPolicy).primaryType
} has mismatched chain ID.`,
});
setIsDisabled(true);
} else {
Expand Down Expand Up @@ -102,19 +111,28 @@ export function CreateSession({
{!error && (
<div className="flex flex-col">
<div className="flex items-center text-sm text-muted-foreground">
<div>Expires in{" "}</div>
<Select value={duration.toString()}
onValueChange={val => {
console.log(val)
}}>
<SelectTrigger className="w-28" >
<SelectValue defaultValue={(60 * 60 * 24).toString()} placeholder="1 HR" />
<div>Expires in </div>
<Select
value={duration.toString()}
onValueChange={(val) => {
setDuration(BigInt(val));
}}
>
<SelectTrigger className="w-28">
<SelectValue
defaultValue={(60 * 60 * 24).toString()}
placeholder="1 HR"
/>
</SelectTrigger>

<SelectContent>
<SelectItem value={(60 * 60).toString()}>1 HR</SelectItem>
<SelectItem value={(60 * 60 * 24).toString()}>24 HRS</SelectItem>
<SelectItem value={(60 * 60 * 24 * 7).toString()}>1 WEEK</SelectItem>
<SelectItem value={(60 * 60 * 24).toString()}>
24 HRS
</SelectItem>
<SelectItem value={(60 * 60 * 24 * 7).toString()}>
1 WEEK
</SelectItem>
<SelectItem value={"never"}>NEVER</SelectItem>
</SelectContent>
</Select>
Expand All @@ -141,7 +159,7 @@ export function CreateSession({
</div>
)}
</Footer>
</Container >
</Container>
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ui-next/src/components/primitives/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const SelectContent = React.forwardRef<
className={cn(
"relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
position === "popper" &&
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
className,
)}
position={position}
Expand All @@ -93,7 +93,7 @@ const SelectContent = React.forwardRef<
className={cn(
"p-1",
position === "popper" &&
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]",
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]",
)}
>
{children}
Expand Down

0 comments on commit e7a2a70

Please sign in to comment.