Something went wrong. Try again.
Central platform for European atproto.<cc> country community websites atproto.eu
community atproto
Something went wrong. Try again.
2.2 kB · 65 lines
Astro
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566---// One delegated handler for all like buttons. Uses the signed-in agent (window.atn)// to write/remove an app.bsky.feed.like record on the user's own PDS.---
<script> import { handleActionError } from '../lib/auth-actions'; import { ensureWriteScope } from '../lib/auth-step-up';
document.addEventListener('click', async (e) => { const target = e.target as HTMLElement | null; const btn = target?.closest('.post-like') as HTMLElement | null; if (!btn) return;
const atn = (window as any).atn; if (!atn?.agent) { // Logged out: no error, just open the post so they can like it there. const url = btn.dataset.url; if (url) window.open(url, '_blank', 'noopener'); return; } // First like grants the activity scope (asked once); if declined/redirecting, stop here. if (!(await ensureWriteScope('like this post'))) return; const agent = atn.agent; const repo = atn.did; const countEl = btn.querySelector('.act-count'); const count = Number(btn.dataset.count ?? '0'); const setCount = (n: number) => { if (countEl) countEl.textContent = String(n); btn.dataset.count = String(n); };
btn.setAttribute('disabled', ''); try { if (btn.dataset.likeUri) { const rkey = btn.dataset.likeUri.split('/').pop(); await agent.com.atproto.repo.deleteRecord({ repo, collection: 'app.bsky.feed.like', rkey, }); delete btn.dataset.likeUri; btn.classList.remove('liked'); setCount(Math.max(0, count - 1)); } else { const res = await agent.com.atproto.repo.createRecord({ repo, collection: 'app.bsky.feed.like', record: { $type: 'app.bsky.feed.like', subject: { uri: btn.dataset.uri, cid: btn.dataset.cid }, createdAt: new Date().toISOString(), }, }); btn.dataset.likeUri = res.data.uri; btn.classList.add('liked'); setCount(count + 1); } } catch (err: any) { await handleActionError(err, 'Like'); } finally { btn.removeAttribute('disabled'); } });</script>