feat(notifications): 알림 종 + 공지 기능 추가
- 헤더 프로필 왼쪽 알림 종: 드롭다운·안읽음 배지·60초 폴링(최초 폴링 도입) - 내 프로젝트 좋아요/즐겨찾기/댓글 시 소유자에게 알림(본인 제외·재토글 도배 방지) - 관리자 공지: /announcements 목록 + 작성/수정/삭제, 등록 시 전 직원에게 fan-out 알림 - notifications/announcements 테이블(멱등) + requireAdmin 가드 + bell/megaphone 아이콘 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -191,3 +191,28 @@
|
||||
.crop-zoom { margin-top: 14px; display: flex; align-items: center; gap: 10px; color: var(--fg2); }
|
||||
.crop-zoom input[type=range] { flex: 1; accent-color: var(--link); }
|
||||
.crop-hint { margin: 10px 0 0; font-size: 11.5px; color: var(--fg3); line-height: 1.5; }
|
||||
|
||||
/* ===================== 공지사항(📢) 페이지 ===================== */
|
||||
/* 작성/수정 공용 폼 */
|
||||
.ann-form { display: flex; flex-direction: column; gap: 10px; background: var(--panel); border: 1px solid var(--border-soft); border-radius: 12px; padding: 16px; }
|
||||
.ann-form-new { margin-bottom: 24px; }
|
||||
.ann-input-title, .ann-input-body {
|
||||
width: 100%; box-sizing: border-box; font-family: inherit; font-size: 14px; color: var(--fg);
|
||||
background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; outline: none;
|
||||
}
|
||||
.ann-input-title { font-weight: 600; }
|
||||
.ann-input-body { resize: vertical; line-height: 1.55; }
|
||||
.ann-input-title:focus, .ann-input-body:focus { border-color: var(--link); }
|
||||
.ann-form-actions { display: flex; align-items: center; gap: 8px; }
|
||||
|
||||
.ann-empty { padding: 28px; text-align: center; color: var(--fg2); font-size: 14px; background: var(--panel); border: 1px solid var(--border-soft); border-radius: 12px; }
|
||||
|
||||
.ann-list { display: flex; flex-direction: column; gap: 14px; }
|
||||
.ann-item { background: var(--card); border: 1px solid var(--border-soft); border-radius: 12px; padding: 18px 20px; }
|
||||
.ann-item-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
|
||||
.ann-item-title { margin: 0; font-size: 16px; font-weight: 700; color: var(--fg); word-break: break-word; }
|
||||
.ann-item-actions { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
|
||||
.ann-del:hover { color: var(--danger); border-color: var(--danger); }
|
||||
.ann-meta { margin-top: 5px; font-size: 12px; color: var(--fg2); }
|
||||
.ann-body { margin-top: 12px; }
|
||||
.ann-item .ann-edit { margin-top: 4px; }
|
||||
|
||||
@@ -64,6 +64,40 @@ html[data-aidev-theme="dark"] .theme-ico-sun { display: inline-flex; }
|
||||
.usermenu-item:hover { background: var(--panel); }
|
||||
.usermenu-ico { display: inline-flex; color: var(--fg2); }
|
||||
|
||||
/* ===================== 알림(🔔) 종 ===================== */
|
||||
.notif { position: relative; }
|
||||
.notif-btn { position: relative; } /* 배지 기준 */
|
||||
/* 안 읽은 수 배지 — 종 우상단 빨강 pill */
|
||||
.notif-badge {
|
||||
position: absolute; top: -4px; right: -4px; min-width: 16px; height: 16px; padding: 0 4px;
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
background: var(--danger); color: #fff; font-size: 10px; font-weight: 700; line-height: 1;
|
||||
border-radius: 999px; border: 1px solid var(--hbg); box-sizing: border-box;
|
||||
}
|
||||
.notif-badge[hidden] { display: none; }
|
||||
/* 드롭다운 패널 — 사용자 메뉴 팝오버와 동일 패턴(더 넓게) */
|
||||
.notif-pop {
|
||||
position: absolute; top: 46px; right: 0; width: min(340px, calc(100vw - 32px));
|
||||
background: var(--card); border: 1px solid var(--border); border-radius: 10px;
|
||||
box-shadow: 0 8px 24px var(--shadow-strong); padding: 6px; z-index: 320; animation: aidev-pop .14s ease-out;
|
||||
}
|
||||
.notif-pop[hidden] { display: none; }
|
||||
.notif-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px 8px; border-bottom: 1px solid var(--divider); margin-bottom: 4px; }
|
||||
.notif-title { font-size: 13px; font-weight: 700; color: var(--fg); }
|
||||
.notif-readall { background: transparent; border: 0; cursor: pointer; font-family: inherit; font-size: 12px; color: var(--link); padding: 2px 4px; }
|
||||
.notif-readall:hover { text-decoration: underline; }
|
||||
.notif-list { max-height: 360px; overflow-y: auto; }
|
||||
.notif-item { display: flex; gap: 9px; align-items: flex-start; padding: 9px 10px; border-radius: 8px; text-decoration: none; color: var(--fg); }
|
||||
.notif-item:hover { background: var(--panel); }
|
||||
.notif-item.unread { background: var(--attn-bg); }
|
||||
.notif-item-ico { flex: 0 0 auto; font-size: 15px; line-height: 1.3; }
|
||||
.notif-item-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
|
||||
.notif-item-text { font-size: 12.5px; line-height: 1.4; color: var(--fg); word-break: break-word; }
|
||||
.notif-item-time { font-size: 11px; color: var(--fg3); }
|
||||
.notif-empty { padding: 22px 10px; text-align: center; color: var(--fg3); font-size: 12.5px; }
|
||||
.notif-foot { display: flex; align-items: center; justify-content: center; gap: 6px; padding: 9px; margin-top: 4px; border-top: 1px solid var(--divider); font-size: 12.5px; font-weight: 600; color: var(--link); text-decoration: none; }
|
||||
.notif-foot:hover { background: var(--panel); border-radius: 6px; }
|
||||
|
||||
/* ===================== 메인 / 섹션 ===================== */
|
||||
.main { max-width: 1180px; margin: 0 auto; padding: 32px 24px 64px; }
|
||||
.section { margin-bottom: 40px; }
|
||||
|
||||
31
public/js/announcements.js
Normal file
31
public/js/announcements.js
Normal file
@@ -0,0 +1,31 @@
|
||||
// 공지 페이지 — 항목별 인라인 수정 폼 토글. (삭제 확인은 comments.js 의 [data-confirm] 핸들러가 처리)
|
||||
// 문서 위임이라 공지 페이지가 아닌 곳에서 로드돼도 해당 요소가 없으면 무동작 → 전 페이지 안전.
|
||||
import { $ } from "./util.js";
|
||||
|
||||
export function initAnnouncements() {
|
||||
document.addEventListener("click", (e) => {
|
||||
// 수정 열기: 보기 숨기고 수정 폼 표시.
|
||||
const eb = e.target.closest("[data-ann-edit]");
|
||||
if (eb) {
|
||||
const id = eb.getAttribute("data-ann-edit");
|
||||
const view = $('[data-ann-view="' + id + '"]');
|
||||
const form = $('[data-ann-editform="' + id + '"]');
|
||||
if (view) view.hidden = true;
|
||||
if (form) {
|
||||
form.hidden = false;
|
||||
const t = $(".ann-input-title", form);
|
||||
if (t) t.focus();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 수정 취소: 폼 숨기고 보기 복원.
|
||||
const cb = e.target.closest("[data-ann-cancel]");
|
||||
if (cb) {
|
||||
const id = cb.getAttribute("data-ann-cancel");
|
||||
const view = $('[data-ann-view="' + id + '"]');
|
||||
const form = $('[data-ann-editform="' + id + '"]');
|
||||
if (form) form.hidden = true;
|
||||
if (view) view.hidden = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import { initReactions } from "./reactions.js";
|
||||
import { initModals, openDetail } from "./modals.js";
|
||||
import { initComments } from "./comments.js";
|
||||
import { initFeed, initMyFilter } from "./feed.js";
|
||||
import { initNotifications } from "./notifications.js";
|
||||
import { initAnnouncements } from "./announcements.js";
|
||||
|
||||
// 서버가 <body data-*> 로 전달한 1회성 신호 처리
|
||||
function initSignals() {
|
||||
@@ -50,6 +52,8 @@ function boot() {
|
||||
initComments();
|
||||
initFeed();
|
||||
initMyFilter();
|
||||
initNotifications();
|
||||
initAnnouncements();
|
||||
initSignals();
|
||||
consoleArt();
|
||||
}
|
||||
|
||||
150
public/js/notifications.js
Normal file
150
public/js/notifications.js
Normal file
@@ -0,0 +1,150 @@
|
||||
// 알림(🔔) 종 — 드롭다운 열기/닫기 · 목록 fetch · 안 읽은 배지 · 60초 폴링.
|
||||
// 서버 API: GET /api/notifications → { unread, items }, POST /api/notifications/read-all.
|
||||
import { $, toast } from "./util.js";
|
||||
|
||||
const POLL_MS = 60000; // 배지 폴링 주기(60초)
|
||||
|
||||
// 종류별 아이콘(가벼운 이모지) · 알림 문구 · 이동 링크.
|
||||
const ICONS = { heart: "❤️", star: "⭐", comment: "💬", announcement: "📢" };
|
||||
|
||||
function itemText(n) {
|
||||
const who = n.actor_name || "누군가";
|
||||
const proj = n.project_title || "프로젝트";
|
||||
if (n.kind === "heart") return who + '님이 «' + proj + '»에 좋아요를 남겼어요';
|
||||
if (n.kind === "star") return who + '님이 «' + proj + '»을(를) 즐겨찾기했어요';
|
||||
if (n.kind === "comment") return who + '님이 «' + proj + '»에 댓글을 남겼어요';
|
||||
if (n.kind === "announcement") return "새 공지: " + (n.announcement_title || "");
|
||||
return "새 알림";
|
||||
}
|
||||
|
||||
function itemHref(n) {
|
||||
if (n.kind === "announcement") return "/announcements";
|
||||
if (n.project_id) return "/projects/" + n.project_id;
|
||||
return "#";
|
||||
}
|
||||
|
||||
// 상대 시간(방금/N분 전/N시간 전/N일 전/날짜).
|
||||
function relTime(iso) {
|
||||
const t = new Date(iso).getTime();
|
||||
if (!t) return "";
|
||||
const s = Math.max(0, (Date.now() - t) / 1000);
|
||||
if (s < 60) return "방금";
|
||||
if (s < 3600) return Math.floor(s / 60) + "분 전";
|
||||
if (s < 86400) return Math.floor(s / 3600) + "시간 전";
|
||||
if (s < 7 * 86400) return Math.floor(s / 86400) + "일 전";
|
||||
const d = new Date(t);
|
||||
return d.getFullYear() + "." + String(d.getMonth() + 1).padStart(2, "0") + "." + String(d.getDate()).padStart(2, "0");
|
||||
}
|
||||
|
||||
// 목록 렌더 — textContent 로만 채워 XSS 방지.
|
||||
function renderList(listEl, items) {
|
||||
listEl.textContent = "";
|
||||
if (!items || !items.length) {
|
||||
const empty = document.createElement("div");
|
||||
empty.className = "notif-empty";
|
||||
empty.textContent = "아직 알림이 없어요";
|
||||
listEl.appendChild(empty);
|
||||
return;
|
||||
}
|
||||
items.forEach((n) => {
|
||||
const a = document.createElement("a");
|
||||
a.className = "notif-item" + (n.is_read ? "" : " unread");
|
||||
a.href = itemHref(n);
|
||||
const ico = document.createElement("span");
|
||||
ico.className = "notif-item-ico";
|
||||
ico.textContent = ICONS[n.kind] || "🔔";
|
||||
const body = document.createElement("span");
|
||||
body.className = "notif-item-body";
|
||||
const text = document.createElement("span");
|
||||
text.className = "notif-item-text";
|
||||
text.textContent = itemText(n);
|
||||
const time = document.createElement("span");
|
||||
time.className = "notif-item-time";
|
||||
time.textContent = relTime(n.created_at);
|
||||
body.appendChild(text);
|
||||
body.appendChild(time);
|
||||
a.appendChild(ico);
|
||||
a.appendChild(body);
|
||||
listEl.appendChild(a);
|
||||
});
|
||||
}
|
||||
|
||||
function setBadge(badgeEl, n) {
|
||||
const count = +n || 0;
|
||||
if (count > 0) {
|
||||
badgeEl.textContent = count > 99 ? "99+" : String(count);
|
||||
badgeEl.hidden = false;
|
||||
} else {
|
||||
badgeEl.hidden = true;
|
||||
}
|
||||
}
|
||||
|
||||
function apiGet() {
|
||||
return fetch("/api/notifications", { headers: { "X-Requested-With": "fetch", "Accept": "application/json" } })
|
||||
.then((r) => (r.ok ? r.json() : Promise.reject(r.status)));
|
||||
}
|
||||
|
||||
function apiReadAll() {
|
||||
return fetch("/api/notifications/read-all", {
|
||||
method: "POST",
|
||||
headers: { "X-Requested-With": "fetch", "Accept": "application/json" },
|
||||
}).then((r) => (r.ok ? r.json() : Promise.reject(r.status)));
|
||||
}
|
||||
|
||||
export function initNotifications() {
|
||||
const btn = $("#notifBtn"), pop = $("#notifPop"), badge = $("#notifBadge"),
|
||||
list = $("#notifList"), readAll = $("#notifReadAll");
|
||||
if (!btn || !pop || !badge || !list) return; // 비로그인 등 종이 없으면 무동작
|
||||
|
||||
// 드롭다운 열 때: 목록을 받아 렌더한 뒤 모두 읽음 처리 → 배지 초기화.
|
||||
function openPanel() {
|
||||
apiGet()
|
||||
.then((data) => {
|
||||
renderList(list, data.items);
|
||||
setBadge(badge, data.unread);
|
||||
if (data.unread > 0) apiReadAll().then(() => setBadge(badge, 0)).catch(() => {});
|
||||
})
|
||||
.catch(() => {
|
||||
list.textContent = "";
|
||||
const err = document.createElement("div");
|
||||
err.className = "notif-empty";
|
||||
err.textContent = "알림을 불러오지 못했어요";
|
||||
list.appendChild(err);
|
||||
});
|
||||
}
|
||||
|
||||
btn.addEventListener("click", (e) => {
|
||||
e.stopPropagation();
|
||||
const willOpen = pop.hidden;
|
||||
pop.hidden = !willOpen;
|
||||
btn.setAttribute("aria-expanded", willOpen ? "true" : "false");
|
||||
if (willOpen) openPanel();
|
||||
});
|
||||
|
||||
// 바깥 클릭 시 닫기(사용자 메뉴와 동일 패턴).
|
||||
document.addEventListener("click", (e) => {
|
||||
if (!pop.hidden && !pop.contains(e.target) && !btn.contains(e.target)) {
|
||||
pop.hidden = true;
|
||||
btn.setAttribute("aria-expanded", "false");
|
||||
}
|
||||
});
|
||||
|
||||
if (readAll) {
|
||||
readAll.addEventListener("click", (e) => {
|
||||
e.stopPropagation();
|
||||
apiReadAll()
|
||||
.then(() => {
|
||||
setBadge(badge, 0);
|
||||
list.querySelectorAll(".notif-item.unread").forEach((el) => el.classList.remove("unread"));
|
||||
})
|
||||
.catch(() => toast("처리에 실패했어요. 다시 시도해 주세요."));
|
||||
});
|
||||
}
|
||||
|
||||
// 최초 1회 + 주기적 폴링: 배지 숫자만 갱신(드롭다운이 닫혀 있을 때).
|
||||
function refreshBadge() {
|
||||
apiGet().then((data) => { if (pop.hidden) setBadge(badge, data.unread); }).catch(() => {});
|
||||
}
|
||||
refreshBadge();
|
||||
setInterval(refreshBadge, POLL_MS);
|
||||
}
|
||||
Reference in New Issue
Block a user