Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add brains list overflow indicator #1842

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { SuggestionKeyDownProps } from "@tiptap/suggestion";
import { forwardRef } from "react";
import { FaAngleDoubleDown } from "react-icons/fa";

import { AddBrainModal } from "@/lib/components/AddBrainModal";

import { AddNewPromptButton } from "./components/AddNewPromptButton";
import { MentionItem } from "./components/MentionItem/MentionItem";
import { useMentionList } from "./hooks/useMentionList";
import { useSuggestionsOverflowHandler } from "./hooks/useSuggestionsOverflowHandler";
import { MentionListProps } from "./types";

export type MentionListRef = {
Expand All @@ -19,19 +21,36 @@ export const MentionList = forwardRef<MentionListRef, MentionListProps>(
ref,
});

const { suggestionsRef, shouldShowScrollToBottomIcon, scrollToBottom } =
useSuggestionsOverflowHandler();

return (
<div className="items flex flex-col p-2 px-4 bg-gray-50 rounded-md shadow-md z-40">
{props.suggestionData.items.map((item, index) => (
<MentionItem
key={item.id}
item={item}
isSelected={index === selectedIndex}
onClick={() => selectItem(index)}
type={props.suggestionData.type}
/>
))}
{isBrain && <AddBrainModal />}
{isPrompt && <AddNewPromptButton />}
<div className="items flex flex-1 flex-col p-2 px-4 bg-gray-50 rounded-md shadow-md z-40 max-h-[200px]">
<div
className="flex flex-1 flex-col overflow-y-auto"
ref={suggestionsRef}
>
{props.suggestionData.items.map((item, index) => (
<MentionItem
key={item.id}
item={item}
isSelected={index === selectedIndex}
onClick={() => selectItem(index)}
type={props.suggestionData.type}
/>
))}
</div>
<div className="relative">
{shouldShowScrollToBottomIcon && (
<FaAngleDoubleDown
size={20}
className="animate-bounce cursor-pointer absolute right-1 top-0 hover:text-primary"
onClick={scrollToBottom}
/>
)}
{isBrain && <AddBrainModal />}
{isPrompt && <AddNewPromptButton />}
</div>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useEffect, useRef, useState } from "react";

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useSuggestionsOverflowHandler = () => {
const [shouldShowScrollToBottomIcon, setShouldShowScrollToBottomIcon] =
useState(false);

const suggestionsRef = useRef<HTMLDivElement>(null);

useEffect(() => {
const scrollContainer = suggestionsRef.current;
const handleScroll = () => {
if (scrollContainer === null) {
return;
}
const SCROLL_POSITION_NOISE = 10;
const asReachedBottom =
scrollContainer.scrollHeight -
scrollContainer.scrollTop -
SCROLL_POSITION_NOISE <=
scrollContainer.clientHeight;

setShouldShowScrollToBottomIcon(!asReachedBottom);
};
scrollContainer?.addEventListener("scroll", handleScroll);

return () => {
scrollContainer?.removeEventListener("scroll", handleScroll);
};
}, []);

const scrollToBottom = () => {
if (suggestionsRef.current) {
suggestionsRef.current.scrollTo({
top: suggestionsRef.current.scrollHeight,
behavior: "smooth",
});
}
};

return {
shouldShowScrollToBottomIcon,
scrollToBottom,
suggestionsRef,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type UseMentionConfigProps = {
suggestionData: SuggestionData;
};

const MAX_ITEMS_DISPLAYED = 15;

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const useMentionConfig = ({
char,
Expand All @@ -35,11 +33,9 @@ export const useMentionConfig = ({
allowSpaces: true,
pluginKey: new PluginKey(mentionKey),
items: ({ query }) =>
items
.filter((item) =>
item.label.toLowerCase().startsWith(query.toLowerCase())
)
.slice(0, MAX_ITEMS_DISPLAYED),
items.filter((item) =>
item.label.toLowerCase().startsWith(query.toLowerCase())
),
render: () => {
let reactRenderer:
| ReactRenderer<
Expand Down
Loading