/* eslint-disable no-unused-vars -- components used only inside JSX look unused to this config, which has no eslint-plugin-react */
// The details of a GitHub write that waits for the person, inside the host's
// approval frame: what it does, where, as which connection, and the exact
// text that will be posted. The frame owns Confirm and Cancel; this card only
// draws.
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { connectorsApi } from '../../../internal/connector-ui/src/api'
import { Notice } from '../../../internal/connector-ui/src/controls'
import { connectionsFor, proposalView } from './proposal'
import './github.css'
function useConnections(repo) {
const [names, setNames] = useState(null)
useEffect(() => {
let live = true
if (!repo) return undefined
connectorsApi.list().then((r) => { if (live) setNames(connectionsFor(r.connectors, repo)) }).catch(() => { if (live) setNames([]) })
return () => { live = false }
}, [repo])
return names
}
function Row({ label, children, testId }) {
return (
{label}
{children}
)
}
export default function ProposalCard({ tool, effects, arguments: args }) {
const { t } = useTranslation('github')
const v = proposalView(tool, args, effects?.sample)
const names = useConnections(v.repo)
const asText = names === null || names.length === 0
? t('approval.asUnknown', { repo: v.repo })
: names.length === 1 ? t('approval.asOne', { name: names[0] }) : t('approval.asFirst', { repo: v.repo })
const hasText = v.body !== ''
const stateText = v.state === 'closed'
? (v.reason ? t('approval.closeReason', { reason: t(`approval.${v.reason}`, { defaultValue: v.reason }) }) : t('approval.close'))
: v.state === 'open' ? t('approval.reopen') : ''
return (
{t(`approval.action.${v.tool}`, { number: v.number, defaultValue: v.tool })}
{v.repo && (
{v.url
? {t('approval.repo', { repo: v.repo })} #{v.number}
: t('approval.repo', { repo: v.repo })}
{v.targetTitle && {v.targetTitle}}
)}
{asText}
{v.tool === 'create-issue' && v.title && {v.title}
}
{v.tool === 'edit-issue' && v.title && {v.title}
}
{v.tool === 'edit-issue' && stateText && {stateText}
}
{v.labels.length > 0 && {v.labels.join(', ')}
}
{v.path && {t('approval.fileLine', { path: v.path, line: v.line, side: v.side === 'LEFT' ? t('approval.sideLeft') : t('approval.sideRight') })}
}
{hasText && (
<>
{t('approval.text')}
{v.body}
{v.cut &&
{t('approval.cut', { bytes: v.bytes })}
}
>
)}
{v.repo &&
{t('approval.public', { repo: v.repo })}}
)
}