Skip to content
Open
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,6 +1,6 @@
'use client'

import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { Label, Switch } from '@/components/emcn'
import { isApiClientError } from '@/lib/api/client/errors'
import { requestJson } from '@/lib/api/client/request'
Expand Down Expand Up @@ -50,6 +50,8 @@ export function ChunkEditor({
onCreated,
}: ChunkEditorProps) {
const textareaRef = useRef<HTMLTextAreaElement>(null)
const tokenizedScrollRef = useRef<HTMLDivElement>(null)
const preservedScrollTopRef = useRef(0)
const { mutateAsync: updateChunk } = useUpdateChunk()
const { mutateAsync: createChunk } = useCreateChunk()

Expand Down Expand Up @@ -170,6 +172,24 @@ export function ChunkEditor({
[saveRef]
)

const hasToggledTokenizerRef = useRef(false)

const handleTokenizerChange = useCallback(
(value: boolean) => {
const source = tokenizerOn ? tokenizedScrollRef.current : textareaRef.current
preservedScrollTopRef.current = source?.scrollTop ?? 0
hasToggledTokenizerRef.current = true
setTokenizerOn(value)
},
[tokenizerOn]
)

useLayoutEffect(() => {
if (!hasToggledTokenizerRef.current) return
const target = tokenizerOn ? tokenizedScrollRef.current : textareaRef.current
if (target) target.scrollTop = preservedScrollTopRef.current
}, [tokenizerOn])
Comment thread
waleedlatif1 marked this conversation as resolved.

const tokenStrings = useMemo(() => {
if (!tokenizerOn || !editedContent) return []
return getTokenStrings(editedContent)
Expand All @@ -196,7 +216,10 @@ export function ChunkEditor({
}}
>
{tokenizerOn ? (
<div className='h-full w-full cursor-default overflow-y-auto whitespace-pre-wrap break-words p-6 font-sans text-[var(--text-body)] text-sm'>
<div
ref={tokenizedScrollRef}
className='h-full w-full cursor-default overflow-y-auto whitespace-pre-wrap break-words p-6 font-sans text-[var(--text-body)] text-sm'
>
{tokenStrings.map((token, index) => (
<span
key={index}
Expand Down Expand Up @@ -232,7 +255,7 @@ export function ChunkEditor({
<div className='flex items-center justify-between border-[var(--border)] border-t px-6 py-2.5'>
<TokenizerToggle
checked={tokenizerOn}
onCheckedChange={setTokenizerOn}
onCheckedChange={handleTokenizerChange}
hoveredTokenIndex={tokenizerOn ? hoveredTokenIndex : null}
/>
<span className='text-[var(--text-secondary)] text-caption'>
Expand Down
Loading