-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCd.tsx
More file actions
190 lines (165 loc) Β· 5.54 KB
/
Cd.tsx
File metadata and controls
190 lines (165 loc) Β· 5.54 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
import { useRef, useState, useEffect } from 'react'
import styled from 'styled-components'
import { EyeSlashWhite } from '@/assets/icons'
import overlayUrl from '@/assets/icons/icn_overlay.svg?url'
import type { CdCustomData } from '@/entities/playlist/types/playlist'
import { THEME_IMAGES_MAP } from '@/pages/mypage/lib/customizeTheme'
import { THEME_PROP_ID_OFFSET } from '@/pages/mypage/types/mypage'
import { flexRowCenter, flexColCenter } from '@/shared/styles/mixins'
interface CdProps {
variant:
| 'xxl'
| 'xl'
| 'lg'
| 'md'
| 'sm'
| 'xs'
| 'share'
| 'customize'
| 'carousel'
| 'responsive'
| 'home'
| 'mycd'
bgColor?: 'none' | 'default' | 'dark'
stickers?: CdCustomData[]
isPublic?: boolean
}
const Cd = ({ variant, bgColor = 'default', stickers, isPublic = true }: CdProps) => {
const baseRef = useRef<HTMLDivElement>(null)
const [dynamicBase, setDynamicBase] = useState(0)
const baseSize = variant === 'responsive' ? dynamicBase : sizeMap[variant].base
const ratio = baseSize / 275
// μ€ν°μ»€ μ΄λ―Έμ§ κ²½λ‘ κ°μ Έμ€κΈ°
const getImagePath = (info: CdCustomData) => {
const { imageUrl, theme, propId } = info
if (imageUrl !== 'DEFAULT') return imageUrl
const convertThemeName =
theme === 'NEONOBJECT'
? 'neonObject'
: theme === 'PEOPLE&ANIMALS'
? 'people&animals'
: theme?.toLowerCase()
const images = THEME_IMAGES_MAP[convertThemeName as keyof typeof THEME_IMAGES_MAP]
if (!images) {
console.warn('Unknown theme:', theme)
return ''
}
const offset = THEME_PROP_ID_OFFSET[convertThemeName as keyof typeof THEME_PROP_ID_OFFSET]
const localIndex = propId - offset
const fileName = localIndex.toString().padStart(2, '0') + '.png'
const key = Object.keys(images).find((k) => k.endsWith('/' + fileName))
if (!key) {
console.warn(`Image not found for theme=${convertThemeName}, propId=${propId}`)
return ''
}
const mod = images[key]
return typeof mod === 'string' ? mod : (mod as { default: string }).default
}
// λ°μνμΌ λ cd ν¬κΈ° λμ κ³μ°
useEffect(() => {
if (variant === 'responsive' && baseRef.current) {
const resizeObserver = new ResizeObserver((entries) => {
const { width } = entries[0].contentRect
setDynamicBase(width)
})
resizeObserver.observe(baseRef.current)
return () => resizeObserver.disconnect()
}
}, [variant])
const Content = (
<Base ref={variant === 'responsive' ? baseRef : undefined} $variant={variant}>
{stickers?.map((sticker) => {
const { cdItemId, xCoordinate, yCoordinate, width, height, scale, angle } = sticker
const src = getImagePath(sticker)
if (!src) return null
return (
<img
key={`${cdItemId}-${xCoordinate}-${yCoordinate}`}
src={src}
alt="cd-sticker"
// crossOrigin="anonymous"
style={{
position: 'absolute',
left: xCoordinate * ratio,
top: yCoordinate * ratio,
width: width * ratio,
height: height * ratio,
transform: `scale(${scale}) rotate(${angle}rad)`,
transformOrigin: 'top left',
pointerEvents: 'none',
}}
/>
)
})}
<Overlay />
{!isPublic && (
<PrivateCover>
<EyeSlashWhite width={16} height={16} />
<span>λΉκ³΅κ°λ CD</span>
</PrivateCover>
)}
</Base>
)
if (bgColor === 'none') return Content
return (
<Container $variant={variant} $bgColor={bgColor}>
{Content}
</Container>
)
}
export default Cd
const sizeMap = {
xxl: { container: 280, base: 280, borderRadius: 0 },
xl: { container: 140, base: 120, borderRadius: 16 },
lg: { container: 124, base: 108, borderRadius: 14 },
md: { container: 104, base: 88, borderRadius: 10 },
sm: { container: 88, base: 72, borderRadius: 10 },
xs: { container: 56, base: 48, borderRadius: 6 },
share: { container: 280, base: 220, borderRadius: 24 },
customize: { container: 220, base: 220, borderRadius: 0 },
carousel: { container: 180, base: 180, borderRadius: 0 },
responsive: { borderRadius: 10 },
home: { container: 180, base: 180, borderRadius: 0 },
mycd: { container: 260, base: 260, borderRadius: 0 },
} as const
interface StyleProps {
$variant: keyof typeof sizeMap
}
interface ContainerProps extends StyleProps {
$bgColor?: 'default' | 'dark'
}
const Container = styled.div<ContainerProps>`
width: ${({ $variant }) =>
$variant === 'responsive' ? '100%' : `${sizeMap[$variant].container}px`};
aspect-ratio: 1 / 1;
border-radius: ${({ $variant }) => sizeMap[$variant].borderRadius}px;
background-color: ${({ $bgColor, theme }) =>
$bgColor === 'dark' ? theme.COLOR['gray-800'] : theme.COLOR['gray-600']};
${flexRowCenter}
`
const Base = styled.div<StyleProps>`
position: relative;
width: ${({ $variant }) => ($variant === 'responsive' ? '82%' : `${sizeMap[$variant].base}px`)};
aspect-ratio: 1 / 1;
border-radius: 100%;
background: ${({ theme }) => theme.GRADIENT.hologram};
overflow: hidden;
`
const Overlay = styled.div`
position: absolute;
inset: 0;
background: url(${overlayUrl}) no-repeat center/cover;
mix-blend-mode: multiply;
pointer-events: none;
`
const PrivateCover = styled.div`
position: relative;
${flexColCenter}
gap: 3px;
width: 100%;
height: 100%;
background-color: rgba(42, 47, 57, 0.7);
& > span {
${({ theme }) => theme.FONT['caption2']}
}
`