|
| 1 | +import { ProgramType } from '@pythnetwork/xc-admin-common' |
| 2 | +import { useProgramContext } from '../contexts/ProgramContext' |
| 3 | +import { Menu, Transition } from '@headlessui/react' |
| 4 | +import { Fragment } from 'react' |
| 5 | + |
| 6 | +const Arrow = ({ className }: { className?: string }) => ( |
| 7 | + <svg |
| 8 | + className={className} |
| 9 | + width="10" |
| 10 | + height="6" |
| 11 | + viewBox="0 0 10 6" |
| 12 | + fill="none" |
| 13 | + xmlns="http://www.w3.org/2000/svg" |
| 14 | + > |
| 15 | + <path |
| 16 | + d="M1 1L5 5L9 1" |
| 17 | + stroke="currentColor" |
| 18 | + strokeWidth="1.5" |
| 19 | + strokeLinecap="round" |
| 20 | + strokeLinejoin="round" |
| 21 | + /> |
| 22 | + </svg> |
| 23 | +) |
| 24 | + |
| 25 | +/** |
| 26 | + * Component that allows users to switch between different Pyth programs |
| 27 | + * (Core, Lazer, etc.) |
| 28 | + */ |
| 29 | +const ProgramSwitch = ({ light = false }: { light?: boolean }) => { |
| 30 | + const { programType, setProgramType } = useProgramContext() |
| 31 | + |
| 32 | + // Convert enum to array of options |
| 33 | + const programOptions = Object.values(ProgramType).map((value) => ({ |
| 34 | + value, |
| 35 | + // Format display name (e.g., "PYTH_CORE" -> "Pyth Core") |
| 36 | + label: value |
| 37 | + .split('_') |
| 38 | + .map((word) => word.charAt(0) + word.slice(1).toLowerCase()) |
| 39 | + .join(' '), |
| 40 | + })) |
| 41 | + |
| 42 | + return ( |
| 43 | + <Menu as="div" className="relative z-[3] block w-[180px] text-left"> |
| 44 | + {({ open }) => ( |
| 45 | + <> |
| 46 | + <Menu.Button |
| 47 | + className={`inline-flex w-full items-center justify-between py-3 px-6 text-sm outline-0 ${ |
| 48 | + light ? 'bg-beige2' : 'bg-darkGray2' |
| 49 | + }`} |
| 50 | + > |
| 51 | + <span className="mr-3"> |
| 52 | + {programOptions.find((option) => option.value === programType) |
| 53 | + ?.label || programType} |
| 54 | + </span> |
| 55 | + <Arrow className={`${open ? 'rotate-180' : ''}`} /> |
| 56 | + </Menu.Button> |
| 57 | + <Transition |
| 58 | + as={Fragment} |
| 59 | + enter="transition ease-out duration-100" |
| 60 | + enterFrom="transform opacity-0 scale-95" |
| 61 | + enterTo="transform opacity-100 scale-100" |
| 62 | + leave="transition ease-in duration-75" |
| 63 | + leaveFrom="transform opacity-100 scale-100" |
| 64 | + leaveTo="transform opacity-0 scale-95" |
| 65 | + > |
| 66 | + <Menu.Items className="absolute right-0 mt-2 w-full origin-top-right"> |
| 67 | + {programOptions.map((option) => ( |
| 68 | + <Menu.Item key={option.value}> |
| 69 | + {({ active }) => ( |
| 70 | + <button |
| 71 | + className={`block w-full py-3 px-6 text-left text-sm ${ |
| 72 | + light |
| 73 | + ? active |
| 74 | + ? 'bg-beige3' |
| 75 | + : 'bg-beige2' |
| 76 | + : active |
| 77 | + ? 'bg-darkGray2' |
| 78 | + : 'bg-darkGray' |
| 79 | + }`} |
| 80 | + onClick={() => |
| 81 | + setProgramType(option.value as ProgramType) |
| 82 | + } |
| 83 | + > |
| 84 | + {option.label} |
| 85 | + </button> |
| 86 | + )} |
| 87 | + </Menu.Item> |
| 88 | + ))} |
| 89 | + </Menu.Items> |
| 90 | + </Transition> |
| 91 | + </> |
| 92 | + )} |
| 93 | + </Menu> |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +export default ProgramSwitch |
0 commit comments