Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(new-channels): sticky scrolling on new message (#1928)

teo69baf91bc78a
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index f1b00c143f..262bddeba2 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -45,6 +45,7 @@ import {
makeAttachmentTrackerPersistenceKey,
makeInputValuePersistenceKey,
} from '@channel/Input/utils/persistence';
+import { createStickyScrollEffect } from './sticky-scroll';
import { createMessageEditor } from './create-message-editor';
import type { ChannelInputProps } from '@channel/Input/ChannelInput';
@@ -131,6 +132,12 @@ export function Channel(props: ChannelProps) {
},
});
+ createStickyScrollEffect({
+ isNearBottom: () => threadListScrollState()?.isNearBottom ?? false,
+ hasMoreBelow: () => threadPaginator.hasMoreShifting(),
+ messages,
+ scrollToBottom: () => threadListNavigation()?.scrollToBottom(),
+ });
const onSend: ChannelInputProps['onSend'] = (snapshot) => {
const senderId = userId();
if (!senderId) return;
diff --git a/js/app/packages/channel/Channel/sticky-scroll.tsx b/js/app/packages/channel/Channel/sticky-scroll.tsx
new file mode 100644
index 0000000000..752260e652
--- /dev/null
+++ b/js/app/packages/channel/Channel/sticky-scroll.tsx
@@ -0,0 +1,46 @@
+import { createEffect, on, type Accessor } from 'solid-js';
+import type { ApiChannelMessage } from '@service-comms/client';
+
+function isNearTrueBottom(
+ isNearBottom: boolean,
+ hasMoreBelow: boolean
+): boolean {
+ return isNearBottom && !hasMoreBelow;
+}
+
+function didInsertMessageOnBottom(
+ currentMessages: Array<ApiChannelMessage>,
+ previousMessages: Array<ApiChannelMessage> | undefined
+): boolean {
+ if (!previousMessages) return false;
+ const lastCurrentMessage = currentMessages.at(-1);
+ const lastPreviousMessage = previousMessages.at(-1);
+ if (!lastCurrentMessage || !lastPreviousMessage) return false;
+ return lastCurrentMessage.id !== lastPreviousMessage.id;
+}
+
+export type StickyScrollerProps = {
+ isNearBottom: Accessor<boolean>;
+ hasMoreBelow: Accessor<boolean>;
+ messages: Accessor<Array<ApiChannelMessage>>;
+ scrollToBottom: () => void;
+};
+
+export function createStickyScrollEffect(props: StickyScrollerProps) {
+ createEffect(
+ on(
+ props.messages,
+ (currentMessages, previousMessage) => {
+ if (
+ didInsertMessageOnBottom(currentMessages, previousMessage) &&
+ isNearTrueBottom(props.isNearBottom(), props.hasMoreBelow())
+ ) {
+ props.scrollToBottom();
+ }
+ },
+ {
+ defer: true,
+ }
+ )
+ );
+}
diff --git a/js/app/packages/channel/Channel/thread-paginator.ts b/js/app/packages/channel/Channel/thread-paginator.ts
index 0f404ae856..32cba86ea9 100644
--- a/js/app/packages/channel/Channel/thread-paginator.ts
+++ b/js/app/packages/channel/Channel/thread-paginator.ts
@@ -2,11 +2,13 @@ import type { UseInfiniteQueryResult } from '@tanstack/solid-query';
import { createSignal, type Accessor, type Setter } from 'solid-js';
import type { Store } from 'solid-js/store';
-type ThreadPaginator = {
+export type ThreadPaginator = {
isPrepending: Accessor<boolean>;
isShifting: Accessor<boolean>;
prependPaginate: () => Promise<void>;
shiftPaginate: () => Promise<void>;
+ hasMorePrepend: Accessor<boolean>;
+ hasMoreShifting: Accessor<boolean>;
};
type PaginateDirectionState = {
@@ -15,6 +17,8 @@ type PaginateDirectionState = {
is: Accessor<boolean>;
setIsPending: Setter<boolean>;
setIs: Setter<boolean>;
+ more: Accessor<boolean>;
+ setMore: Setter<boolean>;
};
function createPaginateDirectionState(
@@ -22,12 +26,15 @@ function createPaginateDirectionState(
): Store<PaginateDirectionState> {
const [pending, setIsPending] = createSignal<boolean>(false);
const [is, setIs] = createSignal<boolean>(false);
+ const [more, setMore] = createSignal<boolean>(false);
return {
direction,
pending,
is,
setIsPending,
setIs,
+ more,
+ setMore,
};
}
@@ -49,6 +56,8 @@ export function createThreadPaginator<T>(
? query.fetchNextPage
: query.fetchPreviousPage;
+ state.setMore(hasMore());
+
if (!hasMore()) return;
if (isFetching() || state.is()) {
state.setIsPending(true);
@@ -69,6 +78,8 @@ export function createThreadPaginator<T>(
};
return {
+ hasMorePrepend: prependPaginateState.more,
+ hasMoreShifting: shiftPaginateState.more,
isPrepending: prependPaginateState.is,
isShifting: shiftPaginateState.is,
prependPaginate: () => paginate(prependPaginateState),