generated from t3-oss/create-t3-turbo
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor session management based on the posts example
- Loading branch information
Showing
6 changed files
with
143 additions
and
46 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
apps/nextjs/src/app/(authorized)/users/settings/components/sessions.tsx
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,100 @@ | ||
"use client" | ||
|
||
import { use } from "react" | ||
import { userAgentFromString } from "next/server" | ||
import { MonitorIcon, SmartphoneIcon, Trash2Icon } from "lucide-react" | ||
|
||
import type { RouterOutputs } from "@acme/api" | ||
import { cn } from "@acme/ui" | ||
import { Button } from "@acme/ui/button" | ||
import { toast } from "@acme/ui/toast" | ||
|
||
import { api } from "@/trpc/react" | ||
|
||
export function SessionList(props: { | ||
sessions: Promise<RouterOutputs["user"]["sessions"]> | ||
sessionId: string | ||
}) { | ||
// TODO: Make `useSuspenseQuery` work without having to pass a promise from RSC | ||
const initialData = use(props.sessions) | ||
const { data: sessions } = api.user.sessions.useQuery(undefined, { | ||
initialData, | ||
}) | ||
|
||
if (sessions.length === 0) { | ||
return ( | ||
<div className="relative flex w-full flex-col gap-4"> | ||
<div className="absolute inset-0 flex flex-col items-center justify-center bg-black/10"> | ||
<p className="text-2xl font-bold text-white">No sessions found</p> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
{sessions.map((session) => { | ||
return ( | ||
<SessionCard | ||
key={session.id} | ||
session={session} | ||
sessionId={props.sessionId} | ||
/> | ||
) | ||
})} | ||
</> | ||
) | ||
} | ||
|
||
export function SessionCard(props: { | ||
session: RouterOutputs["user"]["sessions"][number] | ||
sessionId: string | ||
}) { | ||
const utils = api.useUtils() | ||
const deleteSession = api.user.deleteSession.useMutation({ | ||
onSuccess: async () => { | ||
await utils.user.invalidate() | ||
}, | ||
onError: (err) => { | ||
toast.error( | ||
err?.data?.code === "UNAUTHORIZED" | ||
? "You must be logged in to manage your session" | ||
: "Failed to delete selected session", | ||
) | ||
}, | ||
}) | ||
|
||
const ua = userAgentFromString(props.session.userAgent ?? "") | ||
const isMobile = () => | ||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( | ||
props.session.userAgent ?? "", | ||
) | ||
|
||
return ( | ||
<div className=" flex w-full items-center space-x-4 rounded-md border p-4"> | ||
{isMobile() ? <SmartphoneIcon /> : <MonitorIcon />} | ||
<div className="flex-1 space-y-1"> | ||
<p className="text-sm font-medium leading-none"> | ||
{ua.os.name} - {ua.browser.name} | ||
</p> | ||
<p className="text-sm text-muted-foreground"> | ||
{props.session.ipAddress === "::1" | ||
? "127.0.0.1 (localhost)" | ||
: props.session.ipAddress}{" "} | ||
{props.session.id === props.sessionId && " - Current session"} | ||
</p> | ||
</div> | ||
{props.session.id !== props.sessionId && ( | ||
<Button | ||
size="icon" | ||
variant="destructive" | ||
onClick={() => { | ||
deleteSession.mutate({ sessionId: props.session.id }) | ||
}} | ||
> | ||
<Trash2Icon className="h-4 w-4" /> | ||
</Button> | ||
)} | ||
</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
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