-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubersicht-clock.jsx
224 lines (206 loc) · 5.6 KB
/
ubersicht-clock.jsx
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Command to get current time and username
*
*/
export const command = "date '+%H %M %S %p | %a, %d %b, %Y'; whoami";
// the refresh frequency in milliseconds
export const refreshFrequency = 1000;
/**
* Export language file
*/
import languages from './lang.json';
export const className =
`
top: 30%;
left: 0;
transform: translate(-0%, 0%);
width: 100%;
box-sizing: border-box;
color: #ffffff;
font-family: "space age", -apple-system, BlinkMacSystemFont, Verdana, "Helvetica Neue", Helvetica, sans-serif;
font-weight: 600;
border-radius: 1px;
text-align: justify;
line-height: 1.3;
text-shadow: 0px 0px 2px rgba(0,0,0,0.50);
.container {
display: flex;
flex-direction: column;
}
.time {
font-size: 9.5rem;
text-align: center;
margin-bottom: 0rem;
margin-left: auto;
margin-right: auto;
background: transparent;
}
.date {
font-size: 4.5rem;
text-align: center;
margin-bottom: 0;
margin-top: 1rem;
margin-left: auto;
margin-right: auto;
background: transparent;
}
.flasher-hide {
opacity: 0;
}
.flasher-show {
opacity: 1;
}
.am_pm {
font-size: 2.625rem;
margin: 0;
}
.greeting {
font-size: 3rem;
text-align: center;
margin-top: 0;
margin-left: auto;
margin-right: auto;
}
`;
/**
* Decide if zero should be padded to hour example: 9:30PM => 09:30PM
*/
export const usePadZero = false; // true/false
/**
* Decide if time separator should flashed
*/
export const flashTimeSeparator = true; // true/false
/**
* Decide if seconds should be displayed
*/
export const displaySeconds = false; // true/false
/**
* Decide if 24h time should be converted to military time example: 13:30 => 1:30PM
*/
export const useMilitaryTime = true; // true/false
/**
* Enable date in widget
*/
export const showDate = false; // true or false
/**
* toLocaleDateString dateStyle option
*/
export const dataFormat = {
full: 'full', // Thursday, February 17, 2022
long: 'long', // February 17, 2022
medium: 'medium', // Feb 17, 2022
short: 'short' // 2/17/2022
};
/**
*
* @param {string} dateString A valid date string
* @param {string} style full, long, medium, short
* @param {string} lang local language short code e.g en/en-US for american english
* @returns string
*/
export const setDateStyle = (dateString, style, lang) => {
return new Date(dateString).toLocaleDateString(lang, { dateStyle: style })
}
/**
* Apply css class based on flashTimeSeparator value
*
* @param {string|number} seconds The current second
* @returns {string} css class
*/
export const UseFlashedTimeSeparator = (seconds) => {
if (flashTimeSeparator) {
return (seconds % 2 === 0) ? "flasher-show" : "flasher-hide";
}
return "flasher-show";
};
/**
* add/pad zero to hour less than 10
*
* @param {string|number} number The current hour
* @returns {string|number}
*/
export const padZero = (number) => {
if (usePadZero && useMilitaryTime) {
return (number < 10) ? "0" + number : number;
}
return number;
};
/**
* Translate greeting message to user browser language.
*
* @param {string} processLang The user browser language 2 letter code
* @param {string|number} time The current time(hour)
* @returns {string} The translated greet message if not english
*/
export const translate = (processLang, time) => {
let greeting = "";
if (processLang === 'en') {
greeting = "Good morning";
if (time >= 12 && time < 17) {
greeting = "Good afternoon";
} else if ((time > 17) || (time < 5)) {
greeting = "Good evening";
}
} else {
greeting = languages[processLang][0]["Good morning"];
if (time >= 12 && time < 17) {
greeting = languages[processLang][1]["Good afternoon"];
} else if ((time > 17) || (time < 5)) {
greeting = languages[processLang][2]["Good evening"];
}
}
return greeting;
};
/**
* Convert hours to military time
*
* @param {string} hour the current hour.
*
* @return {number|string} The converted hour, if useMilitaryTime is set to true or 24hour time
*/
export const HoursToMilitaryTime = (hour) => {
if (useMilitaryTime) {
let parsedHour = parseInt(hour);
if (parsedHour > 12)
return parsedHour - 12;
if (parsedHour === 0)
return 12;
return parsedHour;
}
return hour;
};
export const render = ({ output }) => {
// split the whoami & date command output.
const commandValues = output.split("\n");
const username = commandValues[1];
const datetime = commandValues[0].split('|');
const timeArray = datetime[0].split(' ');
let hour = timeArray[0];
const minutes = timeArray[1];
const seconds = timeArray[2];
const AM_PM = timeArray[3];
const userLang = navigator.language || navigator.userLanguage;
const processLang = userLang.substr(0, 2);
const greeting = translate(processLang, hour);
const class_name = UseFlashedTimeSeparator(seconds);
// change the key (default is b => dateFormat.full) keys are full,long,medium,short
const date = setDateStyle(datetime[1], dataFormat.full, userLang);
hour = padZero(HoursToMilitaryTime(hour));
return (
<div className="container">
<h1 className="time">
{hour}
<span className={class_name}>:</span>
{minutes}
{(displaySeconds) ? <span className={class_name}>:</span> : ""}
{(displaySeconds) ? seconds : ""}
{(useMilitaryTime) ? <span className="am_pm">{AM_PM}</span> : ""}
</h1>
{(showDate) ? <h2 className="date"> {date} </h2> : ""}
<p className="greeting">
<span>{greeting}</span>{", "}
<span className="username">{username}</span>
</p>
</div>
);
};