@lark-apaas/coding-template-nestjs-react-fullstack 0.1.53 → 0.1.55-alpha.20261030035108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/client/src/components/business-ui/README.md +10 -0
- package/template/client/src/components/business-ui/api/user-profiles/queries.ts +1 -7
- package/template/client/src/components/business-ui/comment/README.md +139 -0
- package/template/client/src/components/business-ui/comment/app-source.ts +175 -0
- package/template/client/src/components/business-ui/comment/ccm-document-source.ts +252 -0
- package/template/client/src/components/business-ui/comment/comment-content.tsx +235 -0
- package/template/client/src/components/business-ui/comment/comment-input.tsx +191 -0
- package/template/client/src/components/business-ui/comment/comment-provider.tsx +40 -0
- package/template/client/src/components/business-ui/comment/content.ts +329 -0
- package/template/client/src/components/business-ui/comment/document-icon.tsx +104 -0
- package/template/client/src/components/business-ui/comment/document-icons.ts +19 -0
- package/template/client/src/components/business-ui/comment/dom-editor.ts +581 -0
- package/template/client/src/components/business-ui/comment/editor.tsx +496 -0
- package/template/client/src/components/business-ui/comment/entity-store.ts +97 -0
- package/template/client/src/components/business-ui/comment/image-attachments.tsx +374 -0
- package/template/client/src/components/business-ui/comment/mention-appearance.ts +35 -0
- package/template/client/src/components/business-ui/comment/mention-provider.tsx +73 -0
- package/template/client/src/components/business-ui/comment/mention-suggestions.tsx +312 -0
- package/template/client/src/components/business-ui/comment/types.ts +154 -0
- package/template/client/src/components/business-ui/comment/use-mention-search.ts +107 -0
- package/template/client/src/components/business-ui/user-display/user-display.tsx +0 -13
- package/template/client/src/components/business-ui/user-profile/user-profile.tsx +146 -397
- package/template/client/src/components/business-ui/user-select/user-pill.tsx +0 -13
- package/template/client/src/hooks/use-example.ts +45 -36
- package/template/client/src/pages/ExamplePage/ExamplePage.tsx +7 -8
- package/template/client/src/tailwind-theme.css +5 -6
- package/template/eslint.config.js +0 -6
- package/template/package-lock.json +83 -62
- package/template/package.json +5 -4
- package/template/server/modules/hello/hello.controller.ts +4 -8
- package/template/server/modules/hello/hello.service.ts +3 -5
- package/template/shared/api.interface.ts +1 -8
- package/template/client/src/components/business-ui/user-profile/lark-card-pool.ts +0 -217
- package/template/client/src/lib/api-error.ts +0 -36
- package/template/server/modules/hello/hello.dto.ts +0 -21
|
@@ -2,30 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
import { logger } from '@lark-apaas/client-toolkit/logger';
|
|
4
4
|
|
|
5
|
-
import React, {
|
|
6
|
-
useCallback,
|
|
7
|
-
useEffect,
|
|
8
|
-
useId,
|
|
9
|
-
useMemo,
|
|
10
|
-
useRef,
|
|
11
|
-
useState,
|
|
12
|
-
} from 'react';
|
|
5
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
13
6
|
import SHA1 from 'crypto-js/sha1';
|
|
14
7
|
|
|
15
|
-
import { getAssetsUrl } from '@client/src/components/business-ui/api/user-profiles/service';
|
|
16
|
-
import { useUserProfile } from '@client/src/components/business-ui/api/user-profiles/queries';
|
|
17
|
-
import { ErrorImage } from '@client/src/components/business-ui/user-profile/error-image';
|
|
18
8
|
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
getOffscreenHost,
|
|
24
|
-
hasSpare,
|
|
25
|
-
putSpare,
|
|
26
|
-
takeSpare,
|
|
27
|
-
waitForCardReady,
|
|
28
|
-
} from '@client/src/components/business-ui/user-profile/lark-card-pool';
|
|
9
|
+
fetchUserProfile,
|
|
10
|
+
getAssetsUrl,
|
|
11
|
+
} from '@client/src/components/business-ui/api/user-profiles/service';
|
|
12
|
+
import { ErrorImage } from '@client/src/components/business-ui/user-profile/error-image';
|
|
29
13
|
import { getEnv } from '@lark-apaas/client-toolkit/utils/getEnv';
|
|
30
14
|
import { useExternalScript } from '@client/src/components/business-ui/user-profile/user-external-script';
|
|
31
15
|
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
|
@@ -83,6 +67,8 @@ function generateRandomString(length: number): string {
|
|
|
83
67
|
return result;
|
|
84
68
|
}
|
|
85
69
|
|
|
70
|
+
type BaseUserProfileProps = BaseSimpleProfileProps | BaseOfficialProfileProps;
|
|
71
|
+
|
|
86
72
|
const ACCOUNT_STATUS = {
|
|
87
73
|
UNSPECIFIED: 0,
|
|
88
74
|
Inactive: 1,
|
|
@@ -104,31 +90,28 @@ const AccountStatusMap: Record<AccountStatus, string> = {
|
|
|
104
90
|
[ACCOUNT_STATUS.Resigned]: '已停用',
|
|
105
91
|
};
|
|
106
92
|
|
|
107
|
-
interface
|
|
108
|
-
readonly name?: string;
|
|
109
|
-
readonly avatar?: string;
|
|
110
|
-
readonly email?: string;
|
|
111
|
-
readonly userStatus: AccountStatus;
|
|
112
|
-
readonly userType: '_employee' | '_externalUser';
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
interface SimpleProfileData {
|
|
93
|
+
interface BaseSimpleProfileProps {
|
|
116
94
|
readonly useLarkCard: false;
|
|
117
|
-
readonly userProfileInfo:
|
|
95
|
+
readonly userProfileInfo: {
|
|
96
|
+
readonly name?: string;
|
|
97
|
+
readonly avatar?: string;
|
|
98
|
+
readonly email?: string;
|
|
99
|
+
//
|
|
100
|
+
readonly userStatus: AccountStatus;
|
|
101
|
+
readonly userType: '_employee' | '_externalUser';
|
|
102
|
+
};
|
|
118
103
|
}
|
|
119
104
|
|
|
120
|
-
interface
|
|
105
|
+
interface BaseOfficialProfileProps {
|
|
121
106
|
readonly useLarkCard: true;
|
|
122
|
-
readonly larkCardParam:
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
readonly larkOpenID: string;
|
|
131
|
-
readonly targetLarkOpenID: string;
|
|
107
|
+
readonly larkCardParam: {
|
|
108
|
+
readonly needRedirect?: boolean;
|
|
109
|
+
readonly redirectURL?: string;
|
|
110
|
+
readonly larkAppID: string;
|
|
111
|
+
readonly jsAPITicket: string;
|
|
112
|
+
readonly larkOpenID: string;
|
|
113
|
+
readonly targetLarkOpenID: string;
|
|
114
|
+
};
|
|
132
115
|
}
|
|
133
116
|
|
|
134
117
|
/**
|
|
@@ -139,55 +122,15 @@ export interface WithCommonResponse<T> {
|
|
|
139
122
|
data: T;
|
|
140
123
|
}
|
|
141
124
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
* 已注册的 handler 里。用模块级变量保证读到的始终是最新值。
|
|
152
|
-
*/
|
|
153
|
-
let currentRedirectURL = '';
|
|
154
|
-
let authHandlerRegistered = false;
|
|
155
|
-
|
|
156
|
-
function registerLarkAuthHandler(): void {
|
|
157
|
-
if (authHandlerRegistered) return;
|
|
158
|
-
if (typeof globalThis.webComponent === 'undefined') return;
|
|
159
|
-
authHandlerRegistered = true;
|
|
160
|
-
|
|
161
|
-
globalThis.webComponent.onAuthError(function (error: Error) {
|
|
162
|
-
let errorMessage: { msg?: { code?: number; msg?: string } } | undefined;
|
|
163
|
-
try {
|
|
164
|
-
errorMessage = JSON.parse(error.message);
|
|
165
|
-
} catch {
|
|
166
|
-
logger.info('webComponent onAuthError: 非 JSON 错误体', error);
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
// 存在token过期情况,但后端无法识别,需要前端这边主动识别error,并进行鉴权重定向
|
|
170
|
-
if (
|
|
171
|
-
errorMessage?.msg?.code === 20442 &&
|
|
172
|
-
errorMessage?.msg?.msg === 'jsapi-ticket not exist' &&
|
|
173
|
-
currentRedirectURL
|
|
174
|
-
) {
|
|
175
|
-
globalThis.location.replace(currentRedirectURL);
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
globalThis.webComponent.onError(function (error: Error) {
|
|
180
|
-
logger.info('webComponent onError', error);
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/** 在指定容器里创建并渲染一个飞书名片,返回创建出来的 `<tt-profile>` 元素 */
|
|
185
|
-
async function renderLarkProfile(
|
|
186
|
-
param: LarkCardParam,
|
|
187
|
-
host: HTMLElement,
|
|
188
|
-
): Promise<Element | null> {
|
|
189
|
-
const { larkAppID, jsAPITicket, larkOpenID, targetLarkOpenID } = param;
|
|
190
|
-
|
|
125
|
+
async function renderLarkProfile({
|
|
126
|
+
larkAppID,
|
|
127
|
+
jsAPITicket,
|
|
128
|
+
larkOpenID,
|
|
129
|
+
cardRef,
|
|
130
|
+
targetLarkOpenID,
|
|
131
|
+
}: BaseOfficialProfileProps['larkCardParam'] & {
|
|
132
|
+
cardRef: React.RefObject<HTMLDivElement | null>;
|
|
133
|
+
}) {
|
|
191
134
|
const timestamp = Date.now().toString();
|
|
192
135
|
const nonceStr = generateRandomString(10);
|
|
193
136
|
const url = globalThis.location.href.split('#')[0] ?? '';
|
|
@@ -203,53 +146,20 @@ async function renderLarkProfile(
|
|
|
203
146
|
url,
|
|
204
147
|
jsApiList: ['user_profile'],
|
|
205
148
|
locale: ['zh_cn'],
|
|
206
|
-
...(getEnv() === 'BOE'
|
|
149
|
+
...(getEnv() === 'BOE'
|
|
150
|
+
? { env: 'feishu_boe' }
|
|
151
|
+
: {}),
|
|
207
152
|
});
|
|
208
153
|
|
|
209
|
-
await globalThis.webComponent.render(
|
|
154
|
+
const myComponent = await globalThis.webComponent.render(
|
|
210
155
|
'UserProfile',
|
|
211
|
-
{
|
|
212
|
-
|
|
156
|
+
{
|
|
157
|
+
openId: targetLarkOpenID,
|
|
158
|
+
},
|
|
159
|
+
cardRef.current,
|
|
213
160
|
);
|
|
214
161
|
|
|
215
|
-
|
|
216
|
-
return host.lastElementChild;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* 渲染一张完整可用的名片。
|
|
221
|
-
*
|
|
222
|
-
* `visibleHost` 非空时直接渲进可见容器(首次打开,让用户看到飞书自己的骨架屏);
|
|
223
|
-
* 否则开一个专属离屏插槽 —— 并发渲染共用容器会取错元素。插槽只用来隔离本次
|
|
224
|
-
* 渲染,拿到元素后就把它挪到离屏容器下并撤掉插槽,否则空壳 div 会累积。
|
|
225
|
-
*
|
|
226
|
-
* 放在模块级而不是 effect 内,是为了让「真正卸载时补备用卡」那条路径也能用,
|
|
227
|
-
* 不必把它写进 effect 的闭包、从而被迫放宽依赖。
|
|
228
|
-
*/
|
|
229
|
-
async function renderCard(
|
|
230
|
-
param: LarkCardParam,
|
|
231
|
-
visibleHost: HTMLElement | null,
|
|
232
|
-
): Promise<Element | null> {
|
|
233
|
-
const slot = visibleHost ? null : createOffscreenSlot();
|
|
234
|
-
const live = await renderLarkProfile(param, visibleHost ?? slot!);
|
|
235
|
-
|
|
236
|
-
const settle = () => {
|
|
237
|
-
if (!slot) return;
|
|
238
|
-
if (live && live.parentNode === slot) getOffscreenHost().appendChild(live);
|
|
239
|
-
slot.remove();
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
if (!live) {
|
|
243
|
-
settle();
|
|
244
|
-
return null;
|
|
245
|
-
}
|
|
246
|
-
if (!(await waitForCardReady(live))) {
|
|
247
|
-
live.remove();
|
|
248
|
-
settle();
|
|
249
|
-
return null;
|
|
250
|
-
}
|
|
251
|
-
settle();
|
|
252
|
-
return live;
|
|
162
|
+
return myComponent;
|
|
253
163
|
}
|
|
254
164
|
|
|
255
165
|
function UserProfile(props: UserProfileProps) {
|
|
@@ -262,30 +172,96 @@ function UserProfile(props: UserProfileProps) {
|
|
|
262
172
|
user_id ||
|
|
263
173
|
originUserId;
|
|
264
174
|
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
175
|
+
const cardRef = useRef<HTMLDivElement>(null);
|
|
176
|
+
const [loading, setLoading] = useState<boolean>(true);
|
|
177
|
+
const redirectURLRef = useRef<string>('');
|
|
178
|
+
const [response, setResponse] = useState<BaseUserProfileProps>(
|
|
179
|
+
{} as BaseUserProfileProps,
|
|
180
|
+
);
|
|
181
|
+
const [error, setError] = useState<boolean>(false);
|
|
182
|
+
|
|
183
|
+
const onAuthError = useCallback(() => {
|
|
184
|
+
globalThis.webComponent.onAuthError(function (error: Error) {
|
|
185
|
+
const errorMessage = JSON.parse(error.message);
|
|
186
|
+
// 存在token过期情况,但后端无法识别,需要前端这边主动识别error,并进行鉴权重定向
|
|
187
|
+
if (
|
|
188
|
+
errorMessage?.msg?.code === 20442 &&
|
|
189
|
+
errorMessage?.msg?.msg === 'jsapi-ticket not exist'
|
|
190
|
+
) {
|
|
191
|
+
globalThis.location.replace(redirectURLRef.current);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
globalThis.webComponent.onError(function (error: Error) {
|
|
196
|
+
logger.info('webComponent onError', error);
|
|
197
|
+
});
|
|
198
|
+
}, []);
|
|
270
199
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
200
|
+
// 按需注入飞书 H5 JS SDK,仅在组件使用时加载
|
|
201
|
+
const larkSdkURl =
|
|
202
|
+
'https://lf3-cdn-tos.bytegoofy.com/obj/goofy/locl/lark/external_js_sdk/h5-js-sdk-1.2.21.js';
|
|
203
|
+
const scriptOptions = useMemo(
|
|
204
|
+
() => ({ onloadCallback: onAuthError }),
|
|
205
|
+
[onAuthError],
|
|
206
|
+
);
|
|
207
|
+
const scriptStatus = useExternalScript(larkSdkURl, scriptOptions);
|
|
274
208
|
|
|
275
|
-
|
|
276
|
-
|
|
209
|
+
const fetchData = useCallback(async () => {
|
|
210
|
+
if (!userId) return;
|
|
211
|
+
|
|
212
|
+
const controller = new AbortController();
|
|
213
|
+
|
|
214
|
+
setLoading(true);
|
|
215
|
+
setError(false);
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
const data = await fetchUserProfile(userId, accountType, controller.signal);
|
|
219
|
+
setResponse(data as BaseUserProfileProps);
|
|
220
|
+
|
|
221
|
+
if (data.useLarkCard && !data.larkCardParam.needRedirect) {
|
|
222
|
+
redirectURLRef.current = data.larkCardParam.redirectURL ?? '';
|
|
223
|
+
await renderLarkProfile({
|
|
224
|
+
...data.larkCardParam,
|
|
225
|
+
cardRef,
|
|
226
|
+
});
|
|
227
|
+
setLoading(false);
|
|
228
|
+
} else {
|
|
229
|
+
setLoading(false);
|
|
230
|
+
}
|
|
231
|
+
} catch (error: unknown) {
|
|
232
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
233
|
+
// 直接退出,不处理 AbortError
|
|
234
|
+
} else {
|
|
235
|
+
// 最终的UI反馈
|
|
236
|
+
setError(true);
|
|
237
|
+
setLoading(false);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}, [userId, accountType]);
|
|
277
241
|
|
|
278
|
-
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
if (scriptStatus !== 'ready') return;
|
|
244
|
+
|
|
245
|
+
void fetchData();
|
|
246
|
+
}, [fetchData, scriptStatus]);
|
|
247
|
+
|
|
248
|
+
if (error) {
|
|
279
249
|
return (
|
|
280
|
-
<Card
|
|
250
|
+
<Card
|
|
251
|
+
ref={cardRef}
|
|
252
|
+
className="flex min-h-124 w-80 flex-col items-center justify-center gap-4 border-0 p-0"
|
|
253
|
+
>
|
|
281
254
|
<ErrorImage />
|
|
255
|
+
{/* <Button variant="outline" className="border-red-500 text-black hover:bg-red-50" onClick={fetchData}>
|
|
256
|
+
加载失败 请重试
|
|
257
|
+
</Button> */}
|
|
282
258
|
<div>
|
|
283
259
|
<span className="text-sm">加载失败 请</span>
|
|
284
260
|
<Button
|
|
285
261
|
size="sm"
|
|
286
262
|
variant="ghost"
|
|
287
263
|
className="pr-1 pl-1 text-primary! not-disabled:hover:bg-primary/10 not-disabled:hover:text-primary/90 focus-visible:bg-primary/20"
|
|
288
|
-
onClick={
|
|
264
|
+
onClick={fetchData}
|
|
289
265
|
>
|
|
290
266
|
重试
|
|
291
267
|
</Button>
|
|
@@ -293,269 +269,42 @@ function UserProfile(props: UserProfileProps) {
|
|
|
293
269
|
</Card>
|
|
294
270
|
);
|
|
295
271
|
}
|
|
296
|
-
|
|
297
|
-
// isPending 只在「无缓存且首次请求中」为真。
|
|
298
|
-
// 命中缓存时直接跳过 loading,卡片零延迟出现,后台静默 revalidate。
|
|
299
|
-
if (isPending || !data) {
|
|
300
|
-
return (
|
|
301
|
-
<Card className="flex h-130 w-80 flex-col items-center justify-center gap-6 border-0 p-0">
|
|
302
|
-
<Spinner className="size-8 animate-spin text-primary" />
|
|
303
|
-
</Card>
|
|
304
|
-
);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// 业务仓的 tsconfig 继承自 fullstack-presets,是 strictNullChecks: false。
|
|
308
|
-
// TS 在该模式下不做判别联合窄化(本地现写的同形状联合一样窄化失败),
|
|
309
|
-
// 所以这里必须显式断言分支类型,不能简化成 data.userProfileInfo。
|
|
310
|
-
if (!data.useLarkCard) {
|
|
311
|
-
const { userProfileInfo } = data as SimpleProfileData;
|
|
312
|
-
return <SimpleUserProfile userProfileInfo={userProfileInfo} />;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const { larkCardParam } = data as OfficialProfileData;
|
|
316
|
-
// key 绑 openId:切换到另一个人时整个重挂,备用卡引用、文字基线、loading
|
|
317
|
-
// 状态一并重置。否则 spareRef 的 `??=` 会把上一个人的卡片留下来复用。
|
|
318
272
|
return (
|
|
319
|
-
<
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
273
|
+
<Card
|
|
274
|
+
ref={cardRef}
|
|
275
|
+
className="flex h-130 w-80 flex-col items-center justify-center gap-6 border-0 p-0"
|
|
276
|
+
>
|
|
277
|
+
{loading ? <LoadingComponent /> : <BaseUserProfile {...response} />}
|
|
278
|
+
</Card>
|
|
323
279
|
);
|
|
324
280
|
}
|
|
325
281
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
* 人员数据由 `<tt-profile>` 自己拉取,不经过我们的 state,因此这里无法做
|
|
330
|
-
* 字段级的 stale-while-revalidate。退而求其次:缓存整个已渲染的 DOM,
|
|
331
|
-
* 命中即刻展示;内容陈旧时在离屏容器里渲染一份新的,就绪后整体替换,
|
|
332
|
-
* 这样换新过程中用户看到的始终是旧卡片,不会出现骨架屏。
|
|
333
|
-
*/
|
|
334
|
-
function LarkProfileCard(props: { readonly larkCardParam: LarkCardParam }) {
|
|
335
|
-
const { larkCardParam } = props;
|
|
336
|
-
const { needRedirect, redirectURL, targetLarkOpenID } = larkCardParam;
|
|
337
|
-
|
|
338
|
-
// 「打开后更新」渲染出的元素由本实例独占 —— 它会被 attach 进本实例的容器,
|
|
339
|
-
// 取消时也由本实例回收。所以去重必须按实例隔离:ensureRender 的 key 是模块级的,
|
|
340
|
-
// 同一个人的两个实例共享一次渲染的话,两边会拿到同一个 DOM 元素,
|
|
341
|
-
// 后挂载的那个会把节点从前一个容器里抢走(前者变空白),先卸载的那个
|
|
342
|
-
// 还会把另一个正在显示的节点 remove 掉。
|
|
343
|
-
//
|
|
344
|
-
// 备用卡池仍然按人共享 —— 那条路径是 takeSpare 独占领取,不存在同一元素
|
|
345
|
-
// 被两个实例同时持有的问题。
|
|
346
|
-
const instanceId = useId();
|
|
347
|
-
|
|
348
|
-
const hostRef = useRef<HTMLDivElement>(null);
|
|
349
|
-
// 初值在渲染期就定下来:有备用卡就不该出现转圈。effect 在首次绘制之后
|
|
350
|
-
// 才跑,若初值恒为 true,命中缓存也会先闪一帧 loading。
|
|
351
|
-
const [showSpinner, setShowSpinner] = useState(() =>
|
|
352
|
-
!hasSpare(targetLarkOpenID),
|
|
353
|
-
);
|
|
354
|
-
const [renderFailed, setRenderFailed] = useState(false);
|
|
355
|
-
const [retryToken, setRetryToken] = useState(0);
|
|
356
|
-
|
|
357
|
-
// 这次挂载取到的备用卡。takeSpare 是「取走」语义,effect 可能重复执行,
|
|
358
|
-
// 不记在 ref 里第二次就拿不到,会误判成没有缓存而走完整 loading。
|
|
359
|
-
const spareRef = useRef<Element | null>(null);
|
|
360
|
-
// 当前挂在容器里的那一张。卸载清理要用它,而那时 hostRef 可能已被置空。
|
|
361
|
-
const shownRef = useRef<Element | null>(null);
|
|
362
|
-
// 挂上那一刻、本人那一屏的文字。卸载时比对,判断用户有没有改过本人的数据。
|
|
363
|
-
const attachedTextRef = useRef<string>('');
|
|
364
|
-
// 组件是否还活着。异步回调的归属是「组件实例」而不是「某一次 effect 执行」,
|
|
365
|
-
// 所以用它而不是 effect 内的 cancelled 标志 —— 否则依赖一变化,正在途中的
|
|
366
|
-
// 渲染结果就会被当成已取消而丢弃。
|
|
367
|
-
const aliveRef = useRef(true);
|
|
368
|
-
// 已经为哪一轮 retryToken 启动过渲染,保证依赖变化时不重复发起。
|
|
369
|
-
const startedRef = useRef(-1);
|
|
370
|
-
|
|
371
|
-
const scriptOptions = useMemo(
|
|
372
|
-
() => ({ onloadCallback: registerLarkAuthHandler }),
|
|
373
|
-
[],
|
|
374
|
-
);
|
|
375
|
-
const scriptStatus = useExternalScript(LARK_SDK_URL, scriptOptions);
|
|
376
|
-
|
|
377
|
-
// 卸载清理需要的值走 ref,避免为了它们把依赖写宽
|
|
378
|
-
const teardownRef = useRef({ param: larkCardParam, scriptReady: false });
|
|
379
|
-
teardownRef.current = {
|
|
380
|
-
param: larkCardParam,
|
|
381
|
-
scriptReady: scriptStatus === 'ready',
|
|
382
|
-
};
|
|
282
|
+
function LoadingComponent() {
|
|
283
|
+
return <Spinner className="size-8 animate-spin text-primary" />;
|
|
284
|
+
}
|
|
383
285
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
389
|
-
}, [needRedirect, redirectURL]);
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* 只在**真正卸载**时回收。
|
|
393
|
-
*
|
|
394
|
-
* 不能放进主 effect 的 cleanup —— 那个 cleanup 在任何依赖变化时都会跑
|
|
395
|
-
* (scriptStatus 从 loading 变 ready、react-query 刷新导致 larkCardParam
|
|
396
|
-
* 换引用,都是必经路径)。在那里 remove 掉正在显示的卡片,会造成可见的
|
|
397
|
-
* 闪白回退,而且被摘掉的若是「打开后更新」那张,它既不在池里也不在容器里,
|
|
398
|
-
* 直接成了孤儿。
|
|
399
|
-
*/
|
|
400
|
-
useEffect(() => {
|
|
401
|
-
aliveRef.current = true;
|
|
402
|
-
return () => {
|
|
403
|
-
aliveRef.current = false;
|
|
404
|
-
|
|
405
|
-
const shown = shownRef.current;
|
|
406
|
-
shownRef.current = null;
|
|
407
|
-
if (!shown) return;
|
|
408
|
-
|
|
409
|
-
const changed = ownProfileText(shown) !== attachedTextRef.current;
|
|
410
|
-
// 用过的那张直接丢弃 —— 它可能被滚动过、点进过别人的名片,
|
|
411
|
-
// 而这些状态有一半在飞书 React 内部,无法可靠恢复
|
|
412
|
-
shown.remove();
|
|
413
|
-
|
|
414
|
-
const { param, scriptReady } = teardownRef.current;
|
|
415
|
-
if (!changed || !scriptReady) return;
|
|
416
|
-
|
|
417
|
-
// 用户在卡片里改过本人的数据(最典型的是编辑个性签名)。已经备好的那张
|
|
418
|
-
// 是改动之前渲染的,内容已过时:丢掉,并立刻重新备一张 —— 只丢不补的话
|
|
419
|
-
// 下次打开会退回完整 loading。带时间戳的 key 绕开去重,确保拿到的是
|
|
420
|
-
// 改动之后重新渲染的结果。
|
|
421
|
-
dropSpare(param.targetLarkOpenID);
|
|
422
|
-
void ensureRender(
|
|
423
|
-
`${param.targetLarkOpenID}:spare:${String(Date.now())}`,
|
|
424
|
-
async () => {
|
|
425
|
-
const live = await renderCard(param, null);
|
|
426
|
-
if (live) putSpare(param.targetLarkOpenID, live);
|
|
427
|
-
return live;
|
|
428
|
-
},
|
|
429
|
-
).catch((error: unknown) => {
|
|
430
|
-
logger.info('re-prepare spare lark profile failed', error);
|
|
431
|
-
});
|
|
432
|
-
};
|
|
433
|
-
}, []);
|
|
286
|
+
function BaseUserProfile(props: BaseUserProfileProps) {
|
|
287
|
+
const { useLarkCard } = props;
|
|
288
|
+
if (!useLarkCard) {
|
|
289
|
+
const { userProfileInfo } = props as BaseSimpleProfileProps;
|
|
434
290
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
useEffect(() => {
|
|
438
|
-
if (needRedirect) return;
|
|
439
|
-
|
|
440
|
-
const host = hostRef.current;
|
|
441
|
-
if (!host) return;
|
|
442
|
-
|
|
443
|
-
currentRedirectURL = redirectURL ?? '';
|
|
444
|
-
// 脚本已存在时 useExternalScript 会跳过 onloadCallback,这里补注册一次
|
|
445
|
-
if (scriptStatus === 'ready') registerLarkAuthHandler();
|
|
446
|
-
|
|
447
|
-
const attach = (el: Element | null) => {
|
|
448
|
-
if (!aliveRef.current || !el) return;
|
|
449
|
-
host.replaceChildren(el);
|
|
450
|
-
shownRef.current = el;
|
|
451
|
-
attachedTextRef.current = ownProfileText(el);
|
|
452
|
-
setShowSpinner(false);
|
|
453
|
-
setRenderFailed(false);
|
|
454
|
-
};
|
|
455
|
-
|
|
456
|
-
if (!shownRef.current) {
|
|
457
|
-
// 备用卡是渲染好后一直停在离屏、从没显示过的,所以必然是初始态,
|
|
458
|
-
// 而且是活元素 —— 挂上即可交互,不是静态快照
|
|
459
|
-
spareRef.current ??= takeSpare(targetLarkOpenID);
|
|
460
|
-
if (spareRef.current) attach(spareRef.current);
|
|
461
|
-
}
|
|
291
|
+
return <SimpleUserProfile userProfileInfo={userProfileInfo} />;
|
|
292
|
+
}
|
|
462
293
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
if (!hadSpare) setShowSpinner(true);
|
|
469
|
-
|
|
470
|
-
// 打开后更新一次:再渲染一张,就绪后换上去,让这次看到的是最新数据。
|
|
471
|
-
// 有备用卡顶着就离屏渲染(用户看不到骨架屏),没有就直接渲进可见容器。
|
|
472
|
-
void ensureRender(
|
|
473
|
-
`${targetLarkOpenID}:update:${instanceId}:${String(retryToken)}`,
|
|
474
|
-
() => renderCard(larkCardParam, hadSpare ? null : host),
|
|
475
|
-
)
|
|
476
|
-
.then((live) => {
|
|
477
|
-
if (!aliveRef.current) {
|
|
478
|
-
// 组件已卸载。元素此时在常驻离屏容器里,既不会被 attach 也不进
|
|
479
|
-
// 受 maxEntries 约束的池 —— 放着不管会随反复开关一直累积。
|
|
480
|
-
live?.remove();
|
|
481
|
-
return;
|
|
482
|
-
}
|
|
483
|
-
if (live) {
|
|
484
|
-
attach(live);
|
|
485
|
-
return;
|
|
486
|
-
}
|
|
487
|
-
// 渲染没能就绪。已经挂着备用卡就保持现状;什么都没有的话必须收掉
|
|
488
|
-
// loading 并给出重试入口,否则会一直转圈。
|
|
489
|
-
if (!shownRef.current) {
|
|
490
|
-
setShowSpinner(false);
|
|
491
|
-
setRenderFailed(true);
|
|
492
|
-
}
|
|
493
|
-
})
|
|
494
|
-
.catch((error: unknown) => {
|
|
495
|
-
logger.info('update lark profile failed', error);
|
|
496
|
-
if (!aliveRef.current) return;
|
|
497
|
-
setShowSpinner(false);
|
|
498
|
-
if (!shownRef.current) setRenderFailed(true);
|
|
499
|
-
});
|
|
500
|
-
|
|
501
|
-
// 立刻开始备下一张。必须在「打开」时做而不是「关闭」时 —— 真实环境渲染
|
|
502
|
-
// 一张要走鉴权、render、网关取数、图片加载,几秒起步;放在关闭时做的话,
|
|
503
|
-
// 用户点快一点下次打开就还没备好,又掉回完整 loading。
|
|
504
|
-
if (!hasSpare(targetLarkOpenID)) {
|
|
505
|
-
void ensureRender(`${targetLarkOpenID}:spare`, async () => {
|
|
506
|
-
const live = await renderCard(larkCardParam, null);
|
|
507
|
-
if (live) putSpare(targetLarkOpenID, live);
|
|
508
|
-
return live;
|
|
509
|
-
}).catch((error: unknown) => {
|
|
510
|
-
logger.info('prepare spare lark profile failed', error);
|
|
511
|
-
});
|
|
294
|
+
const { larkCardParam } = props;
|
|
295
|
+
const { needRedirect, redirectURL } = larkCardParam;
|
|
296
|
+
if (needRedirect) {
|
|
297
|
+
if (!redirectURL) {
|
|
298
|
+
return null;
|
|
512
299
|
}
|
|
513
|
-
}, [
|
|
514
|
-
instanceId,
|
|
515
|
-
larkCardParam,
|
|
516
|
-
needRedirect,
|
|
517
|
-
redirectURL,
|
|
518
|
-
retryToken,
|
|
519
|
-
scriptStatus,
|
|
520
|
-
targetLarkOpenID,
|
|
521
|
-
]);
|
|
522
|
-
|
|
523
|
-
const handleRetryRender = useCallback(() => {
|
|
524
|
-
setRenderFailed(false);
|
|
525
|
-
setShowSpinner(true);
|
|
526
|
-
setRetryToken((n) => n + 1);
|
|
527
|
-
}, []);
|
|
528
|
-
|
|
529
|
-
if (needRedirect) return null;
|
|
530
300
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
{showSpinner && (
|
|
535
|
-
<Spinner className="absolute size-8 animate-spin text-primary" />
|
|
536
|
-
)}
|
|
537
|
-
{renderFailed && (
|
|
538
|
-
<div className="absolute flex flex-col items-center gap-4">
|
|
539
|
-
<ErrorImage />
|
|
540
|
-
<div>
|
|
541
|
-
<span className="text-sm">加载失败 请</span>
|
|
542
|
-
<Button
|
|
543
|
-
size="sm"
|
|
544
|
-
variant="ghost"
|
|
545
|
-
className="pr-1 pl-1 text-primary! not-disabled:hover:bg-primary/10 not-disabled:hover:text-primary/90 focus-visible:bg-primary/20"
|
|
546
|
-
onClick={handleRetryRender}
|
|
547
|
-
>
|
|
548
|
-
重试
|
|
549
|
-
</Button>
|
|
550
|
-
</div>
|
|
551
|
-
</div>
|
|
552
|
-
)}
|
|
553
|
-
</Card>
|
|
554
|
-
);
|
|
301
|
+
globalThis.location.replace(redirectURL);
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
555
304
|
}
|
|
556
305
|
|
|
557
306
|
function SimpleUserProfile(props: {
|
|
558
|
-
readonly userProfileInfo:
|
|
307
|
+
readonly userProfileInfo: BaseSimpleProfileProps['userProfileInfo'];
|
|
559
308
|
}) {
|
|
560
309
|
const { userProfileInfo } = props;
|
|
561
310
|
|
|
@@ -16,18 +16,6 @@ import {
|
|
|
16
16
|
PopoverTrigger,
|
|
17
17
|
} from '@client/src/components/ui/popover';
|
|
18
18
|
|
|
19
|
-
/**
|
|
20
|
-
* 阻止 Popover 打开时的自动聚焦。
|
|
21
|
-
*
|
|
22
|
-
* Radix 的 FocusScope 在弹层挂载时会把焦点给容器里第一个可聚焦元素。人员卡片
|
|
23
|
-
* 挂载那一刻内容就已经在了(飞书名片里有「消息」等按钮),于是一打开就有个
|
|
24
|
-
* 按钮带上焦点环。这是张只读预览卡,焦点应该留在触发器上 —— Radix 自己的
|
|
25
|
-
* HoverCard 也是这么处理的。
|
|
26
|
-
*/
|
|
27
|
-
const handleProfileOpenAutoFocus = (event: Event) => {
|
|
28
|
-
event.preventDefault();
|
|
29
|
-
};
|
|
30
|
-
|
|
31
19
|
export type UserPillProps = {
|
|
32
20
|
/**
|
|
33
21
|
* 用户 ID,用于 UserProfile 展示
|
|
@@ -129,7 +117,6 @@ export const UserPill: React.FC<UserPillProps> = ({
|
|
|
129
117
|
side="bottom"
|
|
130
118
|
align="start"
|
|
131
119
|
sideOffset={8}
|
|
132
|
-
onOpenAutoFocus={handleProfileOpenAutoFocus}
|
|
133
120
|
className="w-80 p-0"
|
|
134
121
|
>
|
|
135
122
|
<UserProfile userId={userId} accountType={accountType} />
|