improvement(ui): remove React anti-patterns, fix CSP violations by waleedlatif1 · Pull Request #4203 · simstudioai/sim · GitHub
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
33 changes: 25 additions & 8 deletions .claude/commands/you-might-not-need-a-callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export function InlineRenameInput({ value, onChange, onSubmit, onCancel }: Inlin
ref={inputRef}
type='text'
value={value}
size={Math.max(value.length + 2, 5)}
onChange={(e) => onChange(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') onSubmit()
if (e.key === 'Escape') onCancel()
}}
onBlur={onSubmit}
onClick={(e) => e.stopPropagation()}
className='min-w-0 flex-1 truncate border-0 bg-transparent p-0 font-medium text-[var(--text-body)] text-sm outline-none focus:outline-none focus:ring-0'
className='min-w-0 border-0 bg-transparent p-0 font-medium text-[var(--text-body)] text-sm outline-none focus:outline-none focus:ring-0'
/>
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import { memo, useEffect, useRef, useState } from 'react'
import {
Button,
Check,
Expand Down Expand Up @@ -50,7 +50,12 @@ interface MessageActionsProps {
requestId?: string
}

export function MessageActions({ content, chatId, userQuery, requestId }: MessageActionsProps) {
export const MessageActions = memo(function MessageActions({
content,
chatId,
userQuery,
requestId,
}: MessageActionsProps) {
const [copied, setCopied] = useState(false)
const [copiedRequestId, setCopiedRequestId] = useState(false)
const [pendingFeedback, setPendingFeedback] = useState<'up' | 'down' | null>(null)
Expand All @@ -70,7 +75,7 @@ export function MessageActions({ content, chatId, userQuery, requestId }: Messag
}
}, [])

const copyToClipboard = useCallback(async () => {
const copyToClipboard = async () => {
if (!content) return
const text = toPlainText(content)
if (!text) return
Expand All @@ -84,9 +89,9 @@ export function MessageActions({ content, chatId, userQuery, requestId }: Messag
} catch {
/* clipboard unavailable */
}
}, [content])
}

const copyRequestId = useCallback(async () => {
const copyRequestId = async () => {
if (!requestId) return
try {
await navigator.clipboard.writeText(requestId)
Expand All @@ -98,20 +103,17 @@ export function MessageActions({ content, chatId, userQuery, requestId }: Messag
} catch {
/* clipboard unavailable */
}
}, [requestId])
}

const handleFeedbackClick = useCallback(
(type: 'up' | 'down') => {
if (chatId && userQuery) {
setPendingFeedback(type)
setFeedbackText('')
setCopiedRequestId(false)
}
},
[chatId, userQuery]
)
const handleFeedbackClick = (type: 'up' | 'down') => {
if (chatId && userQuery) {
setPendingFeedback(type)
setFeedbackText('')
setCopiedRequestId(false)
}
}

const handleSubmitFeedback = useCallback(() => {
const handleSubmitFeedback = () => {
if (!pendingFeedback || !chatId || !userQuery) return
const text = feedbackText.trim()
if (!text) {
Expand All @@ -128,15 +130,15 @@ export function MessageActions({ content, chatId, userQuery, requestId }: Messag
})
setPendingFeedback(null)
setFeedbackText('')
}, [pendingFeedback, chatId, userQuery, content, feedbackText])
}

const handleModalClose = useCallback((open: boolean) => {
const handleModalClose = (open: boolean) => {
if (!open) {
setPendingFeedback(null)
setFeedbackText('')
setCopiedRequestId(false)
}
}, [])
}

if (!content) return null

Expand Down Expand Up @@ -224,4 +226,4 @@ export function MessageActions({ content, chatId, userQuery, requestId }: Messag
</Modal>
</>
)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface HeaderAction {
icon?: React.ElementType
onClick: () => void
disabled?: boolean
active?: boolean
}

export interface CreateAction {
Expand Down Expand Up @@ -103,7 +104,13 @@ export const ResourceHeader = memo(function ResourceHeader({
onClick={action.onClick}
disabled={action.disabled}
variant='subtle'
className='px-2 py-1 text-caption'
className={cn(
'px-2 py-1 text-caption',
action.active !== undefined && 'rounded-lg',
action.active === true &&
'bg-[var(--surface-active)] hover-hover:bg-[var(--surface-active)]',
action.active === false && 'hover-hover:bg-[var(--surface-hover)]'
)}
>
{ActionIcon && (
<ActionIcon
Expand Down
Loading
Loading