@lark-apaas/client-toolkit 1.1.25-alpha.1 → 1.1.25-user-test.0

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.
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import type { IUserProfile } from '../../apis/udt-types';
3
- export interface UserDisplayProps {
3
+ export interface IUserDisplayProps {
4
4
  /**
5
5
  * 支持传入完整的用户资料(IUserProfile)/ 列表(IUserProfile[])
6
6
  * 或仅传入用户 ID(string)/ 列表(string[])。
@@ -11,5 +11,6 @@ export interface UserDisplayProps {
11
11
  className?: string;
12
12
  style?: React.CSSProperties;
13
13
  showLabel?: boolean;
14
+ showUserProfile?: boolean;
14
15
  }
15
- export declare const UserDisplay: React.FC<UserDisplayProps>;
16
+ export declare const UserDisplay: React.FC<IUserDisplayProps>;
@@ -1,10 +1,11 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useEffect, useMemo, useState } from "react";
3
- import { getDataloom } from "../../integrations/dataloom.js";
3
+ import { getCachedUserProfiles, setCachedUserProfiles } from "../../utils/userProfileCache.js";
4
4
  import { clsxWithTw } from "../../utils/utils.js";
5
5
  import { UserProfile } from "./UserProfile/index.js";
6
6
  import { UserWithAvatar } from "./UserWithAvatar.js";
7
7
  import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover.js";
8
+ import { fetchWithDeduplication } from "../../utils/requestManager.js";
8
9
  const UserDisplay = ({ users, size, className, style, showLabel = true })=>{
9
10
  const normalizedIds = useMemo(()=>{
10
11
  if (!users) return [];
@@ -71,22 +72,20 @@ const UserDisplay = ({ users, size, className, style, showLabel = true })=>{
71
72
  const fetchProfiles = async ()=>{
72
73
  try {
73
74
  setLoadingProfiles(true);
74
- const dataloom = await getDataloom();
75
- if (!dataloom) throw new Error('dataloom client is unavailable');
76
- const ids = normalizedIds.map((id)=>Number(id)).filter((id)=>Number.isFinite(id));
77
- if (!ids.length) {
78
- setResolvedUsers(normalizedIds.map((id)=>inputProfilesMap.get(id) ?? {
79
- user_id: id,
80
- name: '',
81
- avatar: '',
82
- email: '',
83
- status: 1
84
- }));
85
- setLoadingProfiles(false);
86
- return;
87
- }
88
- const response = await dataloom.service.user.getByIds(ids);
89
- const fetchedList = Array.isArray(response?.data) ? response?.data : Array.isArray(response?.data?.user_list) ? response?.data?.user_list : [];
75
+ const { cachedProfiles, remainingIds } = getCachedUserProfiles(normalizedIds);
76
+ if (cachedProfiles.length > 0) setResolvedUsers((prev)=>{
77
+ const updatedUsers = [
78
+ ...prev
79
+ ];
80
+ cachedProfiles.forEach((cached)=>{
81
+ const index = updatedUsers.findIndex((u)=>u.user_id === cached.user_id);
82
+ if (-1 !== index) updatedUsers[index] = cached;
83
+ });
84
+ return updatedUsers;
85
+ });
86
+ if (0 === remainingIds.length) return void setLoadingProfiles(false);
87
+ const fetchedList = await fetchWithDeduplication(remainingIds);
88
+ if (isCancelled || !fetchedList) return;
90
89
  const fetchedMap = new Map();
91
90
  fetchedList.forEach((profile)=>{
92
91
  const id = String(profile?.user_id ?? '');
@@ -99,13 +98,15 @@ const UserDisplay = ({ users, size, className, style, showLabel = true })=>{
99
98
  status: profile?.status ?? 1
100
99
  });
101
100
  });
101
+ setCachedUserProfiles(fetchedList);
102
102
  const mergedUsers = normalizedIds.map((id)=>{
103
103
  const fetched = fetchedMap.get(id);
104
104
  const given = inputProfilesMap.get(id);
105
- const name = given?.name?.trim() ? given.name : fetched?.name ?? '';
106
- const avatar = given?.avatar || fetched?.avatar || '';
107
- const email = given?.email || fetched?.email || '';
108
- const status = given?.status ?? fetched?.status ?? 1;
105
+ const cached = cachedProfiles.find((p)=>p.user_id === id);
106
+ const name = given?.name?.trim() || fetched?.name || cached?.name || '';
107
+ const avatar = given?.avatar || fetched?.avatar || cached?.avatar || '';
108
+ const email = given?.email || fetched?.email || cached?.email || '';
109
+ const status = given?.status ?? fetched?.status ?? cached?.status ?? 1;
109
110
  return {
110
111
  user_id: id,
111
112
  name,
@@ -1,7 +1,7 @@
1
1
  export { UserSelect } from './UserSelect';
2
2
  export type { UserSelectProps } from './UserSelect';
3
3
  export { UserDisplay } from './UserDisplay';
4
- export type { UserDisplayProps } from './UserDisplay';
4
+ export type { IUserDisplayProps as UserDisplayProps } from './UserDisplay';
5
5
  export { UserWithAvatar } from './UserWithAvatar';
6
6
  export type { UserWithAvatarProps } from './type';
7
7
  export { UserProfile } from './UserProfile';
@@ -0,0 +1,2 @@
1
+ import type { IUserProfile } from '../apis/udt-types';
2
+ export declare function fetchWithDeduplication(ids: string[]): Promise<IUserProfile[] | null>;
@@ -0,0 +1,26 @@
1
+ import { getDataloom } from "../integrations/dataloom.js";
2
+ const pendingRequests = new Map();
3
+ async function fetchUserProfilesByIds(ids) {
4
+ try {
5
+ const dataloom = await getDataloom();
6
+ if (!dataloom) throw new Error('Dataloom client is unavailable');
7
+ const numericIds = ids.map(Number).filter(Number.isFinite);
8
+ if (0 === numericIds.length) return [];
9
+ const response = await dataloom.service.user.getByIds(numericIds);
10
+ return Array.isArray(response?.data) ? response.data : response?.data?.user_list || [];
11
+ } catch (error) {
12
+ console.error(`Failed to fetch profiles for user IDs ${ids.join(', ')}:`, error);
13
+ return null;
14
+ }
15
+ }
16
+ function fetchWithDeduplication(ids) {
17
+ const key = ids.sort().join(',');
18
+ if (pendingRequests.has(key)) return pendingRequests.get(key);
19
+ const fetchPromise = fetchUserProfilesByIds(ids);
20
+ pendingRequests.set(key, fetchPromise);
21
+ fetchPromise.finally(()=>{
22
+ pendingRequests.delete(key);
23
+ });
24
+ return fetchPromise;
25
+ }
26
+ export { fetchWithDeduplication };
@@ -0,0 +1,12 @@
1
+ import type { IUserProfile } from '../apis/udt-types';
2
+ export interface ICachedUserProfile {
3
+ profile: IUserProfile;
4
+ timestamp: number;
5
+ }
6
+ export declare const cache: Map<string, ICachedUserProfile>;
7
+ export declare const cacheExpiry: number;
8
+ export declare function setCachedUserProfiles(profiles: IUserProfile[]): void;
9
+ export declare function getCachedUserProfiles(ids: string[]): {
10
+ cachedProfiles: IUserProfile[];
11
+ remainingIds: string[];
12
+ };
@@ -0,0 +1,26 @@
1
+ const cache = new Map();
2
+ const cacheExpiry = 1 / 0;
3
+ function setCachedUserProfiles(profiles) {
4
+ const now = Date.now();
5
+ profiles.forEach((profile)=>{
6
+ if (profile && profile.user_id) cache.set(String(profile.user_id), {
7
+ profile,
8
+ timestamp: now
9
+ });
10
+ });
11
+ }
12
+ function getCachedUserProfiles(ids) {
13
+ const cachedProfiles = [];
14
+ const remainingIds = [];
15
+ const now = Date.now();
16
+ ids.forEach((id)=>{
17
+ const cachedItem = cache.get(id);
18
+ if (cachedItem && now - cachedItem.timestamp < cacheExpiry) cachedProfiles.push(cachedItem.profile);
19
+ else remainingIds.push(id);
20
+ });
21
+ return {
22
+ cachedProfiles,
23
+ remainingIds
24
+ };
25
+ }
26
+ export { cache, cacheExpiry, getCachedUserProfiles, setCachedUserProfiles };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.1.25-alpha.1",
3
+ "version": "1.1.25-user-test.0",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [