Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

2.1 KB
Raw Blame
import { memo, useMemo, useState } from 'react'
import { useTranslation } from '@/i18n/react-i18next-compat'
import { ChevronRightIcon } from 'lucide-react'
import { cn } from '@/lib/utils'
import { Citations, type WebCitation } from '@/components/Citations'
import { faviconForUrl } from '@/lib/webUrl'
import { useRemoteImagesAllowed } from '@/lib/extensions/remoteImages'

export const WebSourcesRow = memo(
  ({ citations }: { citations: WebCitation[] }) => {
    const { t } = useTranslation()
    const [expanded, setExpanded] = useState(false)
    const remoteImages = useRemoteImagesAllowed()

    const unique = useMemo(() => {
      const seen = new Set<string>()
      const out: WebCitation[] = []
      for (const c of citations) {
        if (seen.has(c.url)) continue
        seen.add(c.url)
        out.push(c)
      }
      return out
    }, [citations])

    if (!unique.length) return null

    const preview = unique.slice(0, 4)

    return (
      <div className="mt-3">
        <button
          type="button"
          onClick={() => setExpanded((v) => !v)}
          className="inline-flex items-center gap-2 rounded-full border bg-card/40 py-1 pl-1.5 pr-2.5 text-xs text-muted-foreground transition-colors hover:bg-card/70"
          aria-expanded={expanded}
        >
          {remoteImages && (
            <span className="flex -space-x-1.5">
              {preview.map((c) => (
                <img
                  key={c.url}
                  src={c.favicon || faviconForUrl(c.url)}
                  alt=""
                  className="size-4 rounded-full border border-border/60 bg-white object-contain"
                />
              ))}
            </span>
          )}
          <span className="font-medium">
            {t('chat:webSources', { count: unique.length })}
          </span>
          <ChevronRightIcon
            className={cn(
              'size-3 shrink-0 transition-transform',
              expanded && 'rotate-90'
            )}
          />
        </button>
        {expanded && (
          <Citations payload={{ kind: 'web', citations: unique }} />
        )}
      </div>
    )
  }
)
WebSourcesRow.displayName = 'WebSourcesRow'