221 lines
9.8 KiB
TypeScript
221 lines
9.8 KiB
TypeScript
import { getIndexOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
|
||
import type { Factor, ResIndexDetail } from '@nalssigak/shared/api/client/types.gen';
|
||
import { loadOf } from '@nalssigak/shared/lib';
|
||
import { useQuery } from '@tanstack/react-query';
|
||
import { LinearGradient } from 'expo-linear-gradient';
|
||
import { useEffect } from 'react';
|
||
import { View } from 'react-native';
|
||
import { Text } from '@/components/text';
|
||
import Svg, { Circle, Line, Polygon, Text as SvgText, TSpan } from 'react-native-svg';
|
||
|
||
import { Ic, Skel, StateBox } from '@/components/ui';
|
||
import { useRegion } from '@/lib/region';
|
||
import { useSky } from '@/lib/sky';
|
||
|
||
import { Body, useSoft } from './parts';
|
||
|
||
const SLOT_KEYS = ['morning', 'day', 'evening', 'night'] as const;
|
||
const SLOT_NAMES = { morning: '아침', day: '낮', evening: '저녁', night: '밤' } as const;
|
||
const N4 = [0, 1, 2, 3];
|
||
const N6 = [0, 1, 2, 3, 4, 5];
|
||
|
||
const labelLines = (label: string) => {
|
||
const cut = label.indexOf(' ');
|
||
return cut < 0 ? [label] : [label.slice(0, cut), label.slice(cut + 1)];
|
||
};
|
||
|
||
function Radar({ name, factors, size = 250 }: { name: string; factors: Factor[]; size?: number }) {
|
||
const night = useSky().kind === 'night';
|
||
const c = size / 2;
|
||
const r = size / 2 - 40;
|
||
const n = factors.length;
|
||
const pt = (i: number, v: number) => {
|
||
const a = -Math.PI / 2 + (i * 2 * Math.PI) / n;
|
||
return [c + Math.cos(a) * r * v, c + Math.sin(a) * r * v] as const;
|
||
};
|
||
const ring = (v: number) => factors.map((_, i) => pt(i, v).join(',')).join(' ');
|
||
return (
|
||
<View className="mt-2 items-center" accessibilityRole="image" accessibilityLabel={`${name} 요소별 점수`}>
|
||
<Svg width={size} height={size} viewBox={`0 0 ${size} ${size}`}>
|
||
{[0.25, 0.5, 0.75, 1].map((v) => (
|
||
<Polygon key={v} points={ring(v)} fill={v === 1 ? (night ? '#222a5c' : '#f7f9fc') : 'none'} stroke={night ? 'rgba(255,255,255,0.18)' : '#d9e2ef'} strokeWidth={1.5} />
|
||
))}
|
||
{factors.map((f, i) => (
|
||
<Line key={f.axis} x1={c} y1={c} x2={pt(i, 1)[0]} y2={pt(i, 1)[1]} stroke={night ? 'rgba(255,255,255,0.12)' : '#e3e9f2'} strokeWidth={1.5} />
|
||
))}
|
||
<Polygon points={factors.map((f, i) => pt(i, f.value / 100).join(',')).join(' ')} fill="rgba(255,122,69,0.38)" stroke="#ff7a45" strokeWidth={3} strokeLinejoin="round" />
|
||
{factors.map((f, i) => (
|
||
<Circle key={f.axis} cx={pt(i, f.value / 100)[0]} cy={pt(i, f.value / 100)[1]} r={4.5} fill="#fff" stroke="#ff7a45" strokeWidth={2.5} />
|
||
))}
|
||
{factors.map((f, i) => {
|
||
const lines = labelLines(f.label);
|
||
const [x, y] = pt(i, 1.24);
|
||
return (
|
||
<SvgText key={f.axis} x={x} y={y - (lines.length - 1) * 7} textAnchor="middle" fontFamily="Jua_400Regular" fontSize={13} fill={night ? '#eef1ff' : '#28304a'}>
|
||
{lines.map((line, k) => (
|
||
<TSpan key={k} x={x} dy={k === 0 ? 4 : 14}>
|
||
{line}
|
||
</TSpan>
|
||
))}
|
||
</SvgText>
|
||
);
|
||
})}
|
||
</Svg>
|
||
</View>
|
||
);
|
||
}
|
||
|
||
function GakSkel() {
|
||
const s = useSoft();
|
||
return (
|
||
<View>
|
||
<View className="flex-row items-center gap-3">
|
||
<View className="size-[58px] rounded-full bg-skel" />
|
||
<View className="flex-1 gap-1.5">
|
||
<Skel className="h-6 w-28" />
|
||
<Skel className="w-4/5" />
|
||
</View>
|
||
<Skel className="h-10 w-14" />
|
||
</View>
|
||
<View className="mt-2 aspect-square w-full max-w-[250px] self-center rounded-full bg-skel" />
|
||
<View className="mt-2.5 flex-row gap-1.5">
|
||
{N4.map((i) => (
|
||
<View key={i} className="flex-1 items-center gap-1.5 rounded-2xl px-1 py-2.5" style={{ backgroundColor: s.soft }}>
|
||
<Skel className="w-8" />
|
||
<Skel className="h-6 w-9" />
|
||
</View>
|
||
))}
|
||
</View>
|
||
<View className="mt-3.5 gap-3">
|
||
{N6.map((i) => (
|
||
<View key={i} className="flex-row items-center gap-2.5">
|
||
<Skel className="w-[70px]" />
|
||
<View className="h-[9px] flex-1 rounded-full bg-skel" />
|
||
<Skel className="w-10" />
|
||
</View>
|
||
))}
|
||
</View>
|
||
</View>
|
||
);
|
||
}
|
||
|
||
export function GakSheet({ indexId, onTitle }: { indexId: number; onTitle: (t: string) => void }) {
|
||
const { region } = useRegion();
|
||
const s = useSoft();
|
||
const q = useQuery(getIndexOptions({ path: { index_id: indexId }, query: { region: region.code } }));
|
||
const d: ResIndexDetail | undefined = q.data;
|
||
const g = d?.index;
|
||
|
||
useEffect(() => {
|
||
onTitle(g ? `${g.rank}위 ${g.name}` : '계산 근거');
|
||
}, [g, onTitle]);
|
||
|
||
const load = loadOf(q);
|
||
if (!g) return <Body>{load === 'loading' ? <GakSkel /> : <StateBox load={load} onRetry={() => q.refetch()} empty="이 각의 계산 근거가 아직 없어요." hold={<GakSkel />} boxClassName="inset-0" />}</Body>;
|
||
const factors = d.factors ?? [];
|
||
const bands = d.bands ?? [];
|
||
const now = bands.find((b) => b.current);
|
||
return (
|
||
<Body>
|
||
<View className="flex-row items-center gap-3">
|
||
<Ic name={g.icon} size={58} />
|
||
<View className="flex-1">
|
||
<Text className="font-jua text-[22px] text-ink">{g.name}</Text>
|
||
{d.description && <Text className="mt-0.5 text-[13.5px] font-semibold text-ink2">{d.description}</Text>}
|
||
</View>
|
||
<Text className={`font-bagel text-[42px] ${g.active ? 'text-orange' : 'text-ink3'}`}>{g.score}</Text>
|
||
</View>
|
||
{!g.active && <Text className="mt-2 text-[13px] font-bold text-ink2">지금 시간대엔 해당 없어요. 점수는 오늘 가장 좋은 시간대 기준이에요.</Text>}
|
||
{now && (
|
||
<Text className="mt-3 overflow-hidden rounded-2xl px-3.5 py-2.5 text-[14.5px] font-bold text-ink" style={{ backgroundColor: s.warm }}>
|
||
<Text className="font-jua text-orange">
|
||
{now.low}~{now.high}점{' '}
|
||
</Text>
|
||
{now.text}
|
||
</Text>
|
||
)}
|
||
{factors.length > 2 && <Radar name={g.name} factors={factors} />}
|
||
{g.slots && g.slots.length > 0 && (
|
||
<View className="mt-2.5 flex-row gap-1.5" accessibilityLabel="오늘 시간대별 점수">
|
||
{SLOT_KEYS.map((k) => {
|
||
const sl = g.slots?.find((x) => x.slot === k);
|
||
const cur = g.active && g.slot === k;
|
||
return (
|
||
<View key={k} className="flex-1 items-center rounded-2xl px-1 py-2" style={{ backgroundColor: cur ? s.warm : s.soft }}>
|
||
<Text className="text-xs font-extrabold text-ink2">
|
||
{SLOT_NAMES[k]}
|
||
{cur && ' · 지금'}
|
||
</Text>
|
||
<Text className={`font-bagel text-xl ${sl ? 'text-orange' : 'text-ink3'}`}>{sl ? sl.score : '–'}</Text>
|
||
</View>
|
||
);
|
||
})}
|
||
</View>
|
||
)}
|
||
{g.trend && g.trend.length > 0 && (
|
||
<View className="mt-2.5 flex-row gap-1.5">
|
||
{g.trend.map((t) => (
|
||
<View key={t.day} className="flex-1 items-center rounded-2xl px-1 py-2.5" style={{ backgroundColor: s.soft }}>
|
||
<Text numberOfLines={1} className="text-xs font-extrabold text-ink2">
|
||
{t.label}
|
||
</Text>
|
||
<Text className="font-bagel text-2xl text-orange">{t.score}</Text>
|
||
<Text className={`text-[11px] leading-4 font-extrabold text-ink2 ${t.estimated ? '' : 'opacity-0'}`}>예상</Text>
|
||
</View>
|
||
))}
|
||
</View>
|
||
)}
|
||
{bands.length > 0 && (
|
||
<View className="mt-3.5 gap-1.5" accessibilityLabel="점수 구간별 설명">
|
||
{bands.map((b) => (
|
||
<View key={b.low} className={`flex-row items-start gap-2 ${b.current ? '' : 'opacity-45'}`}>
|
||
<View className={`w-[62px] items-center rounded-full py-px ${b.current ? 'bg-orange' : 'bg-ink3'}`}>
|
||
<Text className="font-jua text-xs text-white">
|
||
{b.low}~{b.high}
|
||
</Text>
|
||
</View>
|
||
<Text className="flex-1 text-[13px] font-semibold text-ink">{b.text}</Text>
|
||
</View>
|
||
))}
|
||
</View>
|
||
)}
|
||
<View className="mt-3.5 gap-2.5">
|
||
{factors.map((f) => (
|
||
<View key={f.axis}>
|
||
<View className="flex-row items-center gap-2.5">
|
||
<Text className="w-[84px] text-[13.5px] font-bold text-ink">{f.label}</Text>
|
||
<View className="h-[9px] flex-1 overflow-hidden rounded-full" style={{ backgroundColor: s.bar }}>
|
||
<LinearGradient colors={['#ffc86e', '#ff7a45']} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} style={{ height: '100%', width: `${f.value}%` }} />
|
||
</View>
|
||
<Text className="w-[52px] text-right text-xs text-ink2">비중 {f.weight}%</Text>
|
||
</View>
|
||
<Text className="mt-0.5 ml-[94px] text-[12.5px] font-semibold text-ink2">{f.note}</Text>
|
||
</View>
|
||
))}
|
||
</View>
|
||
<View className="mt-3.5 gap-1.5">
|
||
{d.zero_reason && (
|
||
<Text className="text-[13px] font-semibold text-ink2">
|
||
<Text className="font-bold text-heart">오늘 걸린 조건:</Text> {d.zero_reason}
|
||
</Text>
|
||
)}
|
||
{factors.length > 0 && (
|
||
<Text className="text-[13px] font-semibold text-ink2">
|
||
<Text className="font-bold text-ink">계산 방법:</Text> 요소 점수에 비중을 곱해 더하고, 가장 낮은 요소가 낮을수록 최대 25%까지 더 깎아요
|
||
</Text>
|
||
)}
|
||
{d.zero_rules && (
|
||
<Text className="text-[13px] font-semibold text-ink2">
|
||
<Text className="font-bold text-ink">점수 깎이는 조건:</Text> {d.zero_rules}
|
||
</Text>
|
||
)}
|
||
{d.limit && (
|
||
<Text className="text-[13px] font-semibold text-ink2">
|
||
<Text className="font-bold text-ink">반영 못 한 것:</Text> {d.limit}
|
||
</Text>
|
||
)}
|
||
</View>
|
||
</Body>
|
||
);
|
||
}
|