여러 줄 주석이 설명보다 경위(예전·실측·지적)를 적고 있어 읽는 사람이 결론을 찾기 어려웠다. - ts·tsx·js·mjs·css·py 478개: 여러 줄 주석은 첫 문장 한 줄로, 과거형·날짜 문장은 삭제 - 주석 위치는 TypeScript 파서·파이썬 tokenize/ast 로 찾는다 — 문자열 안의 # · /* 는 건드리지 않는다 - eslint·ts·noqa·type: ignore 같은 지시 주석은 그대로 둔다 파이썬 275개 정리 전후 AST 동일, TS 298개 주석 뺀 토큰 동일(빈 JSX 주석 10곳만 차이). site·frontend·admin tsc, site vitest 105 passed Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
129 lines
4.6 KiB
TypeScript
129 lines
4.6 KiB
TypeScript
import {useCallback, useEffect, useState} from 'react';
|
|
import {Loader2, MessageCircle, Unlink} from 'lucide-react';
|
|
import {Button} from '@/components/ui/button';
|
|
import {agentApi, type KakaoLinkCode, type KakaoLinkState} from './api';
|
|
|
|
/** 카카오톡 채널 연결 — **'내 사이트' 화면에 한 자리**. */
|
|
export function KakaoChannelCard() {
|
|
const [state, setState] = useState<KakaoLinkState | null>(null);
|
|
const [issued, setIssued] = useState<KakaoLinkCode | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
setState(await agentApi<KakaoLinkState>('/kakao/link'));
|
|
} catch {
|
|
// 연결 상태를 못 읽는 것은 사이트 목록을 못 보여줄 이유가 아니다 — 조용히 접는다.
|
|
setState(null);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void load();
|
|
}, [load]);
|
|
|
|
// 상태를 못 읽었거나(로그인 직후 한순간) 채널이 준비되지 않았으면 접는다(위 주석).
|
|
if (!state || !state.connection_enabled) return null;
|
|
|
|
const linked = state.status === 'LINKED';
|
|
const channelUrl = issued?.channel_url || state.channel_url;
|
|
|
|
async function run(fn: () => Promise<unknown>) {
|
|
setBusy(true);
|
|
setError('');
|
|
try {
|
|
await fn();
|
|
await load();
|
|
} catch (e) {
|
|
setError(e instanceof Error ? e.message : '요청을 처리하지 못했습니다.');
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
const issue = () =>
|
|
run(async () => {
|
|
setIssued(await agentApi<KakaoLinkCode>('/kakao/link/code', {}));
|
|
});
|
|
|
|
const disconnect = () =>
|
|
run(async () => {
|
|
setIssued(null);
|
|
await agentApi('/kakao/link/disconnect', {});
|
|
});
|
|
|
|
return (
|
|
<section className="mb-4 rounded-xl border border-border p-4">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<div className="min-w-0">
|
|
<h2 className="text-sm font-bold">카카오톡으로 관리 · 채널 연결</h2>
|
|
<p className="mt-1 text-xs text-muted-foreground">
|
|
{linked
|
|
? '연결되어 있습니다. 카카오톡에서 가게 정보를 고치고 사이트를 발행할 수 있습니다.'
|
|
: '연결해 두면 카카오톡 대화창에서 가게 정보를 고치고 사이트를 발행할 수 있습니다.'}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex shrink-0 flex-wrap gap-2">
|
|
{!linked && (
|
|
<Button size="sm" variant="primary" disabled={busy} onClick={issue}>
|
|
{busy ? (
|
|
<Loader2 className="size-4 animate-spin" />
|
|
) : (
|
|
<MessageCircle className="size-4" />
|
|
)}
|
|
<span>{state.status === 'PENDING' ? '코드 다시 받기' : '카카오톡 연결'}</span>
|
|
</Button>
|
|
)}
|
|
{(linked || state.status === 'PENDING') && (
|
|
<Button size="sm" variant="ghost" disabled={busy} onClick={disconnect}>
|
|
<Unlink className="size-4" />
|
|
<span>{linked ? '연결 해제' : '연결 취소'}</span>
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{issued && !linked && (
|
|
<div className="mt-3 border-t border-border pt-3">
|
|
<p className="text-xs font-bold">카카오톡 채널에 이 코드를 보내 주세요</p>
|
|
<p className="mt-2 font-mono text-2xl font-bold tracking-[0.3em]">{issued.code}</p>
|
|
<p className="mt-2 text-xs text-muted-foreground">
|
|
{new Date(issued.expires_at).toLocaleTimeString('ko-KR', {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
})}
|
|
까지 쓸 수 있습니다. 화면을 나가면 코드를 다시 볼 수 없어요 — 그때는 다시 받으면 됩니다.
|
|
</p>
|
|
{channelUrl && (
|
|
<p className="mt-2 text-xs">
|
|
<a
|
|
href={channelUrl}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="font-bold underline underline-offset-4"
|
|
>
|
|
카카오톡 채널 열기 →
|
|
</a>
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* 코드를 냈는데 화면을 새로 열어 코드가 사라진 경우. */}
|
|
{!issued && state.status === 'PENDING' && (
|
|
<p className="mt-3 text-xs text-muted-foreground">
|
|
보낸 코드를 기다리고 있습니다. 코드를 잃어버렸다면 다시 받아 주세요.
|
|
</p>
|
|
)}
|
|
|
|
{error && (
|
|
<p role="alert" className="mt-3 text-xs text-destructive">
|
|
{error}
|
|
</p>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|