diff --git a/frontend/public/app/icon-192.png b/frontend/public/app/icon-192.png new file mode 100644 index 0000000..c19f918 Binary files /dev/null and b/frontend/public/app/icon-192.png differ diff --git a/frontend/public/app/icon-512.png b/frontend/public/app/icon-512.png new file mode 100644 index 0000000..c9d03b4 Binary files /dev/null and b/frontend/public/app/icon-512.png differ diff --git a/frontend/public/app/icon-maskable-512.png b/frontend/public/app/icon-maskable-512.png new file mode 100644 index 0000000..9e38154 Binary files /dev/null and b/frontend/public/app/icon-maskable-512.png differ diff --git a/frontend/public/manifest.webmanifest b/frontend/public/manifest.webmanifest index fea580a..9d23a7c 100644 --- a/frontend/public/manifest.webmanifest +++ b/frontend/public/manifest.webmanifest @@ -3,12 +3,33 @@ "short_name": "날씨각", "description": "오늘 날씨에 맞는 일을 점수로. 노을이 예쁜 시간과 이번 주말 가까운 축제까지.", "lang": "ko-KR", - "start_url": "/", + "start_url": "/?source=homescreen", "display": "standalone", "background_color": "#d6ebff", "theme_color": "#d6ebff", "icons": [ - { "src": "/favicon.svg", "sizes": "any", "type": "image/svg+xml" }, - { "src": "/apple-touch-icon.png", "sizes": "180x180", "type": "image/png" } - ] + { + "src": "/app/icon-192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/app/icon-512.png", + "sizes": "512x512", + "type": "image/png" + }, + { + "src": "/app/icon-maskable-512.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "maskable" + }, + { + "src": "/favicon.svg", + "sizes": "any", + "type": "image/svg+xml" + } + ], + "id": "/", + "scope": "/" } diff --git a/frontend/public/sw.js b/frontend/public/sw.js new file mode 100644 index 0000000..7362fba --- /dev/null +++ b/frontend/public/sw.js @@ -0,0 +1,4 @@ +// 홈 화면 설치 조건을 맞추려고 둔 서비스 워커. 화면을 캐시하지 않고 네트워크로 그대로 보낸다 (옛 화면이 남지 않게) +self.addEventListener('install', () => self.skipWaiting()) +self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim())) +self.addEventListener('fetch', () => {}) diff --git a/frontend/src/b/Sheets.tsx b/frontend/src/b/Sheets.tsx index 17f5114..f162edd 100644 --- a/frontend/src/b/Sheets.tsx +++ b/frontend/src/b/Sheets.tsx @@ -20,12 +20,14 @@ import { unlikeCommentMutation, updateMeMutation, } from '../api/client/@tanstack/react-query.gen' -import type { CommentData, Factor, ResAuth, ResCommentList, ResIndexDetail } from '../api/client/types.gen' +import { AuthProvider, type CommentData, type Factor, type ResAuth, type ResCommentList, type ResIndexDetail } from '../api/client/types.gen' import { track } from '../lib/analytics' import { cx } from '../lib/utils' import { useAuth } from './authContext' import { ago, ARTICLE_CAT, articleMeta, AVATAR_COLORS, cardBg, CHARACTERS, dateRange, loadOf, MOODS, shortDay } from './lib' import { GoogleButton } from './googleLogin' +import { useInstall } from './install' +import { InstallSheet } from './installBanner' import { MixSheet } from './myGak' import { ownPage, useRegion } from './regionContext' import { useSky } from './skyContext' @@ -74,7 +76,7 @@ export function SheetProvider({ children }: { children: ReactNode }) { ) } -const TITLES = { article: '읽을거리', seasonWeek: '이번 주 특집', share: '보내기', comments: '댓글', login: '로그인', me: '내 정보', region: '지역 바꾸기' } as const +const TITLES = { article: '읽을거리', seasonWeek: '이번 주 특집', share: '보내기', comments: '댓글', login: '로그인', me: '내 정보', region: '지역 바꾸기', install: '홈 화면에 추가' } as const /** 아래에서 올라오는 창: 네이티브 라 ESC·포커스 가두기·포커스 되돌리기가 기본으로 된다 */ function SheetHost({ state, onClose }: { state: SheetState; onClose: () => void }) { @@ -135,6 +137,7 @@ function SheetHost({ state, onClose }: { state: SheetState; onClose: () => void {state.type === 'me' && } {state.type === 'region' && } {state.type === 'mix' && } + {state.type === 'install' && } )} {!state && } @@ -866,6 +869,7 @@ function LoginSheet({ onDone, onLoggedIn }: { onDone: () => void; onLoggedIn: () /* ---------- 내 정보: 캐릭터·닉네임·로그아웃 ---------- */ function MeSheet({ onDone }: { onDone: () => void }) { + const install = useInstall() const { me, logout } = useAuth() const queryClient = useQueryClient() const update = useMutation(updateMeMutation()) @@ -899,8 +903,11 @@ function MeSheet({ onDone }: { onDone: () => void }) {
-

{me.login_id}(으)로 로그인했어요

+

{me.provider === AuthProvider.GOOGLE ? '구글 계정으로 로그인했어요' : `${me.login_id}(으)로 로그인했어요`}

+

내 캐릭터

diff --git a/frontend/src/b/TodayPage.tsx b/frontend/src/b/TodayPage.tsx index 16c84be..49fb94d 100644 --- a/frontend/src/b/TodayPage.tsx +++ b/frontend/src/b/TodayPage.tsx @@ -5,6 +5,7 @@ import { getFeedOptions, getWeatherTodayOptions } from '../api/client/@tanstack/ import { RainType, Sky, type BandArticle, type BandEvent, type Hero as HeroData, type ResWeatherToday, type TalkData, type TopIndex, type WeatherSlot } from '../api/client/types.gen' import { cx } from '../lib/utils' import { cardBg, fall, kindOfHour, kst, type Load, loadOf, MEDALS, type TimeKind, WEEKDAYS } from './lib' +import { InstallBanner } from './installBanner' import { MyGakRow } from './myGak' import { fromShare } from './myGakState' import { PostCard, PostCardSkel, PostFrame } from './Post' @@ -400,6 +401,7 @@ export function TodayPage() { ) : ( feed.refetch()} empty="오늘 계산된 각이 없어요." hold={} className="inset-x-3 top-3 bottom-1" /> )} +
{load === 'loading' && } {/* 글이 없거나 못 받아도 스켈레톤 두 장 자리는 그대로 둔다 */} diff --git a/frontend/src/b/install.ts b/frontend/src/b/install.ts new file mode 100644 index 0000000..bd5d3f2 --- /dev/null +++ b/frontend/src/b/install.ts @@ -0,0 +1,90 @@ +import { useSyncExternalStore } from 'react' +import { track } from '../lib/analytics' +import { useSheet } from './sheetContext' + +type InstallPromptEvent = Event & { prompt: () => Promise; userChoice: Promise<{ outcome: 'accepted' | 'dismissed' }> } + +const DISMISS_KEY = 'nalssigak.b.installDismissed' +const DISMISS_DAYS = 14 + +// 안드로이드 Chrome 은 설치할 수 있을 때 이 이벤트를 한 번 준다. 앱이 뜨기 전에 올 수 있어 모듈을 불러올 때 바로 받아 둔다 +let deferred: InstallPromptEvent | null = null +const listeners = new Set<() => void>() +const notify = () => listeners.forEach((cb) => cb()) + +if (typeof window !== 'undefined') { + window.addEventListener('beforeinstallprompt', (e) => { + e.preventDefault() + deferred = e as InstallPromptEvent + notify() + }) + window.addEventListener('appinstalled', () => { + deferred = null + track('install_done', {}) + notify() + }) +} + +export type Platform = 'standalone' | 'inapp' | 'ios' | 'android' | 'other' + +/** 지금 이 기기에서 홈 화면 추가를 어떻게 안내할지 */ +export function platform(): Platform { + const ua = navigator.userAgent + if (window.matchMedia('(display-mode: standalone)').matches || (navigator as Navigator & { standalone?: boolean }).standalone) return 'standalone' + if (/KAKAOTALK|Instagram|FBAN|FBAV|NAVER\(inapp|; wv\)|Line\//i.test(ua)) return 'inapp' + if (/iPhone|iPad|iPod/.test(ua) || (/Macintosh/.test(ua) && navigator.maxTouchPoints > 1)) return 'ios' + if (/Android/.test(ua)) return 'android' + return 'other' +} + +/** 설치창을 바로 띄울 수 있는지 (안드로이드 Chrome 등) */ +export function useCanPrompt() { + return useSyncExternalStore( + (cb) => { + listeners.add(cb) + return () => listeners.delete(cb) + }, + () => deferred != null, + ) +} + +/** 설치창을 띄운다. 띄웠으면 true */ +export async function promptInstall(): Promise { + if (!deferred) return false + const e = deferred + deferred = null + notify() + await e.prompt() + const { outcome } = await e.userChoice + track('install_choice', { outcome }) + return true +} + +export function dismissed(): boolean { + try { + const at = Number(localStorage.getItem(DISMISS_KEY)) + return at > 0 && Date.now() - at < DISMISS_DAYS * 86_400_000 + } catch { + // 저장소를 못 쓰는 브라우저에선 이번 방문 동안만 보인다 + return false + } +} + +export function dismiss() { + try { + localStorage.setItem(DISMISS_KEY, String(Date.now())) + } catch (e) { + console.warn('[install] 닫은 시각을 저장하지 못함', e) + } +} + +/** 홈 화면에 추가: 설치창을 띄울 수 있으면 띄우고, 아니면 기기별 안내 창을 연다 */ +export function useInstall() { + const { open } = useSheet() + const canPrompt = useCanPrompt() + return async (from: 'banner' | 'menu') => { + const shown = canPrompt && (await promptInstall()) + track('install_click', { from, method: shown ? 'prompt' : 'guide' }) + if (!shown) open({ type: 'install' }) + } +} diff --git a/frontend/src/b/installBanner.tsx b/frontend/src/b/installBanner.tsx new file mode 100644 index 0000000..eeea93e --- /dev/null +++ b/frontend/src/b/installBanner.tsx @@ -0,0 +1,103 @@ +import { useEffect, useState } from 'react' +import { track } from '../lib/analytics' +import { dismiss, dismissed, platform, useInstall } from './install' + +const SITE = 'https://weather.o2o.kr' + +/** 오늘 탭의 작은 설치 카드. 오늘의 각까지 본 뒤 자리에 놓고, 닫으면 2주 숨기고, 홈 화면에서 연 경우엔 안 보인다 */ +export function InstallBanner() { + const install = useInstall() + const [hidden, setHidden] = useState(() => platform() === 'standalone' || dismissed()) + + useEffect(() => { + if (!hidden) track('install_banner_view', {}) + }, [hidden]) + + if (hidden) return null + return ( + + ) +} + +function Steps({ items }: { items: string[] }) { + return ( +
    + {items.map((t, i) => ( +
  1. + {i + 1} + {t} +
  2. + ))} +
+ ) +} + +/** 설치창을 띄울 수 없는 기기의 안내 */ +export function InstallSheet() { + const p = platform() + const [copied, setCopied] = useState(false) + const copy = () => + navigator.clipboard.writeText(SITE).then( + () => setCopied(true), + (e: unknown) => console.error('[install] copy', e), + ) + + return ( +
+ {p === 'standalone' &&

이미 홈 화면에서 열었어요. 다음에도 아이콘을 눌러 바로 여세요.

} + {p === 'ios' && ( + <> +

아이폰은 Safari에서 이렇게 추가해요.

+ + + )} + {p === 'android' && ( + <> +

지금 브라우저에선 설치창을 바로 띄울 수 없어요. 메뉴에서 추가해 주세요.

+ + + )} + {p === 'inapp' && ( + <> +

카카오톡 같은 앱 안에서는 홈 화면에 추가할 수 없어요. Safari나 Chrome에서 열어 주세요.

+ + + )} + {p === 'other' && ( + <> +

휴대폰에서 열면 홈 화면에 추가할 수 있어요.

+ + + )} + {(p === 'inapp' || p === 'other') && ( +
+ {SITE} + +
+ )} +
+ ) +} diff --git a/frontend/src/b/sheetContext.ts b/frontend/src/b/sheetContext.ts index 954d6e2..fcd7ce9 100644 --- a/frontend/src/b/sheetContext.ts +++ b/frontend/src/b/sheetContext.ts @@ -14,6 +14,7 @@ export type SheetState = | { type: 'me' } | { type: 'region' } | { type: 'mix'; shared?: MyGak } + | { type: 'install' } | null export type SheetApi = { open: (s: Exclude) => void; close: () => void } diff --git a/frontend/src/lib/analytics.ts b/frontend/src/lib/analytics.ts index 2c44413..c320529 100644 --- a/frontend/src/lib/analytics.ts +++ b/frontend/src/lib/analytics.ts @@ -6,6 +6,11 @@ type Events = { login: { method: 'id' | 'google' } sign_up: { method: 'id' } region_change: { region_name: string; method: 'list' | 'location' } + install_banner_view: Record + install_click: { from: 'banner' | 'menu'; method: 'prompt' | 'guide' } + install_choice: { outcome: 'accepted' | 'dismissed' } + install_done: Record + app_launch: { mode: 'standalone' } } declare global { diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index 2ccc94b..cd3ae90 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -4,6 +4,8 @@ import { createRoot } from 'react-dom/client' import './index.css' import './api/setup' import App from './App.tsx' +import './b/install' +import { track } from './lib/analytics' // iOS는 user-scalable=no를 무시해서 두 손가락 확대를 직접 막는다 for (const type of ['gesturestart', 'gesturechange']) document.addEventListener(type, (e) => e.preventDefault(), { passive: false }) @@ -13,6 +15,12 @@ const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: 5 * 60 * 1000, retry: 1, refetchOnWindowFocus: false, placeholderData: keepPreviousData } }, }) +// 홈 화면 설치 조건(서비스 워커)과, 홈 화면 아이콘으로 연 횟수 +if (import.meta.env.PROD && 'serviceWorker' in navigator) { + navigator.serviceWorker.register('/sw.js').catch((e: unknown) => console.error('[sw] register', e)) +} +if (window.matchMedia('(display-mode: standalone)').matches || (navigator as Navigator & { standalone?: boolean }).standalone) track('app_launch', { mode: 'standalone' }) + createRoot(document.getElementById('root')!).render(