Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

1.9 KB
Raw Blame
import { useEffect, useState } from 'react'
import { isImageName } from '@shared/models'
import { Icon } from '../../components/Icon'
import { Text } from '../../components/Text'
import type { OutgoingFile } from '../../store/outgoing-files'
import { attachmentSymbol } from './Attachments'
import { useTr } from '../../lib/use-language'

/**
 * A local attachment, shown before it is sent: images as a whole-image thumbnail with the
 * remove button in the corner, anything else (or an image that won't decode) as a name chip.
 */
export function ComposerAttachment({ file, remove }: { file: OutgoingFile; remove: () => void }) {
  const tr = useTr()
  const [url, setUrl] = useState<string | null>(null)
  const [broken, setBroken] = useState(false)

  useEffect(() => {
    if (!isImageName(file.name)) return
    const objectURL = URL.createObjectURL(new Blob([file.data as BlobPart]))
    setUrl(objectURL)
    return () => URL.revokeObjectURL(objectURL)
  }, [file])

  if (url && !broken) {
    return (
      <span className="attachment-tile" aria-label={tr('Image attachment: {name}').replaceAll('{name}', () => file.name)} title={file.name}>
        <img src={url} alt="" onError={() => setBroken(true)} />
        <button className="press attachment-remove" aria-label={`Remove ${file.name}`} title="Remove" onClick={remove}>
          <Icon name="xmark" size={11} weight="bold" color="#fff" />
        </button>
      </span>
    )
  }
  return (
    <span className="file-chip">
      <Icon name={attachmentSymbol(file.name)} size={10} color="var(--secondary)" />
      <Text size="footnote" tone="text" truncate className="file-chip-name">{file.name}</Text>
      <button className="file-chip-remove" aria-label={`Remove ${file.name}`} title="Remove" onClick={remove}>
        <Icon name="xmark" size={10} weight="bold" color="var(--secondary)" />
      </button>
    </span>
  )
}