-
Notifications
You must be signed in to change notification settings - Fork 14
test: replace hash scroll source snapshot #532
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
230 changes: 230 additions & 0 deletions
230
packages/app/src/pages/session/use-session-hash-scroll-core.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import type { UserMessage } from "@opencode-ai/sdk/v2" | ||
| import { createEffect, createMemo, onCleanup, onMount } from "solid-js" | ||
| import { messageIdFromHash } from "./message-id-from-hash" | ||
|
|
||
| export type SessionHashScrollInput = { | ||
| sessionKey: () => string | ||
| sessionID: () => string | undefined | ||
| messagesReady: () => boolean | ||
| visibleUserMessages: () => UserMessage[] | ||
| historyMore: () => boolean | ||
| historyLoading: () => boolean | ||
| loadMore: (sessionID: string) => Promise<void> | ||
| turnStart: () => number | ||
| currentMessageId: () => string | undefined | ||
| pendingMessage: () => string | undefined | ||
| setPendingMessage: (value: string | undefined) => void | ||
| setActiveMessage: (message: UserMessage | undefined) => void | ||
| markHashTarget: (index: number) => void | ||
| autoScroll: { pause: () => void; forceScrollToBottom: () => void } | ||
| scroller: () => HTMLDivElement | undefined | ||
| anchor: (id: string) => string | ||
| scheduleScrollState: (el: HTMLDivElement) => void | ||
| consumePendingMessage: (key: string) => string | undefined | ||
| onMessageNavigation?: (messageID: string) => void | ||
| onMessageHashCleared?: () => void | ||
| } | ||
|
|
||
| type SessionHashScrollLocation = { hash: string; pathname: string; search: string } | ||
| type SessionHashScrollNavigate = (to: string, options?: { replace?: boolean }) => void | ||
|
|
||
| export const createSessionHashScroll = ( | ||
| input: SessionHashScrollInput, | ||
| location: SessionHashScrollLocation, | ||
| navigate: SessionHashScrollNavigate, | ||
| ) => { | ||
| const visibleUserMessages = createMemo(() => input.visibleUserMessages()) | ||
| const messageById = createMemo(() => new Map(visibleUserMessages().map((m) => [m.id, m]))) | ||
| const messageIndex = createMemo(() => new Map(visibleUserMessages().map((m, i) => [m.id, i]))) | ||
| let pendingKey = "" | ||
| let clearingHash: string | undefined | ||
|
|
||
| const frames = new Set<number>() | ||
| const queue = (fn: () => void) => { | ||
| const id = requestAnimationFrame(() => { | ||
| frames.delete(id) | ||
| fn() | ||
| }) | ||
| frames.add(id) | ||
| } | ||
| const cancel = () => { | ||
| for (const id of frames) cancelAnimationFrame(id) | ||
| frames.clear() | ||
| } | ||
|
|
||
| const clearMessageHash = () => { | ||
| cancel() | ||
| input.consumePendingMessage(input.sessionKey()) | ||
| if (input.pendingMessage()) input.setPendingMessage(undefined) | ||
| if (!location.hash) return | ||
| clearingHash = location.hash | ||
| navigate(location.pathname + location.search, { replace: true }) | ||
| input.onMessageHashCleared?.() | ||
| } | ||
|
|
||
| const updateHash = (id: string) => { | ||
| const hash = `#${input.anchor(id)}` | ||
| if (location.hash === hash) return | ||
| clearingHash = undefined | ||
| navigate(location.pathname + location.search + hash, { | ||
| replace: true, | ||
| }) | ||
| } | ||
|
|
||
| const scrollToElement = (el: HTMLElement, behavior: ScrollBehavior) => { | ||
| const root = input.scroller() | ||
| if (!root) return false | ||
|
|
||
| const a = el.getBoundingClientRect() | ||
| const b = root.getBoundingClientRect() | ||
| const sticky = root.querySelector("[data-session-title]") | ||
| const inset = sticky instanceof HTMLElement ? sticky.offsetHeight : 0 | ||
| const top = Math.max(0, a.top - b.top + root.scrollTop - inset) | ||
| root.scrollTo({ top, behavior }) | ||
| return true | ||
| } | ||
|
|
||
| const seek = (id: string, behavior: ScrollBehavior, left = 4): boolean => { | ||
| const el = document.getElementById(input.anchor(id)) | ||
| if (el) return scrollToElement(el, behavior) | ||
| if (left <= 0) return false | ||
| queue(() => { | ||
| seek(id, behavior, left - 1) | ||
| }) | ||
| return false | ||
| } | ||
|
|
||
| const scrollToMessage = (message: UserMessage, behavior: ScrollBehavior = "smooth") => { | ||
| cancel() | ||
| input.onMessageNavigation?.(message.id) | ||
| if (input.currentMessageId() !== message.id) input.setActiveMessage(message) | ||
|
|
||
| const index = messageIndex().get(message.id) ?? -1 | ||
| if (index !== -1) input.markHashTarget(index) | ||
| if (index !== -1 && index < input.turnStart()) { | ||
| queue(() => { | ||
| seek(message.id, behavior) | ||
| }) | ||
|
|
||
| updateHash(message.id) | ||
| return | ||
| } | ||
|
|
||
| if (seek(message.id, behavior)) { | ||
| updateHash(message.id) | ||
| return | ||
| } | ||
|
|
||
| updateHash(message.id) | ||
| } | ||
|
|
||
| const applyHash = (behavior: ScrollBehavior) => { | ||
| const hash = location.hash.slice(1) | ||
| if (!hash) { | ||
| input.autoScroll.forceScrollToBottom() | ||
| const el = input.scroller() | ||
| if (el) input.scheduleScrollState(el) | ||
| return | ||
| } | ||
|
|
||
| const messageId = messageIdFromHash(hash) | ||
| if (messageId) { | ||
| input.autoScroll.pause() | ||
| const msg = messageById().get(messageId) | ||
| if (msg) { | ||
| scrollToMessage(msg, behavior) | ||
| return | ||
| } | ||
| input.onMessageNavigation?.(messageId) | ||
| return | ||
| } | ||
|
|
||
| const target = document.getElementById(hash) | ||
| if (target) { | ||
| input.autoScroll.pause() | ||
| scrollToElement(target, behavior) | ||
| return | ||
| } | ||
|
|
||
| input.autoScroll.forceScrollToBottom() | ||
| const el = input.scroller() | ||
| if (el) input.scheduleScrollState(el) | ||
| } | ||
|
|
||
| createEffect(() => { | ||
| const hash = location.hash | ||
| if (!hash) { | ||
| clearingHash = undefined | ||
| } else if (hash === clearingHash) { | ||
| return | ||
| } else { | ||
| clearingHash = undefined | ||
| } | ||
| if (!input.sessionID() || !input.messagesReady()) return | ||
| cancel() | ||
| queue(() => applyHash("auto")) | ||
| }) | ||
|
|
||
| createEffect(() => { | ||
| if (!input.sessionID() || !input.messagesReady()) return | ||
|
|
||
| visibleUserMessages() | ||
| input.turnStart() | ||
|
|
||
| let targetId = input.pendingMessage() | ||
| if (!targetId) { | ||
| const key = input.sessionKey() | ||
| if (pendingKey !== key) { | ||
| pendingKey = key | ||
| const next = input.consumePendingMessage(key) | ||
| if (next) { | ||
| input.setPendingMessage(next) | ||
| targetId = next | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!targetId && !clearingHash) targetId = messageIdFromHash(location.hash) | ||
| if (!targetId) return | ||
|
|
||
| const pending = input.pendingMessage() === targetId | ||
| const msg = messageById().get(targetId) | ||
| if (!msg) return | ||
|
|
||
| if (pending) input.setPendingMessage(undefined) | ||
| if (input.currentMessageId() === targetId && !pending) return | ||
|
|
||
| input.autoScroll.pause() | ||
| cancel() | ||
| queue(() => scrollToMessage(msg, "auto")) | ||
| }) | ||
|
|
||
| createEffect(() => { | ||
| const sessionID = input.sessionID() | ||
| if (!sessionID || !input.messagesReady()) return | ||
|
|
||
| visibleUserMessages() | ||
|
|
||
| let targetId = input.pendingMessage() | ||
| if (!targetId && !clearingHash) targetId = messageIdFromHash(location.hash) | ||
| if (!targetId) return | ||
| if (messageById().has(targetId)) return | ||
| if (!input.historyMore() || input.historyLoading()) return | ||
|
|
||
| void input.loadMore(sessionID) | ||
| }) | ||
|
|
||
| onMount(() => { | ||
| if (typeof window !== "undefined" && "scrollRestoration" in window.history) { | ||
| window.history.scrollRestoration = "manual" | ||
| } | ||
| }) | ||
|
|
||
| onCleanup(cancel) | ||
|
|
||
| return { | ||
| clearMessageHash, | ||
| scrollToMessage, | ||
| applyHash, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.