Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 13 additions & 10 deletions frontend/src/components/CitationLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip"
import { FileType } from "shared/types"
import { getFileType } from "shared/fileUtils"

export interface Citation {
url: string
title: string
docId: string
itemId?: string
clId?: string
chunkIndex?: number
}

export const createCitationLink =
Expand All @@ -33,9 +34,10 @@ export const createCitationLink =
const [isTooltipOpen, setIsTooltipOpen] = useState(false)

// Extract citation index from children (which should be the citation number like "1", "2", etc.)
const citationIndex =
typeof children === "string" ? parseInt(children) - 1 : -1

const parts = typeof children === "string" ? children.split("_") : []
const citationIndex = parts.length > 0 ? parseInt(parts[0]) - 1 : -1
let chunkIndex = parts.length > 1 ? parseInt(parts[1]) : undefined

// Get citation by index if valid, otherwise fall back to URL matching
const citation =
citationIndex >= 0 && citationIndex < citations.length
Expand All @@ -44,6 +46,11 @@ export const createCitationLink =
? citations.find((c) => c.url === href)
: undefined

if(chunkIndex !== undefined && citation) {
children = (citationIndex + 1).toString()
if(getFileType({type: "", name: citation?.title ?? ""}) === FileType.SPREADSHEET) chunkIndex = Math.max(chunkIndex - 1, 0)
}

if (citation && citation.clId && citation.itemId) {
return (
<TooltipProvider delayDuration={200}>
Expand All @@ -56,11 +63,7 @@ export const createCitationLink =
e.preventDefault()
e.stopPropagation()
if (onCitationClick) {
if (citation.chunkIndex !== undefined) {
onCitationClick(citation, citation.chunkIndex)
} else {
onCitationClick(citation)
}
onCitationClick(citation, chunkIndex)
}
setIsTooltipOpen(false)
}}
Expand Down Expand Up @@ -139,7 +142,7 @@ export const createCitationLink =
const isNumericChild =
typeof children === "string" &&
!isNaN(parseInt(children)) &&
parseInt(children).toString() === children.trim()
parseInt(children).toString() === children.split("_")[0].trim()

return (
<a {...linkProps} href={href} target="_blank" rel="noopener noreferrer">
Expand Down
Loading