-
Notifications
You must be signed in to change notification settings - Fork 1
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
WIP: clickable results #395
Open
Bran18
wants to merge
4
commits into
develop
Choose a base branch
from
refactor-clickable-results
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+957
−372
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
apps/masterbots.ai/components/routes/chat/chat-clickable-list.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
import React, { type ReactNode, useState } from 'react' | ||
import { cn } from '@/lib/utils' | ||
import { | ||
extractTextContent, | ||
extractContextFromText, | ||
extractTitleFromHeading, | ||
formatFollowUpQuery, | ||
safeInvoke | ||
} from '@/lib/clickable-content-extraction' | ||
|
||
interface EnhancedClickableTextProps { | ||
children: ReactNode | ||
sendMessageFromResponse?: (message: string) => void | ||
} | ||
|
||
/** | ||
* EnhancedClickableText Component - Makes text elements clickable based on common AI generation patterns | ||
* | ||
* This component identifies and makes clickable: | ||
* 1. Section headings (like "1. Architecture") | ||
* 2. List items with bold/strong text as the first element | ||
* 3. Text patterns with colons separating titles from content | ||
*/ | ||
export function EnhancedClickableText({ | ||
children, | ||
sendMessageFromResponse | ||
}: EnhancedClickableTextProps) { | ||
// Track hover state for enhanced visual cues | ||
const [hoveredElement, setHoveredElement] = useState<string | null>(null) | ||
|
||
// Check if an element is a list item (li) | ||
const isListItem = (element: React.ReactElement): boolean => { | ||
return element.type === 'li' | ||
} | ||
|
||
// Check if an element is a heading (h1, h2, h3, etc.) | ||
const isHeading = (element: React.ReactElement): boolean => { | ||
return typeof element.type === 'string' && /^h[1-6]$/.test(element.type) | ||
} | ||
|
||
// Check if an element is a strong/bold tag | ||
const isStrong = (element: React.ReactElement): boolean => { | ||
return element.type === 'strong' || element.type === 'b' | ||
} | ||
|
||
// Check if a text string matches a section heading pattern (e.g., "1. Architecture") | ||
const isSectionHeading = (text: string): boolean => { | ||
return /^\d+\.\s+[A-Z][a-zA-Z\s]+$/.test(text) | ||
} | ||
|
||
// Handle click events for clickable elements | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const handleClick = (text: string, contextText: any = '') => { | ||
if (!sendMessageFromResponse) { | ||
console.warn('sendMessageFromResponse function is not provided') | ||
return | ||
} | ||
|
||
const query = formatFollowUpQuery(text, contextText) | ||
safeInvoke(sendMessageFromResponse, query.trim()) | ||
} | ||
|
||
// Process section headings (e.g., "1. Architecture") | ||
const processSectionHeading = (element: React.ReactElement) => { | ||
const headingText = extractTextContent(element.props.children) | ||
|
||
if (isSectionHeading(headingText)) { | ||
const title = extractTitleFromHeading(headingText) | ||
const elementId = `heading-${title.replace(/\s+/g, '-').toLowerCase()}` | ||
const isHovered = hoveredElement === elementId | ||
|
||
return React.cloneElement(element, { | ||
...element.props, | ||
children: ( | ||
<button | ||
className={cn( | ||
'clickable-text p-0 m-0 font-bold text-left bg-transparent', | ||
'border-none cursor-pointer relative transition-colors duration-200 w-full', | ||
isHovered | ||
? 'text-accent underline' | ||
: 'hover:text-accent hover:underline' | ||
)} | ||
onClick={() => handleClick(title)} | ||
onMouseEnter={() => setHoveredElement(elementId)} | ||
onMouseLeave={() => setHoveredElement(null)} | ||
aria-label={`Get more details about ${title}`} | ||
type="button" | ||
> | ||
{element.props.children} | ||
</button> | ||
) | ||
}) | ||
} | ||
|
||
return element | ||
} | ||
|
||
// Process list items with bold text | ||
const processListItemWithBold = (element: React.ReactElement) => { | ||
// Check if this list item contains a strong/bold element | ||
let hasStrongElement = false | ||
let strongText = '' | ||
let fullItemText = '' | ||
|
||
// First, extract the full text of the list item | ||
fullItemText = extractTextContent(element.props.children) | ||
|
||
// Process the children to find strong elements | ||
React.Children.forEach(element.props.children, (child: React.ReactNode) => { | ||
if (React.isValidElement(child) && isStrong(child)) { | ||
hasStrongElement = true | ||
strongText = extractTextContent( | ||
(child as React.ReactElement).props.children | ||
) | ||
} | ||
}) | ||
|
||
if (hasStrongElement) { | ||
// Extract context using our utility function | ||
const contextText = extractContextFromText(fullItemText, strongText) | ||
|
||
// Process the children to make the bold elements clickable | ||
const processedChildren = React.Children.map( | ||
element.props.children, | ||
child => { | ||
if (React.isValidElement(child) && isStrong(child)) { | ||
const elementId = `strong-${strongText.replace(/\s+/g, '-').toLowerCase()}` | ||
const isHovered = hoveredElement === elementId | ||
|
||
return React.cloneElement( | ||
// biome-ignore lint/complexity/noBannedTypes: <explanation> | ||
child as React.ReactElement<React.PropsWithChildren<{}>>, | ||
{ | ||
...(child as React.ReactElement).props, | ||
children: ( | ||
<button | ||
className={cn( | ||
'clickable-text p-0 m-0 font-semibold text-left bg-transparent', | ||
'border-none cursor-pointer relative transition-colors duration-200', | ||
isHovered | ||
? 'text-accent underline' | ||
: 'hover:text-accent hover:underline' | ||
)} | ||
onClick={() => handleClick(strongText, contextText)} | ||
onMouseEnter={() => setHoveredElement(elementId)} | ||
onMouseLeave={() => setHoveredElement(null)} | ||
aria-label={`Get more details about ${strongText}`} | ||
type="button" | ||
> | ||
{(child as React.ReactElement).props.children} | ||
</button> | ||
) | ||
} | ||
) | ||
} | ||
return child | ||
} | ||
) | ||
|
||
return React.cloneElement(element, { | ||
...element.props, | ||
children: processedChildren | ||
}) | ||
} | ||
|
||
return element | ||
} | ||
|
||
// Main processing function for React elements | ||
const processContent = (content: ReactNode): ReactNode => { | ||
// Handle string content directly | ||
if (typeof content === 'string') { | ||
return content | ||
} | ||
|
||
// Skip non-string/non-object content | ||
if (typeof content !== 'object' || content === null) { | ||
return content | ||
} | ||
|
||
// Process arrays of elements | ||
if (Array.isArray(content)) { | ||
return content.map((item, index) => ( | ||
<React.Fragment | ||
key={`content-item-${ | ||
// biome-ignore lint/suspicious/noArrayIndexKey: <explanation> | ||
index | ||
}`} | ||
> | ||
{processContent(item)} | ||
</React.Fragment> | ||
)) | ||
} | ||
|
||
// Process React elements | ||
if (React.isValidElement(content)) { | ||
// Process headings | ||
if (isHeading(content)) { | ||
return processSectionHeading(content) | ||
} | ||
|
||
// Process list items | ||
if (isListItem(content)) { | ||
return processListItemWithBold(content) | ||
} | ||
|
||
// Process children of other elements | ||
// biome-ignore lint/complexity/useOptionalChain: <explanation> | ||
if (content.props && content.props.children) { | ||
return React.cloneElement( | ||
// biome-ignore lint/complexity/noBannedTypes: <explanation> | ||
content as React.ReactElement<React.PropsWithChildren<{}>>, | ||
{ | ||
...(content as React.ReactElement).props, | ||
children: processContent( | ||
(content as React.ReactElement).props.children | ||
) | ||
} | ||
) | ||
} | ||
} | ||
|
||
return content | ||
} | ||
|
||
return <>{processContent(children)}</> | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider using a configurable transformation list to handle different element types, reducing nested conditionals in
processContent
.Consider abstracting the conditional branching into a configurable transformation list so that you avoid writing separate deep branches. For example, you could create an array of transformation objects that encapsulates the conditions and transformation functions. Then in your recursive
processContent
, check if any transformation applies before processing children. This keeps the recursion logic flatter and more declarative.For instance, you can refactor like this:
This approach maintains functionality while reducing nested conditional logic.