From 22575cc7fa179ee170ab3415534d746839683b41 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 24 Sep 2024 14:56:16 +0300 Subject: [PATCH] expo-showup --- src/components/Views/Expo/Expo.tsx | 63 ++++++++++++++++-------------- src/routes/expo/[...layout].tsx | 22 +++++++---- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/components/Views/Expo/Expo.tsx b/src/components/Views/Expo/Expo.tsx index 294a9748..fefd6924 100644 --- a/src/components/Views/Expo/Expo.tsx +++ b/src/components/Views/Expo/Expo.tsx @@ -1,6 +1,7 @@ import { A } from '@solidjs/router' import { clsx } from 'clsx' import { For, Show, createEffect, createMemo, createSignal, on, onCleanup, onMount } from 'solid-js' + import { ConditionalWrapper } from '~/components/_shared/ConditionalWrapper' import { LoadMoreItems, LoadMoreWrapper } from '~/components/_shared/LoadMoreWrapper' import { Loading } from '~/components/_shared/Loading' @@ -13,6 +14,8 @@ import getRandomTopShoutsQuery from '~/graphql/query/core/articles-load-random-t import { LoadShoutsFilters, LoadShoutsOptions, Shout } from '~/graphql/schema/core.gen' import { LayoutType } from '~/types/common' import { getUnixtime } from '~/utils/date' +import { restoreScrollPosition, saveScrollPosition } from '~/utils/scroll' +import { byCreated } from '~/utils/sort' import { ArticleCard } from '../../Feed/ArticleCard' import styles from './Expo.module.scss' @@ -33,21 +36,9 @@ export const Expo = (props: Props) => { const [favoriteTopArticles, setFavoriteTopArticles] = createSignal([]) const [reactedTopMonthArticles, setReactedTopMonthArticles] = createSignal([]) - const [expoShouts, setExpoShouts] = createSignal([]) const { feedByLayout, expoFeed, setExpoFeed } = useFeed() const layouts = createMemo(() => (props.layout ? [props.layout] : EXPO_LAYOUTS)) - const loadMoreFiltered = async () => { - const limit = SHOUTS_PER_PAGE - const offset = (props.layout ? feedByLayout()[props.layout] : expoFeed())?.length - const filters: LoadShoutsFilters = { layouts: layouts(), featured: true } - const options: LoadShoutsOptions = { filters, limit, offset } - const shoutsFetcher = loadShouts(options) - const result = await shoutsFetcher() - result && setExpoFeed(result) - return result as LoadMoreItems - } - const loadRandomTopArticles = async () => { const options: LoadShoutsOptions = { filters: { layouts: layouts(), featured: true }, @@ -76,20 +67,15 @@ export const Expo = (props: Props) => { }) createEffect( - on( - () => props.layout, - () => { - setExpoShouts([]) - setFavoriteTopArticles([]) - setReactedTopMonthArticles([]) - loadRandomTopArticles() - loadRandomTopMonthArticles() - } - ) + on(layouts, (lll) => { + console.debug('layouts changed', lll) + loadRandomTopArticles() + loadRandomTopMonthArticles() + }) ) onCleanup(() => { - setExpoShouts([]) + setExpoFeed([]) }) const ExpoTabs = () => (
@@ -134,10 +120,10 @@ export const Expo = (props: Props) => {
) - const ExpoGrid = () => ( + const ExpoGrid = (props: Props) => (
- + {(shout) => (
{ 0} keyed={true}> - + {(shout) => (
{
) + const [loadMoreVisible, setLoadMoreVisible] = createSignal(false) + + // дозагрузка + const loadMore = async () => { + saveScrollPosition() + const limit = SHOUTS_PER_PAGE + const offset = (props.layout ? feedByLayout()[props.layout] : expoFeed())?.length + const filters: LoadShoutsFilters = { layouts: layouts(), featured: true } + const options: LoadShoutsOptions = { filters, limit, offset } + const shoutsFetcher = loadShouts(options) + const result = await shoutsFetcher() + setLoadMoreVisible(Boolean(result?.length)) + const expoFeedUpdater = (layout?: LayoutType) => (prev: Shout[]) => + Array.from(new Set((layout ? prev || [] : expoFeed())?.concat(result || [])))?.sort(byCreated) + result && setExpoFeed(expoFeedUpdater(props.layout)) + restoreScrollPosition() + return result as LoadMoreItems + } + return (
- 0} fallback={}> - - + }> +
diff --git a/src/routes/expo/[...layout].tsx b/src/routes/expo/[...layout].tsx index bc60e0cf..764fc186 100644 --- a/src/routes/expo/[...layout].tsx +++ b/src/routes/expo/[...layout].tsx @@ -1,5 +1,5 @@ import { Params, RouteSectionProps, createAsync } from '@solidjs/router' -import { Show, createEffect, createMemo, on } from 'solid-js' +import { Show, onMount } from 'solid-js' import { TopicsNav } from '~/components/TopicsNav' import { Expo } from '~/components/Views/Expo' import { PageLayout } from '~/components/_shared/PageLayout' @@ -32,9 +32,9 @@ export default (props: RouteSectionProps) => { async () => props.data || (await fetchExpoShouts(props.params.layout ? [props.params.layout] : EXPO_LAYOUTS)) ) - const layout = createMemo(() => props.params.layout) - const title = createMemo(() => { - switch (layout()) { + + const getTitle = (l: string) => { + switch (l) { case 'audio': { return t('Audio') } @@ -51,15 +51,21 @@ export default (props: RouteSectionProps) => { return t('Art') } } + } + + onMount(() => { + document.title = getTitle(props.params.layout || '') }) - createEffect(on(title, (ttl) => (document.title = ttl), { defer: true })) - return ( - + - {(sss) => } + {(sss) => } )