-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseProceedRecommendResult.ts
More file actions
131 lines (106 loc) · 3.26 KB
/
useProceedRecommendResult.ts
File metadata and controls
131 lines (106 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use client";
import { useState, useRef, useCallback } from "react";
import { useParams, useRouter } from "next/navigation";
import { RecommendationResultStatus } from "#/constants/gathering/opinion";
import {
usePostProceedRecommendResult,
useGetRecommendResult,
} from "#/hooks/apis";
import { isApiError } from "#/utils/api";
import { toast } from "#/utils/toast";
const NAVIGATION_DELAY = 2_500;
const PROCEED_STATUS = {
IDLE: "idle",
PROCESSING: "processing",
} as const;
type ProceedState =
| { status: typeof PROCEED_STATUS.IDLE }
| { status: typeof PROCEED_STATUS.PROCESSING; startTime: number };
export const useProceedRecommendResult = () => {
const router = useRouter();
const { accessKey } = useParams<{ accessKey: string }>();
const [proceedState, setProceedState] = useState<ProceedState>({
status: PROCEED_STATUS.IDLE,
});
const { refetch: fetchRecommendResult } = useGetRecommendResult(accessKey);
const { mutateAsync: proceedMutation } =
usePostProceedRecommendResult(accessKey);
const isProcessingRef = useRef(false);
const isPending = proceedState.status === PROCEED_STATUS.PROCESSING;
const onResultComplete = useCallback(async () => {
if (!isProcessingRef.current) {
return;
}
const remaining = Math.max(0, NAVIGATION_DELAY);
setTimeout(async () => {
const { data: latestResult } = await fetchRecommendResult();
if (latestResult?.status === RecommendationResultStatus.COMPLETED) {
router.push(`/gathering/${accessKey}/opinion/result`);
return;
}
isProcessingRef.current = false;
setProceedState({ status: PROCEED_STATUS.IDLE });
toast.warning(
"추천 결과가 아직 준비되지 않았습니다. 잠시 후 다시 시도해주세요.",
);
}, remaining);
}, [accessKey, router, fetchRecommendResult]);
const proceed = async () => {
if (isProcessingRef.current) {
return;
}
try {
const { data: latestResult } = await fetchRecommendResult();
if (!latestResult?.status) {
isProcessingRef.current = true;
const startTime = Date.now();
setProceedState({
status: PROCEED_STATUS.PROCESSING,
startTime,
});
await proceedMutation(accessKey);
return;
}
switch (latestResult.status) {
case RecommendationResultStatus.COMPLETED: {
isProcessingRef.current = true;
const startTime = Date.now();
setProceedState({
status: PROCEED_STATUS.PROCESSING,
startTime,
});
setTimeout(() => {
router.push(`/gathering/${accessKey}/opinion/result`);
}, NAVIGATION_DELAY);
return;
}
case RecommendationResultStatus.FAILED:
toast.warning(
"추천 결과 생성에 실패했습니다. 다시 시도해주세요.",
);
return;
case RecommendationResultStatus.PENDING: {
isProcessingRef.current = true;
const startTime = Date.now();
setProceedState({
status: PROCEED_STATUS.PROCESSING,
startTime,
});
return;
}
}
} catch (error) {
setProceedState({ status: PROCEED_STATUS.IDLE });
isProcessingRef.current = false;
const errorMessage = isApiError(error)
? error.message
: "추천 결과 생성 요청에 실패했습니다. 다시 시도해주세요.";
toast.warning(errorMessage);
}
};
return {
proceed,
isPending,
onResultComplete,
};
};