2022-09-29 17:37:21 +00:00
|
|
|
import { createLazyMemo } from '@solid-primitives/memo'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { createSignal } from 'solid-js'
|
|
|
|
|
2023-11-28 13:18:25 +00:00
|
|
|
import { apiClient } from '../../graphql/client/core'
|
2023-12-26 20:33:45 +00:00
|
|
|
import { Author, QueryLoad_Authors_ByArgs } from '../../graphql/schema/core.gen'
|
2023-11-28 18:04:51 +00:00
|
|
|
import { byStat } from '../../utils/sortby'
|
2022-09-22 09:37:49 +00:00
|
|
|
|
2022-11-22 09:27:01 +00:00
|
|
|
export type AuthorsSortBy = 'shouts' | 'name' | 'followers'
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2023-12-24 17:29:16 +00:00
|
|
|
const [sortAllBy, setSortAllBy] = createSignal<AuthorsSortBy>('name')
|
2022-10-05 15:56:59 +00:00
|
|
|
|
2022-10-21 18:17:04 +00:00
|
|
|
export const setAuthorsSort = (sortBy: AuthorsSortBy) => setSortAllBy(sortBy)
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
const [authorEntities, setAuthorEntities] = createSignal<{ [authorSlug: string]: Author }>({})
|
|
|
|
const [authorsByTopic, setAuthorsByTopic] = createSignal<{ [topicSlug: string]: Author[] }>({})
|
2022-09-13 09:59:04 +00:00
|
|
|
|
2022-09-29 17:37:21 +00:00
|
|
|
const sortedAuthors = createLazyMemo(() => {
|
2022-09-28 20:16:44 +00:00
|
|
|
const authors = Object.values(authorEntities())
|
|
|
|
switch (sortAllBy()) {
|
2022-11-22 09:27:01 +00:00
|
|
|
case 'followers': {
|
|
|
|
authors.sort(byStat('followers'))
|
2022-09-28 20:16:44 +00:00
|
|
|
break
|
2022-10-07 19:35:53 +00:00
|
|
|
}
|
|
|
|
case 'shouts': {
|
2022-11-22 09:27:01 +00:00
|
|
|
authors.sort(byStat('shouts'))
|
2022-10-05 15:56:59 +00:00
|
|
|
break
|
2022-10-07 19:35:53 +00:00
|
|
|
}
|
|
|
|
case 'name': {
|
2022-09-28 20:16:44 +00:00
|
|
|
authors.sort((a, b) => a.name.localeCompare(b.name))
|
|
|
|
break
|
2022-10-07 19:35:53 +00:00
|
|
|
}
|
2022-09-28 20:16:44 +00:00
|
|
|
}
|
|
|
|
return authors
|
|
|
|
})
|
2022-09-13 13:38:26 +00:00
|
|
|
|
2023-12-25 07:32:53 +00:00
|
|
|
export const addAuthors = (authors: Author[]) => {
|
2023-11-14 15:10:00 +00:00
|
|
|
const newAuthorEntities = authors.filter(Boolean).reduce(
|
|
|
|
(acc, author) => {
|
|
|
|
acc[author.slug] = author
|
|
|
|
return acc
|
|
|
|
},
|
|
|
|
{} as Record<string, Author>,
|
|
|
|
)
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2023-09-01 14:28:50 +00:00
|
|
|
setAuthorEntities((prevAuthorEntities) =>
|
|
|
|
Object.keys(newAuthorEntities).reduce(
|
|
|
|
(acc, authorSlug) => {
|
|
|
|
acc[authorSlug] = {
|
|
|
|
...acc[authorSlug],
|
2023-11-14 15:10:00 +00:00
|
|
|
...newAuthorEntities[authorSlug],
|
2023-09-01 14:28:50 +00:00
|
|
|
}
|
|
|
|
return acc
|
|
|
|
},
|
2023-11-14 15:10:00 +00:00
|
|
|
{ ...prevAuthorEntities },
|
|
|
|
),
|
2023-09-01 14:28:50 +00:00
|
|
|
)
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 21:04:09 +00:00
|
|
|
export const loadAuthor = async ({
|
|
|
|
slug,
|
|
|
|
author_id,
|
|
|
|
}: {
|
|
|
|
slug: string
|
2023-12-27 23:04:12 +00:00
|
|
|
author_id?: number
|
2023-12-27 21:04:09 +00:00
|
|
|
}): Promise<Author> => {
|
|
|
|
const author = await apiClient.getAuthor({ slug, author_id })
|
2022-10-05 15:11:14 +00:00
|
|
|
addAuthors([author])
|
2023-11-23 18:15:06 +00:00
|
|
|
return author
|
2022-10-05 15:11:14 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
export const addAuthorsByTopic = (newAuthorsByTopic: { [topicSlug: string]: Author[] }) => {
|
|
|
|
const allAuthors = Object.values(newAuthorsByTopic).flat()
|
2022-09-13 09:59:04 +00:00
|
|
|
addAuthors(allAuthors)
|
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
setAuthorsByTopic((prevAuthorsByTopic) => {
|
|
|
|
return Object.entries(newAuthorsByTopic).reduce((acc, [topicSlug, authors]) => {
|
2022-09-13 09:59:04 +00:00
|
|
|
if (!acc[topicSlug]) {
|
|
|
|
acc[topicSlug] = []
|
|
|
|
}
|
|
|
|
|
|
|
|
authors.forEach((author) => {
|
|
|
|
if (!acc[topicSlug].some((a) => a.slug === author.slug)) {
|
|
|
|
acc[topicSlug].push(author)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return acc
|
2022-09-28 20:16:44 +00:00
|
|
|
}, prevAuthorsByTopic)
|
|
|
|
})
|
2022-09-13 09:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-24 12:56:30 +00:00
|
|
|
export const loadAllAuthors = async (): Promise<void> => {
|
|
|
|
const authors = await apiClient.getAllAuthors()
|
2022-09-09 11:53:35 +00:00
|
|
|
addAuthors(authors)
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:59:04 +00:00
|
|
|
type InitialState = {
|
|
|
|
authors?: Author[]
|
2022-11-22 09:27:01 +00:00
|
|
|
sortBy?: AuthorsSortBy
|
2022-09-13 09:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 20:33:45 +00:00
|
|
|
export const loadAuthors = async (args: QueryLoad_Authors_ByArgs): Promise<void> => {
|
|
|
|
const authors = await apiClient.loadAuthorsBy(args)
|
2023-12-27 20:40:00 +00:00
|
|
|
console.debug(`[load_authors_by] loaded ${Object.keys(authors).length} authors with stat`)
|
2023-12-24 12:56:30 +00:00
|
|
|
addAuthors(authors)
|
|
|
|
}
|
|
|
|
|
2022-09-23 07:38:48 +00:00
|
|
|
export const useAuthorsStore = (initialState: InitialState = {}) => {
|
2022-11-22 09:27:01 +00:00
|
|
|
if (initialState.sortBy) {
|
|
|
|
setSortAllBy(initialState.sortBy)
|
|
|
|
}
|
2022-09-28 20:16:44 +00:00
|
|
|
addAuthors([...(initialState.authors || [])])
|
2022-09-13 09:59:04 +00:00
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
return { authorEntities, sortedAuthors, authorsByTopic }
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|