Something went wrong. Try again.
Central platform for European atproto.<cc> country community websites atproto.eu
community atproto
Something went wrong. Try again.
2.1 kB · 64 lines
Astro
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465---// One delegated handler for all repost buttons. Signed in: write/remove an// app.bsky.feed.repost record on the user's own PDS. Signed out: no error — open the post// so they can repost it there.---
<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-repost') as HTMLElement | null; if (!btn) return;
const atn = (window as any).atn; if (!atn?.agent) { const url = btn.dataset.url; if (url) window.open(url, '_blank', 'noopener'); return; } if (!(await ensureWriteScope('repost this'))) 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.repostUri) { const rkey = btn.dataset.repostUri.split('/').pop(); await agent.com.atproto.repo.deleteRecord({ repo, collection: 'app.bsky.feed.repost', rkey, }); delete btn.dataset.repostUri; btn.classList.remove('reposted'); setCount(Math.max(0, count - 1)); } else { const res = await agent.com.atproto.repo.createRecord({ repo, collection: 'app.bsky.feed.repost', record: { $type: 'app.bsky.feed.repost', subject: { uri: btn.dataset.uri, cid: btn.dataset.cid }, createdAt: new Date().toISOString(), }, }); btn.dataset.repostUri = res.data.uri; btn.classList.add('reposted'); setCount(count + 1); } } catch (err: any) { await handleActionError(err, 'Repost'); } finally { btn.removeAttribute('disabled'); } });</script>