-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next/web): display ticket viewers (#949)
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
next/web/src/App/Admin/Tickets/Ticket/components/TicketViewers.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,22 @@ | ||
import { Avatar, Tooltip } from 'antd'; | ||
|
||
import { UseTicketViewersOptions, useTicketViewers } from '../hooks/useTicketViewers'; | ||
|
||
interface TicketViewersProps { | ||
ticket: UseTicketViewersOptions; | ||
} | ||
|
||
export function TicketViewers({ ticket }: TicketViewersProps) { | ||
const viewers = useTicketViewers(ticket); | ||
return ( | ||
<Avatar.Group maxCount={4} style={{ display: 'flex' }}> | ||
{viewers?.map((viewer) => ( | ||
<Tooltip key={viewer.id} title={viewer.nickname}> | ||
<Avatar style={{ backgroundColor: '#' + viewer.id.slice(-12, -6) }}> | ||
{viewer.nickname.slice(0, 1)} | ||
</Avatar> | ||
</Tooltip> | ||
))} | ||
</Avatar.Group> | ||
); | ||
} |
55 changes: 55 additions & 0 deletions
55
next/web/src/App/Admin/Tickets/Ticket/hooks/useTicketViewers.ts
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,55 @@ | ||
import { useMemo } from 'react'; | ||
import { useQuery } from 'react-query'; | ||
import moment from 'moment'; | ||
import _ from 'lodash'; | ||
|
||
import { http } from '@/leancloud'; | ||
import { useUsers } from '@/api/user'; | ||
|
||
export interface UseTicketViewersOptions { | ||
id: string; | ||
createdAt: string; | ||
} | ||
|
||
export function useTicketViewers(ticket: UseTicketViewersOptions) { | ||
const refetchInterval = useMemo(() => { | ||
const hours = moment().diff(ticket.createdAt, 'hour'); | ||
if (hours < 1) { | ||
// 1 小时内创建的工单每 10 秒刷新一次 | ||
return 10000; | ||
} | ||
// 增加刷新时间, 上限为 24 小时 / 40 秒 | ||
return Math.floor(30000 * (Math.min(24, hours) / 24)) + 10000; | ||
}, [ticket.createdAt]); | ||
|
||
const { data: viewerIds } = useQuery({ | ||
queryKey: ['TicketViewers', ticket.id], | ||
queryFn: async () => { | ||
const res = await http.get<string[]>(`/api/2/tickets/${ticket.id}/viewers`, { | ||
params: { excludeSelf: 1 }, | ||
}); | ||
return res.data; | ||
}, | ||
cacheTime: 0, | ||
refetchInterval, | ||
keepPreviousData: true, | ||
}); | ||
|
||
const sortedIds = useMemo(() => (viewerIds || []).slice().sort(), [viewerIds]); | ||
|
||
const { data: viewers } = useUsers({ | ||
id: sortedIds, | ||
queryOptions: { | ||
enabled: sortedIds.length > 0, | ||
keepPreviousData: true, | ||
}, | ||
}); | ||
|
||
return useMemo(() => { | ||
if (viewerIds && viewers) { | ||
const viewerMap = _.keyBy(viewers, (v) => v.id); | ||
return viewerIds.map((id) => viewerMap[id]).filter(Boolean); | ||
} | ||
return []; | ||
}, [viewerIds, viewers]); | ||
} |