diff --git a/app/components/DocumentContext.tsx b/app/components/DocumentContext.tsx index a69333b7a8..b3b7d82082 100644 --- a/app/components/DocumentContext.tsx +++ b/app/components/DocumentContext.tsx @@ -137,7 +137,11 @@ class DocumentContext { } private updateTasks() { - const tasks = this.editor?.getTasks() ?? []; + // Without an editor, keep the task counts that the server sent. + if (!this.editor) { + return; + } + const tasks = this.editor.getTasks(); const total = tasks.length ?? 0; const completed = tasks.filter((t) => t.completed).length ?? 0; this.document?.updateTasks(total, completed); diff --git a/app/components/Header.tsx b/app/components/Header.tsx index b655635f92..c9be294edd 100644 --- a/app/components/Header.tsx +++ b/app/components/Header.tsx @@ -81,22 +81,40 @@ function Header({ left, title, actions, hasSidebar, className, ref }: Props) { }); }, []); + const [wrapper, setWrapper] = React.useState(null); + const [breadcrumbs, setBreadcrumbs] = React.useState(null); const setBreadcrumbRef = React.useCallback( (node: HTMLDivElement | null) => { if (node?.firstElementChild) { breadcrumbsMeasureRef(node.firstElementChild as HTMLDivElement); } + setBreadcrumbs(node?.firstElementChild ?? null); }, [breadcrumbsMeasureRef] ); - const breadcrumbMakesCompact = breadcrumbsSize.width > size.width / 3; - const isCompact = size.width < 1000 || breadcrumbMakesCompact; + // useMeasure reports sizes after the first paint, measure before it so that + // the actions do not switch between compact and full on mount. + const [initialSize, setInitialSize] = React.useState({ + width: 0, + breadcrumbs: 0, + }); + React.useLayoutEffect(() => { + setInitialSize({ + width: wrapper?.getBoundingClientRect().width ?? 0, + breadcrumbs: breadcrumbs?.getBoundingClientRect().width ?? 0, + }); + }, [wrapper, breadcrumbs]); + const width = size.width || initialSize.width; + const breadcrumbsWidth = breadcrumbsSize.width || initialSize.breadcrumbs; + + const breadcrumbMakesCompact = breadcrumbsWidth > width / 3; + const isCompact = width < 1000 || breadcrumbMakesCompact; return ( ([ref, internalMeasureRef, setWrapper])} align="center" shrink={false} className={className} diff --git a/app/components/PaginatedList.tsx b/app/components/PaginatedList.tsx index d15cfcbcb0..e52fb81c44 100644 --- a/app/components/PaginatedList.tsx +++ b/app/components/PaginatedList.tsx @@ -24,6 +24,13 @@ export interface PaginatedItem { createdAt?: string; } +/** Fetch functions mapped to the options of their completed first pages. */ +const completedFetches = new WeakMap>(); + +// oxlint-disable-next-line no-explicit-any +const getOptionsKey = (options: Record | undefined) => + JSON.stringify(options ?? {}); + /** * Props for the PaginatedList component * @template T Type of items in the list, must extend PaginatedItem @@ -130,9 +137,16 @@ const PaginatedList = ({ const [error, setError] = React.useState(); const [isFetchingMore, setIsFetchingMore] = React.useState(false); - const [isFetching, setIsFetching] = React.useState(false); + + // The mount effect starts the fetch, so start in the fetching state. + const [isFetching, setIsFetching] = React.useState(!!fetch); + + // Cached items are only in order once this list has been fetched, items that + // other requests put in the store would otherwise move when the page lands. const [isFetchingInitial, setIsFetchingInitial] = React.useState( - !items?.length + () => + !items?.length || + (!!fetch && !completedFetches.get(fetch)?.has(getOptionsKey(options))) ); const [fetchCounter, setFetchCounter] = React.useState(0); const [renderCount, setRenderCount] = React.useState(Pagination.defaultLimit); @@ -182,6 +196,11 @@ const PaginatedList = ({ if (offset !== 0) { setRenderCount((prevCount) => prevCount + limit); + } else { + completedFetches.set( + fetch, + (completedFetches.get(fetch) ?? new Set()).add(getOptionsKey(options)) + ); } if (results.length === 0 || results.length < limit) { diff --git a/app/scenes/Collection/components/CollectionMeta.tsx b/app/scenes/Collection/components/CollectionMeta.tsx index 8b234fc965..480a53b9a1 100644 --- a/app/scenes/Collection/components/CollectionMeta.tsx +++ b/app/scenes/Collection/components/CollectionMeta.tsx @@ -73,12 +73,14 @@ export const CollectionMeta = observer(function CollectionMeta_({ )} - {documentCount !== undefined && ( - - - {t("{{ count }} document", { count: documentCount })} - - )} + {/* Reserve the space while the count loads, so the line does not grow. */} + + + {t("{{ count }} document", { count: documentCount ?? 0 })} + {collection.ownerIds.length > 0 && ( @@ -89,6 +91,11 @@ export const CollectionMeta = observer(function CollectionMeta_({ ); }); +const DocumentCount = styled.span<{ $hidden: boolean }>` + font-variant-numeric: tabular-nums; + visibility: ${(props) => (props.$hidden ? "hidden" : "visible")}; +`; + const IconLabel = styled(Flex)` gap: 2px; cursor: default; diff --git a/app/scenes/Document/components/DocumentMeta.tsx b/app/scenes/Document/components/DocumentMeta.tsx index 82da7e4dde..1835cbe82d 100644 --- a/app/scenes/Document/components/DocumentMeta.tsx +++ b/app/scenes/Document/components/DocumentMeta.tsx @@ -109,28 +109,39 @@ function TitleDocumentMeta({ to, document, revision, rtl, ...rest }: Props) { )} - {totalViewers && can.listViews && !(document as Document).isDraft ? ( - - - - {t("Viewed by")}{" "} - {onlyYou - ? t("only you") - : `${totalViewers} ${ - totalViewers === 1 ? t("person") : t("people") - }`} - - + {can.listViews && !(document as Document).isDraft ? ( + totalViewers ? ( + + + + {t("Viewed by")}{" "} + {onlyYou + ? t("only you") + : `${totalViewers} ${ + totalViewers === 1 ? t("person") : t("people") + }`} + + + ) : ( + // Reserve the line while views load, it stacks on mobile. + + {t("Viewed by")} {t("only you")} + + ) ) : null} ); } +const ViewsPlaceholder = styled.span` + visibility: hidden; +`; + export const Meta = styled(DocumentMeta)<{ $rtl?: boolean }>` ${headerMetaStyles} `; diff --git a/app/scenes/Document/hooks/useDocumentSidebar.tsx b/app/scenes/Document/hooks/useDocumentSidebar.tsx index 2292ca85fe..a26d21e604 100644 --- a/app/scenes/Document/hooks/useDocumentSidebar.tsx +++ b/app/scenes/Document/hooks/useDocumentSidebar.tsx @@ -113,8 +113,9 @@ export default function useDocumentSidebar() { const panel = ui.getRightSidebar(pane); // A history panel outside of a history route is closed by the effect below, // so it is treated as closed here – otherwise the sidebar renders open for a - // frame and then animates away again. - const isOpen = panel !== null && (panel !== "history" || isHistoryRoute); + // frame and then animates away again. A history route is open before the + // effect sets the panel, so that loading one does not animate the sidebar in. + const isOpen = isHistoryRoute || (panel !== null && panel !== "history"); const wasOpenRef = React.useRef(isOpen); React.useEffect(() => { @@ -156,7 +157,8 @@ export default function useDocumentSidebar() { pane, ]); - React.useEffect(() => { + // A layout effect, so that a sidebar open on load is in the first paint. + React.useLayoutEffect(() => { if (isOpen) { setSidebar( @@ -167,7 +169,8 @@ export default function useDocumentSidebar() { wasOpenRef.current = isOpen; }, [isOpen, setSidebar]); - React.useEffect( + // A layout effect, so that it runs before the setup of a replacement instance. + React.useLayoutEffect( () => () => { setSidebar(null); }, diff --git a/app/scenes/Search/Search.tsx b/app/scenes/Search/Search.tsx index b7af234e8e..ebc406da5d 100644 --- a/app/scenes/Search/Search.tsx +++ b/app/scenes/Search/Search.tsx @@ -218,6 +218,10 @@ function Search() { const isResultCountStale = resultCount?.params !== requestParams; const total = titleFilter || error ? undefined : resultCount?.total; + // Reserve the space of the first count, so the sort input does not move. + const isResultCountPending = + isSearchable && !titleFilter && !error && !resultCount; + // Only updatable documents are selectable, matching the per-item checkboxes. const itemIds = React.useMemo( () => @@ -451,11 +455,14 @@ function Search() { aria-busy={isResultCountStale} $disabled={isResultCountStale && total !== undefined} > - {total !== undefined && ( - <> - {t("{{ count }} result", { count: total })} + {(total !== undefined || isResultCountPending) && ( + + {t("{{ count }} result", { count: total ?? 0 })} · - + )} {sortInput} @@ -565,6 +572,13 @@ const FilterChips = styled(Flex)` `}; `; +const ResultCount = styled.span<{ $hidden: boolean }>` + display: flex; + gap: 6px; + font-variant-numeric: tabular-nums; + visibility: ${(props) => (props.$hidden ? "hidden" : "visible")}; +`; + const ResultsMeta = styled(Text)<{ $disabled: boolean }>` display: flex; align-items: center; diff --git a/app/stores/base/Store.ts b/app/stores/base/Store.ts index 71774c5e3d..40f2634157 100644 --- a/app/stores/base/Store.ts +++ b/app/stores/base/Store.ts @@ -673,7 +673,10 @@ export default abstract class Store { ? res.data : res.data[this.responseKey]; response = items.map(this.add); - this.isLoaded = true; + // A filtered list is a subset, so it does not mean the store is loaded. + if (!params?.filters) { + this.isLoaded = true; + } }); response[PAGINATION_SYMBOL] = res.pagination;