webapp/src/context/following.tsx

135 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-02-04 11:25:21 +00:00
import { Accessor, JSX, createContext, createEffect, createSignal, useContext } from 'solid-js'
2024-01-31 12:34:15 +00:00
import { createStore } from 'solid-js/store'
import { apiClient } from '../graphql/client/core'
import { Author, AuthorFollowsResult, FollowingEntity } from '../graphql/schema/core.gen'
2024-01-31 12:34:15 +00:00
import { useSession } from './session'
interface FollowingContextType {
loading: Accessor<boolean>
2024-04-08 12:49:40 +00:00
followers: Accessor<Array<Author>>
2024-03-29 09:58:32 +00:00
subscriptions: AuthorFollowsResult
2024-04-08 13:14:19 +00:00
setSubscriptions: (subscriptions: AuthorFollowsResult) => void
2024-01-31 12:34:15 +00:00
setFollowing: (what: FollowingEntity, slug: string, value: boolean) => void
loadSubscriptions: () => void
follow: (what: FollowingEntity, slug: string) => Promise<void>
unfollow: (what: FollowingEntity, slug: string) => Promise<void>
2024-02-08 09:11:52 +00:00
isOwnerSubscribed: (id: number | string) => boolean
2024-01-31 12:34:15 +00:00
}
const FollowingContext = createContext<FollowingContextType>()
export function useFollowing() {
return useContext(FollowingContext)
}
2024-03-29 09:58:32 +00:00
const EMPTY_SUBSCRIPTIONS: AuthorFollowsResult = {
2024-01-31 12:34:15 +00:00
topics: [],
authors: [],
communities: [],
}
export const FollowingProvider = (props: { children: JSX.Element }) => {
const [loading, setLoading] = createSignal<boolean>(false)
2024-04-08 12:49:40 +00:00
const [followers, setFollowers] = createSignal<Array<Author>>([])
2024-04-08 13:14:19 +00:00
const [subscriptions, setSubscriptions] = createStore<AuthorFollowsResult>(EMPTY_SUBSCRIPTIONS)
const { author, session } = useSession()
2024-01-31 12:34:15 +00:00
const fetchData = async () => {
setLoading(true)
try {
if (apiClient.private) {
console.debug('[context.following] fetching subs data...')
const result = await apiClient.getAuthorFollows({ user: session()?.user.id })
2024-01-31 12:34:15 +00:00
setSubscriptions(result || EMPTY_SUBSCRIPTIONS)
console.info('[context.following] subs:', subscriptions)
}
} catch (error) {
console.info('[context.following] cannot get subs', error)
} finally {
setLoading(false)
}
}
const follow = async (what: FollowingEntity, slug: string) => {
if (!author()) return
try {
await apiClient.follow({ what, slug })
setSubscriptions((prevSubscriptions) => {
const updatedSubs = { ...prevSubscriptions }
if (!updatedSubs[what]) updatedSubs[what] = []
const exists = updatedSubs[what]?.some((entity) => entity.slug === slug)
if (!exists) updatedSubs[what].push(slug)
return updatedSubs
})
} catch (error) {
console.error(error)
}
}
const unfollow = async (what: FollowingEntity, slug: string) => {
if (!author()) return
try {
await apiClient.unfollow({ what, slug })
} catch (error) {
console.error(error)
}
}
2024-01-31 14:59:03 +00:00
createEffect(() => {
2024-01-31 15:35:40 +00:00
if (author()) {
2024-04-08 12:49:40 +00:00
try {
const { authors, followers, topics } = session().user.app_data
setSubscriptions({ authors, topics })
setFollowers(followers)
2024-04-08 12:54:01 +00:00
if (!authors) fetchData()
} catch (e) {
2024-04-08 12:49:40 +00:00
console.error(e)
}
2024-01-31 12:34:15 +00:00
}
})
const setFollowing = (what: FollowingEntity, slug: string, value = true) => {
setSubscriptions((prevSubscriptions) => {
const updatedSubs = { ...prevSubscriptions }
if (!updatedSubs[what]) updatedSubs[what] = []
if (value) {
const exists = updatedSubs[what]?.some((entity) => entity.slug === slug)
if (!exists) updatedSubs[what].push(slug)
} else {
updatedSubs[what] = (updatedSubs[what] || []).filter((x) => x !== slug)
}
return updatedSubs
})
try {
;(value ? follow : unfollow)(what, slug)
} catch (error) {
console.error(error)
} finally {
fetchData()
}
}
2024-02-08 09:11:52 +00:00
const isOwnerSubscribed = (id?: number | string) => {
if (!author() || !subscriptions) return
const isAuthorSubscribed = subscriptions.authors?.some((authorEntity) => authorEntity.id === id)
const isTopicSubscribed = subscriptions.topics?.some((topicEntity) => topicEntity.slug === id)
return !!isAuthorSubscribed || !!isTopicSubscribed
2024-02-05 12:34:47 +00:00
}
2024-01-31 12:34:15 +00:00
const value: FollowingContextType = {
loading,
subscriptions,
setSubscriptions,
2024-02-05 12:34:47 +00:00
isOwnerSubscribed,
2024-01-31 12:34:15 +00:00
setFollowing,
2024-04-08 12:49:40 +00:00
followers,
2024-01-31 12:34:15 +00:00
loadSubscriptions: fetchData,
follow,
unfollow,
}
return <FollowingContext.Provider value={value}>{props.children}</FollowingContext.Provider>
}