From a8f9e185fdf444770a843ae6675c37a94fbb41fb Mon Sep 17 00:00:00 2001 From: ElRodrigote Date: Tue, 24 Dec 2024 14:59:44 +0100 Subject: [PATCH 1/4] fix: update the frequency card logic to support our default strategies --- .../stackbox/FrequencyOptionsCard.tsx | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/app/components/stackbox/FrequencyOptionsCard.tsx b/packages/app/components/stackbox/FrequencyOptionsCard.tsx index 4c1d66da..3e5c73c1 100644 --- a/packages/app/components/stackbox/FrequencyOptionsCard.tsx +++ b/packages/app/components/stackbox/FrequencyOptionsCard.tsx @@ -2,13 +2,32 @@ import { ChangeEvent, useEffect, useState } from "react"; -import { add } from "date-fns"; +import { add, daysToWeeks } from "date-fns"; import { twMerge } from "tailwind-merge"; +import { useStrategyContext } from "@/contexts"; import { FREQUENCY_OPTIONS } from "@/models"; import { BodyText, RadioButton, TextInput } from "@/ui"; import { cx } from "class-variance-authority"; +const parseDaysToFrequencyAmount = ( + days: number, + frequency: FREQUENCY_OPTIONS +) => { + switch (frequency) { + case FREQUENCY_OPTIONS.hour: + return days * 24; + case FREQUENCY_OPTIONS.day: + return days; + case FREQUENCY_OPTIONS.week: + return daysToWeeks(days); + case FREQUENCY_OPTIONS.month: + return days / 30; + default: + throw new Error("Invalid frequency option"); + } +}; + interface FrequencyOptionsCardProps { frequency: FREQUENCY_OPTIONS; setEndDate: (date: Date) => void; @@ -63,6 +82,8 @@ export const FrequencyOptionsCard = ({ ); const [customFrequency, setCustomFrequency] = useState(""); + const { selectedStrategy } = useStrategyContext(); + const handleCustomFrequencyChange = ( event: ChangeEvent ) => { @@ -97,6 +118,26 @@ export const FrequencyOptionsCard = ({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [defaultFrequency, customFrequency]); + useEffect(() => { + if (selectedStrategy) { + const frequencyAmount = parseDaysToFrequencyAmount( + selectedStrategy.daysAmount, + frequency + ).toString(); + + const isDefaultStrategy = + defaultFrequencyOptions[frequency].includes(frequencyAmount); + + if (isDefaultStrategy) { + setDefaultFrequency(frequencyAmount); + setCustomFrequency(""); + } else { + setCustomFrequency(frequencyAmount); + setDefaultFrequency(""); + } + } + }, [frequency, selectedStrategy]); + return (
Date: Tue, 24 Dec 2024 15:00:09 +0100 Subject: [PATCH 2/4] chore: update our default strategies to fit in our frequency constrains --- packages/app/components/strategies/constants.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/app/components/strategies/constants.tsx b/packages/app/components/strategies/constants.tsx index 8c7d9250..324ae423 100644 --- a/packages/app/components/strategies/constants.tsx +++ b/packages/app/components/strategies/constants.tsx @@ -93,7 +93,7 @@ export const STRATEGY_CATEGORIES: { [chainId: number]: ChainStrategies } = { { id: 3, buyToken: gnosisTokens.WETH, - daysAmount: 10, + daysAmount: 4, frequency: FREQUENCY_OPTIONS.hour, sellAmountPerTimeframe: 5, sellToken: gnosisTokens.USDC, @@ -136,7 +136,7 @@ export const STRATEGY_CATEGORIES: { [chainId: number]: ChainStrategies } = { { id: 3, buyToken: arbitrumTokens.WETH, - daysAmount: 10, + daysAmount: 4, frequency: FREQUENCY_OPTIONS.hour, sellAmountPerTimeframe: 5, sellToken: arbitrumTokens.USDC, @@ -179,7 +179,7 @@ export const STRATEGY_CATEGORIES: { [chainId: number]: ChainStrategies } = { { id: 3, buyToken: baseTokens.CBBTC, - daysAmount: 10, + daysAmount: 4, frequency: FREQUENCY_OPTIONS.hour, sellAmountPerTimeframe: 5, sellToken: baseTokens.USDC, From 5bbb08571243456f0d72714c22b2a1e59e5c1e27 Mon Sep 17 00:00:00 2001 From: ElRodrigote Date: Tue, 24 Dec 2024 15:06:47 +0100 Subject: [PATCH 3/4] feat: deselect current strategy when we change the frequency values --- packages/app/components/stackbox/FrequencyOptionsCard.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/app/components/stackbox/FrequencyOptionsCard.tsx b/packages/app/components/stackbox/FrequencyOptionsCard.tsx index 3e5c73c1..910022d3 100644 --- a/packages/app/components/stackbox/FrequencyOptionsCard.tsx +++ b/packages/app/components/stackbox/FrequencyOptionsCard.tsx @@ -82,7 +82,7 @@ export const FrequencyOptionsCard = ({ ); const [customFrequency, setCustomFrequency] = useState(""); - const { selectedStrategy } = useStrategyContext(); + const { deselectStrategy, selectedStrategy } = useStrategyContext(); const handleCustomFrequencyChange = ( event: ChangeEvent @@ -103,6 +103,8 @@ export const FrequencyOptionsCard = ({ setDefaultFrequency(""); setCustomFrequency(newValue); } + + if (selectedStrategy) deselectStrategy(); }; useEffect(() => { @@ -161,6 +163,7 @@ export const FrequencyOptionsCard = ({ onChange={(event) => { setDefaultFrequency(event.target.value as FREQUENCY_OPTIONS); if (!!customFrequency) setCustomFrequency(""); + if (selectedStrategy) deselectStrategy(); }} value={freqOption} > From 22605e31100dc436ded181de13d0bc4807f51427 Mon Sep 17 00:00:00 2001 From: ElRodrigote Date: Fri, 27 Dec 2024 13:14:14 +0100 Subject: [PATCH 4/4] chore: update the frequency parsing util fn --- .../stackbox/FrequencyOptionsCard.tsx | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/app/components/stackbox/FrequencyOptionsCard.tsx b/packages/app/components/stackbox/FrequencyOptionsCard.tsx index 910022d3..4d7277f6 100644 --- a/packages/app/components/stackbox/FrequencyOptionsCard.tsx +++ b/packages/app/components/stackbox/FrequencyOptionsCard.tsx @@ -10,24 +10,6 @@ import { FREQUENCY_OPTIONS } from "@/models"; import { BodyText, RadioButton, TextInput } from "@/ui"; import { cx } from "class-variance-authority"; -const parseDaysToFrequencyAmount = ( - days: number, - frequency: FREQUENCY_OPTIONS -) => { - switch (frequency) { - case FREQUENCY_OPTIONS.hour: - return days * 24; - case FREQUENCY_OPTIONS.day: - return days; - case FREQUENCY_OPTIONS.week: - return daysToWeeks(days); - case FREQUENCY_OPTIONS.month: - return days / 30; - default: - throw new Error("Invalid frequency option"); - } -}; - interface FrequencyOptionsCardProps { frequency: FREQUENCY_OPTIONS; setEndDate: (date: Date) => void; @@ -73,6 +55,26 @@ const getCroppedFrequency = (frequency: FREQUENCY_OPTIONS) => { return isMonthFrequency ? frequency.substring(0, 2) : frequency.charAt(0); }; +const parseDaysToFrequencyAmount = ( + days: number, + frequency: FREQUENCY_OPTIONS +) => { + switch (frequency) { + case FREQUENCY_OPTIONS.hour: + return days * 24; + case FREQUENCY_OPTIONS.day: + return days; + case FREQUENCY_OPTIONS.week: + return daysToWeeks(days); + case FREQUENCY_OPTIONS.month: + return days / 30; + default: { + console.error("Invalid frequency option", frequency); + return maxCustomFrequencies[frequency]; + } + } +}; + export const FrequencyOptionsCard = ({ frequency, setEndDate,