refactor(auth): unify current user state usage across UI

This commit is contained in:
zetaloop
2026-02-22 08:03:09 +08:00
parent 7bcb73f139
commit 312061330c
8 changed files with 47 additions and 44 deletions
+6 -7
View File
@@ -11,12 +11,11 @@ import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
import { Separator } from "@/components/ui/separator"
import { Switch } from "@/components/ui/switch"
import { Textarea } from "@/components/ui/textarea"
import { currentUser } from "@/lib/mock"
import type { UserRole } from "@/lib/types"
import { useAuthStore } from "@/store/auth"
export default function SettingsPage() {
const { currentRole, verifiedRoles, switchRole } = useAuthStore()
const { currentRole, verifiedRoles, switchRole, user } = useAuthStore()
const isRoleVerified = (role: UserRole) => verifiedRoles.includes(role)
@@ -31,8 +30,8 @@ export default function SettingsPage() {
<CardContent className="flex items-center gap-4">
<div className="relative">
<Avatar className="h-20 w-20">
<AvatarImage src={currentUser.avatar} />
<AvatarFallback className="text-lg">{currentUser.nickname[0]}</AvatarFallback>
<AvatarImage src={user?.avatar} />
<AvatarFallback className="text-lg">{user?.nickname?.[0] ?? "?"}</AvatarFallback>
</Avatar>
<button
type="button"
@@ -52,15 +51,15 @@ export default function SettingsPage() {
<CardContent className="space-y-4">
<div className="space-y-2">
<Label htmlFor="nickname"></Label>
<Input id="nickname" defaultValue={currentUser.nickname} />
<Input id="nickname" defaultValue={user?.nickname ?? ""} />
</div>
<div className="space-y-2">
<Label htmlFor="bio"></Label>
<Textarea id="bio" defaultValue={currentUser.bio} rows={3} />
<Textarea id="bio" defaultValue={user?.bio ?? ""} rows={3} />
</div>
<div className="space-y-2">
<Label htmlFor="phone"></Label>
<Input id="phone" defaultValue={currentUser.phone} disabled />
<Input id="phone" defaultValue={user?.phone ?? ""} disabled />
<p className="text-xs text-muted-foreground"></p>
</div>
<Button></Button>
+2 -2
View File
@@ -9,7 +9,7 @@ import { z } from "zod"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { currentUser } from "@/lib/mock"
import { getCurrentUserForLogin } from "@/lib/api"
import { useAuthStore } from "@/store/auth"
const loginSchema = z.object({
@@ -30,7 +30,7 @@ export default function LoginPage() {
const onSubmit = async (_data: z.infer<typeof loginSchema>) => {
await new Promise((r) => setTimeout(r, 500))
login(currentUser, ["consumer", "player", "owner"])
login(getCurrentUserForLogin(), ["consumer", "player", "owner"])
router.push("/")
}
+2 -2
View File
@@ -9,7 +9,7 @@ import { z } from "zod"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { currentUser } from "@/lib/mock"
import { getCurrentUserForLogin } from "@/lib/api"
import { useAuthStore } from "@/store/auth"
const registerSchema = z
@@ -37,7 +37,7 @@ export default function RegisterPage() {
const onSubmit = async (_data: z.infer<typeof registerSchema>) => {
await new Promise((r) => setTimeout(r, 500))
login(currentUser, ["consumer"])
login(getCurrentUserForLogin(), ["consumer"])
router.push("/")
}
+5 -7
View File
@@ -28,8 +28,8 @@ export default function ChatDetailPage({ params }: { params: Promise<{ id: strin
)
}
const currentUserId = user?.id ?? session.participants[0].id
const other = session.participants.find((p) => p.id !== currentUserId) ?? session.participants[1]
const userId = user?.id ?? session.participants[0].id
const other = session.participants.find((p) => p.id !== userId) ?? session.participants[1]
return (
<div className="flex flex-col h-[calc(100vh-3.5rem)]">
@@ -69,7 +69,7 @@ export default function ChatDetailPage({ params }: { params: Promise<{ id: strin
</div>
)
}
const isMine = msg.senderId === currentUserId
const isMine = msg.senderId === userId
return (
<div key={msg.id} className={cn("flex gap-2", isMine && "flex-row-reverse")}>
<Avatar className="h-8 w-8 shrink-0">
@@ -107,13 +107,11 @@ export default function ChatDetailPage({ params }: { params: Promise<{ id: strin
const text = input.trim()
if (!text) return
const sender = session.participants.find(
(participant) => participant.id === currentUserId,
)
const sender = session.participants.find((participant) => participant.id === userId)
sendTextMessage(
session.id,
{
id: currentUserId,
id: userId,
name: sender?.name ?? user?.nickname ?? "",
avatar: sender?.avatar ?? user?.avatar ?? "",
},
+3 -3
View File
@@ -55,7 +55,7 @@ export default function OrderListPage() {
const orders = useOrderStore((state) => state.orders)
const sessions = useChatStore((state) => state.sessions)
const ensureOrderSession = useChatStore((state) => state.ensureOrderSession)
const currentUserId = user?.id ?? "u1"
const userId = user?.id ?? "u1"
const ownerShopId = "shop1"
useEffect(() => {
@@ -74,8 +74,8 @@ export default function OrderListPage() {
}, [orders, ensureOrderSession])
const roleFiltered = orders.filter((order) => {
if (currentRole === "consumer") return order.consumerId === currentUserId
if (currentRole === "player") return order.playerId === currentUserId
if (currentRole === "consumer") return order.consumerId === userId
if (currentRole === "player") return order.playerId === userId
return order.shopId === ownerShopId
})