diff --git a/moon/apps/web/components/MrView/LabelItem.tsx b/moon/apps/web/components/MrView/LabelItem.tsx index 35690db01..076dd3a7f 100644 --- a/moon/apps/web/components/MrView/LabelItem.tsx +++ b/moon/apps/web/components/MrView/LabelItem.tsx @@ -1,36 +1,96 @@ -import { ConditionalWrap } from '@gitmono/ui' -import { useGetOrganizationMember } from '@/hooks/useGetOrganizationMember' -import { MemberHovercard } from '../InlinePost/MemberHovercard' -import { MemberAvatar } from '../MemberAvatar' -import { UserLinkByName } from './components/UserLinkByName' +import {ConditionalWrap} from '@gitmono/ui' +import {useGetOrganizationMember} from '@/hooks/useGetOrganizationMember' +import {MemberHovercard} from '../InlinePost/MemberHovercard' +import {MemberAvatar} from '../MemberAvatar' +import {UserLinkByName} from './components/UserLinkByName' import HandleTime from './components/HandleTime' -import { ConversationItem } from '@gitmono/types/generated' - +import {ConversationItem, GetApiLabelByIdData} from '@gitmono/types/generated' +import {getFontColor} from "@/utils/getFontColor"; +import {legacyApiClient} from "@/utils/queryClient"; +import {useEffect, useMemo, useState} from "react"; +import {apiErrorToast} from "@/utils/apiErrorToast"; interface LabelItemProps { conv: ConversationItem } -const LabelItem = ({ conv }: LabelItemProps) => { - const { data: member } = useGetOrganizationMember({ username: conv.username }) +type LabelList = ({ color: string; description: string; id: number; name: string; })[] + +function LabelItem({conv}: LabelItemProps) { + const {data: member} = useGetOrganizationMember({username: conv.username}) + const comment = conv.comment?.split(' ') ?? [] + const match = useMemo( + () => + conv.comment?.match(/\[(.*?)]/) ?? [] + , [conv.comment]) + const [idList, setIdList] = useState([]) + const [labelList, setLabelList] = useState([]); + + useEffect(() => { + const res = (match.length > 1) ? match[1].split(", ") : [] + + setIdList(res) + }, [match]) + useEffect(() => { + if (idList.length === 0) return; + + Promise.all( + idList.map(id => { + const numId = parseInt(id, 10); + + if (isNaN(numId)) return Promise.reject(new Error('Invalid ID')); + return legacyApiClient.v1.getApiLabelById().request(numId); + }) + ).then((res: GetApiLabelByIdData[]) => { + const fetchedLabels = res + .filter(res => res?.data) + .map(res => res!!.data!!) ?? []; + + setLabelList(fetchedLabels); + }).catch(err => + apiErrorToast(err) + ) + }, [idList]) return ( <> -
-
- ( - - - {c} - - - )} - > - {member ? : 'Avatar not found'} - +
+ ( + + + {c} + + + )} + > + {member ? : 'Avatar not found'} + +
+ {conv.username} + {comment[1]} + { + labelList.map(label => { + const fontColor = getFontColor(label.color) + + return + {label.name} + + }) + }
-
{conv.comment}
@@ -39,4 +99,4 @@ const LabelItem = ({ conv }: LabelItemProps) => { ) } -export default LabelItem +export default LabelItem \ No newline at end of file diff --git a/moon/apps/web/hooks/useGetLabelById.ts b/moon/apps/web/hooks/useGetLabelById.ts new file mode 100644 index 000000000..4e814a9f7 --- /dev/null +++ b/moon/apps/web/hooks/useGetLabelById.ts @@ -0,0 +1,10 @@ +import {useQuery} from "@tanstack/react-query"; +import {GetApiLabelByIdData} from "@gitmono/types"; +import {legacyApiClient} from "@/utils/queryClient"; + +export function useGetLabelById(id: number) { + return useQuery({ + queryKey: ['label', id], + queryFn: () => legacyApiClient.v1.getApiLabelById().request(id), + }) +} \ No newline at end of file diff --git a/moon/packages/types/generated.ts b/moon/packages/types/generated.ts index e277dab61..59886333a 100644 --- a/moon/packages/types/generated.ts +++ b/moon/packages/types/generated.ts @@ -3122,6 +3122,18 @@ export type CommonResultIssueDetailRes = { req_result: boolean } +export type CommonResultLabelItem = { + data?: { + color: string + description: string + /** @format int64 */ + id: number + name: string + } + err_message: string + req_result: boolean +} + export type CommonResultMRDetailRes = { data?: { assignees: string[] @@ -4688,7 +4700,7 @@ export type PostApiLabelListData = CommonResultCommonPageLabelItem export type PostApiLabelNewData = CommonResultString -export type GetApiLabelByIdData = CommonResultCommonPageLabelItem +export type GetApiLabelByIdData = CommonResultLabelItem export type GetApiLatestCommitParams = { refs?: string