|
| 1 | +/** |
| 2 | + * <estreuv-message-list> — playground 예제 컴포넌트 (라이브러리 본체 아님). |
| 3 | + * |
| 4 | + * 강결합 데모의 중심: 메시지 데이터를 보유하고, 공유 intent 의 `sidebarActive`(활성 폴더)로 |
| 5 | + * 필터해 렌더한다. 메시지 "읽음" → 폴더별 미읽음 counts 재계산 → `requestIntentUpdate({counts, |
| 6 | + * notifCount})` (event-up). 그러면 owner article 이 intent 를 갱신 → 사이드바 배지 + notif 타일이 |
| 7 | + * 동시에 prop-down 반응 (단일 소스). onShow 에 "수신 시뮬" 타이머 시작 / onHide 정지 (lifecycle). |
| 8 | + * |
| 9 | + * EstreUV 를 npm 의존으로 쓰는 실사용자 컴포넌트 작성 예시 — importmap 으로 estreuv/lit 해소. |
| 10 | + */ |
| 11 | +import { EstreUVElement } from 'estreuv'; |
| 12 | +import { html, css } from 'lit'; |
| 13 | + |
| 14 | +const FOLDERS = ['Inbox', 'Archive', 'Trash']; |
| 15 | +let _seq = 200; |
| 16 | +const mk = (from, sub, folder, read = false) => ({ id: ++_seq, from, sub, folder, read }); |
| 17 | + |
| 18 | +export class EstreuvMessageList extends EstreUVElement { |
| 19 | + static properties = { |
| 20 | + ...EstreUVElement.properties, |
| 21 | + _messages: { state: true }, |
| 22 | + }; |
| 23 | + |
| 24 | + static styles = css` |
| 25 | + :host { display: block; flex: 1; min-width: 0; } |
| 26 | + .empty { padding: 56px 20px; text-align: center; opacity: 0.55; } |
| 27 | + .msg { |
| 28 | + display: flex; gap: 12px; padding: 11px 14px; border-radius: 12px; |
| 29 | + cursor: pointer; border: 1px solid transparent; |
| 30 | + } |
| 31 | + .msg:hover { background: rgba(127,127,127,0.10); } |
| 32 | + .av { |
| 33 | + width: 34px; height: 34px; border-radius: 50%; flex: none; |
| 34 | + display: flex; align-items: center; justify-content: center; font-weight: 700; |
| 35 | + background: rgba(127,127,127,0.15); |
| 36 | + } |
| 37 | + .from { font-weight: 600; font-size: 0.95rem; } |
| 38 | + .sub { opacity: 0.6; font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } |
| 39 | + .msg.read { opacity: 0.5; } |
| 40 | + .msg.unread .from::before { |
| 41 | + content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; |
| 42 | + background: var(--estreuv-tile-color, #2a8c82); margin-right: 7px; vertical-align: middle; |
| 43 | + } |
| 44 | + `; |
| 45 | + |
| 46 | + constructor() { |
| 47 | + super(); |
| 48 | + this._messages = [ |
| 49 | + mk('Estre CI', 'Release workflow: estreuv published', 'Inbox'), |
| 50 | + mk('SoliEstre', 'Re: docs 사이트 VitePress 구성', 'Inbox'), |
| 51 | + mk('GitHub', '[EstreUV.js] CI passed on main', 'Inbox'), |
| 52 | + mk('npm', 'Weekly download report', 'Archive', true), |
| 53 | + mk('Antigravity', 'Browser verification PASS', 'Archive', true), |
| 54 | + mk('notice', 'deprecated reminder', 'Trash', true), |
| 55 | + ]; |
| 56 | + this._timer = null; |
| 57 | + } |
| 58 | + |
| 59 | + get activeFolder() { return this.intent?.sidebarActive ?? 'Inbox'; } |
| 60 | + |
| 61 | + /** 폴더별 미읽음 counts {Inbox,Archive,Trash} + 총합을 intent 로 위임 (event-up) */ |
| 62 | + _publishCounts() { |
| 63 | + const counts = Object.fromEntries(FOLDERS.map(f => [f, 0])); |
| 64 | + this._messages.forEach(m => { if (!m.read) counts[m.folder]++; }); |
| 65 | + const notifCount = Object.values(counts).reduce((a, b) => a + b, 0); |
| 66 | + this.requestIntentUpdate({ counts, notifCount }); |
| 67 | + } |
| 68 | + |
| 69 | + _read(m) { |
| 70 | + if (m.read) return; |
| 71 | + m.read = true; |
| 72 | + this._messages = [...this._messages]; // reactive |
| 73 | + this._publishCounts(); |
| 74 | + } |
| 75 | + |
| 76 | + /** 외부(또는 타이머)에서 수신 시뮬 — Inbox 에 새 메시지 */ |
| 77 | + simulateIncoming() { |
| 78 | + const samples = [ |
| 79 | + ['Estrelle', 'New build artifact ready'], |
| 80 | + ['Show GN', 'New comment on your post'], |
| 81 | + ['create-estreuv', 'npx scaffold tested'], |
| 82 | + ]; |
| 83 | + const s = samples[Math.floor(Math.random() * samples.length)]; |
| 84 | + this._messages = [mk(s[0], s[1], 'Inbox'), ...this._messages]; |
| 85 | + this._publishCounts(); |
| 86 | + } |
| 87 | + |
| 88 | + render() { |
| 89 | + const msgs = this._messages.filter(m => m.folder === this.activeFolder); |
| 90 | + if (!msgs.length) return html`<div class="empty">이 폴더에 메시지가 없습니다</div>`; |
| 91 | + return html`${msgs.map(m => html` |
| 92 | + <div class="msg ${m.read ? 'read' : 'unread'}" @click=${() => this._read(m)}> |
| 93 | + <div class="av">${(m.from[0] || '?').toUpperCase()}</div> |
| 94 | + <div style="flex:1;min-width:0"> |
| 95 | + <div class="from">${m.from}</div> |
| 96 | + <div class="sub">${m.sub}</div> |
| 97 | + </div> |
| 98 | + </div>`)}`; |
| 99 | + } |
| 100 | + |
| 101 | + firstUpdated() { this._publishCounts(); } |
| 102 | + |
| 103 | + // ─── EstreUI lifecycle — 수신 시뮬 타이머를 가시성에 결속 ─── |
| 104 | + onShow(handle) { |
| 105 | + super.onShow(handle); |
| 106 | + if (!this._timer) this._timer = setInterval(() => this.simulateIncoming(), 8000); |
| 107 | + } |
| 108 | + onHide(handle) { |
| 109 | + super.onHide(handle); |
| 110 | + if (this._timer) { clearInterval(this._timer); this._timer = null; } |
| 111 | + } |
| 112 | + disconnectedCallback() { |
| 113 | + if (this._timer) { clearInterval(this._timer); this._timer = null; } |
| 114 | + super.disconnectedCallback(); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +customElements.define('estreuv-message-list', EstreuvMessageList); |
0 commit comments