forked from sindresorhus/emoj
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
208 lines (173 loc) · 4.66 KB
/
ui.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
const React = require('react');
const {useState, useCallback, useEffect} = require('react');
const {Box, Text, useApp, useInput} = require('ink');
const TextInput = require('ink-text-input').default;
const skinTone = require('skin-tone');
const mem = require('mem');
const emoj = require('.');
// From https://usehooks.com/useDebounce/
const useDebouncedValue = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(timer);
};
}, [value, delay]);
return debouncedValue;
};
// Limit it to 7 results so not to overwhelm the user
// This also reduces the chance of showing unrelated emojis
const fetch = mem(async string => {
const array = await emoj(string);
return array.slice(0, 7);
});
const STAGE_CHECKING = 0;
const STAGE_SEARCH = 1;
const STAGE_COPIED = 2;
const QueryInput = ({query, placeholder, onChange}) => (
<Box>
<Text bold color="cyan">
›{' '}
</Text>
<TextInput showCursor={false} value={query} placeholder={placeholder} onChange={onChange}/>
</Box>
);
const CopiedMessage = ({emoji}) => (
<Text color="green">
{`${emoji} has been copied to the clipboard`}
</Text>
);
const Search = ({query, emojis, skinNumber, selectedIndex, onChangeQuery}) => {
const list = emojis.map((emoji, index) => (
<Text
key={emoji}
backgroundColor={index === selectedIndex && 'gray'}
>
{' '}
{skinTone(emoji, skinNumber)}
{' '}
</Text>
));
return (
<Box flexDirection="column" paddingTop={1} paddingBottom={emojis.length === 0 ? 2 : 0}>
<QueryInput
query={query}
placeholder="Relevant emojis will appear when you start writing"
onChange={onChangeQuery}
/>
<Box paddingTop={1}>
{list}
</Box>
</Box>
);
};
const Emoj = ({skinNumber: initialSkinNumber, onSelectEmoji}) => {
const {exit} = useApp();
const [stage, setStage] = useState(STAGE_CHECKING);
const [query, setQuery] = useState('');
const [emojis, setEmojis] = useState([]);
const [skinNumber, setSkinNumber] = useState(initialSkinNumber);
const [selectedIndex, setSelectedIndex] = useState(0);
const [selectedEmoji, setSelectedEmoji] = useState();
useEffect(() => {
if (selectedEmoji && stage === STAGE_COPIED) {
onSelectEmoji(selectedEmoji);
}
}, [selectedEmoji, stage, onSelectEmoji]);
const changeQuery = useCallback(query => {
setSelectedIndex(0);
setEmojis([]);
setQuery(query);
});
useEffect(() => {
setStage(STAGE_SEARCH);
}, []);
const debouncedQuery = useDebouncedValue(query, 200);
useEffect(() => {
if (debouncedQuery.length <= 1) {
return;
}
let isCanceled = false;
const run = async () => {
const emojis = await fetch(debouncedQuery);
// Don't update state when this effect was canceled to avoid
// results that don't match the search query
if (!isCanceled) {
setEmojis(emojis);
}
};
run();
return () => {
isCanceled = true;
};
}, [debouncedQuery]);
useInput((input, key) => {
if (key.escape || (key.ctrl && input === 'c')) {
exit();
return;
}
if (key.return) {
if (emojis.length > 0) {
setSelectedEmoji(skinTone(emojis[selectedIndex], skinNumber));
setStage(STAGE_COPIED);
}
return;
}
// Select emoji by typing a number
// Catch all 10 keys, but handle only the same amount of keys
// as there are currently emojis
const numKey = Number(input);
if (numKey >= 0 && numKey <= 9) {
if (numKey >= 1 && numKey <= emojis.length) {
setSelectedEmoji(skinTone(emojis[numKey - 1], skinNumber));
setStage(STAGE_COPIED);
}
return;
}
// Filter out all ansi sequences except the up/down keys which change the skin tone
// and left/right keys which select emoji inside a list
const isArrowKey = key.upArrow || key.downArrow || key.leftArrow || key.rightArrow;
if (!isArrowKey || query.length <= 1) {
return;
}
if (key.upArrow && skinNumber < 5) {
setSkinNumber(skinNumber + 1);
}
if (key.downArrow && skinNumber > 0) {
setSkinNumber(skinNumber - 1);
}
if (key.rightArrow) {
if (selectedIndex < emojis.length - 1) {
setSelectedIndex(selectedIndex + 1);
} else {
setSelectedIndex(0);
}
}
if (key.leftArrow) {
if (selectedIndex > 0) {
setSelectedIndex(selectedIndex - 1);
} else {
setSelectedIndex(emojis.length - 1);
}
}
});
return (
<>
{stage === STAGE_COPIED && <CopiedMessage emoji={selectedEmoji}/>}
{stage === STAGE_SEARCH && (
<Search
query={query}
emojis={emojis}
skinNumber={skinNumber}
selectedIndex={selectedIndex}
onChangeQuery={changeQuery}
/>
)}
</>
);
};
module.exports = Emoj;