Skip to content
Merged
Show file tree
Hide file tree
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
298 changes: 1 addition & 297 deletions frontend/src/pages/integration/TeamAnalysisResultPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ import {
FiShare2,
} from 'react-icons/fi'
import { Link, useNavigate } from 'react-router-dom'
import MessageText from '../../components/slack/MessageText'
import ErrorBoundary from '../../components/common/ErrorBoundary'
import { useAnalysisData } from '../../hooks'
import { ServiceResource } from '../../lib/integrationService'
import { renderPlainText, extractSectionContent, isObviouslyNotJson } from '../../utils/textRenderer'

interface ChannelAnalysisListProps {
title: string
Expand All @@ -73,148 +73,6 @@ const ChannelAnalysisList: FC<ChannelAnalysisListProps> = ({
emptyMessage = 'No information available.',
}) => {
const navigate = useNavigate()
const renderPlainText = (
text: string | unknown,
workspaceUuid: string | undefined
) => {
const textStr = typeof text === 'string' ? text : String(text || '')
if (!textStr || textStr.trim().length === 0) {
return <Text color="gray.500">No content available</Text>
}

let cleanedText = textStr

if (/^\s*\{\s*\}\s*$/.test(cleanedText)) {
return <Text color="gray.500">No content available</Text>
}

cleanedText = cleanedText.replace(/\\n/g, '\n')

const isLikelyPlainText =
/^[A-Za-z]/.test(cleanedText.trim()) &&
!cleanedText.includes('```json') &&
!(cleanedText.trim().startsWith('{') && cleanedText.trim().endsWith('}'))

if (isLikelyPlainText) {
return (
<Box className="formatted-text">
{cleanedText.split('\n').map((paragraph, index) => (
<Box key={index} mb={2}>
{paragraph.trim() ? (
<MessageText
text={paragraph}
workspaceUuid={workspaceUuid ?? ''}
resolveMentions={true}
fallbackToSimpleFormat={true}
/>
) : (
<Box height="0.7em" />
)}
</Box>
))}
</Box>
)
}

if (
cleanedText.includes('{') &&
cleanedText.includes('}') &&
cleanedText.includes('"')
) {
try {
const contentMatch = cleanedText.match(/"[^"]+"\s*:\s*"([^"]*)"/)
if (contentMatch && contentMatch[1]) {
cleanedText = contentMatch[1].replace(/\\n/g, '\n')
} else {
cleanedText = cleanedText
.replace(/[{}"]/g, '') // Remove braces and quotes
.replace(/[\w_]+\s*:/g, '') // Remove field names
.replace(/,\s*/g, '\n') // Replace commas with newlines
.trim()
}
} catch (e) {
console.warn('Error cleaning text content:', e)
}
}

const hasMarkdownHeaders = /^#+\s+.+$/m.test(cleanedText)

return (
<Box className="formatted-text">
{cleanedText.split('\n').map((paragraph, index) => {
if (!paragraph.trim()) {
return <Box key={index} height="0.7em" />
}

if (hasMarkdownHeaders && /^(#+)\s+(.+)$/.test(paragraph)) {
const match = paragraph.match(/^(#+)\s+(.+)$/)
if (match) {
const level = match[1].length
const headerText = match[2]

const isTabHeader = [
'Summary',
'Topics',
'Contributors',
'Highlights',
].some((tab) =>
headerText.toLowerCase().includes(tab.toLowerCase())
)

if (isTabHeader) {
return <Box key={index} height="0.5em" /> // Skip this header
}

const size = level === 1 ? 'lg' : level === 2 ? 'md' : 'sm'
return (
<Heading
as={`h${Math.min(level, 6)}` as React.ElementType}
size={size}
mt={4}
mb={2}
key={index}
>
{headerText}
</Heading>
)
}
}

if (
paragraph.trim().startsWith('- ') ||
paragraph.trim().startsWith('* ')
) {
return (
<Box key={index} mb={2} pl={4} display="flex">
<Box as="span" mr={2}>
</Box>
<Box flex="1">
<MessageText
text={paragraph.trim().substring(2)}
workspaceUuid={workspaceUuid ?? ''}
resolveMentions={true}
fallbackToSimpleFormat={true}
/>
</Box>
</Box>
)
}

return (
<Box key={index} mb={2}>
<MessageText
text={paragraph}
workspaceUuid={workspaceUuid ?? ''}
resolveMentions={true}
fallbackToSimpleFormat={true}
/>
</Box>
)
})}
</Box>
)
}

const filteredAnalyses =
reportResult?.resource_analyses &&
Expand Down Expand Up @@ -490,153 +348,7 @@ Generated using Toban Contribution Viewer with ${modelUsed}
})
}

/**
* Render plain text with proper formatting and support for markdown-like syntax
*/
const renderPlainText = (text: string | unknown, workspace_uuid: string) => {
const textStr = typeof text === 'string' ? text : String(text || '')
if (!textStr || textStr.trim().length === 0) {
return <Text color="gray.500">No content available</Text>
}

let cleanedText = textStr

if (/^\s*\{\s*\}\s*$/.test(cleanedText)) {
return <Text color="gray.500">No content available</Text>
}

cleanedText = cleanedText.replace(/\\n/g, '\n')

const isLikelyPlainText =
/^[A-Za-z]/.test(cleanedText.trim()) &&
!cleanedText.includes('```json') &&
!(cleanedText.trim().startsWith('{') && cleanedText.trim().endsWith('}'))

if (isLikelyPlainText) {
console.log('Content appears to be plain text, rendering directly')
return (
<Box className="formatted-text">
{cleanedText.split('\n').map((paragraph, index) => (
<Box key={index} mb={2}>
{paragraph.trim() ? (
<MessageText
text={paragraph}
resolveMentions={true}
fallbackToSimpleFormat={true}
workspaceUuid={workspace_uuid ?? ''}
/>
) : (
<Box height="0.7em" />
)}
</Box>
))}
</Box>
)
}

if (
cleanedText.includes('{') &&
cleanedText.includes('}') &&
cleanedText.includes('"')
) {
try {
const contentMatch = cleanedText.match(/"[^"]+"\s*:\s*"([^"]*)"/)
if (contentMatch && contentMatch[1]) {
cleanedText = contentMatch[1].replace(/\\n/g, '\n')
} else {
cleanedText = cleanedText
.replace(/[{}"]/g, '') // Remove braces and quotes
.replace(/[\w_]+\s*:/g, '') // Remove field names
.replace(/,\s*/g, '\n') // Replace commas with newlines
.trim()
}
} catch (e) {
console.warn('Error cleaning text content:', e)
}
}

const hasMarkdownHeaders = /^#+\s+.+$/m.test(cleanedText)

return (
<Box className="formatted-text">
{cleanedText.split('\n').map((paragraph, index) => {
if (!paragraph.trim()) {
return <Box key={index} height="0.7em" />
}

if (hasMarkdownHeaders && /^(#+)\s+(.+)$/.test(paragraph)) {
const match = paragraph.match(/^(#+)\s+(.+)$/)
if (match) {
const level = match[1].length
const headerText = match[2]

const isTabHeader = [
'Summary',
'Topics',
'Contributors',
'Highlights',
].some((tab) =>
headerText.toLowerCase().includes(tab.toLowerCase())
)

if (isTabHeader) {
return <Box key={index} height="0.5em" /> // Skip this header
}

const size = level === 1 ? 'lg' : level === 2 ? 'md' : 'sm'
return (
<Heading
as={`h${Math.min(level, 6)}` as React.ElementType}
size={size}
mt={4}
mb={2}
key={index}
>
{headerText}
</Heading>
)
}
}

if (
paragraph.trim().startsWith('- ') ||
paragraph.trim().startsWith('* ')
) {
return (
<Box key={index} mb={2} pl={4} display="flex">
<Box as="span" mr={2}>
</Box>
<Box flex="1">
<MessageText
text={paragraph.trim().substring(2)}
resolveMentions={true}
fallbackToSimpleFormat={true}
workspaceUuid={workspace_uuid ?? ''}
/>
</Box>
</Box>
)
}

return (
<Box key={index} mb={2}>
<MessageText
text={paragraph}
resolveMentions={true}
fallbackToSimpleFormat={true}
workspaceUuid={workspace_uuid ?? ''}
/>
</Box>
)
})}
</Box>
)
}

const isObviouslyNotJson = (str: string) => {
return !str.includes('{') && !str.includes('"') && !str.includes(':')
}

const fixedAnalysis = analysis
? {
Expand Down Expand Up @@ -667,14 +379,6 @@ Generated using Toban Contribution Viewer with ${modelUsed}
fixedAnalysis.contributor_insights
}

const extractSectionContent = (text: string, sectionName: string) => {
const regex = new RegExp(
`#+\\s*${sectionName}\\s*\\n([\\s\\S]*?)(?=#+\\s*|$)`,
'i'
)
const match = text.match(regex)
return match ? match[1].trim() : ''
}

if (analysis && fixedAnalysis) {
if (
Expand Down
11 changes: 0 additions & 11 deletions frontend/src/utils/textRenderer.ts

This file was deleted.

Loading
Loading