From 0c0084365d7bd5f90ac31e092dc956ea9b030f2c Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Wed, 16 Nov 2022 09:33:58 +0300 Subject: [PATCH] wip-refactoring --- src/components/Pages/LayoutShoutsPage.tsx | 15 ++-- src/components/Views/Article.tsx | 20 ++++-- src/components/Views/Feed.tsx | 64 +++++++++-------- src/pages/[...slug].astro | 2 +- src/pages/author/[slug]/index.astro | 2 +- src/pages/search.astro | 2 +- src/pages/topic/[slug].astro | 2 +- src/stores/zine/layouts.ts | 76 +++++--------------- src/stores/zine/reactions.ts | 87 ++++++++++------------- src/utils/apiClient.ts | 27 ++++--- 10 files changed, 134 insertions(+), 163 deletions(-) diff --git a/src/components/Pages/LayoutShoutsPage.tsx b/src/components/Pages/LayoutShoutsPage.tsx index d2eabce8..84052e2c 100644 --- a/src/components/Pages/LayoutShoutsPage.tsx +++ b/src/components/Pages/LayoutShoutsPage.tsx @@ -1,9 +1,9 @@ import { PageWrap } from '../_shared/PageWrap' import type { PageProps } from '../types' import { createMemo, createSignal, For, onCleanup, onMount, Show } from 'solid-js' -import { resetSortedArticles } from '../../stores/zine/articles' +import { loadShoutsBy, resetSortedArticles } from '../../stores/zine/articles' import { useRouter } from '../../stores/router' -import { LayoutType, loadRecentLayoutShouts, useLayoutsStore } from '../../stores/zine/layouts' +import { LayoutType, useLayoutsStore } from '../../stores/zine/layouts' import { Loading } from '../Loading' import { restoreScrollPosition, saveScrollPosition } from '../../utils/scroll' import type { Shout } from '../../graphql/types.gen' @@ -28,14 +28,13 @@ export const LayoutShoutsPage = (props: PageProps) => { return page.params.layout as LayoutType }) const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false) - const { sortedLayoutShouts } = useLayoutsStore(layout(), props.shouts) + const { sortedLayoutShouts, loadLayoutShoutsBy } = useLayoutsStore(layout(), props.shouts) const sortedArticles = createMemo(() => sortedLayoutShouts().get(layout()) || []) const loadMoreLayout = async (kind: LayoutType) => { saveScrollPosition() - - const { hasMore } = await loadRecentLayoutShouts({ - layout: kind, - amount: LOAD_MORE_PAGE_SIZE, + const { hasMore } = await loadLayoutShoutsBy({ + by: { layout: kind }, + limit: LOAD_MORE_PAGE_SIZE, offset: sortedArticles().length }) setIsLoadMoreButtonVisible(hasMore) @@ -63,7 +62,7 @@ export const LayoutShoutsPage = (props: PageProps) => { onMount(async () => { if (!isLoaded()) { - await loadRecentLayoutShouts({ layout: layout(), amount: PRERENDERED_ARTICLES_COUNT, offset: 0 }) + await loadShoutsBy({ by: { layout: layout() }, limit: PRERENDERED_ARTICLES_COUNT, offset: 0 }) } }) diff --git a/src/components/Views/Article.tsx b/src/components/Views/Article.tsx index 7be606eb..f5d729c5 100644 --- a/src/components/Views/Article.tsx +++ b/src/components/Views/Article.tsx @@ -1,13 +1,14 @@ import { createEffect, createSignal, Show, Suspense } from 'solid-js' import { FullArticle } from '../Article/FullArticle' import { t } from '../../utils/intl' -import type { Shout } from '../../graphql/types.gen' -import { loadArticleReactions, useReactionsStore } from '../../stores/zine/reactions' +import type { Shout, Reaction } from '../../graphql/types.gen' +import { useReactionsStore } from '../../stores/zine/reactions' import '../../styles/Article.scss' interface ArticlePageProps { article: Shout + reactions?: Reaction[] } const ARTICLE_COMMENTS_PAGE_SIZE = 50 @@ -15,13 +16,20 @@ const ARTICLE_COMMENTS_PAGE_SIZE = 50 export const ArticleView = (props: ArticlePageProps) => { const [getCommentsPage] = createSignal(1) const [getIsCommentsLoading, setIsCommentsLoading] = createSignal(false) - const reactionslist = useReactionsStore() + const { + reactionsByShout, + sortedReactions, + createReaction, + updateReaction, + deleteReaction, + loadReactionsBy + } = useReactionsStore({ reactions: props.reactions }) createEffect(async () => { try { setIsCommentsLoading(true) - await loadArticleReactions({ - articleSlug: props.article.slug, + await loadReactionsBy({ + by: { shout: props.article.slug }, limit: ARTICLE_COMMENTS_PAGE_SIZE, offset: getCommentsPage() * ARTICLE_COMMENTS_PAGE_SIZE }) @@ -36,7 +44,7 @@ export const ArticleView = (props: ArticlePageProps) => { r.shout.slug === props.article.slug)} + reactions={reactionsByShout()[props.article.slug]} isCommentsLoading={getIsCommentsLoading()} /> diff --git a/src/components/Views/Feed.tsx b/src/components/Views/Feed.tsx index 62681044..d6ef4795 100644 --- a/src/components/Views/Feed.tsx +++ b/src/components/Views/Feed.tsx @@ -1,56 +1,41 @@ -import { createMemo, createSignal, For, onMount, Show } from 'solid-js' +import { createEffect, createSignal, For, onMount, Show } from 'solid-js' import '../../styles/Feed.scss' import stylesBeside from '../../components/Feed/Beside.module.scss' import { Icon } from '../_shared/Icon' -import { byCreated, sortBy } from '../../utils/sortby' import { TopicCard } from '../Topic/Card' import { ArticleCard } from '../Feed/Card' import { AuthorCard } from '../Author/Card' import { t } from '../../utils/intl' import { FeedSidebar } from '../Feed/Sidebar' import CommentCard from '../Article/Comment' -import { loadShoutsBy, useArticlesStore } from '../../stores/zine/articles' +import { useArticlesStore } from '../../stores/zine/articles' import { useReactionsStore } from '../../stores/zine/reactions' import { useAuthorsStore } from '../../stores/zine/authors' import { useTopicsStore } from '../../stores/zine/topics' import { useTopAuthorsStore } from '../../stores/zine/topAuthors' import { useSession } from '../../context/session' +import { Collab, ReactionKind, Shout } from '../../graphql/types.gen' +import { collabs, setCollabs } from '../../stores/editor' -// const AUTHORSHIP_REACTIONS = [ -// ReactionKind.Accept, -// ReactionKind.Reject, -// ReactionKind.Propose, -// ReactionKind.Ask -// ] +const AUTHORSHIP_REACTIONS = [ + ReactionKind.Accept, + ReactionKind.Reject, + ReactionKind.Propose, + ReactionKind.Ask +] export const FEED_PAGE_SIZE = 20 export const FeedView = () => { // state - const { sortedArticles } = useArticlesStore() - const reactions = useReactionsStore() + const { sortedArticles, loadShoutsBy } = useArticlesStore() + const { sortedReactions: topComments, loadReactionsBy } = useReactionsStore({}) const { sortedAuthors } = useAuthorsStore() const { topTopics } = useTopicsStore() const { topAuthors } = useTopAuthorsStore() const { session } = useSession() - - const topReactions = createMemo(() => sortBy(reactions(), byCreated)) - const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false) - // const expectingFocus = createMemo(() => { - // // 1 co-author notifications needs - // // TODO: list of articles where you are co-author - // // TODO: preload proposals - // // TODO: (maybe?) and changes history - // console.debug(reactions().filter((r) => r.kind in AUTHORSHIP_REACTIONS)) - // - // // 2 community self-regulating mechanics - // // TODO: query all new posts to be rated for publishing - // // TODO: query all reactions where user is in authors list - // return [] - // }) - const loadMore = async () => { const { hasMore } = await loadShoutsBy({ by: { visibility: 'community' }, @@ -60,8 +45,27 @@ export const FeedView = () => { setIsLoadMoreButtonVisible(hasMore) } - onMount(() => { - loadMore() + onMount(async () => { + // load 5 recent comments overall + await loadReactionsBy({ by: {}, limit: 5, offset: 0 }) + + // load recent shouts not only published ( visibility = community ) + await loadMore() + + // TODO: load collabs + // await loadCollabs() + + // load recent editing shouts ( visibility = authors ) + const userslug = session().user.slug + await loadShoutsBy({ by: { author: userslug, visibility: 'authors' }, limit: 15, offset: 0 }) + const collaborativeShouts = sortedArticles().filter((s: Shout, n: number, arr: Shout[]) => { + if (s.visibility !== 'authors') { + arr.splice(n, 1) + return arr + } + }) + // load recent reactions on collabs + await loadReactionsBy({ by: { shouts: [...collaborativeShouts], body: true }, limit: 5, offset: 0 }) }) return ( @@ -122,7 +126,7 @@ export const FeedView = () => {