nicolium: try to not add empty timeline gaps

Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
nicole mikołajczyk
2026-03-11 05:25:01 +01:00
parent 543732d002
commit acc0d9de17
3 changed files with 36 additions and 9 deletions

View File

@ -21,7 +21,7 @@ const useTimeline = (
timelineId: string,
fetcher: TimelineFetcher,
streamConfig?: StreamConfig,
restoring?: boolean,
restoringMaxId?: string,
) => {
const timeline = useStoreTimeline(timelineId);
const timelineActions = useTimelinesActions();
@ -34,23 +34,30 @@ const useTimeline = (
}, [timelineId]);
const fetchInitial = useCallback(
async (isRestoring = restoring) => {
async (isRestoring = !!restoringMaxId) => {
timelineActions.setLoading(timelineId, true);
try {
const response = await fetcher();
const [response, shouldInsertGap] = await Promise.all([
fetcher(),
!restoringMaxId
? Promise.resolve(false)
: fetcher({ since_id: restoringMaxId, limit: 1 })
.then((res) => res.items.length > 0)
.catch(() => true),
]);
importEntities({ statuses: response.items });
timelineActions.expandTimeline(
timelineId,
response.items,
!!response.next,
true,
isRestoring,
isRestoring && shouldInsertGap,
);
} catch (error) {
timelineActions.setError(timelineId, true);
}
},
[timelineId, restoring],
[timelineId, restoringMaxId],
);
const fetchNextPage = useCallback(async () => {

View File

@ -1,6 +1,7 @@
import { useRef } from 'react';
import { useClient } from '@/hooks/use-client';
import { incrementId } from '@/utils/strings';
import { useTimeline } from './use-timeline';
@ -30,8 +31,10 @@ const useHomeTimeline = (
return useTimeline(
'home',
(paginationParams) => {
const initialPagination = restoreMaxId.current ? { max_id: restoreMaxId.current } : undefined;
if (paginationParams == null) {
const initialPagination = restoreMaxId.current
? { max_id: incrementId(restoreMaxId.current) }
: undefined;
if (!paginationParams) {
restoreMaxId.current = undefined;
}
@ -41,7 +44,7 @@ const useHomeTimeline = (
});
},
{ stream },
!!maxId,
maxId,
);
};

View File

@ -2,4 +2,21 @@
// https://stackoverflow.com/a/1026087
const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
export { capitalize };
const incrementId = (str: string): string => {
const CHARS = str.match(/[a-z]/i)
? '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
: '0123456789';
const chars = [...str];
for (let i = chars.length - 1; i >= 0; i--) {
const idx = CHARS.indexOf(chars[i]);
if (idx === -1) continue;
if (idx < CHARS.length - 1) {
chars[i] = CHARS[idx + 1];
return chars.join('');
}
chars[i] = CHARS[0];
}
return CHARS[1] + chars.join('');
};
export { capitalize, incrementId };