-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspotlight.tsx
105 lines (82 loc) · 3.49 KB
/
spotlight.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
import React, { useContext } from 'react';
import { Context } from '../../lib/store';
const SpotLight = () => {
const {state, dispatch} = useContext(Context) as AppStoreContextType;
const currState = React.useRef(state);
const [ show , setShow ] = React.useState(false);
const [ keysPressed , setKeys ] = React.useState<Record<string, boolean>>({});
const onKeyDown = (e : KeyboardEvent) => {
var map = keysPressed;
map[e.key] = true;
// control and space together
if ( ( map[17] || map['Meta']) && (map[32] || map[' ']) ){
setShow(true);
}
setKeys(map);
}
const onKeyUp = (e: KeyboardEvent) => {
var map = keysPressed;
delete map[e.key];
setKeys(map);
}
const onClick = (e: MouseEvent) => {
var spot = document.getElementById('spotlight'), targetEl = e.target as any;
do {
if (targetEl === spot) {
return;
}
targetEl = targetEl!.parentNode;
} while (targetEl!.parentNode && targetEl);
// outside
show && setShow(false);
}
React.useEffect(() => {
document.addEventListener('keydown', onKeyDown , false );
document.addEventListener('keyup', onKeyUp , false );
document.addEventListener('click' , onClick , false );
return () => {
document.removeEventListener('keydown' , onKeyDown , false );
document.removeEventListener('keyup' , onKeyUp , false );
document.removeEventListener('click' , onClick , false );
}
}, []);
React.useEffect(() => {
currState.current = state;
}, [state]);
const validOptions = [ 'terminal.app', 'mail.app' , 'finder.app' ];
const [ value , setValue ] = React.useState('');
const onClickSpot = (application: string) => {
setValue(application);
}
const getSuggestions = () => {
return validOptions.map( app =>{
if ( app.indexOf(value) !== -1)
return <div key={app} onClick={() => onClickSpot(app)} className="text-white text-md p-2 hover:bg-gray-100 rounded-xl hover:text-black">{app}</div>
})
}
const handleKeyDown = (e: React.KeyboardEvent) => {
if ( e.key === 'Enter' ){
var index = validOptions.indexOf(value);
if ( index === -1 ){
setShow(false);
return;
}
if ( index === 0) {
dispatch({ type: "NEW_WINDOW", payload: { id: 'TERMINAL', header: true, title: "Terminal" } })
} else if ( index === 1 ){
dispatch({ type: "NEW_WINDOW", payload: { id: 'MAIL', header: true, title: "Mail" } })
} else if ( index === 2 ){
dispatch({ type: "NEW_WINDOW", payload: { id: 'PROFILE', header: true, title: "Finder" } })
}
setShow(false);
}
}
return (
<div id="spotlight" className={ "absolute top-1/4 left-1/4 z-10 w-6/12 flex flex-col h-30 rounded-xl " + ( show ? "" : "hidden")} style={{backgroundColor : "rgb(224, 252, 242, 0.6)"}} >
<input value={value} onChange={(e) => setValue(e.target.value)} onKeyDown={handleKeyDown}
className="focus:outline-none border-b-2 border-gray-400 h-4 w-11/12 bg-transparent ml-4 p-5 pl-2 pr-1"></input>
<div>{getSuggestions()}</div>
</div>
)
}
export default SpotLight;