-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dashboard sessions management screen (#26)
- Loading branch information
1 parent
95d7b49
commit d1e2bfd
Showing
32 changed files
with
1,323 additions
and
542 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
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
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<template> | ||
<div> | ||
<div | ||
v-if="isExpired" | ||
class="text-neutral-500" | ||
> | ||
Expired | ||
</div> | ||
<div | ||
v-else-if="isRevoked" | ||
class="text-neutral-500" | ||
> | ||
Revoked | ||
</div> | ||
<div v-else-if="status === SessionStatus.NotInitialized"> | ||
Not initialized | ||
</div> | ||
<div v-else> | ||
<div :title="`${sessionExpiry.formattedDate} at ${sessionExpiry.formattedTime}`"> | ||
<span v-if="sessionExpiry.isToday">Expires {{ expiresIn }}</span> | ||
<span v-else-if="sessionExpiry.isTomorrow">Expires tomorrow at {{ sessionExpiry.formattedTime }}</span> | ||
<span v-else>Expires on {{ sessionExpiry.formattedDate }} at {{ sessionExpiry.formattedTime }}</span> | ||
</div> | ||
<div class="session-row-line"> | ||
<div | ||
class="bg-white rounded-full h-full will-change-[width] transition-[width] duration-300" | ||
:style="{ width: `${timeLeftPercentage}%` }" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { SessionStatus } from "zksync-sso/utils"; | ||
const props = defineProps<{ | ||
status: SessionStatus; | ||
isExpired: boolean; | ||
now: number; | ||
createdAt: number; | ||
expiresAt: number; | ||
}>(); | ||
const expiresIn = useTimeAgo(props.expiresAt, { showSecond: true, updateInterval: 1000 }); | ||
const sessionExpiry = computed(() => { | ||
const expiresDate = new Date(props.expiresAt); | ||
const nowDate = new Date(props.now); | ||
return formatExpiryDate({ | ||
expiresAt: expiresDate, | ||
now: nowDate, | ||
}); | ||
}); | ||
const timeLeft = computed<number>(() => Math.max(0, props.expiresAt - props.now)); | ||
const timeTotal = computed<number>(() => Math.max(0, props.expiresAt - props.createdAt)); | ||
const timeLeftPercentage = computed<number>(() => Math.min(100, (timeLeft.value / timeTotal.value) * 100)); | ||
const isRevoked = computed(() => props.status === SessionStatus.Closed); | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<template> | ||
<div class="session-row"> | ||
<div class="session-id-container"> | ||
<div :title="sessionId"> | ||
#{{ index }} | ||
</div> | ||
<a | ||
class="session-created-time-ago" | ||
:title="fullCreatedAtDate || ''" | ||
:href="`${defaultChain.blockExplorers?.native.url}/tx/${transactionHash}`" | ||
target="_blank" | ||
> | ||
{{ createdTimeAgo }} | ||
</a> | ||
</div> | ||
<div class="session-expiry-container"> | ||
<SessionRowExpiry | ||
v-if="sessionState" | ||
:status="sessionState.status" | ||
:is-expired="isExpired" | ||
:created-at="timestamp" | ||
:expires-at="expiresAt" | ||
:now="now" | ||
/> | ||
</div> | ||
<div class="session-spend-limit-container"> | ||
<SessionRowSpendLimit | ||
v-if="sessionState" | ||
:config="session" | ||
:state="sessionState" | ||
:now="now" | ||
:is-inactive="isInactive" | ||
/> | ||
</div> | ||
<div class="session-buttons-container"> | ||
<ZkButton | ||
v-if="sessionState && sessionState.status === SessionStatus.Active && !isExpired" | ||
title="Revoke Session" | ||
type="danger" | ||
class="ml-auto" | ||
:ui="{ button: 'block p-2.5 aspect-square', base: 'p-0' }" | ||
:loading="sessionsInProgress" | ||
@click="revokeSession()" | ||
> | ||
<HandRaisedIcon | ||
class="h-5 w-5" | ||
aria-hidden="true" | ||
/> | ||
<span class="sr-only">Revoke Session</span> | ||
</ZkButton> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { HandRaisedIcon } from "@heroicons/vue/24/outline"; | ||
import type { Hash } from "viem"; | ||
import { SessionKeyModuleAbi } from "zksync-sso/abi"; | ||
import { type SessionConfig, type SessionState, SessionStatus } from "zksync-sso/utils"; | ||
const props = defineProps<{ | ||
session: SessionConfig; | ||
index: number; | ||
sessionId: Hash; | ||
transactionHash: Hash; | ||
blockNumber: bigint; | ||
timestamp: number; | ||
}>(); | ||
const _now = useNow({ interval: 1000 }); | ||
const now = computed(() => _now.value.getTime()); | ||
const createdTimeAgo = useTimeAgo(props.timestamp); | ||
const fullCreatedAtDate = computed(() => new Date(props.timestamp).toLocaleString()); | ||
const expiresAt = computed<number>(() => bigintDateToDate(props.session.expiresAt).getTime()); | ||
const timeLeft = computed<number>(() => Math.max(0, expiresAt.value - now.value)); | ||
const isExpired = computed(() => timeLeft.value <= 0); | ||
const { defaultChain, getClient, getPublicClient } = useClientStore(); | ||
const { address } = storeToRefs(useAccountStore()); | ||
const { | ||
inProgress: sessionsInProgress, | ||
execute: revokeSession, | ||
} = useAsync(async () => { | ||
const client = getClient({ chainId: defaultChain.id }); | ||
const paymasterAddress = contractsByChain[defaultChain.id].accountPaymaster; | ||
await client.revokeSession({ | ||
sessionId: props.sessionId, | ||
paymaster: { | ||
address: paymasterAddress, | ||
}, | ||
}); | ||
await fetchSessionState(); | ||
}); | ||
const { | ||
result: sessionState, | ||
execute: fetchSessionState, | ||
} = useAsync(async () => { | ||
const client = getPublicClient({ chainId: defaultChain.id }); | ||
const res = await client.readContract({ | ||
address: contractsByChain[defaultChain.id].session, | ||
abi: SessionKeyModuleAbi, | ||
functionName: "sessionState", | ||
args: [address.value!, props.session], | ||
}); | ||
return res as SessionState; | ||
}); | ||
const isInactive = computed(() => isExpired.value || !sessionState.value || sessionState.value.status === SessionStatus.Closed || sessionState.value.status === SessionStatus.NotInitialized); | ||
fetchSessionState(); | ||
</script> | ||
|
||
<style lang="scss"> | ||
/* Not scoped for a reason. Shares classes with RowLoader.vue */ | ||
.session-row { | ||
@apply grid px-4 items-center text-sm; | ||
@apply grid-cols-2 gap-y-2 py-4 gap-x-8; | ||
@apply md:grid-cols-[6rem_1fr_1fr_45px] md:py-7 md:h-[100px]; | ||
grid-template-areas: | ||
"session-id-container session-buttons-container" | ||
"session-expiry-container session-expiry-container" | ||
"session-spend-limit-container session-spend-limit-container"; | ||
@media screen and (min-width: 768px) { | ||
grid-template-areas: "session-id-container session-expiry-container session-spend-limit-container session-buttons-container"; | ||
} | ||
.session-id-container { | ||
grid-area: session-id-container; | ||
} | ||
.session-expiry-container { | ||
grid-area: session-expiry-container; | ||
} | ||
.session-spend-limit-container { | ||
grid-area: session-spend-limit-container; | ||
} | ||
.session-buttons-container { | ||
grid-area: session-buttons-container; | ||
} | ||
.session-expiry-container, .session-spend-limit-container { | ||
@apply py-2 md:py-0; | ||
} | ||
.session-created-time-ago { | ||
@apply text-neutral-500 text-xs; | ||
} | ||
.session-row-line { | ||
@apply bg-neutral-900 rounded-full w-full h-1 mt-1; | ||
} | ||
} | ||
</style> |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<template> | ||
<div class="session-row"> | ||
<div class="session-id-container"> | ||
<div> | ||
<CommonContentLoader :length="10" /> | ||
</div> | ||
<CommonContentLoader | ||
class="session-created-time-ago" | ||
:length="20" | ||
/> | ||
</div> | ||
<div class="session-expiry-container"> | ||
<CommonContentLoader :length="30" /> | ||
<CommonContentLoader class="block session-row-line" /> | ||
</div> | ||
<div class="session-spend-limit-container"> | ||
<CommonContentLoader :length="30" /> | ||
<CommonContentLoader class="block session-row-line" /> | ||
</div> | ||
<div class="session-buttons-container"> | ||
<div class="rounded-full w-[43px] h-[43px] overflow-hidden"> | ||
<CommonContentLoader class="block h-full w-full" /> | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
Oops, something went wrong.