-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
App.svelte
243 lines (216 loc) · 4.81 KB
/
App.svelte
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<script>
import qs from 'querystringify'
import {
selectTextOnFocus,
blurOnEscape,
getLocalStorageBoolean,
detectLocale,
detectTimeZone,
encodeSnowflake,
decodeSnowflake,
isSnowy,
} from './util.js'
import Help from './Help.svelte'
import Output from './Output.svelte'
import Share from './Share.svelte'
import Credits from './Credits.svelte'
import { validateSnowflake } from './convert'
import Switch from './Switch.svelte'
import IconMoon from './IconMoon.svelte'
import IconSun from './IconSun.svelte'
import LetItSnow from './LetItSnow.svelte'
import { onMount } from 'svelte'
const dynamicMode = window.__SNOWSTAMP_DYNAMIC__
const { SNOWFLAKE_EPOCH } = process.env
const EPOCH = +SNOWFLAKE_EPOCH || undefined
const queries = qs.parse(location.search)
let snowflake = queries.s || (queries.f && decodeSnowflake(queries.f)) || '',
timestamp,
error,
url
let shareStamp = getLocalStorageBoolean('shareStamp', true)
let shortenSnowflake = getLocalStorageBoolean('shortenSnowflake', true)
let darkMode = getLocalStorageBoolean('darkMode', true)
let letItSnow, snowComponent
let locale, tz
$: updateSnowflake(snowflake)
$: dynamicMode && updateShareOptions(shareStamp, shortenSnowflake)
$: {
if (darkMode) window.document.body.classList.add('dark-mode')
else window.document.body.classList.remove('dark-mode')
localStorage.setItem('darkMode', darkMode)
}
onMount(() =>
setTimeout(() => window.document.body.classList.remove('preload'))
)
// Validate snowflake and update timestamp or error
function updateSnowflake() {
timestamp = null
error = null
letItSnow = false
if (!snowflake.trim()) return
try {
timestamp = validateSnowflake(snowflake, EPOCH)
updateURL()
} catch (e) {
if (isSnowy(snowflake)) {
letItSnow = true
} else {
error = e.message || e
}
}
}
// Update share options
function updateShareOptions() {
localStorage.setItem('shareStamp', shareStamp)
localStorage.setItem('shortenSnowflake', shortenSnowflake)
updateURL()
}
// Update the URL
function updateURL() {
const query = {}
if (timestamp) {
if (dynamicMode && shareStamp) {
if (locale === undefined) locale = detectLocale()
if (locale) query.l = locale
if (tz === undefined) tz = detectTimeZone()
query.z = tz
}
if (shortenSnowflake) {
query.f = encodeSnowflake(snowflake)
} else {
query.s = snowflake
}
}
window.history.replaceState(null, null, qs.stringify(query, '?'))
url = window.location.href
}
</script>
<main>
<hgroup>
<h2 on:click={() => snowComponent.addSnowflake()}>
<img src="logo-header.png" height="113" alt="Snow-Stamp" />
</h2>
<h1>Discord Snowflake to Timestamp Converter</h1>
</hgroup>
<div id="dark-toggle">
<div style="display:inline-block;position:relative; top:13px; right:2px;">
{#if darkMode}
<IconMoon />
{:else}
<IconSun />
{/if}
</div>
<Switch bind:checked={darkMode} />
</div>
<p style="margin-bottom: 0.5em;">
Paste in a Discord snowflake to get the timestamp
</p>
<Help />
<div
style="position: relative; display: inline-flex; margin-bottom: 2em; width: 100%;"
>
<div class="input-icon">❄</div>
<input
type="text"
bind:value={snowflake}
use:selectTextOnFocus
use:blurOnEscape
placeholder="e.g. 86913608335773696"
/>
</div>
{#if timestamp}
<Output {timestamp} {darkMode} />
<!-- TODO: Make this a button within the output box -->
<Share
bind:url
bind:shareStamp
bind:shortenSnowflake
{timestamp}
{dynamicMode}
/>
<hr />
{/if}
{#if error}
<p style="margin-top: 0.2em;">❌ {error}</p>
{/if}
<LetItSnow bind:this={snowComponent} {letItSnow} />
<Credits />
</main>
<style>
main {
text-align: center;
max-width: 364px;
margin: 0 auto;
position: relative;
display: flex;
flex-direction: column;
box-sizing: border-box;
padding: 20px 8px 4px;
min-height: 100%;
}
main h2 {
margin: 0;
user-select: none;
cursor: pointer;
display: inline-block;
}
h1 {
color: #008dad;
font-size: 2em;
margin: 8px 0 0;
}
:global(body.dark-mode) h1 {
color: #69eaff;
}
#dark-toggle {
position: absolute;
top: 8px;
right: 8px;
}
p {
font-size: 1.3em;
}
input {
font-size: 2.5em;
width: 100%;
margin: 0;
padding: 0.5em 0.5em 0.5em 2em;
}
.input-icon {
display: inline-block;
position: absolute;
font-size: 2.5em;
line-height: 1;
height: 100%;
opacity: 0.6;
top: 4px;
padding-top: 0.5em;
padding-left: 0.5em;
}
hr {
border: 1px solid #ccc;
width: 380px;
margin-block-start: 0;
margin-block-end: 0;
}
:global(body.dark-mode) hr {
border: 1px solid #343b3f;
}
@media (min-width: 750px) {
main {
max-width: 716px;
}
}
@media (max-width: 749px) {
input {
font-size: 1.3em;
}
.input-icon {
font-size: 1.3em;
}
hr {
width: 240px;
}
}
</style>