diff --git a/index.html b/index.html index 1591c29..26b31de 100644 --- a/index.html +++ b/index.html @@ -399,7 +399,7 @@ const sb = supabase.createClient(SUPABASE_URL, SUPABASE_KEY, { auth: { persistSe const state = { user: null, view: 'feed', notes: [], hasMore: false, loading: false, - tag: null, pinned: false, query: '', + tag: null, pinned: false, archived: false, archivedCount: 0, query: '', tags: [], settings: null, reminders: [], files: [], remindMode: false, editing: null, boardNotes: {}, boardPicking: false, pickTags: [], @@ -565,7 +565,7 @@ function onNoteChange({ eventType, new: row, old }) { if (!state.notes.some((n) => n.id === row.id) && matchesFilter(row)) state.notes.unshift(row); } else if (eventType === 'UPDATE') { const i = state.notes.findIndex((n) => n.id === row.id); - if (i >= 0) state.notes[i] = row; + if (i >= 0) { if (matchesFilter(row)) state.notes[i] = row; else state.notes.splice(i, 1); } } if (state.editing == null) { renderFeed(); if (state.view === 'board') loadBoard(); } scheduleTagRefresh(); @@ -587,6 +587,8 @@ function parseQuery(q) { } function matchesFilter(n) { + // Archived notes only show in the Archived view, or when searching. + if (state.archived ? !n.archived_at : (n.archived_at && !state.query)) return false; if (state.tag && !n.tags.includes(state.tag)) return false; if (state.pinned && !n.pinned) return false; const { words, tags } = parseQuery(state.query); @@ -600,8 +602,10 @@ async function loadNotes(reset) { state.loading = true; if (reset) renderStatus(true); const offset = reset ? 0 : state.notes.length; - let q = sb.from('notes').select('id,body,tags,source,pinned,attachments,created_at,updated_at') + let q = sb.from('notes').select('id,body,tags,source,pinned,attachments,archived_at,created_at,updated_at') .order('created_at', { ascending: false }).range(offset, offset + PAGE - 1); + if (state.archived) q = q.not('archived_at', 'is', null); + else if (!state.query) q = q.is('archived_at', null); if (state.tag) q = q.contains('tags', [state.tag]); if (state.pinned) q = q.eq('pinned', true); const { words, tags } = parseQuery(state.query); @@ -615,13 +619,13 @@ async function loadNotes(reset) { } state.notes = reset ? data : state.notes.concat(data); state.hasMore = data.length === PAGE; - if (!state.tag && !state.pinned && !state.query) lsSet(LS.cache, { uid: state.user.id, notes: state.notes.slice(0, PAGE) }); + if (!state.tag && !state.pinned && !state.archived && !state.query) lsSet(LS.cache, { uid: state.user.id, notes: state.notes.slice(0, PAGE) }); renderFeed(); } async function loadTags() { - const { data, error } = await sb.rpc('tag_counts'); - if (!error) { state.tags = data; renderTags(); } + const [{ data, error }, { data: archived }] = await Promise.all([sb.rpc('tag_counts'), sb.rpc('archived_count')]); + if (!error) { state.tags = data; state.archivedCount = Number(archived) || 0; renderTags(); } } async function loadSettings() { @@ -783,6 +787,7 @@ const ICON = { copy: '', edit: '', bell: '', + archive: '', trash: '', file: '', check: '', @@ -809,12 +814,14 @@ function noteHtml(n, compact = false) { ${n.source && n.source !== 'web' ? `${esc(n.source)}` : ''} ${n._queued ? 'waiting to sync' : ''} ${n.pinned ? `${ICON.pin}` : ''} + ${n.archived_at ? 'archived' : ''} ${compact ? '' : ``} + @@ -825,7 +832,9 @@ function renderFeed() { const feed = $('#feed'); if (!state.notes.length) { const filtered = state.tag || state.pinned || state.query; - feed.innerHTML = state.loading ? '' : `
${filtered ? 'Nothing matches' : 'Your feed is empty'}${filtered ? 'Try a different search or tag.' : 'Type a thought above and hit Enter. Add #tags to file it.'}
`; + feed.innerHTML = state.loading ? '' : state.archived && !filtered + ? '
No archived notesUse the box icon on a note to archive it. It leaves your feed but stays searchable.
' + : `
${filtered ? 'Nothing matches' : 'Your feed is empty'}${filtered ? 'Try a different search or tag.' : 'Type a thought above and hit Enter. Add #tags to file it.'}
`; } else { let html = '', lastDay = ''; for (const n of state.notes) { @@ -849,6 +858,7 @@ function renderStatus(loading) { if (q) bits.push(`${q} note${q > 1 ? 's' : ''} waiting to sync`); if (state.tag) bits.push(`Showing `); else if (state.pinned) bits.push('Showing pinned notes '); + else if (state.archived) bits.push('Showing archived notes '); if (state.query && !loading) bits.push(`${state.notes.length}${state.hasMore ? '+' : ''} result${state.notes.length === 1 ? '' : 's'}`); el.innerHTML = bits.join(' '); el.classList.toggle('hidden', !bits.length); @@ -856,8 +866,9 @@ function renderStatus(loading) { function renderTags() { const total = state.tags.reduce((a, t) => a + Number(t.count), 0); - let html = ``; + let html = ``; html += ``; + html += ``; for (const t of state.tags) { html += ``; } @@ -943,7 +954,7 @@ document.addEventListener('click', async (e) => { const tagBtn = e.target.closest('[data-tag]'); if (tagBtn && !e.target.closest('.board-picker')) { setFilter({ tag: tagBtn.dataset.tag }); return; } const f = e.target.closest('[data-filter]'); - if (f) { setFilter(f.dataset.filter === 'pinned' ? { pinned: true } : {}); return; } + if (f) { setFilter(f.dataset.filter === 'pinned' ? { pinned: true } : f.dataset.filter === 'archived' ? { archived: true } : {}); return; } const actEl = e.target.closest('[data-act]'); if (!actEl) return; const act = actEl.dataset.act; @@ -969,11 +980,36 @@ document.addEventListener('click', async (e) => { state.editing = null; rerender(); } else if (act === 'save') { await saveEdit(note); + } else if (act === 'archive') { + archiveNote(note, !note.archived_at); } else if (act === 'delete') { deleteNote(note); } }); +async function archiveNote(note, archive, quiet = false) { + const prev = note.archived_at; + const archived_at = archive ? new Date().toISOString() : null; + note.archived_at = archived_at; + const idx = state.notes.findIndex((n) => n.id === note.id); + if (idx >= 0 && !matchesFilter(note)) state.notes.splice(idx, 1); + if (archive) for (const k in state.boardNotes) state.boardNotes[k] = state.boardNotes[k].filter((n) => n.id !== note.id); + rerender(); + const { error } = await sb.from('notes').update({ archived_at }).eq('id', note.id); + if (error) { + note.archived_at = prev; + toast('Couldn\'t ' + (archive ? 'archive' : 'unarchive') + ': ' + error.message); + loadNotes(true); if (state.view === 'board') loadBoard(); + return; + } + scheduleTagRefresh(); + if (state.view === 'board' && !archive) loadBoard(); + if (!quiet) toast(archive ? 'Note archived' : 'Note moved back to your feed', 'Undo', async () => { + await archiveNote(note, !archive, true); + if (matchesFilter(note) && !state.notes.some((n) => n.id === note.id)) loadNotes(true); + }); +} + document.addEventListener('keydown', (e) => { if (e.target.id === 'edit-text') { if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { e.preventDefault(); const n = findNote(state.editing); if (n) saveEdit(n); } @@ -1005,7 +1041,7 @@ function deleteNote(note) { undone = true; await del; const { _queued, ...row } = note; - const { data, error } = await sb.from('notes').insert({ id: row.id, body: row.body, tags: row.tags, source: row.source, pinned: row.pinned, attachments: row.attachments, created_at: row.created_at }).select().single(); + const { data, error } = await sb.from('notes').insert({ id: row.id, body: row.body, tags: row.tags, source: row.source, pinned: row.pinned, attachments: row.attachments, archived_at: row.archived_at ?? null, created_at: row.created_at }).select().single(); if (error) { toast('Couldn\'t restore: ' + error.message); return; } state.notes.splice(Math.max(0, idx), 0, data); renderFeed(); scheduleTagRefresh(); if (state.view === 'board') loadBoard(); @@ -1027,8 +1063,8 @@ function openLightbox(src) { // ------------------------------------------------------------------ // Filters, search, views // ------------------------------------------------------------------ -function setFilter({ tag = null, pinned = false }) { - state.tag = tag; state.pinned = pinned; +function setFilter({ tag = null, pinned = false, archived = false }) { + state.tag = tag; state.pinned = pinned; state.archived = archived; if (state.view !== 'feed') switchView('feed'); renderTags(); loadNotes(true); window.scrollTo({ top: 0, behavior: 'smooth' }); @@ -1084,7 +1120,7 @@ $('#menu').addEventListener('click', async (e) => { async function exportNotes() { const all = []; let from = 0; for (;;) { - const { data, error } = await sb.from('notes').select('id,body,tags,source,pinned,attachments,created_at,updated_at').order('created_at').range(from, from + 999); + const { data, error } = await sb.from('notes').select('id,body,tags,source,pinned,attachments,archived_at,created_at,updated_at').order('created_at').range(from, from + 999); if (error) { toast('Export failed: ' + error.message); return; } all.push(...data); if (data.length < 1000) break; from += 1000; } @@ -1102,7 +1138,7 @@ async function exportNotes() { async function loadBoard() { const cols = state.settings?.board_tags || []; if (cols.length < 2) { state.boardPicking = true; state.pickTags = [...cols]; renderBoard(); return; } - const results = await Promise.all(cols.map((t) => sb.from('notes').select('id,body,tags,source,pinned,attachments,created_at').contains('tags', [t]).order('pinned', { ascending: false }).order('created_at', { ascending: false }).limit(100))); + const results = await Promise.all(cols.map((t) => sb.from('notes').select('id,body,tags,source,pinned,attachments,created_at').contains('tags', [t]).is('archived_at', null).order('pinned', { ascending: false }).order('created_at', { ascending: false }).limit(100))); cols.forEach((t, i) => { state.boardNotes[t] = results[i].data || []; }); renderBoard(); } diff --git a/supabase/migrations/20261004230000_archive_notes.sql b/supabase/migrations/20261004230000_archive_notes.sql new file mode 100644 index 0000000..ca4f648 --- /dev/null +++ b/supabase/migrations/20261004230000_archive_notes.sql @@ -0,0 +1,15 @@ +alter table public.notes add column archived_at timestamptz; +create index notes_user_archived on public.notes (user_id, archived_at); + +-- Tag counts reflect what's in the feed: archived notes are not counted. +create or replace function public.tag_counts() +returns table (tag text, count bigint) language sql stable security invoker set search_path = '' as $$ + select t, count(*) from public.notes n, unnest(n.tags) t + where n.user_id = (select auth.uid()) and n.archived_at is null + group by t order by count(*) desc, t; +$$; + +create or replace function public.archived_count() +returns bigint language sql stable security invoker set search_path = '' as $$ + select count(*) from public.notes n where n.user_id = (select auth.uid()) and n.archived_at is not null; +$$; diff --git a/sw.js b/sw.js index 3461a67..2963fc4 100644 --- a/sw.js +++ b/sw.js @@ -1,6 +1,6 @@ // Network-first service worker: always try GitHub Pages for the latest // version, fall back to the cached copy when offline. -var CACHE = 'brainfeed-v4'; +var CACHE = 'brainfeed-v5'; var ASSETS = [ './', './index.html',