From 58c4d6eae7748cadcc1ab14747c5bf49ed349513 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 8 Apr 2024 15:49:40 +0300 Subject: [PATCH] app-data-author --- src/components/Views/Author/Author.tsx | 26 +++++++++++----- .../ProfileSubscriptions.tsx | 30 ++++++++----------- src/context/following.tsx | 15 ++++++++-- src/context/session.tsx | 14 +++++++-- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/components/Views/Author/Author.tsx b/src/components/Views/Author/Author.tsx index a21cebcc..efd1bb73 100644 --- a/src/components/Views/Author/Author.tsx +++ b/src/components/Views/Author/Author.tsx @@ -38,26 +38,36 @@ const LOAD_MORE_PAGE_SIZE = 9 export const AuthorView = (props: Props) => { const { t } = useLocalize() - const { loadSubscriptions } = useFollowing() + const { subscriptions, followers } = useFollowing() + const { session } = useSession() const { sortedArticles } = useArticlesStore({ shouts: props.shouts }) const { authorEntities } = useAuthorsStore({ authors: [props.author] }) const { page: getPage, searchParams } = useRouter() const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false) const [isBioExpanded, setIsBioExpanded] = createSignal(false) - const [followers, setFollowers] = createSignal([]) + const [author, setAuthor] = createSignal() const [following, setFollowing] = createSignal>([]) const [showExpandBioControl, setShowExpandBioControl] = createSignal(false) const [commented, setCommented] = createSignal() const modal = MODALS[searchParams().m] // current author - const [author, setAuthor] = createSignal() createEffect(() => { - try { - const a = authorEntities()[props.authorSlug] - setAuthor(a) - } catch (error) { - console.debug(error) + if(props.authorSlug) { + if (session()?.user?.app_data?.profile?.slug === props.authorSlug) { + console.info('my own profile') + const {profile, authors, topics} = session().user.app_data + setAuthor(profile) + setFollowing([...authors, ...topics]) + } + } else { + try { + const a = authorEntities()[props.authorSlug] + setAuthor(a) + console.debug('[Author] expecting following data fetched') + } catch (error) { + console.debug(error) + } } }) diff --git a/src/components/Views/ProfileSubscriptions/ProfileSubscriptions.tsx b/src/components/Views/ProfileSubscriptions/ProfileSubscriptions.tsx index 6e1b4d8d..82fe575a 100644 --- a/src/components/Views/ProfileSubscriptions/ProfileSubscriptions.tsx +++ b/src/components/Views/ProfileSubscriptions/ProfileSubscriptions.tsx @@ -1,6 +1,7 @@ import { clsx } from 'clsx' import { For, Show, createEffect, createSignal, onMount } from 'solid-js' +import { useFollowing } from '../../../context/following' import { useLocalize } from '../../../context/localize' import { useSession } from '../../../context/session' import { apiClient } from '../../../graphql/client/core' @@ -20,23 +21,20 @@ import stylesSettings from '../../../styles/FeedSettings.module.scss' export const ProfileSubscriptions = () => { const { t, lang } = useLocalize() - const { author } = useSession() - const [following, setFollowing] = createSignal>([]) - const [filtered, setFiltered] = createSignal>([]) - const [subscriptionFilter, setSubscriptionFilter] = createSignal('all') + const { author, session } = useSession() + const { subscriptions } = useFollowing() + const [following, setFollowing] = (createSignal < Array < Author) | (Topic >> []) + const [filtered, setFiltered] = (createSignal < Array < Author) | (Topic >> []) + const [subscriptionFilter, setSubscriptionFilter] = createSignal < SubscriptionFilter > 'all' const [searchQuery, setSearchQuery] = createSignal('') - const fetchSubscriptions = async () => { - try { - const slug = author()?.slug - const authorFollows = await apiClient.getAuthorFollows({ slug }) - setFollowing([...authorFollows['authors']]) - setFiltered([...authorFollows['authors'], ...authorFollows['topics']]) - } catch (error) { - console.error('[fetchSubscriptions] :', error) - throw error + createEffect(() => { + if (subscriptions()) { + const { authors, topics } = subscriptions() + setFollowing([...authors, ...topics]) + setFiltered([...authors, ...topics]) } - } + }) createEffect(() => { if (following()) { @@ -53,10 +51,6 @@ export const ProfileSubscriptions = () => { } }) - onMount(async () => { - await fetchSubscriptions() - }) - return (
diff --git a/src/context/following.tsx b/src/context/following.tsx index f3208ca9..6ee4621e 100644 --- a/src/context/following.tsx +++ b/src/context/following.tsx @@ -2,12 +2,13 @@ import { Accessor, JSX, createContext, createEffect, createSignal, useContext } import { createStore } from 'solid-js/store' import { apiClient } from '../graphql/client/core' -import { AuthorFollows, FollowingEntity } from '../graphql/schema/core.gen' +import { AuthorFollows, FollowingEntity, Author } from '../graphql/schema/core.gen' import { useSession } from './session' interface FollowingContextType { loading: Accessor + followers: Accessor> subscriptions: AuthorFollows setSubscriptions: (subscriptions: AuthorFollows) => void setFollowing: (what: FollowingEntity, slug: string, value: boolean) => void @@ -31,6 +32,7 @@ const EMPTY_SUBSCRIPTIONS: AuthorFollows = { export const FollowingProvider = (props: { children: JSX.Element }) => { const [loading, setLoading] = createSignal(false) + const [followers, setFollowers] = createSignal>([]) const [subscriptions, setSubscriptions] = createStore(EMPTY_SUBSCRIPTIONS) const { author, session } = useSession() @@ -77,8 +79,14 @@ export const FollowingProvider = (props: { children: JSX.Element }) => { createEffect(() => { if (author()) { - console.debug('[context.following] author update detect') - fetchData() + try { + const { authors, followers, topics } = session().user.app_data + setSubscriptions({ authors, topics }) + setFollowers(followers) + if(!authors) fetchData() + } catch(e) { + console.error(e) + } } }) @@ -116,6 +124,7 @@ export const FollowingProvider = (props: { children: JSX.Element }) => { setSubscriptions, isOwnerSubscribed, setFollowing, + followers, loadSubscriptions: fetchData, follow, unfollow, diff --git a/src/context/session.tsx b/src/context/session.tsx index 73659a6c..aba1ace5 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -199,6 +199,7 @@ export const SessionProvider = (props: { } onCleanup(() => clearTimeout(minuteLater)) + const authorData = async () => { const u = session()?.user return u ? (await apiClient.getAuthorId({ user: u.id.trim() })) || null : null @@ -217,7 +218,15 @@ export const SessionProvider = (props: { apiClient.connect(token) inboxClient.connect(token) } - if (!author()) loadAuthor() + + try { + const { profile } = session().user.app_data + setAuthor(profile) + addAuthors([profile]) + if(!profile) loadAuthor() + } catch(e) { + console.error(e) + } setIsSessionLoaded(true) } @@ -262,8 +271,7 @@ export const SessionProvider = (props: { () => props.onStateChangeCallback, () => { props.onStateChangeCallback(session()) - }, - { defer: true }, + } ), )