|
| 1 | +// src/hooks/useHydrateIssueDetail.ts |
| 2 | +import { useEffect, useRef } from 'react'; |
| 3 | +import type { SubmitHandleRef } from '../components/DetailView/TextEditor/lexical-plugins/SubmitHandlePlugin'; |
| 4 | +import type { StatusCode, PriorityCode } from '../types/listItem'; |
| 5 | +import type { ViewIssueDetailDto } from '../types/issue'; |
| 6 | +import type { SimpleGoal } from '../types/goal'; |
| 7 | + |
| 8 | +type Params = { |
| 9 | + issueDetail?: ViewIssueDetailDto | undefined; |
| 10 | + issueId?: number; |
| 11 | + editorRef: React.RefObject<SubmitHandleRef | null>; |
| 12 | + |
| 13 | + // ์ธ๋ถ ์ต์
/๋งคํ ์ค๋น์ฌ๋ถ ํ๋จ์ฉ |
| 14 | + workspaceMembers?: Array<{ memberId: number; name: string }>; |
| 15 | + simpleGoals?: SimpleGoal[]; // ๋ชฉํ ์ฐ๊ฒฐ์ฉ ๊ฐ๋จ ๋ชฉ๋ก |
| 16 | + nameToId: Record<string, number>; |
| 17 | + |
| 18 | + // ์ํ ์ธํฐ๋ค |
| 19 | + setTitle: (v: string) => void; |
| 20 | + setState: (v: StatusCode) => void; |
| 21 | + setPriority: (v: PriorityCode) => void; |
| 22 | + setSelectedDate: (v: [Date | null, Date | null]) => void; |
| 23 | + setManagersId: (v: number[]) => void; |
| 24 | + setGoalId: (v: number | null) => void; // ๋จ์ผ ์ ํ(์์ ๊ฐ๋ฅ) |
| 25 | +}; |
| 26 | + |
| 27 | +export const useHydrateIssueDetail = ({ |
| 28 | + issueDetail, |
| 29 | + issueId, |
| 30 | + editorRef, |
| 31 | + workspaceMembers, |
| 32 | + simpleGoals, |
| 33 | + nameToId, |
| 34 | + setTitle, |
| 35 | + setState, |
| 36 | + setPriority, |
| 37 | + setSelectedDate, |
| 38 | + setManagersId, |
| 39 | + setGoalId, |
| 40 | +}: Params) => { |
| 41 | + const hydratedRef = useRef(false); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + if (!issueDetail) return; |
| 45 | + if (!Number.isFinite(issueId)) return; |
| 46 | + if (hydratedRef.current) return; |
| 47 | + |
| 48 | + // ์ต์
์ค๋น์ฌ๋ถ ํ๋จ |
| 49 | + // - ๋ด๋น์๊ฐ ์กด์ฌํ๋ฉด ๋ฉค๋ฒ ์ต์
์ค๋น ํ์ |
| 50 | + const membersReady = |
| 51 | + (workspaceMembers?.length ?? 0) > 0 || (issueDetail.managers?.cnt ?? 0) === 0; |
| 52 | + |
| 53 | + // - ๋ชฉํ ์ฐ๊ฒฐ(๋จ์ผ) ์ธํ
์ฉ: ์๋ฒ ์๋ต์ goal.id๊ฐ ์์ผ๋ฉด ๋ฐ๋ก ์ธํ
๊ฐ๋ฅ |
| 54 | + // goal.id๊ฐ ์๊ณ title๋ง ์์ ๊ฒฝ์ฐ, simpleGoals ์ค๋น ํ title->id ๋งคํ ํ์ |
| 55 | + const needGoalsByTitle = !!issueDetail.goal?.title && issueDetail.goal?.id == null; |
| 56 | + const goalsReady = needGoalsByTitle ? (simpleGoals?.length ?? 0) > 0 : true; |
| 57 | + |
| 58 | + if (!membersReady || !goalsReady) return; |
| 59 | + |
| 60 | + // 1) ๊ธฐ๋ณธ ํ๋ |
| 61 | + setTitle(issueDetail.title ?? ''); |
| 62 | + setState((issueDetail.state ?? 'NONE') as StatusCode); |
| 63 | + setPriority((issueDetail.priority ?? 'NONE') as PriorityCode); |
| 64 | + |
| 65 | + // 2) ๊ธฐํ |
| 66 | + const s = issueDetail.deadline?.start ? new Date(issueDetail.deadline.start) : null; |
| 67 | + const e = issueDetail.deadline?.end ? new Date(issueDetail.deadline.end) : null; |
| 68 | + setSelectedDate([s, e]); |
| 69 | + |
| 70 | + // 3) ๋ด๋น์ ids |
| 71 | + if ((issueDetail.managers?.cnt ?? 0) > 0) { |
| 72 | + const managerNames = issueDetail.managers?.info?.map((m) => m.name) ?? []; |
| 73 | + const ids = managerNames |
| 74 | + .map((n) => nameToId[n]) |
| 75 | + .filter((v): v is number => typeof v === 'number'); |
| 76 | + setManagersId(ids); |
| 77 | + } else { |
| 78 | + setManagersId([]); |
| 79 | + } |
| 80 | + |
| 81 | + // 4) ๋ชฉํ goalId (๋จ์ผ) |
| 82 | + // - ์ฐ์ ์๋ต์ id๊ฐ ์์ผ๋ฉด ๊ทธ๊ฑธ ์ฌ์ฉ |
| 83 | + // - ์๊ณ title๋ง ์์ผ๋ฉด simpleGoals์์ title๋ก ์ฐพ์ id ๋งคํ |
| 84 | + // - ๋ ๋ค ์์ผ๋ฉด null |
| 85 | + if (issueDetail.goal?.id != null) { |
| 86 | + setGoalId(issueDetail.goal.id); |
| 87 | + } else if (issueDetail.goal?.title) { |
| 88 | + const map = new Map((simpleGoals ?? []).map((g) => [g.title, g.id] as const)); |
| 89 | + const mapped = map.get(issueDetail.goal.title); |
| 90 | + setGoalId(typeof mapped === 'number' ? mapped : null); |
| 91 | + } else { |
| 92 | + setGoalId(null); |
| 93 | + } |
| 94 | + |
| 95 | + // 5) ์๋ํฐ ์ญ์ง๋ ฌํ |
| 96 | + editorRef.current?.loadJson?.(issueDetail.content ?? ''); |
| 97 | + |
| 98 | + hydratedRef.current = true; |
| 99 | + }, [ |
| 100 | + issueDetail, |
| 101 | + issueId, |
| 102 | + editorRef, |
| 103 | + workspaceMembers, |
| 104 | + simpleGoals, |
| 105 | + nameToId, |
| 106 | + setTitle, |
| 107 | + setState, |
| 108 | + setPriority, |
| 109 | + setSelectedDate, |
| 110 | + setManagersId, |
| 111 | + setGoalId, |
| 112 | + ]); |
| 113 | +}; |
0 commit comments