2024-05-05 16:13:48 +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'
|
2024-04-25 10:55:58 +00:00
|
|
|
import { Author, AuthorFollowsResult, Community, FollowingEntity, Topic } from '../graphql/schema/core.gen'
|
2024-01-31 12:34:15 +00:00
|
|
|
|
|
|
|
import { useSession } from './session'
|
|
|
|
|
2024-03-11 05:20:00 +00:00
|
|
|
export type SubscriptionsData = {
|
|
|
|
topics?: Topic[]
|
|
|
|
authors?: Author[]
|
|
|
|
communities?: Community[]
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscribeAction = { slug: string; type: 'subscribe' | 'unsubscribe' }
|
|
|
|
|
2024-01-31 12:34:15 +00:00
|
|
|
interface FollowingContextType {
|
|
|
|
loading: Accessor<boolean>
|
2024-05-05 16:13:48 +00:00
|
|
|
followers: Accessor<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-03-11 05:20:00 +00:00
|
|
|
// followers: Accessor<Author[]>
|
|
|
|
subscribeInAction?: Accessor<SubscribeAction>
|
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-05-05 16:13:48 +00:00
|
|
|
const [followers, setFollowers] = createSignal<Author[]>([])
|
2024-04-08 13:14:19 +00:00
|
|
|
const [subscriptions, setSubscriptions] = createStore<AuthorFollowsResult>(EMPTY_SUBSCRIPTIONS)
|
2024-02-21 14:25:28 +00:00
|
|
|
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...')
|
2024-02-21 14:25:28 +00:00
|
|
|
const result = await apiClient.getAuthorFollows({ user: session()?.user.id })
|
2024-01-31 12:34:15 +00:00
|
|
|
setSubscriptions(result || EMPTY_SUBSCRIPTIONS)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.info('[context.following] cannot get subs', error)
|
|
|
|
} finally {
|
|
|
|
setLoading(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 08:52:39 +00:00
|
|
|
createEffect(() => {
|
2024-03-15 14:57:03 +00:00
|
|
|
console.info('[context.following] subs:', subscriptions)
|
2024-03-12 08:52:39 +00:00
|
|
|
})
|
|
|
|
|
2024-04-25 10:53:51 +00:00
|
|
|
const [subscribeInAction, setSubscribeInAction] = createSignal<SubscribeAction>()
|
2024-01-31 12:34:15 +00:00
|
|
|
const follow = async (what: FollowingEntity, slug: string) => {
|
2024-03-15 14:57:03 +00:00
|
|
|
if (!author()) return
|
|
|
|
setSubscribeInAction({ slug, type: 'subscribe' })
|
2024-01-31 12:34:15 +00:00
|
|
|
try {
|
2024-03-15 14:57:03 +00:00
|
|
|
const subscriptionData = await apiClient.follow({ what, slug })
|
2024-01-31 12:34:15 +00:00
|
|
|
setSubscriptions((prevSubscriptions) => {
|
2024-03-15 14:57:03 +00:00
|
|
|
if (!prevSubscriptions[what]) prevSubscriptions[what] = []
|
|
|
|
prevSubscriptions[what].push(subscriptionData)
|
|
|
|
return prevSubscriptions
|
|
|
|
})
|
2024-01-31 12:34:15 +00:00
|
|
|
} catch (error) {
|
2024-03-15 14:57:03 +00:00
|
|
|
console.error(error)
|
2024-03-11 05:20:00 +00:00
|
|
|
} finally {
|
2024-03-15 14:57:03 +00:00
|
|
|
setSubscribeInAction() // Сбрасываем состояние действия подписки.
|
2024-01-31 12:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const unfollow = async (what: FollowingEntity, slug: string) => {
|
|
|
|
if (!author()) return
|
2024-03-14 06:22:43 +00:00
|
|
|
setSubscribeInAction({ slug: slug, type: 'unsubscribe' })
|
2024-01-31 12:34:15 +00:00
|
|
|
try {
|
|
|
|
await apiClient.unfollow({ what, slug })
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
2024-03-11 05:20:00 +00:00
|
|
|
} finally {
|
|
|
|
setSubscribeInAction()
|
2024-01-31 12:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:59:03 +00:00
|
|
|
createEffect(() => {
|
2024-03-15 12:58:22 +00:00
|
|
|
if (author()) {
|
2024-04-08 12:49:40 +00:00
|
|
|
try {
|
2024-04-17 15:47:24 +00:00
|
|
|
const appdata = session()?.user.app_data
|
|
|
|
if (appdata) {
|
|
|
|
const { authors, followers, topics } = appdata
|
|
|
|
setSubscriptions({ authors, topics })
|
|
|
|
setFollowers(followers)
|
|
|
|
if (!authors) fetchData()
|
|
|
|
}
|
2024-04-08 12:54:01 +00:00
|
|
|
} catch (e) {
|
2024-04-08 12:49:40 +00:00
|
|
|
console.error(e)
|
|
|
|
}
|
2024-03-15 12:58:22 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const value: FollowingContextType = {
|
|
|
|
loading,
|
|
|
|
subscriptions,
|
|
|
|
setSubscriptions,
|
|
|
|
setFollowing,
|
2024-04-08 12:49:40 +00:00
|
|
|
followers,
|
2024-01-31 12:34:15 +00:00
|
|
|
loadSubscriptions: fetchData,
|
|
|
|
follow,
|
|
|
|
unfollow,
|
2024-03-11 05:20:00 +00:00
|
|
|
// followers,
|
|
|
|
subscribeInAction,
|
2024-01-31 12:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return <FollowingContext.Provider value={value}>{props.children}</FollowingContext.Provider>
|
|
|
|
}
|