Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Nov 9, 2024
1 parent 5fdd193 commit 084a4a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { LocationProvider } from "@/contexts/LocationContext";
import { greetings } from "@/constants/Greetings";
import { untitledMaps } from "@/constants/UntitledMap";
import { tridentPlaceholders } from "@/constants/TridentPlaceholder";
import { useScrollToBottom } from "@/hooks/scrollToBottom";

export default function Home() {
// all state
Expand All @@ -48,29 +49,23 @@ export default function Home() {
"/map_styles/fiord-color-gl-style/style.json"
);

// floating chat button state
const [showingFloatingChat, setShowingFloatingChat] = useState(true);

// dialogue ref and state
const dialogueRef = useRef<HTMLDivElement | null>(null);
const dialogueEndRef = useRef<HTMLDivElement | null>(null);
const [dialogueList, setDialogueList] = useState<DialogueElement[]>([]);
const scrollToBottom = useScrollToBottom(dialogueEndRef);

// communication state
const [responding, setResponding] = useState(false);
const [mapping, setMapping] = useState(false);
const [pastMessages, setPastMessages] = useState<Array<any> | undefined>();

// floating chat button state
const [showingFloatingChat, setShowingFloatingChat] = useState(true);

// input ref and state
const [inputText, setInputText] = useState("");

const scrollToBottom = useCallback(async () => {
await sleep(50);
if (dialogueEndRef.current) {
dialogueEndRef.current.scrollIntoView({ behavior: "instant" });
}
}, []);

const onChangeFloatingChatButton = useCallback(
(showing: boolean) => {
setShowingFloatingChat(showing);
Expand All @@ -81,6 +76,7 @@ export default function Home() {
[scrollToBottom]
);

// base maps style change
const onSelectMapStyleJsonUrl = useCallback(
(e: React.ChangeEvent<HTMLSelectElement>) => {
setMapStyleJsonUrl(e.target.value);
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/scrollToBottom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use client";

import { sleep } from "@/utils/sleep";
import { useCallback, RefObject } from "react";

export const useScrollToBottom = (
dialogueEndRef: RefObject<HTMLDivElement>
) => {
const scrollToBottom = useCallback(async () => {
await sleep(50);
if (dialogueEndRef.current) {
dialogueEndRef.current.scrollIntoView({ behavior: "instant" });
}
}, [dialogueEndRef]);

return scrollToBottom;
};

0 comments on commit 084a4a9

Please sign in to comment.