-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDurationController.tsx
142 lines (128 loc) · 4.39 KB
/
DurationController.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useEffect, useRef } from "react";
import { TabList, Tab } from "@/components/ui/tab-list";
import { BottomSheetHeader } from "@/components/ui/bottom-sheet-header";
import { DurationValueList } from "./components/DurationValueList";
import { HoursDurationValue } from "./components/HoursDurationValue";
import { useTradeStore } from "@/stores/tradeStore";
import { useDeviceDetection } from "@/hooks/useDeviceDetection";
import { PrimaryButton } from "@/components/ui/primary-button";
import { generateDurationValues as getDurationValues } from "@/utils/duration";
import { useBottomSheetStore } from "@/stores/bottomSheetStore";
import { useDebounce } from "@/hooks/useDebounce";
import { DesktopTradeFieldCard } from "@/components/ui/desktop-trade-field-card";
import type { DurationRangesResponse } from "@/services/api/rest/duration/types";
const DURATION_TYPES: Tab[] = [
{ label: "Ticks", value: "tick" },
{ label: "Seconds", value: "second" },
{ label: "Minutes", value: "minute" },
{ label: "Hours", value: "hour" },
// { label: "End Time", value: "day" },
] as const;
type DurationType = keyof DurationRangesResponse;
interface DurationControllerProps {
onClose?: () => void;
}
export const DurationController: React.FC<DurationControllerProps> = ({
onClose,
}) => {
const { duration, setDuration } = useTradeStore();
const { isDesktop } = useDeviceDetection();
const { setBottomSheet } = useBottomSheetStore();
const isInitialRender = useRef(true);
useEffect(() => {
isInitialRender.current = true;
return () => {
isInitialRender.current = false;
};
}, []);
// Initialize local state for both mobile and desktop
const [localDuration, setLocalDuration] = React.useState(duration);
const [value, type] = localDuration.split(" ");
const selectedType = type as DurationType;
const selectedValue: string | number =
type === "hour" ? value : parseInt(value, 10);
// Use debounced updates for desktop scroll
useDebounce(
localDuration,
(value) => {
if (isDesktop) {
setDuration(value);
}
},
300
);
const handleTypeSelect = (type: DurationType) => {
const newDuration =
type === "hour" ? "1:0 hour" : `${getDurationValues(type)[0]} ${type}`;
setLocalDuration(newDuration);
};
const handleValueSelect = (value: number | string) => {
const newDuration = `${value} ${selectedType}`;
setLocalDuration(newDuration);
};
const handleValueClick = (value: number | string) => {
const newDuration = `${value} ${selectedType}`;
setLocalDuration(newDuration);
setDuration(newDuration); // Update store immediately on click
if (isDesktop) {
onClose?.();
}
};
const handleSave = () => {
setDuration(localDuration);
if (isDesktop) {
onClose?.();
} else {
setBottomSheet(false);
}
};
const content = (
<>
<div className={isDesktop ? "flex" : ""}>
{!isDesktop && <BottomSheetHeader title="Duration" />}
<TabList
tabs={DURATION_TYPES}
selectedValue={selectedType}
onSelect={handleTypeSelect as (value: string) => void}
variant={isDesktop ? "vertical" : "chip"}
/>
<div className={`flex-1 relative bg-white ${isDesktop ? "px-2" : "px-8"}`}>
{selectedType === "hour" ? (
<HoursDurationValue
selectedValue={selectedValue.toString()}
onValueSelect={(value) => {
handleValueSelect(value);
}}
onValueClick={handleValueClick}
isInitialRender={isInitialRender}
/>
) : (
<DurationValueList
key={selectedType}
selectedValue={selectedValue as number}
durationType={selectedType}
onValueSelect={handleValueSelect}
onValueClick={handleValueClick}
getDurationValues={getDurationValues}
/>
)}
</div>
</div>
{!isDesktop && (
<div className="w-full p-3">
<PrimaryButton className="rounded-3xl" onClick={handleSave}>
Save
</PrimaryButton>
</div>
)}
</>
);
if (isDesktop) {
return (
<DesktopTradeFieldCard className="p-0">
<div className="w-[368px]">{content}</div>
</DesktopTradeFieldCard>
);
}
return <div className="flex flex-col h-full">{content}</div>;
};