122 lines
5.8 KiB
TypeScript
122 lines
5.8 KiB
TypeScript
import type { CSSProperties } from 'react'
|
||
|
||
const SHARED = new Set(['bear', 'bike', 'bronze', 'camping', 'carwash', 'chimaek', 'cloud', 'coldwind', 'delivery', 'dog', 'garden', 'glasses', 'gold', 'hiking', 'iced_hot', 'jjamppong', 'laundry', 'letter', 'makeup', 'makgeolli', 'maple', 'moon', 'muggy', 'naengmyeon', 'photo', 'picnic', 'rabbit', 'rain', 'running', 'silver', 'snow', 'stars', 'sun_cloud', 'sun', 'sunset', 'taxi', 'umbrella', 'vent', 'walk', 'yajang', 'baseball', 'study', 'netflix', 'chestnut', 'peanut', 'tangerine', 'sweet_potato', 'fallen_leaf', 'kimchi', 'apple'])
|
||
const OWN = new Set(['fireworks', 'bulb', 'calendar', 'thermo', 'sheaf'])
|
||
|
||
/** 아이콘 이름 → 파일 경로 (공용 public/icons, B 전용 public/b/icons) */
|
||
export function iconSrc(name: string) {
|
||
if (OWN.has(name)) return `/b/icons/${name}.png`
|
||
return `/icons/${SHARED.has(name) ? name : 'sun_cloud'}.png`
|
||
}
|
||
|
||
export const MEDALS = ['gold', 'silver', 'bronze'] as const
|
||
|
||
/** 고를 수 있는 프로필 캐릭터 (백엔드 CHARACTERS 와 같아야 한다) */
|
||
export const CHARACTERS = [
|
||
['rabbit', '토끼'],
|
||
['bear', '곰'],
|
||
['dog', '강아지'],
|
||
['sun', '해님'],
|
||
['moon', '달님'],
|
||
['cloud', '구름'],
|
||
] as const
|
||
|
||
/** 프로필 배경색 (백엔드 COLORS 와 같아야 한다) */
|
||
export const AVATAR_COLORS: Record<string, string> = { orange: '#ff8e4e', blue: '#4f97e3', green: '#1f9d62', purple: '#7a5cd6', pink: '#d6457a', brown: '#a8643a' }
|
||
|
||
/** 오늘 기분 (백엔드 MOODS 와 같아야 한다) */
|
||
export const MOODS = [
|
||
['clear', '☀️', '맑음'],
|
||
['partly', '⛅', '구름조금'],
|
||
['cloudy', '☁️', '흐림'],
|
||
['rain', '🌧️', '비'],
|
||
['thunder', '⚡', '번개'],
|
||
['snow', '❄️', '눈'],
|
||
['fog', '🌫️', '안개'],
|
||
['rainbow', '🌈', '무지개'],
|
||
] as const
|
||
|
||
export const moodEmoji = (mood?: string | null) => MOODS.find(([k]) => k === mood)?.[1]
|
||
|
||
export const fall = (d: string, f = '4.8s', amp?: string) => ({ '--d': d, '--f': f, ...(amp ? { '--amp': amp } : {}) }) as CSSProperties
|
||
|
||
export const WEEKDAYS = ['일', '월', '화', '수', '목', '금', '토'] as const
|
||
|
||
const KST = new Intl.DateTimeFormat('en-US', { timeZone: 'Asia/Seoul', year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', hourCycle: 'h23', weekday: 'short' })
|
||
const WD_EN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
||
|
||
/** 한국 시각으로 쪼개기 (브라우저 시간대와 상관없이) */
|
||
export function kst(input: string | Date) {
|
||
const parts = Object.fromEntries(KST.formatToParts(typeof input === 'string' ? new Date(input) : input).map((p) => [p.type, p.value]))
|
||
return { y: +parts.year, mo: +parts.month, d: +parts.day, h: +parts.hour, mi: +parts.minute, wd: WD_EN.indexOf(parts.weekday) }
|
||
}
|
||
|
||
const h12 = (h: number) => h % 12 || 12
|
||
|
||
/** 18:25 → "6:25" */
|
||
export function clockShort(iso: string) {
|
||
const t = kst(iso)
|
||
return `${h12(t.h)}:${String(t.mi).padStart(2, '0')}`
|
||
}
|
||
|
||
/** 18:30 → "6시 반", 18:25 → "6시 25분" */
|
||
export function timeWords(iso: string) {
|
||
const t = kst(iso)
|
||
return `${h12(t.h)}시${t.mi === 0 ? '' : t.mi === 30 ? ' 반' : ` ${t.mi}분`}`
|
||
}
|
||
|
||
/** 발표 시각 → "방금" · "12분 전" · "3시간 전" */
|
||
export function ago(iso: string | null | undefined, now = Date.now()) {
|
||
if (!iso) return '오늘'
|
||
const min = Math.max(0, Math.round((now - new Date(iso).getTime()) / 60000))
|
||
if (min < 5) return '방금'
|
||
if (min < 60) return `${min}분 전`
|
||
return `${Math.floor(min / 60)}시간 전`
|
||
}
|
||
|
||
/** 날짜 칸 이름: 오늘 · 내일 · "토 10.4" */
|
||
export function dayLabel(date: string, index: number) {
|
||
if (index === 0) return '오늘'
|
||
if (index === 1) return '내일'
|
||
const [, m, d] = date.split('-').map(Number)
|
||
const wd = new Date(`${date}T12:00:00+09:00`).getUTCDay()
|
||
return `${WEEKDAYS[wd]} ${m}.${d}`
|
||
}
|
||
|
||
export type TimeKind = 'morning' | 'day' | 'sunset' | 'night'
|
||
|
||
// 서울 월별 대략 일몰 시각(시). 서버 값(/v1/feed hero.sky_mode)이 없을 때만 쓴다
|
||
const SUNSET_HOUR = [17.7, 18.2, 18.6, 19.1, 19.5, 19.9, 19.9, 19.5, 18.9, 18.2, 17.6, 17.3]
|
||
|
||
/** 피드와 같은 시간대: 아침 05–11 · 낮 11–일몰 2시간 전 · 저녁(sunset) 일몰 2시간 전–21 · 밤 21–05 */
|
||
export function kindOfHour(h: number, month = new Date().getMonth() + 1, minute = 0): TimeKind {
|
||
const at = h + minute / 60
|
||
if (at >= 5 && at < 11) return 'morning'
|
||
if (at >= 11 && at < 21) return at < SUNSET_HOUR[month - 1] - 2 ? 'day' : 'sunset'
|
||
return 'night'
|
||
}
|
||
|
||
/** "서울 마포구" → "서울" */
|
||
export const sidoOf = (regionName: string) => regionName.split(' ')[0]
|
||
|
||
/** 2026-10-02 ~ 2026-10-11 → "10.2 ~ 10.11", 하루짜리는 "10.3" */
|
||
export function dateRange(start: string, end: string) {
|
||
return start === end ? shortDay(start) : `${shortDay(start)} ~ ${shortDay(end)}`
|
||
}
|
||
|
||
const CARD_BG = ['night', 'butter', 'lilac', 'mint', 'peach', 'sky'] as const
|
||
const ICON_BG: Record<string, (typeof CARD_BG)[number] | 'dusk'> = { picnic: 'mint', walk: 'mint', sunset: 'dusk', chimaek: 'peach', photo: 'butter', fireworks: 'night', sheaf: 'butter', rain: 'sky', calendar: 'sky', bulb: 'lilac', maple: 'peach', snow: 'sky', thermo: 'sky', umbrella: 'lilac' }
|
||
|
||
/** API에 배경색이 없어 아이콘·순서로 카드 배경을 고른다 */
|
||
export function cardBg(icon: string, index: number) {
|
||
return ICON_BG[icon] && icon !== 'calendar' ? ICON_BG[icon] : CARD_BG[index % CARD_BG.length]
|
||
}
|
||
|
||
/** "2026-09-23" → "9.23" */
|
||
export const shortDay = (day: string) => day.split('-').slice(1).map(Number).join('.')
|
||
|
||
export const ARTICLE_CAT: Record<number, [string, string]> = { 1: ['계절 소식', 'text-[#1f9d62]'], 2: ['날씨 상식', 'text-[#7a5cd6]'], 3: ['각 원리', 'text-(--orange)'] }
|
||
|
||
/** 읽을거리 한 줄 정보: "계절 소식 · 3분" */
|
||
export const articleMeta = (a: { category: number; read_minutes: number }) => `${ARTICLE_CAT[a.category]?.[0] ?? '읽을거리'} · ${a.read_minutes}분`
|