-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasteSummaryCard.tsx
More file actions
200 lines (179 loc) · 5.94 KB
/
TasteSummaryCard.tsx
File metadata and controls
200 lines (179 loc) · 5.94 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"use client";
import { CircleIcon } from "#/icons/circleIcon";
import { XIcon } from "#/icons/xIcon";
import { CATEGORY, CATEGORY_LABEL } from "#/constants/gathering/opinion";
import type { Category } from "#/types/gathering";
import Image from "next/image";
// "ANY"를 제외한 구체적 카테고리 목록
const foodCategories: Category[] = [
CATEGORY.KOREAN,
CATEGORY.JAPANESE,
CATEGORY.CHINESE,
CATEGORY.WESTERN,
CATEGORY.ASIAN,
CATEGORY.ANY,
];
const nonAnyFoodCategories: Category[] = [
CATEGORY.KOREAN,
CATEGORY.JAPANESE,
CATEGORY.CHINESE,
CATEGORY.WESTERN,
CATEGORY.ASIAN,
];
/** 앞 단어 끝 음절의 받침 여부로 을/를 반환 */
function getEulReul(text: string): "을" | "를" {
if (!text) return "을";
const code = text.charCodeAt(text.length - 1);
// 비한글 문자로 끝나는 경우 보수적으로 "을" 반환 (현재 레이블은 모두 한글로 끝남)
if (code < 0xac00 || code > 0xd7a3) return "을";
return (code - 0xac00) % 28 !== 0 ? "을" : "를";
}
function formatFoodList(keys: Category[]): string {
return keys.map((k) => CATEGORY_LABEL[k]).join(", ");
}
interface TasteSummary {
dislikeHighlight: string;
dislikeSuffix: string;
preferenceHighlight: string;
preferenceSuffix: string;
conclusion: string;
}
function computeTasteSummary(
preferences: Record<string, number>,
dislikes: Record<string, number>,
): TasteSummary {
const votedPreferences = foodCategories.filter(
(c) => (preferences[c] ?? 0) >= 1,
);
const votedPreferencesWithoutAny = votedPreferences.filter(
(c) => c !== CATEGORY.ANY,
);
const votedDislikes = foodCategories.filter((c) => (dislikes[c] ?? 0) >= 1);
const votedDislikesWithoutAny = votedDislikes.filter(
(c) => c !== CATEGORY.ANY,
);
const isAllAnyPreferences = votedPreferencesWithoutAny.length === 0;
const isAllAnyDislikes = votedDislikesWithoutAny.length === 0;
// 라인 1: 선호 행
let preferenceHighlight: string;
let preferenceSuffix: string;
if (isAllAnyPreferences) {
preferenceHighlight = "아무 음식";
preferenceSuffix = "이나 상관 없고";
} else if (
!isAllAnyPreferences &&
votedPreferencesWithoutAny.length === 1
) {
const foodLabel = CATEGORY_LABEL[votedPreferencesWithoutAny[0]];
preferenceHighlight = foodLabel;
preferenceSuffix = `${getEulReul(foodLabel)} 먹고 싶어하고`;
} else {
preferenceHighlight = formatFoodList(votedPreferencesWithoutAny);
preferenceSuffix = "중";
}
// 라인 2: 불호 행
let dislikeHighlight: string;
let dislikeSuffix: string;
if (isAllAnyDislikes) {
dislikeHighlight = "피하고 싶은 음식";
dislikeSuffix = "없어서";
} else if (votedDislikesWithoutAny.length === nonAnyFoodCategories.length) {
dislikeHighlight = "피하고 싶은 음식";
dislikeSuffix = "이 모두 달라서";
} else {
const lastLabel =
CATEGORY_LABEL[
votedDislikesWithoutAny[votedDislikesWithoutAny.length - 1]
];
dislikeHighlight = formatFoodList(votedDislikesWithoutAny);
dislikeSuffix = `${getEulReul(lastLabel)} 빼고`;
}
// 라인 3: 결론
const safePreferences = votedPreferencesWithoutAny.filter(
(preference) =>
!votedDislikesWithoutAny.some((dislike) => dislike === preference),
);
const hasIntersection = votedPreferencesWithoutAny.some((preference) =>
votedDislikesWithoutAny.some((dislike) => dislike === preference),
);
let conclusion: string;
if (
votedDislikesWithoutAny.length === 0 &&
votedPreferencesWithoutAny.length === 1
) {
conclusion = `평점 3.5 이상의 ${CATEGORY_LABEL[votedPreferencesWithoutAny[0]]} 맛집 추천`;
} else if (hasIntersection && safePreferences.length > 0) {
conclusion = `불호가 없는 ${formatFoodList(safePreferences)} 추천`;
} else {
conclusion = "평점 3.5 이상의 맛집 추천";
}
return {
dislikeHighlight,
dislikeSuffix,
preferenceHighlight,
preferenceSuffix,
conclusion,
};
}
export interface TasteSummaryCardProps {
preferences: Record<string, number>;
dislikes: Record<string, number>;
}
export const TasteSummaryCard = ({
preferences,
dislikes,
}: TasteSummaryCardProps) => {
const {
dislikeHighlight,
dislikeSuffix,
preferenceHighlight,
preferenceSuffix,
conclusion,
} = computeTasteSummary(preferences, dislikes);
return (
<div className="ygi:flex ygi:flex-col ygi:gap-5 ygi:rounded-md ygi:bg-surface-white ygi:p-5">
{/* 캐릭터 일러스트 */}
<div className="ygi:relative ygi:h-30.25 ygi:w-full ygi:overflow-hidden ygi:rounded-md ygi:bg-surface-gray">
<div className="ygi:absolute ygi:bottom-0 ygi:left-1/2 ygi:h-26.5 ygi:w-52.75 ygi:-translate-x-1/2">
<Image
src="/images/result/taste-characters.png"
alt="캐릭터 일러스트"
priority
fill
/>
</div>
</div>
{/* 텍스트 요약 */}
<div className="ygi:flex ygi:w-full ygi:flex-col ygi:gap-2">
{/* 선호 행 */}
<div className="ygi:flex ygi:w-full ygi:items-center">
<div className="ygi:flex ygi:h-5 ygi:w-5 ygi:shrink-0 ygi:items-center ygi:justify-center ygi:rounded ygi:bg-palette-secondary-500">
<CircleIcon size={11} className="ygi:text-white" />
</div>
<span className="ygi:ml-1.5 ygi:shrink-0 ygi:body-16-sb ygi:text-palette-secondary-700">
{preferenceHighlight}
</span>
<span className="ygi:ml-1 ygi:body-16-sb ygi:text-text-primary">
{preferenceSuffix}
</span>
</div>
{/* 불호 행 */}
<div className="ygi:flex ygi:w-full ygi:items-center">
<div className="ygi:flex ygi:h-5 ygi:w-5 ygi:shrink-0 ygi:items-center ygi:justify-center ygi:rounded ygi:bg-palette-primary-500">
<XIcon size={11} className="ygi:text-white" />
</div>
<span className="ygi:ml-1.5 ygi:shrink-0 ygi:body-16-sb ygi:text-text-interactive">
{dislikeHighlight}
</span>
<span className="ygi:ml-1 ygi:body-16-sb ygi:text-text-primary">
{dislikeSuffix}
</span>
</div>
{/* 결론 */}
<span className="ygi:body-16-sb ygi:text-text-primary">
{conclusion}
</span>
</div>
</div>
);
};