This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / web / src / lib / components / profile / counts.svelte.ts
1.5 kB 53 lines
1import { getContext } from 'svelte'; 2import { createOptimisticCount, type OptimisticCount } from '$lib/optimistic.svelte'; 3import type { ProfileCounts } from './types'; 4 5export type ProfileCountName = keyof ProfileCounts; 6 7export interface ProfileCountsContext { 8 readonly did: string; 9 readonly value: ProfileCounts; 10 adjust(subjectDid: string, name: ProfileCountName, delta: 1 | -1): void; 11} 12 13export const PROFILE_COUNTS_KEY = Symbol('profile-counts'); 14 15export const createProfileCounts = ( 16 did: () => string, 17 loaded: () => ProfileCounts 18): ProfileCountsContext => { 19 const counter = (name: ProfileCountName): OptimisticCount => 20 createOptimisticCount({ key: did, loaded: () => loaded()[name] }); 21 22 const counts = { 23 repos: counter('repos'), 24 stars: counter('stars'), 25 strings: counter('strings'), 26 followers: counter('followers'), 27 following: counter('following'), 28 vouches: counter('vouches') 29 }; 30 31 return { 32 get did() { 33 return did(); 34 }, 35 get value() { 36 return { 37 repos: counts.repos.value, 38 stars: counts.stars.value, 39 strings: counts.strings.value, 40 followers: counts.followers.value, 41 following: counts.following.value, 42 vouches: counts.vouches.value 43 }; 44 }, 45 adjust(subjectDid, name, delta) { 46 if (subjectDid !== did()) return; 47 counts[name].adjust(delta); 48 } 49 }; 50}; 51 52export const getProfileCounts = (): ProfileCountsContext | null => 53 getContext<ProfileCountsContext | null>(PROFILE_COUNTS_KEY);