Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(views): load more views in the sidebar as you scroll (#111189)

Shy Alter25bcacc04b75
diff --git a/frontend/src/layout/today/TodayViewsSidebar.tsx b/frontend/src/layout/today/TodayViewsSidebar.tsx
index 0154b4388ab9..75d0d7b57deb 100644
--- a/frontend/src/layout/today/TodayViewsSidebar.tsx
+++ b/frontend/src/layout/today/TodayViewsSidebar.tsx
@@ -1,5 +1,7 @@
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
+import { useCallback, useEffect, useState } from 'react'
+import { useInView } from 'react-intersection-observer'
import { IconGridMasonry } from '@posthog/icons'
import { Button, Spinner } from '@posthog/quill'
@@ -20,12 +22,15 @@ import { TodayViewsFilterMenu } from './TodayViewsFilterMenu'
import { todayViewsLogic } from './todayViewsLogic'
import { shortTimeAgo } from './todayWorkItems'
+const SCROLL_PREFETCH_MARGIN = '600px 0px'
+
/** The Views sub-nav: recently viewed views, then the full list, newest first. */
export function TodayViewsSidebar(): JSX.Element {
const {
recentViews,
recentReady,
recentItems,
+ recentHasMore,
recentViewsLoading,
recentUnavailable,
recentQuery,
@@ -33,7 +38,28 @@ export function TodayViewsSidebar(): JSX.Element {
buildingViewIds,
recentlyViewed: recents,
} = useValues(todayViewsLogic)
- const { loadRecentViews, setRecentQuery, clearRecentSearchAndFilters } = useActions(todayViewsLogic)
+ const { loadRecentViews, loadMoreRecentViews, setRecentQuery, clearRecentSearchAndFilters } =
+ useActions(todayViewsLogic)
+ const [scrollRoot, setScrollRoot] = useState<Element | null>(null)
+ const { ref: inViewRef, inView: endInView } = useInView({
+ root: scrollRoot,
+ rootMargin: SCROLL_PREFETCH_MARGIN,
+ skip: !scrollRoot,
+ })
+ const endRef = useCallback(
+ (node: HTMLDivElement | null) => {
+ setScrollRoot(node?.closest('.TodayPaneSearchList') ?? null)
+ inViewRef(node)
+ },
+ [inViewRef]
+ )
+ const loadedCount = recentViews.items.length
+
+ useEffect(() => {
+ if (endInView && recentHasMore) {
+ loadMoreRecentViews()
+ }
+ }, [endInView, recentHasMore, loadedCount, loadMoreRecentViews])
const narrowed = recentQuery.trim() !== '' || recentFiltersActive
const { location } = useValues(router)
const path = removeProjectIdIfPresent(location.pathname)
@@ -141,6 +167,12 @@ export function TodayViewsSidebar(): JSX.Element {
/>
)
})}
+ {recentHasMore && (
+ <div className="TodayPane__state" aria-busy>
+ <Spinner />
+ </div>
+ )}
+ <div ref={endRef} aria-hidden />
</>
)}
</div>
diff --git a/frontend/src/layout/today/todayViewsLogic.ts b/frontend/src/layout/today/todayViewsLogic.ts
index e2afd0e5b46c..90fb6273e4d8 100644
--- a/frontend/src/layout/today/todayViewsLogic.ts
+++ b/frontend/src/layout/today/todayViewsLogic.ts
@@ -5,7 +5,7 @@ import type { LocationChangedPayload } from 'kea-router/lib/types'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'
-import { ViewFeed, ViewFeedQuery, ViewSourceState } from 'scenes/views/viewFeed'
+import { VIEW_FEED_PAGE_SIZE, ViewFeed, ViewFeedQuery, ViewSourceState } from 'scenes/views/viewFeed'
import { selectViewFeed, viewFeedLogic } from 'scenes/views/viewFeedLogic'
import { ViewItem } from 'scenes/views/viewsUtils'
@@ -19,8 +19,6 @@ import { TodayRecentEntry, todayRecentsLogic } from './todayRecentsLogic'
import { railPaneForPath } from './todayShellLogic'
import { DEFAULT_VIEWS_FILTERS, TodayViewsFilters, filterRecentViews, viewsFiltersActive } from './todayViewsFilters'
-// The sidebar shows the most recent views of every type in one list. The Views page lists the rest.
-const RECENT_LIMIT = 12
const ALL_RECENT_QUERY: ViewFeedQuery = { type: 'all', search: '' }
const FIRST_BUILD_POLL_MS = 5000
// pinned: task run statuses from the tasks API
@@ -47,6 +45,7 @@ export interface todayViewsLogicValues {
recentFeedQuery: ViewFeedQuery
recentFilters: TodayViewsFilters
recentFiltersActive: boolean
+ recentHasMore: boolean
recentItems: ViewItem[]
recentQuery: string
recentReady: boolean
@@ -100,6 +99,9 @@ export interface todayViewsLogicActions {
}
payload?: void
}
+ loadMoreRecentViews: () => {
+ value: true
+ }
loadRecentViews: () => {
value: true
}
@@ -149,6 +151,7 @@ export interface todayViewsLogicMeta {
recentFilters: TodayViewsFilters,
user: UserType | null
) => ViewItem[]
+ recentHasMore: (recentViews: ViewFeed, recentFeedQuery: ViewFeedQuery, recentQuery: string) => boolean
buildingViewIds: (
allRecentViews: ViewFeed,
firstBuilds: Record<string, FirstBuildState>,
@@ -207,6 +210,7 @@ export const todayViewsLogic = kea<todayViewsLogicType>([
})),
actions({
loadRecentViews: true,
+ loadMoreRecentViews: true,
setRecentQuery: (query: string) => ({ query }),
setRecentSearch: (search: string) => ({ search }),
setRecentFilters: (filters: TodayViewsFilters) => ({ filters }),
@@ -321,13 +325,16 @@ export const todayViewsLogic = kea<todayViewsLogicType>([
): ViewItem[] => {
const userUuid = user?.uuid ?? null
if (recentViews.initialized && recentQuery.trim() === recentFeedQuery.search) {
- return recentFeedQuery.search || viewsFiltersActive(recentFilters)
- ? filterRecentViews(recentViews.items, '', recentFilters, userUuid)
- : recentViews.items.slice(0, RECENT_LIMIT)
+ return filterRecentViews(recentViews.items, '', recentFilters, userUuid)
}
return filterRecentViews(allRecentViews.items, recentQuery, recentFilters, userUuid)
},
],
+ recentHasMore: [
+ (s) => [s.recentViews, s.recentFeedQuery, s.recentQuery],
+ (recentViews: ViewFeed, recentFeedQuery: ViewFeedQuery, recentQuery: string): boolean =>
+ recentViews.initialized && recentViews.hasMore && recentQuery.trim() === recentFeedQuery.search,
+ ],
buildingViewIds: [
(s) => [s.allRecentViews, s.firstBuilds, s.openCanvasBuilding],
(
@@ -370,8 +377,13 @@ export const todayViewsLogic = kea<todayViewsLogicType>([
actions.setRecentSearch(query.trim())
}
},
- setRecentSearch: () => actions.ensureFeed(values.recentFeedQuery, RECENT_LIMIT),
- setRecentFilters: () => actions.ensureFeed(values.recentFeedQuery, RECENT_LIMIT),
+ loadMoreRecentViews: () => {
+ if (values.recentHasMore) {
+ actions.ensureFeed(values.recentFeedQuery, values.recentViews.items.length + VIEW_FEED_PAGE_SIZE)
+ }
+ },
+ setRecentSearch: () => actions.ensureFeed(values.recentFeedQuery, VIEW_FEED_PAGE_SIZE),
+ setRecentFilters: () => actions.ensureFeed(values.recentFeedQuery, VIEW_FEED_PAGE_SIZE),
loadFirstBuildsSuccess: ({ firstBuilds }) => {
const building = Object.values(firstBuilds).filter(
(build) => !build.hasVersion && !!build.runStatus && !TERMINAL_RUN_STATUSES.has(build.runStatus)