Skip to content

Commit 15f62a9

Browse files
committed
feat: display user info in profile page
1 parent 92c168e commit 15f62a9

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

src/apis/user.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import useSWR from 'swr'
2+
3+
import { NotFoundError } from '../utils/error'
4+
import { UserApi } from '../utils/maa-copilot-client'
5+
6+
export function useUserInfo({
7+
userId,
8+
suspense,
9+
}: {
10+
userId?: string
11+
suspense?: boolean
12+
}) {
13+
return useSWR(
14+
userId ? ['user', userId] : null,
15+
async ([, userId]) => {
16+
const res = await new UserApi({
17+
sendToken: 'never',
18+
requireData: true,
19+
}).getUserInfo({
20+
userId,
21+
})
22+
23+
// FIXME: 严谨一点!!!
24+
if (res.data.userName === '未知用户:(') {
25+
throw new NotFoundError()
26+
}
27+
28+
return res.data
29+
},
30+
{ suspense },
31+
)
32+
}

src/components/Suspensable.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ interface SuspensableProps {
1111
pendingTitle?: string
1212

1313
fetcher?: () => void
14+
errorFallback?: (params: { error: Error }) => JSX.Element | undefined
1415
}
1516

1617
export const Suspensable: FCC<SuspensableProps> = ({
1718
children,
1819
retryDeps = [],
1920
pendingTitle = '加载中',
2021
fetcher,
22+
errorFallback,
2123
}) => {
2224
const resetError = useRef<() => void>()
2325

@@ -30,13 +32,18 @@ export const Suspensable: FCC<SuspensableProps> = ({
3032
return (
3133
<ErrorBoundary
3234
fallback={({ resetError: _resetError, error }) => {
35+
const fallback = errorFallback?.({ error })
36+
if (fallback !== undefined) {
37+
return fallback
38+
}
39+
3340
resetError.current = _resetError
3441

3542
return (
3643
<NonIdealState
3744
icon="issue"
3845
title="加载失败"
39-
description={fetcher ? '数据加载失败,请尝试' : error.message}
46+
description={fetcher ? '数据加载失败,请重试' : error.message}
4047
className="py-8"
4148
action={
4249
fetcher && (

src/pages/profile.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import { Button, ButtonGroup, Card } from '@blueprintjs/core'
22

33
import { ComponentType, useState } from 'react'
4-
import { useParams } from 'react-router-dom'
4+
import { Navigate, useParams } from 'react-router-dom'
55

66
import { withGlobalErrorBoundary } from 'components/GlobalErrorBoundary'
77
import { OperationList } from 'components/OperationList'
88
import { OperationSetList } from 'components/OperationSetList'
99
import { OperationDrawer } from 'components/drawer/OperationDrawer'
1010

11+
import { useUserInfo } from '../apis/user'
1112
import { CardTitle } from '../components/CardTitle'
1213
import { withSuspensable } from '../components/Suspensable'
14+
import { NotFoundError } from '../utils/error'
1315

14-
export const _ProfilePage: ComponentType = () => {
16+
const _ProfilePage: ComponentType = () => {
1517
const { id } = useParams()
16-
17-
// edge case?
1818
if (!id) {
19+
// edge case?
1920
throw new Error('ID 无效')
2021
}
2122

23+
const { data: userInfo } = useUserInfo({ userId: id, suspense: true })
24+
2225
const [listMode, setListMode] = useState<'operation' | 'operationSet'>(
2326
'operation',
2427
)
@@ -55,7 +58,7 @@ export const _ProfilePage: ComponentType = () => {
5558
<div className="md:w-1/3 order-1 md:order-2">
5659
<div className="sticky top-20">
5760
<Card className="flex flex-col mb-4 space-y-2">
58-
<CardTitle icon="user">用户名</CardTitle>
61+
<CardTitle icon="user">{userInfo?.userName}</CardTitle>
5962
</Card>
6063
</div>
6164
</div>
@@ -66,6 +69,11 @@ export const _ProfilePage: ComponentType = () => {
6669
}
6770
_ProfilePage.displayName = 'ProfilePage'
6871

69-
export const ProfilePage = withGlobalErrorBoundary(
70-
withSuspensable(_ProfilePage),
71-
)
72+
export const ProfilePage = withSuspensable(_ProfilePage, {
73+
errorFallback: ({ error }) => {
74+
if (error instanceof NotFoundError) {
75+
return <Navigate to="/404" replace />
76+
}
77+
return undefined
78+
},
79+
})

src/utils/error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class InvalidTokenError extends Error {
2727
message = this.message || '登录失效,请重新登录'
2828
}
2929

30+
export class NotFoundError extends Error {
31+
message = this.message || '资源不存在'
32+
}
33+
3034
export class NetworkError extends Error {
3135
message = this.message || '网络错误'
3236
}

0 commit comments

Comments
 (0)