import { useAuthStore } from '@/stores/auth' import type { ViewerState } from '@atcute/bluesky/types/app/actor/defs' export function useFollowToggle() { const auth = useAuthStore() async function toggle(profile: { did: string; viewer?: ViewerState }) { if (!auth.isAuthenticated || !auth.session) return const rpc = auth.getRpc() const original = profile.viewer?.following profile.viewer = profile.viewer || {} if (original) profile.viewer.following = undefined else profile.viewer.following = `at://${profile.did}/app.bsky.graph.follow/temporary` try { if (original) { const rkey = original.split('/').pop()! await rpc.post('com.atproto.repo.deleteRecord', { input: { collection: 'app.bsky.graph.follow', repo: auth.session.info.sub, rkey }, }) } else { const { data, ok } = await rpc.post('com.atproto.repo.createRecord', { input: { collection: 'app.bsky.graph.follow', repo: auth.session.info.sub, record: { $type: 'app.bsky.graph.follow', subject: profile.did, createdAt: new Date().toISOString(), }, }, }) if (ok) profile.viewer.following = data.uri } } catch (e) { // revert profile.viewer.following = original throw e } } return { toggle } }