forked from DustinBrett/daedalOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseProcessContextState.ts
More file actions
146 lines (140 loc) · 3.67 KB
/
useProcessContextState.ts
File metadata and controls
146 lines (140 loc) · 3.67 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
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
143
144
145
146
import { useCallback, useState } from "react";
import {
closeProcess,
maximizeProcess,
minimizeProcess,
openProcess,
setIcon,
setProcessArgument,
setProcessElement,
setTitle,
setUrl,
} from "contexts/process/functions";
import {
type ProcessArguments,
type ProcessElements,
type Processes,
} from "contexts/process/types";
import { TRANSITIONS_IN_MILLISECONDS } from "utils/constants";
type ProcessContextState = {
argument: (
id: string,
name: keyof ProcessArguments,
value: ProcessArguments[keyof ProcessArguments]
) => void;
close: (id: string, closing?: boolean) => void;
closeProcessesByUrl: (closeUrl: string) => void;
closeWithTransition: (id: string) => void;
icon: (id: string, newIcon: string) => void;
linkElement: (
id: string,
name: keyof ProcessElements,
element: HTMLElement
) => void;
maximize: (id: string) => void;
minimize: (id: string) => void;
open: (
id: string,
processArguments?: ProcessArguments,
icon?: string
) => void;
processes: Processes;
title: (id: string, newTitle: string) => void;
url: (id: string, newUrl: string) => void;
};
const useProcessContextState = (): ProcessContextState => {
const [processes, setProcesses] = useState<Processes>(
Object.create(null) as Processes
);
const argument = useCallback(
(
id: string,
name: keyof ProcessArguments,
value: ProcessArguments[keyof ProcessArguments]
) => setProcesses(setProcessArgument(id, name, value)),
[]
);
const close = useCallback(
(id: string, closing?: boolean) => setProcesses(closeProcess(id, closing)),
[]
);
const icon = useCallback(
(id: string, newIcon: string) => setProcesses(setIcon(id, newIcon)),
[]
);
const maximize = useCallback(
(id: string) => setProcesses(maximizeProcess(id)),
[]
);
const minimize = useCallback(
(id: string) => setProcesses(minimizeProcess(id)),
[]
);
const open = useCallback(
(id: string, processArguments?: ProcessArguments, initialIcon?: string) => {
if (id === "ExternalURL") {
const { url: externalUrl = "" } = processArguments || {};
if (
externalUrl.startsWith("http:") ||
externalUrl.startsWith("https:")
) {
window.open(
decodeURIComponent(externalUrl),
"_blank",
"noopener,noreferrer"
);
}
} else {
setProcesses(openProcess(id, processArguments || {}, initialIcon));
}
},
[]
);
const linkElement = useCallback(
(id: string, name: keyof ProcessElements, element: HTMLElement) =>
setProcesses(setProcessElement(id, name, element)),
[]
);
const title = useCallback(
(id: string, newTitle: string) => setProcesses(setTitle(id, newTitle)),
[]
);
const url = useCallback(
(id: string, newUrl: string) => setProcesses(setUrl(id, newUrl)),
[]
);
const closeWithTransition = useCallback(
(id: string): void => {
close(id, true);
window.setTimeout(() => close(id), TRANSITIONS_IN_MILLISECONDS.WINDOW);
},
[close]
);
const closeProcessesByUrl = useCallback(
(closeUrl: string): void =>
setProcesses((currentProcesses) => {
Object.entries(currentProcesses).forEach(
([id, { url: processUrl }]) => {
if (processUrl === closeUrl) closeWithTransition(id);
}
);
return currentProcesses;
}),
[closeWithTransition]
);
return {
argument,
close,
closeProcessesByUrl,
closeWithTransition,
icon,
linkElement,
maximize,
minimize,
open,
processes,
title,
url,
};
};
export default useProcessContextState;