webapp/src/components/Views/ProfileSubscriptions/ProfileSubscriptions.tsx

122 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-04-08 15:19:43 +00:00
import { clsx } from 'clsx'
2024-05-20 11:16:54 +00:00
import { For, Show, createEffect, createSignal, on } from 'solid-js'
2024-04-08 15:19:43 +00:00
import { useFollowing } from '../../../context/following'
import { useLocalize } from '../../../context/localize'
import { Author, Topic } from '../../../graphql/schema/core.gen'
2024-05-20 11:16:54 +00:00
import { FollowsFilter } from '../../../pages/types'
2024-04-08 15:19:43 +00:00
import { dummyFilter } from '../../../utils/dummyFilter'
import { isAuthor } from '../../../utils/isAuthor'
import { AuthorBadge } from '../../Author/AuthorBadge'
import { ProfileSettingsNavigation } from '../../Nav/ProfileSettingsNavigation'
import { TopicBadge } from '../../Topic/TopicBadge'
import { Loading } from '../../_shared/Loading'
import { SearchField } from '../../_shared/SearchField'
2024-04-08 15:19:43 +00:00
import styles from '../../../pages/profile/Settings.module.scss'
import stylesSettings from '../../../styles/FeedSettings.module.scss'
export const ProfileSubscriptions = () => {
2024-04-08 15:19:43 +00:00
const { t, lang } = useLocalize()
2024-05-20 11:16:54 +00:00
const { follows } = useFollowing()
const [flatFollows, setFlatFollows] = createSignal<Array<Author | Topic>>([])
2024-04-08 15:19:43 +00:00
const [filtered, setFiltered] = createSignal<Array<Author | Topic>>([])
2024-05-20 11:16:54 +00:00
const [followsFilter, setFollowsFilter] = createSignal<FollowsFilter>('all')
2024-04-08 15:19:43 +00:00
const [searchQuery, setSearchQuery] = createSignal('')
2024-05-20 11:16:54 +00:00
createEffect(() => setFlatFollows([...(follows?.authors || []), ...(follows?.topics || [])]))
createEffect(
on([flatFollows, followsFilter], ([flat, mode]) => {
if (mode === 'authors') {
setFiltered(flat.filter((s) => 'name' in s))
} else if (mode === 'topics') {
setFiltered(flat.filter((s) => 'title' in s))
2024-04-08 13:14:19 +00:00
} else {
2024-05-20 11:16:54 +00:00
setFiltered(flat)
2024-04-08 13:04:10 +00:00
}
2024-05-20 11:16:54 +00:00
}),
{ defer: true },
)
createEffect(() => {
if (searchQuery()) {
2024-05-20 11:16:54 +00:00
setFiltered(dummyFilter(flatFollows(), searchQuery(), lang()))
}
2024-04-08 15:19:43 +00:00
})
return (
<div class="wide-container">
<div class="row">
<div class="col-md-5">
2024-04-08 15:19:43 +00:00
<div class={clsx('left-navigation', styles.leftNavigation)}>
<ProfileSettingsNavigation />
</div>
</div>
<div class="col-md-19">
<div class="row">
<div class="col-md-20 col-lg-18 col-xl-16">
2024-04-08 15:19:43 +00:00
<h1>{t('My subscriptions')}</h1>
<p class="description">{t('Here you can manage all your Discours subscriptions')}</p>
2024-05-20 11:16:54 +00:00
<Show when={flatFollows()} fallback={<Loading />}>
<ul class="view-switcher">
2024-04-08 14:48:58 +00:00
<li
class={clsx({
2024-05-20 11:16:54 +00:00
'view-switcher__item--selected': followsFilter() === 'all',
2024-04-08 14:48:58 +00:00
})}
>
2024-05-20 11:16:54 +00:00
<button type="button" onClick={() => setFollowsFilter('all')}>
2024-04-08 15:19:43 +00:00
{t('All')}
</button>
</li>
2024-04-08 14:48:58 +00:00
<li
class={clsx({
2024-05-20 11:16:54 +00:00
'view-switcher__item--selected': followsFilter() === 'authors',
2024-04-08 14:48:58 +00:00
})}
>
2024-05-20 11:16:54 +00:00
<button type="button" onClick={() => setFollowsFilter('authors')}>
2024-04-08 15:19:43 +00:00
{t('Authors')}
</button>
</li>
2024-04-08 14:48:58 +00:00
<li
class={clsx({
2024-05-20 11:16:54 +00:00
'view-switcher__item--selected': followsFilter() === 'topics',
2024-04-08 14:48:58 +00:00
})}
>
2024-05-20 11:16:54 +00:00
<button type="button" onClick={() => setFollowsFilter('topics')}>
2024-04-08 15:19:43 +00:00
{t('Topics')}
</button>
</li>
</ul>
2024-04-08 15:19:43 +00:00
<div class={clsx('pretty-form__item', styles.searchContainer)}>
<SearchField
onChange={(value) => setSearchQuery(value)}
class={styles.searchField}
variant="bordered"
/>
</div>
2024-04-08 15:19:43 +00:00
<div class={clsx(stylesSettings.settingsList, styles.topicsList)}>
<For each={filtered()}>
{(followingItem) => (
<div>
{isAuthor(followingItem) ? (
2024-05-20 11:16:54 +00:00
<AuthorBadge minimize={true} author={followingItem} />
) : (
2024-05-20 11:16:54 +00:00
<TopicBadge minimize={true} topic={followingItem} />
)}
</div>
)}
</For>
</div>
</Show>
</div>
</div>
</div>
</div>
</div>
2024-04-08 15:19:43 +00:00
)
}