webapp/src/components/Views/AllAuthors.tsx

141 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-10-07 19:35:53 +00:00
import { createEffect, createMemo, For, Show } from 'solid-js'
2022-09-09 11:53:35 +00:00
import type { Author } from '../../graphql/types.gen'
import { AuthorCard } from '../Author/Card'
2022-09-22 09:37:49 +00:00
import { Icon } from '../Nav/Icon'
2022-09-09 11:53:35 +00:00
import { t } from '../../utils/intl'
2022-10-21 18:17:04 +00:00
import { useAuthorsStore, setAuthorsSort } from '../../stores/zine/authors'
2022-09-22 09:37:49 +00:00
import { handleClientRouteLinkClick, useRouter } from '../../stores/router'
2022-09-30 14:22:33 +00:00
import { useAuthStore } from '../../stores/auth'
2022-11-09 19:02:12 +00:00
import styles from '../../styles/AllTopics.module.scss'
import { clsx } from 'clsx'
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
type AllAuthorsPageSearchParams = {
by: '' | 'name' | 'shouts' | 'rating'
}
type Props = {
authors: Author[]
}
export const AllAuthorsView = (props: Props) => {
2022-10-05 11:13:15 +00:00
const { sortedAuthors } = useAuthorsStore({ authors: props.authors })
2022-09-30 14:22:33 +00:00
const { session } = useAuthStore()
2022-10-05 15:56:59 +00:00
createEffect(() => {
2022-10-21 18:17:04 +00:00
setAuthorsSort(searchParams().by || 'shouts')
2022-10-05 15:56:59 +00:00
})
2022-10-04 12:16:07 +00:00
const subscribed = (s) => Boolean(session()?.news?.authors && session()?.news?.authors?.includes(s || ''))
2022-09-14 11:28:43 +00:00
2022-10-21 18:17:04 +00:00
const { searchParams } = useRouter<AllAuthorsPageSearchParams>()
2022-09-14 11:28:43 +00:00
2022-10-05 15:56:59 +00:00
const byLetter = createMemo<{ [letter: string]: Author[] }>(() => {
return sortedAuthors().reduce((acc, author) => {
2022-10-19 14:26:49 +00:00
if (!author.name) {
// name === null for new users
return acc
}
2022-10-05 17:26:47 +00:00
const letter = author.name[0].toUpperCase()
2022-10-05 15:56:59 +00:00
if (!acc[letter]) {
acc[letter] = []
}
acc[letter].push(author)
return acc
}, {} as { [letter: string]: Author[] })
})
const sortedKeys = createMemo<string[]>(() => {
const keys = Object.keys(byLetter())
keys.sort()
return keys
2022-10-05 11:13:15 +00:00
})
2022-10-05 15:56:59 +00:00
// log.debug(getSearchParams())
2022-09-09 11:53:35 +00:00
return (
2022-11-11 10:22:07 +00:00
<div class={clsx(styles.allTopicsPage, 'container')}>
2022-09-28 20:16:44 +00:00
<Show when={sortedAuthors().length > 0}>
2022-11-11 10:22:07 +00:00
<div class="shift-content">
<div class="row">
<div class={clsx(styles.pageHeader, 'col-lg-10 col-xl-8')}>
<h1>{t('Authors')}</h1>
<p>{t('Subscribe who you like to tune your personal feed')}</p>
2022-09-09 11:53:35 +00:00
</div>
2022-11-11 10:22:07 +00:00
</div>
<div class="row">
<div class="col-lg-10 col-xl-8">
<ul class={clsx(styles.viewSwitcher, 'view-switcher')}>
<li classList={{ selected: searchParams().by === 'shouts' }}>
<a href="/authors?by=shouts" onClick={handleClientRouteLinkClick}>
{t('By shouts')}
</a>
</li>
<li classList={{ selected: searchParams().by === 'rating' }}>
<a href="/authors?by=rating" onClick={handleClientRouteLinkClick}>
{t('By rating')}
</a>
</li>
<li classList={{ selected: !searchParams().by || searchParams().by === 'name' }}>
<a href="/authors" onClick={handleClientRouteLinkClick}>
{t('By alphabet')}
</a>
</li>
<li class="view-switcher__search">
<a href="/authors/search">
<Icon name="search" />
{t('Search author')}
</a>
</li>
</ul>
<Show
when={!searchParams().by || searchParams().by === 'name'}
fallback={() => (
<div class={styles.stats}>
<For each={sortedAuthors()}>
{(author) => (
<AuthorCard
author={author}
compact={false}
hasLink={true}
subscribed={subscribed(author.slug)}
noSocialButtons={true}
isAuthorsList={true}
/>
)}
</For>
</div>
)}
>
<For each={sortedKeys()}>
{(letter) => (
<div class={clsx(styles.group, 'group')}>
<h2>{letter}</h2>
<div class="container">
<div class="row">
<For each={byLetter()[letter]}>
{(author: Author) => (
<div class={clsx(styles.topic, 'topic col-sm-6 col-md-3')}>
<div class="topic-title">
<a href={`/author/${author.slug}`}>{author.name}</a>
2022-09-09 11:53:35 +00:00
</div>
2022-11-11 10:22:07 +00:00
</div>
)}
</For>
2022-09-09 11:53:35 +00:00
</div>
</div>
2022-11-11 10:22:07 +00:00
</div>
)}
</For>
</Show>
2022-09-09 11:53:35 +00:00
</div>
</div>
</div>
</Show>
</div>
)
}