-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchTitle.tsx
More file actions
160 lines (145 loc) · 5.03 KB
/
SearchTitle.tsx
File metadata and controls
160 lines (145 loc) · 5.03 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
import { AnimatePresence, Variants, useCycle } from 'framer-motion';
import { useEffect, useRef, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { MARKET_CODES } from '@ts/Constants';
import useAuthInfo from '@hooks/useAuthInfo';
import { useQueryComponent } from '@hooks/useQueryComponent';
import { webPath } from '@router/index';
import Button from '@components/Common/Button';
import { useBuyExperimentMutation } from '@controllers/experiment/query';
import { useScoreQuery } from '@controllers/stocks/query';
import { StockDetailInfo } from '@controllers/stocks/types';
import KoreaPNG from '@assets/flags/korea.png';
import OverseaPNG from '@assets/flags/oversea.png';
import {
SearchTitleContainer,
SearchTitleDetailContainer,
SearchTitleDetailSymbol,
SearchTitleHeaderContainer,
SearchTitleHeaderText,
SearchTitleHeaderTextAnimated,
SearchTitlePrice,
SearchTitlePriceWrapper,
SearchTitleScoreBadge,
} from './SearchTitle.Style';
const BASE_DELAY = 1500;
const SearchTitleName = ({ stockInfo: { symbolName, country, price, stockId } }: { stockInfo: StockDetailInfo }) => {
const { state } = useLocation();
const titleTextRef = useRef<HTMLDivElement>(null);
const [animated, setAnimated] = useState<boolean>(false);
const [animationDelay, setAnimationDelay] = useState<any>({
initial: BASE_DELAY,
animate: BASE_DELAY,
instant: BASE_DELAY,
});
const [animation, cycleAnimation] = useCycle(...Object.keys(animationDelay));
const [stockScore, suspend] = useQueryComponent({ query: useScoreQuery(stockId, country) });
const concurrency = country === 'KOREA' ? '₩' : '$';
useEffect(() => {
if (titleTextRef.current) {
const { offsetWidth, scrollWidth } = titleTextRef.current;
setAnimated(scrollWidth > offsetWidth);
setAnimationDelay({
...animationDelay,
animate: BASE_DELAY * (scrollWidth / offsetWidth - 1) * 2,
});
}
}, [state]);
useEffect(() => {
const interval = setInterval(() => {
if (animated) cycleAnimation();
}, animationDelay[animation]);
return () => clearInterval(interval);
}, [animation, animated]);
const variants: Variants = {
initial: {
transform: 'translateX(0%)',
},
animate: {
transform:
'translateX(' + ((titleTextRef.current?.offsetWidth ?? 0) - (titleTextRef.current?.scrollWidth ?? 0)) + 'px)',
transition: {
duration: animationDelay['animate'] / 1000,
ease: 'linear',
}, // 애니메이션
},
instant: {
transform: 'translateX(0%)',
transition: {
delay: animationDelay['instant'] / 1000,
duration: 0,
ease: 'linear',
}, // 즉시 이동
},
};
return (
<SearchTitleHeaderContainer>
<SearchTitleHeaderText ref={titleTextRef}>
{symbolName}
<AnimatePresence>
<SearchTitleHeaderTextAnimated variants={variants} animate={animation}>
{symbolName}
</SearchTitleHeaderTextAnimated>
</AnimatePresence>
</SearchTitleHeaderText>
<SearchTitlePriceWrapper>
<SearchTitlePrice>
{concurrency}
{price.toLocaleString()}
</SearchTitlePrice>
{!suspend && stockScore && (
<SearchTitleScoreBadge>
<p>인간지표 {stockScore.score}점</p>
</SearchTitleScoreBadge>
)}
</SearchTitlePriceWrapper>
</SearchTitleHeaderContainer>
);
};
const SearchTitleDetail = ({
stockInfo: { exchangeNum, symbol, priceDiff, priceDiffPerCent, country },
}: {
stockInfo: StockDetailInfo;
}) => {
const marketCode = MARKET_CODES[exchangeNum];
const diffSign = priceDiff > 0 ? '+' : priceDiff < 0 ? '-' : '';
const diffPercentText = Math.abs(priceDiffPerCent).toFixed(2);
const diffValueText = diffSign + Math.abs(priceDiff).toLocaleString();
const flag = country === 'KOREA' ? KoreaPNG : OverseaPNG;
return (
<SearchTitleDetailContainer delta={priceDiff}>
<span className="market-code">{marketCode}</span>
<SearchTitleDetailSymbol>
<p>{symbol}</p>
<img src={flag} alt="arrow" />
</SearchTitleDetailSymbol>
<span className="price-diff">
{diffValueText}
{`(${diffPercentText}%)`}
</span>
</SearchTitleDetailContainer>
);
};
const SearchTitle = ({ stockInfo }: { stockInfo: StockDetailInfo }) => {
const navigate = useNavigate();
const { isLogin } = useAuthInfo();
const { mutate: buyExperiment } = useBuyExperimentMutation();
const handleClickBuy = () => {
if (!isLogin) {
navigate(webPath.labStep(), { state: { step: 0 } });
return;
}
buyExperiment({ stockId: stockInfo.stockId, country: stockInfo.country });
navigate(webPath.labStep(), { state: { step: 4 } });
};
return (
stockInfo && (
<SearchTitleContainer>
<SearchTitleName stockInfo={stockInfo} />
<SearchTitleDetail stockInfo={stockInfo} />
<Button onClick={handleClickBuy}>모의 매수하기</Button>
</SearchTitleContainer>
)
);
};
export default SearchTitle;