import { Listbox, Transition } from '@headlessui/react'
import { PythCluster } from '@pythnetwork/client'
import { MultisigInstruction } from '@pythnetwork/xc-admin-common'
import { getInstructionsSummary } from './utils'
import { getMappingCluster } from '../../InstructionViews/utils'
import CopyText from '../../common/CopyText'
import Arrow from '@images/icons/down.inline.svg'
import { Fragment, useState, useMemo, useContext } from 'react'
import { usePythContext } from '../../../contexts/PythContext'
import { ClusterContext } from '../../../contexts/ClusterContext'
export const InstructionsSummary = ({
instructions,
cluster,
}: {
instructions: MultisigInstruction[]
cluster: PythCluster
}) => (
{getInstructionsSummary({ instructions, cluster }).map((instruction) => (
))}
)
const SummaryItem = ({
instruction,
}: {
instruction: ReturnType[number]
}) => {
switch (instruction.name) {
case 'addPublisher':
case 'delPublisher': {
return (
{instruction.name}: {instruction.count}
)
}
default: {
return (
{instruction.name}: {instruction.count}
)
}
}
}
type AddRemovePublisherDetailsProps = {
isAdd: boolean
summaries: {
readonly priceAccount: string
readonly pub: string
}[]
}
const AddRemovePublisherDetails = ({
isAdd,
summaries,
}: AddRemovePublisherDetailsProps) => {
const { cluster } = useContext(ClusterContext)
const { priceAccountKeyToSymbolMapping, publisherKeyToNameMapping } =
usePythContext()
const publisherKeyToName =
publisherKeyToNameMapping[getMappingCluster(cluster)]
const [groupBy, setGroupBy] = useState<'publisher' | 'price account'>(
'publisher'
)
const grouped = useMemo(
() =>
Object.groupBy(summaries, (summary) =>
groupBy === 'publisher' ? summary.pub : summary.priceAccount
),
[groupBy, summaries]
)
return (
{groupBy === 'publisher' ? 'Publisher' : 'Price Account'}
{groupBy === 'publisher'
? isAdd
? 'Added To'
: 'Removed From'
: `${isAdd ? 'Added' : 'Removed'} Publishers`}
{Object.entries(grouped).map(([groupKey, summaries = []]) => (
<>
{groupKey}
{summaries.map((summary, index) => (
-
{groupBy === 'publisher'
? summary.priceAccount
: summary.pub}
))}
>
))}
)
}
const KeyAndName = ({
mapping,
children,
}: {
mapping: { [key: string]: string }
children: string
}) => {
const name = useMemo(() => mapping[children], [mapping, children])
return (
)
}
type SelectProps = {
items: T[]
value: T
onChange: (newValue: T) => void
}
const Select = ({
items,
value,
onChange,
}: SelectProps) => (
{({ open }) => (
<>
{value}
{items.map((item) => (
{item}
))}
>
)}
)