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

Text Mode: Log window rescrolls to top #46

Open
jektvik opened this issue Oct 21, 2024 · 2 comments
Open

Text Mode: Log window rescrolls to top #46

jektvik opened this issue Oct 21, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@jektvik
Copy link

jektvik commented Oct 21, 2024

The log scrolls to the bottom on first render. But after pushing additional lines to the log, it resets itself to the first line on subsequent rerenders and there doesn't seem to be any way of forcing him to follow the new lines pushed to the bottom. One thing that fixes it is to remove the ScrollFollow component, but then scrolling becomes jerky,

Here's my code which first loads a payload of logs lines on first render, and then pushes additional lines from a websocket channel (however the websocket is handled indepentently from the built-in logviewer api) .

  useEffect(() => {
    const channel = supabase
      .channel('schema-db-changes')
      .on<LogValues>(
        'postgres_changes',
        {
          event: 'INSERT',
          schema: 'public'
        },
        (payload) => logs && setLogs([...logs, payload.new])
      )
      .subscribe()

    return () => {
      ;(async function subscribeToChanges() {
        await supabase.removeChannel(channel)
      })()
    }
  }, [logs])

  useEffect(() => {
    ;(async function getLogs() {
      const { data: respLogs } = await supabase
        .from('application_logs')
        .select()
        .order('created', { ascending: false }) // Sort by 'created' in descending order
        .limit(1000)

      if (respLogs?.length) {
        setLogs(respLogs.reverse())
      }
    })()
  }, [])

  return (
    <Card height={'100%'}>
      <div style={{ height: 500 }}>
        <ScrollFollow
          startFollowing={true}
          render={({ follow, onScroll }) => (
            <LazyLog
              // stream
              // follow={follow}
              scrollToLine={9999}
              onScroll={onScroll}
              caseInsensitive
              enableHotKeys
              enableSearch
              extraLines={1}
              height="520"
              onLineContentClick={function noRefCheck() {}}
              selectableLines
              text={logs
                ?.map(
                  (c) =>
                    // @ts-expect-error just being lazy for typing
                    `${new Date(c.created).toLocaleString('en-GB')}   ${c
                      .values?.['Message']}`
                )
                .join('\n')}
            />
          )}
        />
      </div>
    </Card>
@melloware
Copy link
Owner

Yeah this was reported before: #25 Any help debugging would be great

@melloware melloware added the bug Something isn't working label Oct 21, 2024
@melloware melloware changed the title Log window rescrolls to top Text Mode: Log window rescrolls to top Oct 21, 2024
@jektvik
Copy link
Author

jektvik commented Oct 21, 2024

Pass, howver an eventual workaround that worked was to force rerender they entire component every time new data arrives. I use the key method for it:

useEffect(
 ...yourDataFetchingLogic
forceRerender()
...
,[yourDep])
...
  const [key, setKey] = useState(0)
  const forceRerender = () => {
    setKey((prevKey) => prevKey + 1) // Increment key to force re-render
  }
...
<MyTerminal key={key} logs={logs} />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants