Files
Folo/apps/renderer/src/modules/entry-column/index.tsx
T
edd4f9e5a6 feat: mobile design (#1568)
* init

Signed-off-by: Innei <i@innei.in>

* feat: feed column

Signed-off-by: Innei <i@innei.in>

* feat: float bar

Signed-off-by: Innei <tukon479@gmail.com>

* feat: profile button

Signed-off-by: Innei <tukon479@gmail.com>

* chore: comment

Signed-off-by: Innei <tukon479@gmail.com>

* feat: layout

Signed-off-by: Innei <tukon479@gmail.com>

* feat: split mobile entry content

Signed-off-by: Innei <tukon479@gmail.com>

* feat: add history stack

Signed-off-by: Innei <tukon479@gmail.com>

* feat: return back

Signed-off-by: Innei <tukon479@gmail.com>

* fix: return back position

Signed-off-by: Innei <tukon479@gmail.com>

* feat: entry list header

Signed-off-by: Innei <tukon479@gmail.com>

* feat: sync selector

Signed-off-by: Innei <tukon479@gmail.com>

* feat: modal stack mobile

Signed-off-by: Innei <tukon479@gmail.com>

* fix: modal selector

Signed-off-by: Innei <tukon479@gmail.com>

* cleanup

Signed-off-by: Innei <tukon479@gmail.com>

* fix: sheet and optimize entry readers

Signed-off-by: Innei <tukon479@gmail.com>

* fix: scroller

Signed-off-by: Innei <tukon479@gmail.com>

* feat: entry contents

Signed-off-by: Innei <tukon479@gmail.com>

* feat: entry content

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* feat: user profile sheet

Signed-off-by: Innei <tukon479@gmail.com>

* fix: entry content header

Signed-off-by: Innei <tukon479@gmail.com>

* fix: entry header

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

* fix: return back

Signed-off-by: Innei <tukon479@gmail.com>

* feat: image view

Signed-off-by: Innei <tukon479@gmail.com>

* feat: audio

Signed-off-by: Innei <tukon479@gmail.com>

* feat: inline player

Signed-off-by: Innei <tukon479@gmail.com>

* feat: pull to refresh

Signed-off-by: Innei <tukon479@gmail.com>

* cleanup

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

* chore: fix import, remove no support display

* chore: auto-fix linting and formatting issues

* feat: achievement modal in mobile

Signed-off-by: Innei <tukon479@gmail.com>

* fix: subview mobile

Signed-off-by: Innei <tukon479@gmail.com>

* feat: power

Signed-off-by: Innei <tukon479@gmail.com>

* feat: wallet mobile design

Signed-off-by: Innei <tukon479@gmail.com>

* feat: pwa support (#1575)

Co-authored-by: hyoban <hyoban@users.noreply.github.com>

* chore: auto-fix linting and formatting issues

* fix import

* discover

* fix: border color

Signed-off-by: Innei <tukon479@gmail.com>

* fix: modal async

Signed-off-by: Innei <tukon479@gmail.com>

* feat: mobile setting

Signed-off-by: Innei <tukon479@gmail.com>

* fix: entry header width

Signed-off-by: Innei <tukon479@gmail.com>

* fix: remove unused hide filter

* feat: settings

Signed-off-by: Innei <tukon479@gmail.com>

* feat: setting done

Signed-off-by: Innei <tukon479@gmail.com>

* action card

* fix discover feed form

* popular filter

* inbox table

* fix: picture masonry pull to refresh

Signed-off-by: Innei <tukon479@gmail.com>

* fix: scroll element

Signed-off-by: Innei <tukon479@gmail.com>

* fix: radio in mobile

Signed-off-by: Innei <tukon479@gmail.com>

* feat: to scientific notation

Signed-off-by: Innei <tukon479@gmail.com>

* fix: login

Signed-off-by: Innei <tukon479@gmail.com>

* ResponsiveSelect for action card

* chore: remove theme color

Signed-off-by: Innei <tukon479@gmail.com>

* type check

* type check

* fix: setting import circular

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: remove hijack pushState

Signed-off-by: Innei <tukon479@gmail.com>

* fix: float bar

Signed-off-by: Innei <tukon479@gmail.com>

* fix: scroll end trigger

Signed-off-by: Innei <tukon479@gmail.com>

* fix: dark theme color

Signed-off-by: Innei <tukon479@gmail.com>

* fix: overscroll-behavior only in destop

Signed-off-by: Innei <tukon479@gmail.com>

* fix: ux

Signed-off-by: Innei <tukon479@gmail.com>

* fix: color

Signed-off-by: Innei <tukon479@gmail.com>

* fix: list scroll area

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* merge

* update

* update favicon link

* fix: reload prompt on mobile

* test

Signed-off-by: Innei <i@innei.in>

* test

Signed-off-by: Innei <i@innei.in>

* fix: ios

Signed-off-by: Innei <i@innei.in>

* try patch precaching

* try

* try

* build

* try

* try

* feat: ctx menu on safari

Signed-off-by: Innei <tukon479@gmail.com>

* try

* clear timeout

Signed-off-by: Innei <tukon479@gmail.com>

* try

* fix

* deny og

* fix patch

* pre cache exclude

* feat: add all

Signed-off-by: Innei <tukon479@gmail.com>

* i18n

Signed-off-by: Innei <tukon479@gmail.com>

* feat: slide

Signed-off-by: Innei <tukon479@gmail.com>

* feat: ico

Signed-off-by: Innei <tukon479@gmail.com>

* fix: build

Signed-off-by: Innei <tukon479@gmail.com>

* test network first

Signed-off-by: Innei <tukon479@gmail.com>

* test

Signed-off-by: Innei <tukon479@gmail.com>

* fix: icon padding

* test

Signed-off-by: Innei <tukon479@gmail.com>

* use react-ios-pwa-prompt

* fix: icon path

* update

* fix: prompt

Signed-off-by: Innei <tukon479@gmail.com>

* feat: redesign toc

Signed-off-by: Innei <tukon479@gmail.com>

* ctx

Signed-off-by: Innei <tukon479@gmail.com>

* ready to prod

Signed-off-by: Innei <tukon479@gmail.com>

* feat: startup screen

Signed-off-by: Innei <tukon479@gmail.com>

* feat: floatbar

Signed-off-by: Innei <tukon479@gmail.com>

* fix: switch view

Signed-off-by: Innei <tukon479@gmail.com>

* fix: keep scroll map

Signed-off-by: Innei <tukon479@gmail.com>

* enable hoverOnlyWhenSupported

* fix: count

Signed-off-by: Innei <tukon479@gmail.com>

* fix: hoverOnlyWhenSupported only in web

Signed-off-by: Innei <tukon479@gmail.com>

* fix

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

---------

Signed-off-by: Innei <i@innei.in>
Signed-off-by: Innei <tukon479@gmail.com>
Co-authored-by: Innei <Innei@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: hyoban <hyoban@users.noreply.github.com>
Co-authored-by: lawvs <18554747+lawvs@users.noreply.github.com>
Co-authored-by: lawvs <lawvs@users.noreply.github.com>
2024-11-22 19:32:39 +08:00

409 lines
12 KiB
TypeScript

import { useMobile } from "@follow/components/hooks/useMobile.js"
import { Button } from "@follow/components/ui/button/index.js"
import { FeedViewType, views } from "@follow/constants"
import { useTitle, useTypeScriptHappyCallback } from "@follow/hooks"
import type { FeedModel } from "@follow/models/types"
import { isBizId } from "@follow/utils/utils"
import type { FunctionComponent } from "react"
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react"
import { useTranslation } from "react-i18next"
import type {
ScrollSeekConfiguration,
VirtuosoGridProps,
VirtuosoHandle,
VirtuosoProps,
} from "react-virtuoso"
import { VirtuosoGrid } from "react-virtuoso"
import { useGeneralSettingKey } from "~/atoms/settings/general"
import { setUISetting, useUISettingKey } from "~/atoms/settings/ui"
import { FeedFoundCanBeFollowError } from "~/components/errors/FeedFoundCanBeFollowErrorFallback"
import { FeedNotFound } from "~/components/errors/FeedNotFound"
import { ReactVirtuosoItemPlaceholder } from "~/components/ui/placeholder"
import { FEED_COLLECTION_LIST, ROUTE_FEED_PENDING } from "~/constants"
import { ENTRY_COLUMN_LIST_SCROLLER_ID } from "~/constants/dom"
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { useRouteParams, useRouteParamsSelector } from "~/hooks/biz/useRouteParams"
import { useFeed } from "~/queries/feed"
import { entryActions, getEntry, useEntry } from "~/store/entry"
import { useFeedById, useFeedHeaderTitle } from "~/store/feed"
import { useSubscriptionByFeedId } from "~/store/subscription"
import { useEntriesByView, useEntryMarkReadHandler } from "./hooks"
import { useSnapEntryIdList } from "./hooks/useEntryIdListSnap"
import { EntryItem, EntryItemSkeleton } from "./item"
import { PictureMasonry } from "./Items/picture-masonry"
import { EntryListHeader } from "./layouts/EntryListHeader"
import { EntryEmptyList, EntryList, EntryListContent } from "./lists"
import { girdClassNames } from "./styles"
import { EntryColumnWrapper } from "./wrapper"
const scrollSeekConfiguration: ScrollSeekConfiguration = {
enter: (velocity) => Math.abs(velocity) > 1000,
exit: (velocity) => Math.abs(velocity) < 1000,
}
const prevScrollTopMap = {}
export type VirtuosoComponentProps = { onlyShowArchivedButton: boolean }
type VirtuosoComponentPropsContext = { context?: VirtuosoComponentProps }
function EntryColumnImpl() {
const { t } = useTranslation()
const virtuosoRef = useRef<VirtuosoHandle>(null)
const [isArchived, setIsArchived] = useState(false)
const unreadOnly = useGeneralSettingKey("unreadOnly")
const entries = useEntriesByView({
onReset: useCallback(() => {
virtuosoRef.current?.scrollTo({
top: 0,
})
}, []),
isArchived,
})
const { entriesIds, isFetchingNextPage, groupedCounts } = entries
useSnapEntryIdList(entriesIds)
const prevEntriesIdsRef = useRef(entriesIds)
useEffect(() => {
if (entriesIds.length > 0) {
prevEntriesIdsRef.current = entriesIds
}
}, [entriesIds])
const {
entryId: activeEntryId,
view,
feedId: routeFeedId,
isPendingEntry,
isCollection,
inboxId,
listId,
} = useRouteParams()
useEffect(() => {
setIsArchived(false)
}, [view, routeFeedId])
const activeEntry = useEntry(activeEntryId)
const feed = useFeedById(routeFeedId)
const title = useFeedHeaderTitle()
useTitle(title)
useEffect(() => {
if (!activeEntryId) return
if (isCollection || isPendingEntry) return
const feedId = activeEntry?.feedId
if (!feedId) return
entryActions.markRead({ feedId, entryId: activeEntryId, read: true })
}, [activeEntry?.feedId, activeEntryId, isCollection, isPendingEntry])
const isInteracted = useRef(false)
const handleMarkReadInRange = useEntryMarkReadHandler(entriesIds)
useEffect(() => {
if (isArchived) {
if (entries.hasNextPage) {
entries.fetchNextPage()
} else {
entries.refetch()
}
}
}, [isArchived])
// Common conditions for both showArchivedButton and shouldLoadArchivedEntries
const commonConditions =
!isArchived && !unreadOnly && !isCollection && routeFeedId !== ROUTE_FEED_PENDING
// Determine if the archived button should be shown
const showArchivedButton = commonConditions && entries.totalCount < 40 && feed?.type === "feed"
const hasNoEntries = entries.queryTotalCount === 0 && !entries.isLoading
// Determine if archived entries should be loaded
const shouldLoadArchivedEntries =
commonConditions && (feed?.type === "feed" || !feed) && !inboxId && !listId && hasNoEntries
// automatically fetch archived entries when there is no entries in timeline
useEffect(() => {
if (shouldLoadArchivedEntries) {
setIsArchived(true)
}
}, [shouldLoadArchivedEntries])
const finalEntriesIds =
hasNoEntries && !isArchived ? prevEntriesIdsRef.current || entriesIds : entriesIds
const scrollRef = useRef<HTMLDivElement>(null)
const handleScroll = useCallback(
(e: React.UIEvent<HTMLDivElement>) => {
if (!isInteracted.current) {
isInteracted.current = true
}
const { scrollTop } = e.currentTarget
if (!routeFeedId) return
prevScrollTopMap[routeFeedId] = scrollTop
},
[routeFeedId],
)
const virtuosoOptions = {
components: {
List: EntryListContent,
Footer: useCallback(
({ context }: VirtuosoComponentPropsContext) => {
if (!isFetchingNextPage) {
if (showArchivedButton) {
return (
<div className="flex justify-center py-4">
<Button variant="outline" onClick={() => setIsArchived(true)}>
{t("words.load_archived_entries")}
</Button>
</div>
)
} else {
return null
}
} else {
if (context?.onlyShowArchivedButton) return null
return (
<EntryItemSkeleton
view={view}
count={Math.min(
entries.data?.pages?.[0].data?.length || 20,
entries.data?.pages.at(-1)?.remaining || 20,
)}
/>
)
}
},
[isFetchingNextPage, showArchivedButton, t, view, entries.data?.pages],
),
ScrollSeekPlaceholder: useCallback(() => <EntryItemSkeleton view={view} count={1} />, [view]),
},
scrollSeekConfiguration,
rangeChanged: (...args: any[]) => {
handleMarkReadInRange &&
// @ts-expect-error
handleMarkReadInRange(...args, isInteracted.current)
},
customScrollParent: scrollRef.current!,
initialScrollTop: prevScrollTopMap[routeFeedId || ""] || 0,
totalCount: entries.totalCount,
endReached: useCallback(async () => {
if (!entries.isFetchingNextPage) {
const remaining = entries.data?.pages.at(-1)?.remaining
if (entries.hasNextPage && remaining) {
await entries.fetchNextPage()
}
}
}, [entries]),
data: finalEntriesIds,
onScroll: handleScroll,
itemContent: useTypeScriptHappyCallback(
(_, entryId) => {
if (!entryId) return <ReactVirtuosoItemPlaceholder />
return <EntryItem key={entryId} entryId={entryId} view={view} />
},
[view],
),
} satisfies VirtuosoProps<string, VirtuosoComponentProps>
const navigate = useNavigateEntry()
const isRefreshing = entries.isFetching && !entries.isFetchingNextPage
const isMobile = useMobile()
return (
<div
data-hide-in-print
className="relative flex h-full flex-1 flex-col @container"
onClick={
isMobile
? undefined
: () =>
navigate({
entryId: null,
})
}
data-total-count={virtuosoOptions.totalCount}
>
{virtuosoOptions.totalCount === 0 &&
!entries.isLoading &&
!entries.error &&
feed?.type === "feed" && <AddFeedHelper />}
<EntryListHeader
refetch={entries.refetch}
isRefreshing={isRefreshing}
totalCount={virtuosoOptions.totalCount}
hasUpdate={entries.hasUpdate}
/>
<EntryColumnWrapper
onScroll={handleScroll}
onPullToRefresh={entries.refetch}
key={`${routeFeedId}-${view}`}
>
{virtuosoOptions.totalCount === 0 && !showArchivedButton ? (
entries.isLoading ? null : (
<EntryEmptyList />
)
) : view && views[view].gridMode ? (
<ListGird
virtuosoOptions={virtuosoOptions}
virtuosoRef={virtuosoRef}
hasNextPage={entries.hasNextPage}
/>
) : (
<EntryList
{...virtuosoOptions}
virtuosoRef={virtuosoRef}
refetch={entries.refetch}
groupCounts={groupedCounts}
/>
)}
</EntryColumnWrapper>
</div>
)
}
const ListGird = ({
virtuosoOptions,
virtuosoRef,
hasNextPage,
}: {
virtuosoOptions: Omit<
VirtuosoGridProps<string, VirtuosoComponentProps>,
"data" | "endReached"
> & {
data: string[]
endReached: () => Promise<any>
}
virtuosoRef: React.RefObject<VirtuosoHandle>
hasNextPage: boolean
}) => {
const isMobile = useMobile()
const masonry = useUISettingKey("pictureViewMasonry") || isMobile
const view = useRouteParamsSelector((s) => s.view)
const feedId = useRouteParamsSelector((s) => s.feedId)
const filterNoImage = useUISettingKey("pictureViewFilterNoImage")
const nextData = useMemo(() => {
if (filterNoImage) {
return virtuosoOptions.data.filter((entryId) => {
const entry = getEntry(entryId)
return entry?.entries.media?.length && entry.entries.media.length > 0
})
}
return virtuosoOptions.data
}, [virtuosoOptions.data, filterNoImage])
const { t } = useTranslation()
if (nextData.length === 0 && virtuosoOptions.data.length > 0) {
return (
<div className="center absolute inset-x-0 inset-y-12 -translate-y-12 flex-col gap-5">
<i className="i-mgc-photo-album-cute-fi size-12" />
<div className="text-sm text-muted-foreground">
{t("entry_column.filtered_content_tip")}
</div>
<Button
onClick={() => {
setUISetting("pictureViewFilterNoImage", false)
}}
>
Show All
</Button>
</div>
)
}
const hasFilteredContent = nextData.length < virtuosoOptions.data.length
const FilteredContentTip = hasFilteredContent && !hasNextPage && (
<div className="center mb-6 flex flex-col gap-5">
<i className="i-mgc-photo-album-cute-fi size-12" />
<div>{t("entry_column.filtered_content_tip_2")}</div>
<Button
onClick={() => {
setUISetting("pictureViewFilterNoImage", false)
}}
>
Show All
</Button>
</div>
)
if (masonry && view === FeedViewType.Pictures) {
return (
<>
<PictureMasonry
key={feedId}
hasNextPage={virtuosoOptions.totalCount! > virtuosoOptions.data.length}
endReached={virtuosoOptions.endReached}
data={nextData}
/>
{virtuosoOptions.components?.Footer &&
(virtuosoOptions.components.Footer as FunctionComponent<VirtuosoComponentPropsContext>)({
context: { onlyShowArchivedButton: true },
})}
{FilteredContentTip}
</>
)
}
return (
<>
<VirtuosoGrid
id={ENTRY_COLUMN_LIST_SCROLLER_ID}
listClassName={girdClassNames}
{...virtuosoOptions}
data={nextData}
ref={virtuosoRef}
/>
{FilteredContentTip}
</>
)
}
const AddFeedHelper = () => {
const feedId = useRouteParamsSelector((s) => s.feedId)
const feedQuery = useFeed({ id: feedId })
const hasSubscription = useSubscriptionByFeedId(feedId || "")
if (hasSubscription) {
return null
}
if (!feedId) {
return
}
if (feedId === FEED_COLLECTION_LIST || feedId === ROUTE_FEED_PENDING) {
return null
}
if (!isBizId(feedId)) {
return null
}
if (feedQuery.error && feedQuery.error.statusCode === 404) {
throw new FeedNotFound()
}
if (!feedQuery.data) {
return null
}
throw new FeedFoundCanBeFollowError(feedQuery.data.feed as FeedModel)
}
export const EntryColumn = memo(EntryColumnImpl)