import { InformationCircleIcon } from '@heroicons/react/outline' import { ChevronDown, Plus, Trash2 } from 'lucide-react' import { useEffect, useState } from 'react' import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, Slider_Shadcn_, cn, } from 'ui' import { ComputeBadge } from 'ui-patterns/ComputeBadge' import pricingAddOn from '~/data/PricingAddOnTable.json' const plans = [ { name: 'Pro', price: 25, }, { name: 'Team', price: 599, }, ] const findIntanceValueByColumn = (instance: any, column: string) => instance.columns?.find((col: any) => col.key === column)?.value const parsePrice = (price: string) => parseInt(price?.toString().replace('$', '').replace(',', '')) const ComputePricingCalculator = () => { const computeInstances = pricingAddOn.database.rows const priceSteps = computeInstances.map((instance) => parsePrice(findIntanceValueByColumn(instance, 'pricing')) ) // Base discount credits that come with every paid plan const COMPUTE_CREDITS = 10 const [activePlan, setActivePlan] = useState(plans[0]) const [activeInstances, setActiveInstances] = useState([{ ...computeInstances[0], position: 0 }]) // Final calculated price: plan cost + compute aggregate - compute credits const [activePrice, setActivePrice] = useState(activePlan.price + priceSteps[0] - COMPUTE_CREDITS) useEffect(() => { setActivePrice(activePlan.price + priceSteps[0] - COMPUTE_CREDITS) }, []) const handleUpdateInstance = (position: number, value: number[]) => { const newArray = activeInstances.map((activeInstance) => { // only update the instance corresponding to the correct slider if (activeInstance.position === position) { return { ...computeInstances[value[0] - 1], position } } else { return activeInstance } }) setActiveInstances(newArray) } const calculateComputeAggregate = (price: number) => { activeInstances.map( (activeInstance: any) => (price += parsePrice(findIntanceValueByColumn(activeInstance, 'pricing'))) ) return price } const calculatePrice = () => { let aggregatePrice = 0 const computeAggregate = calculateComputeAggregate(aggregatePrice) return setActivePrice(computeAggregate + activePlan.price - COMPUTE_CREDITS) } useEffect(() => { calculatePrice() }, [activeInstances, activePlan]) const removeInstance = (position: number) => { const newArray = activeInstances .filter((activeInstance) => activeInstance.position !== position) .map((instance, index) => { instance.position = index return instance }) setActiveInstances(newArray) } const findSliderComputeValue = (activeInstance: any) => { // find index of compute based on active compute name const selectedCompute = computeInstances .map((compute) => findIntanceValueByColumn(compute, 'plan')) .indexOf(findIntanceValueByColumn(activeInstance, 'plan')) return [selectedCompute + 1] } const PriceSummary = () => (
Project {activeInstance.position + 1}