@tradejs/app 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ 'use client';
2
+
3
+ import { useEffect, useMemo, useRef, useState } from 'react';
4
+ import { API } from '@tradejs/core/api';
5
+ import { toaster } from '#ui';
6
+ import { FIXED_SYMBOLS, type ChartWindow } from './derivativesDashboardConfig';
7
+ import { loadDerivativesDashboardData } from './derivativesDashboardLoader';
8
+ import {
9
+ buildDerivativesDashboardViewModel,
10
+ type DerivativesInterval,
11
+ type DetailRow,
12
+ type PriceRow,
13
+ type SummaryResponse,
14
+ } from './derivativesViewModel';
15
+
16
+ export const useDerivativesDashboard = () => {
17
+ const [hours, setHours] = useState('24');
18
+ const [selectedInterval, setSelectedInterval] =
19
+ useState<DerivativesInterval>('1h');
20
+ const [summary, setSummary] = useState<SummaryResponse | null>(null);
21
+ const [detailsBySymbol, setDetailsBySymbol] = useState<
22
+ Record<string, DetailRow[]>
23
+ >({});
24
+ const [pricesBySymbol, setPricesBySymbol] = useState<
25
+ Record<string, PriceRow[]>
26
+ >({});
27
+ const [chartWindow, setChartWindow] = useState<ChartWindow>(() => {
28
+ const endTimestamp = Date.now();
29
+ return {
30
+ startTimestamp: endTimestamp - 24 * 60 * 60 * 1000,
31
+ endTimestamp,
32
+ };
33
+ });
34
+ const [loading, setLoading] = useState(false);
35
+ const [error, setError] = useState('');
36
+ const requestRevision = useRef(0);
37
+
38
+ useEffect(() => {
39
+ const revision = ++requestRevision.current;
40
+ setLoading(true);
41
+ setError('');
42
+
43
+ void loadDerivativesDashboardData({
44
+ hours,
45
+ selectedInterval,
46
+ client: API,
47
+ })
48
+ .then((data) => {
49
+ if (requestRevision.current !== revision) return;
50
+ setSummary(data.summary);
51
+ setDetailsBySymbol(data.detailsBySymbol);
52
+ setPricesBySymbol(data.pricesBySymbol);
53
+ setChartWindow(data.chartWindow);
54
+ })
55
+ .catch((loadError) => {
56
+ if (requestRevision.current !== revision) return;
57
+ const message =
58
+ (loadError as Error)?.message || 'Failed to load derivatives';
59
+ setError(message);
60
+ toaster.error({
61
+ title: 'Failed to load derivatives',
62
+ description: message,
63
+ });
64
+ })
65
+ .finally(() => {
66
+ if (requestRevision.current === revision) setLoading(false);
67
+ });
68
+
69
+ return () => {
70
+ if (requestRevision.current === revision) requestRevision.current += 1;
71
+ };
72
+ }, [hours, selectedInterval]);
73
+
74
+ const dashboard = useMemo(
75
+ () =>
76
+ buildDerivativesDashboardViewModel({
77
+ symbols: FIXED_SYMBOLS,
78
+ selectedInterval,
79
+ summary,
80
+ detailsBySymbol,
81
+ pricesBySymbol,
82
+ summaryLoading: loading,
83
+ detailLoading: loading,
84
+ summaryError: error,
85
+ detailError: error,
86
+ }),
87
+ [
88
+ detailsBySymbol,
89
+ error,
90
+ loading,
91
+ pricesBySymbol,
92
+ selectedInterval,
93
+ summary,
94
+ ],
95
+ );
96
+
97
+ return {
98
+ hours,
99
+ setHours,
100
+ selectedInterval,
101
+ setSelectedInterval,
102
+ chartWindow,
103
+ dashboard,
104
+ };
105
+ };
@@ -4,7 +4,10 @@ import { Suspense, useCallback, useEffect, useMemo, useState } from 'react';
4
4
  import { Box, Button, ClientOnly, Flex } from '@chakra-ui/react';
5
5
  import { usePathname } from 'next/navigation';
6
6
  import { FiFolder } from 'react-icons/fi';
7
- import type { StrategyChartsSnapshotResponse } from '@tradejs/types';
7
+ import type {
8
+ RuntimeStrategiesResponse,
9
+ StrategyChartsSnapshotResponse,
10
+ } from '@tradejs/types';
8
11
  import {
9
12
  deleteStrategyCard,
10
13
  getAiStrategies,
@@ -20,7 +23,6 @@ import {
20
23
  BulkDeleteToolbar,
21
24
  useBulkSelection,
22
25
  } from '#components/Shared/BulkSelection';
23
- import type { RuntimeStrategiesResponse } from '#app/lib/runtimeStrategyContracts';
24
26
  import { CARD_PAGE_CONTENT_MAX_WIDTH } from '#app/lib/cardPageLayout';
25
27
  import { EmptyState, Segment, Select, toaster } from '#ui';
26
28