Skip to content

Commit ec14b30

Browse files
author
Theodore Li
committed
Add resource tab to url state, scroll correctly on new tab
1 parent 4bb4b1b commit ec14b30

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/preview-panel.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { createContext, memo, useCallback, useContext, useMemo, useRef } from 'react'
3+
import { createContext, memo, useCallback, useContext, useEffect, useMemo, useRef } from 'react'
44
import { useRouter } from 'next/navigation'
55
import type { Components, ExtraProps } from 'react-markdown'
66
import ReactMarkdown from 'react-markdown'
@@ -281,11 +281,11 @@ function isInternalHref(
281281
if (href.startsWith('#')) return { pathname: '', hash: href }
282282
try {
283283
const url = new URL(href, origin)
284-
if (url.origin === origin) {
284+
if (url.origin === origin && url.pathname.startsWith('/workspace/')) {
285285
return { pathname: url.pathname, hash: url.hash }
286286
}
287287
} catch {
288-
if (href.startsWith('/')) {
288+
if (href.startsWith('/workspace/')) {
289289
const hashIdx = href.indexOf('#')
290290
if (hashIdx === -1) return { pathname: href, hash: '' }
291291
return { pathname: href.slice(0, hashIdx), hash: href.slice(hashIdx) }
@@ -370,6 +370,22 @@ const MarkdownPreview = memo(function MarkdownPreview({
370370
[onCheckboxToggle]
371371
)
372372

373+
const hasScrolledToHash = useRef(false)
374+
useEffect(() => {
375+
const hash = window.location.hash
376+
if (!hash || hasScrolledToHash.current) return
377+
const id = hash.slice(1)
378+
const el = document.getElementById(id)
379+
if (!el) return
380+
hasScrolledToHash.current = true
381+
const container = el.closest('.overflow-auto') as HTMLElement | null
382+
if (container) {
383+
container.scrollTo({ top: el.offsetTop - container.offsetTop, behavior: 'smooth' })
384+
} else {
385+
el.scrollIntoView({ behavior: 'smooth' })
386+
}
387+
}, [content])
388+
373389
const committedMarkdown = useMemo(
374390
() =>
375391
committed ? (

apps/sim/app/workspace/[workspaceId]/home/home.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useCallback, useEffect, useRef, useState } from 'react'
44
import { createLogger } from '@sim/logger'
5-
import { useParams, useRouter } from 'next/navigation'
5+
import { useParams, useRouter, useSearchParams } from 'next/navigation'
66
import { usePostHog } from 'posthog-js/react'
77
import { PanelLeft } from '@/components/emcn/icons'
88
import { useSession } from '@/lib/auth/auth-client'
@@ -28,6 +28,8 @@ interface HomeProps {
2828
export function Home({ chatId }: HomeProps = {}) {
2929
const { workspaceId } = useParams<{ workspaceId: string }>()
3030
const router = useRouter()
31+
const searchParams = useSearchParams()
32+
const initialResourceId = searchParams.get('resource')
3133
const { data: session } = useSession()
3234
const posthog = usePostHog()
3335
const posthogRef = useRef(posthog)
@@ -160,7 +162,10 @@ export function Home({ chatId }: HomeProps = {}) {
160162
} = useChat(
161163
workspaceId,
162164
chatId,
163-
getMothershipUseChatOptions({ onResourceEvent: handleResourceEvent })
165+
getMothershipUseChatOptions({
166+
onResourceEvent: handleResourceEvent,
167+
initialActiveResourceId: initialResourceId,
168+
})
164169
)
165170

166171
const [editingInputValue, setEditingInputValue] = useState('')
@@ -183,6 +188,13 @@ export function Home({ chatId }: HomeProps = {}) {
183188
[editQueuedMessage]
184189
)
185190

191+
useEffect(() => {
192+
if (!activeResourceId) return
193+
const url = new URL(window.location.href)
194+
url.searchParams.set('resource', activeResourceId)
195+
window.history.replaceState(null, '', url.toString())
196+
}, [activeResourceId])
197+
186198
useEffect(() => {
187199
wasSendingRef.current = false
188200
if (resolvedChatId) markRead(resolvedChatId)

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,11 @@ export interface UseChatOptions {
377377
onToolResult?: (toolName: string, success: boolean, result: unknown) => void
378378
onTitleUpdate?: () => void
379379
onStreamEnd?: (chatId: string, messages: ChatMessage[]) => void
380+
initialActiveResourceId?: string | null
380381
}
381382

382383
export function getMothershipUseChatOptions(
383-
options: Pick<UseChatOptions, 'onResourceEvent' | 'onStreamEnd'> = {}
384+
options: Pick<UseChatOptions, 'onResourceEvent' | 'onStreamEnd' | 'initialActiveResourceId'> = {}
384385
): UseChatOptions {
385386
return {
386387
apiPath: MOTHERSHIP_CHAT_API_PATH,
@@ -416,6 +417,7 @@ export function useChat(
416417
const [resolvedChatId, setResolvedChatId] = useState<string | undefined>(initialChatId)
417418
const [resources, setResources] = useState<MothershipResource[]>([])
418419
const [activeResourceId, setActiveResourceId] = useState<string | null>(null)
420+
const initialActiveResourceIdRef = useRef(options?.initialActiveResourceId)
419421
const onResourceEventRef = useRef(options?.onResourceEvent)
420422
onResourceEventRef.current = options?.onResourceEvent
421423
const apiPathRef = useRef(options?.apiPath ?? MOTHERSHIP_CHAT_API_PATH)
@@ -845,7 +847,12 @@ export function useChat(
845847
const persistedResources = history.resources.filter((r) => r.id !== 'streaming-file')
846848
if (persistedResources.length > 0) {
847849
setResources(persistedResources)
848-
setActiveResourceId(persistedResources[persistedResources.length - 1].id)
850+
const initialId = initialActiveResourceIdRef.current
851+
const restoredId =
852+
initialId && persistedResources.some((r) => r.id === initialId)
853+
? initialId
854+
: persistedResources[persistedResources.length - 1].id
855+
setActiveResourceId(restoredId)
849856

850857
for (const resource of persistedResources) {
851858
if (resource.type !== 'workflow') continue

0 commit comments

Comments
 (0)