-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseResourceCardState.js
More file actions
73 lines (59 loc) · 1.84 KB
/
Copy pathuseResourceCardState.js
File metadata and controls
73 lines (59 loc) · 1.84 KB
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
import { useState, useEffect } from "react";
import { useConfig } from "../../../hooks/useConfig";
import useResourceStore from "../../../store/resourceStore";
const useResourceCardState = (resource) => {
// UI state
const [isExpanded, setIsExpanded] = useState(false);
const [showYaml, setShowYaml] = useState(false);
const [showLogs, setShowLogs] = useState(false);
const [confirmUrl, setConfirmUrl] = useState(null);
const [confirmAction, setConfirmAction] = useState(null);
// User permissions - fetch from global config cache
const { data: config } = useConfig();
const userType = config?.userType;
// Action timing
const [realtimeElapsed, setRealtimeElapsed] = useState(0);
// Get action loading state from store
const { actionLoading } = useResourceStore();
const actionKey = `${resource.type}-${resource.uuid}`;
const currentAction = actionLoading[actionKey];
// Handle action timing updates
useEffect(() => {
if (!currentAction) {
setRealtimeElapsed(0);
return;
}
setRealtimeElapsed(currentAction.elapsedSeconds);
const interval = setInterval(() => {
setRealtimeElapsed((prev) => prev + 1);
}, 1000);
return () => clearInterval(interval);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentAction?.action]);
// Sync with store elapsed time
useEffect(() => {
if (currentAction?.elapsedSeconds) {
setRealtimeElapsed(currentAction.elapsedSeconds);
}
}, [currentAction?.elapsedSeconds]);
return {
// UI state
isExpanded,
setIsExpanded,
// Modal states
showYaml,
setShowYaml,
showLogs,
setShowLogs,
confirmUrl,
setConfirmUrl,
confirmAction,
setConfirmAction,
// User permissions
userType,
// Action state
currentAction,
realtimeElapsed,
};
};
export default useResourceCardState;