-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
220 lines (193 loc) · 5.07 KB
/
utils.ts
File metadata and controls
220 lines (193 loc) · 5.07 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
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
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
const IMAGEKIT_URL = 'https://ik.imagekit.io/odzx7thry/hack_stack'
export const TECH_TAGS = [
'Open Source',
'SaaS',
'New',
]
export type ColorVariant =
| 'success'
| 'warning'
| 'danger'
| 'default'
| 'primary'
| 'secondary'
| undefined
export const colorMap: Record<string, ColorVariant> = {
category: 'secondary',
block: 'warning',
tech: 'danger'
}
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function getIconAsset(icon: string) {
return `/assets/icons/${icon}.svg`
}
export function convertFileToBase64(file: File) {
return new Promise<string>((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result as string)
reader.onerror = (error) => reject(error)
reader.readAsDataURL(file)
})
}
export function getTechLogo(tech: string, theme: string | undefined) {
const techExtension = tech?.split('.')?.pop() ?? 'svg'
const logoName =
theme === 'dark'
? `${tech?.replace(techExtension, `dark.${techExtension}`)}`
: tech
return `${IMAGEKIT_URL}/logos/${logoName}`
}
export function getRandomBackground() {
return `${Math.floor(Math.random() * 25) + 1}.jpeg`
}
export function getCardBackground(bg: string) {
return `${IMAGEKIT_URL}/backgrounds/${bg}`
}
export function getRandomHackStackEmailBanner() {
const logos = ['clair', 'skylar', 'jesse', 'harper']
const randomIndex = Math.floor(Math.random() * logos.length)
return `${IMAGEKIT_URL}/emails/hackstack-${logos[randomIndex]}.png`
}
export function getHackStackFigure(figure: string) {
return `${IMAGEKIT_URL}/emails/hackstack-${figure}.png`
}
export function getEmailLogo() {
return `${IMAGEKIT_URL}/emails/icon.png`
}
export function getUniqueHueStyle(str: string) {
let hash = 0
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = (hash << 5) - hash + char
}
const hueRotateValue = Math.abs(hash) % 360
return {
filter: `hue-rotate(-${hueRotateValue}deg)`
}
}
export function generateRandomStackName() {
const adjectives = [
'Agile',
'Dynamic',
'Robust',
'Innovative',
'Modern',
'Scalable',
'Elastic',
'Cloud-native',
'Microservices',
'Responsive',
'Automated',
'Decentralized',
'Smart',
'Adaptive',
'Efficient',
'Versatile',
'Harmonious',
'Optimized',
'Intelligent',
'Proactive',
'Secure',
'Collaborative',
'Distributed',
'Synergistic',
'Resilient',
'Efficient',
'Interactive',
'Seamless',
'Streamlined'
]
const nouns = [
'Phoenix',
'Quantum',
'Fusion',
'Matrix',
'Pulse',
'Nova',
'Zenith',
'Orbit',
'Velocity',
'Cyber',
'Sprint',
'Harmony',
'Cosmic',
'Inferno',
'Ignition',
'Gravity',
'Nebula',
'Galaxy',
'Aurora',
'Horizon',
'Vortex',
'Pinnacle',
'Expanse',
'Catalyst',
'Cortex',
'Pinnacle',
'Venture',
'Apex',
'Horizon',
'Catalyst'
]
const getRandomItem = (words: string[]) =>
words[Math.floor(Math.random() * words.length)]
const randomAdjective = getRandomItem(adjectives)
const randomNoun = getRandomItem(nouns)
return `${randomAdjective} ${randomNoun} Stack`
}
export const formatNumber = (num: number) => {
if (num < 1000) {
return num.toString()
}
if (num < 1000000) {
return `${(num / 1000).toFixed(0)}k`
}
return `${(num / 1000000).toFixed(0)}M`
}
export const timeAgo = (date: string | number, includeTime = false) => {
const currentDate = new Date()
const inputDate = new Date(date)
const timeDifference = currentDate.getTime() - inputDate.getTime()
const secondsInMs = 1000
const minutesInMs = secondsInMs * 60
const hoursInMs = minutesInMs * 60
const daysInMs = hoursInMs * 24
const monthsInMs = daysInMs * 30 // Assuming a month is 30 days for simplicity
const yearsInMs = daysInMs * 365 // Assuming a year is 365 days for simplicity
const yearsAgo = Math.floor(timeDifference / yearsInMs)
const monthsAgo = Math.floor((timeDifference % yearsInMs) / monthsInMs)
const daysAgo = Math.floor((timeDifference % monthsInMs) / daysInMs)
const hoursAgo = Math.floor((timeDifference % daysInMs) / hoursInMs)
const minutesAgo = Math.floor((timeDifference % hoursInMs) / minutesInMs)
let result = ''
if (yearsAgo > 0) {
result += yearsAgo + (yearsAgo === 1 ? ' year' : ' years')
}
if (monthsAgo > 0) {
result +=
(result ? ', ' : '') +
monthsAgo +
(monthsAgo === 1 ? ' month' : ' months')
}
if (daysAgo > 0) {
result +=
(result ? ', ' : '') + daysAgo + (daysAgo === 1 ? ' day' : ' days')
}
if (includeTime) {
if (hoursAgo > 0) {
result +=
(result ? ', ' : '') + hoursAgo + (hoursAgo === 1 ? ' hour' : ' hours')
}
if (minutesAgo > 0) {
result +=
(result ? ', ' : '') +
minutesAgo +
(minutesAgo === 1 ? ' minute' : ' minutes')
}
}
return result ? `${result} ago` : 'Just now'
}