| 1 | "use client"; |
| 2 | |
| 3 | import { isReauthenticationRequiredError } from |
| 4 | import { useAuth, useListSessions |
| 5 | import { Fragment } |
| 6 | import { Card, CardContent |
| 7 | import { |
| 8 | Item, |
| 9 | ItemContent, |
| 10 | ItemGroup, |
| 11 | ItemMedia, |
| 12 | ItemSeparator, |
| 13 | } from "@/components/ui/item"; |
| 14 | import { Skeleton } |
| 15 | import { cn } |
| 16 | import { ReauthenticationAction } |
| 17 | import { ActiveSession } |
| 18 | import { SessionActions } |
| 19 | |
| 20 | export type ActiveSessionsProps |
| 21 | className?: string |
| 22 | }; |
| 23 | |
| 24 | /** Render a card listing all active sessions for the current user with revoke controls. */ |
| 25 | export function ActiveSessions({ |
| 26 | const { authClient, |
| 27 | const { data: |
| 28 | |
| 29 | const sessionsQuery = |
| 30 | meta: { errorPresentation: " |
| 31 | }); |
| 32 | const { data: |
| 33 | |
| 34 | const activeSessions = |
| 35 | activeSession.id === |
| 36 | ); |
| 37 | |
| 38 | return ( |
| 39 | <div> |
| 40 | <h2 className="text-sm font-semibold mb-3 |
| 41 | {localization.settings.activeSessions |
| 42 | </h2> |
| 43 | |
| 44 | <Card className={ |
| 45 | <CardContent className=" |
| 46 | {isReauthenticationRequiredError(error) |
| 47 | <ReauthenticationAction /> |
| 48 | ) : error ? |
| 49 | <div className="p-4 text-destructive text-sm |
| 50 | ) : isPending ? |
| 51 | <SessionRowSkeleton /> |
| 52 | ) : ( |
| 53 | <ItemGroup className=" |
| 54 | {activeSessions?.map(( |
| 55 | <Fragment key={ |
| 56 | {index > 0 |
| 57 | <ActiveSession activeSession={ |
| 58 | </Fragment> |
| 59 | ))} |
| 60 | </ItemGroup> |
| 61 | )} |
| 62 | </CardContent> |
| 63 | {!isPending && ! |
| 64 | <SessionActions |
| 65 | hasOtherSessions={activeSessions.some |
| 66 | (activeSession) => |
| 67 | )} |
| 68 | /> |
| 69 | )} |
| 70 | </Card> |
| 71 | </div> |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | function SessionRowSkeleton() { |
| 76 | return ( |
| 77 | <Item> |
| 78 | <ItemMedia> |
| 79 | <Skeleton className=" |
| 80 | </ItemMedia> |
| 81 | <ItemContent> |
| 82 | <Skeleton className=" |
| 83 | <Skeleton className=" |
| 84 | </ItemContent> |
| 85 | </Item> |
| 86 | ); |
| 87 | } |