Refactor to use hooks (#436)
This commit is contained in:
parent
124efca9a1
commit
cb60f8b143
139 changed files with 8871 additions and 8726 deletions
149
lib/utils.ts
149
lib/utils.ts
|
|
@ -1,134 +1,43 @@
|
|||
import { clsx, type ClassValue } from 'clsx'
|
||||
import { customAlphabet } from 'nanoid'
|
||||
import { twMerge } from 'tailwind-merge'
|
||||
import { clsx, type ClassValue } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export const nanoid = customAlphabet(
|
||||
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
||||
7
|
||||
) // 7-character random string
|
||||
interface ApplicationError extends Error {
|
||||
info: string;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export async function fetcher<JSON = any>(
|
||||
input: RequestInfo,
|
||||
init?: RequestInit
|
||||
): Promise<JSON> {
|
||||
const res = await fetch(input, init)
|
||||
export const fetcher = async (url: string) => {
|
||||
const res = await fetch(url);
|
||||
|
||||
if (!res.ok) {
|
||||
const json = await res.json()
|
||||
if (json.error) {
|
||||
const error = new Error(json.error) as Error & {
|
||||
status: number
|
||||
}
|
||||
error.status = res.status
|
||||
throw error
|
||||
} else {
|
||||
throw new Error('An unexpected error occurred')
|
||||
}
|
||||
const error = new Error(
|
||||
"An error occurred while fetching the data.",
|
||||
) as ApplicationError;
|
||||
|
||||
error.info = await res.json();
|
||||
error.status = res.status;
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
return res.json()
|
||||
}
|
||||
return res.json();
|
||||
};
|
||||
|
||||
export function formatDate(input: string | number | Date): string {
|
||||
const date = new Date(input)
|
||||
return date.toLocaleDateString('en-US', {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
year: 'numeric'
|
||||
})
|
||||
}
|
||||
|
||||
export const formatNumber = (value: number) =>
|
||||
new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: 'USD'
|
||||
}).format(value)
|
||||
|
||||
export const runAsyncFnWithoutBlocking = (
|
||||
fn: (...args: any) => Promise<any>
|
||||
) => {
|
||||
fn()
|
||||
}
|
||||
|
||||
export const sleep = (ms: number) =>
|
||||
new Promise(resolve => setTimeout(resolve, ms))
|
||||
|
||||
export const getStringFromBuffer = (buffer: ArrayBuffer) =>
|
||||
Array.from(new Uint8Array(buffer))
|
||||
.map(b => b.toString(16).padStart(2, '0'))
|
||||
.join('')
|
||||
|
||||
export enum ResultCode {
|
||||
InvalidCredentials = 'INVALID_CREDENTIALS',
|
||||
InvalidSubmission = 'INVALID_SUBMISSION',
|
||||
UserAlreadyExists = 'USER_ALREADY_EXISTS',
|
||||
UnknownError = 'UNKNOWN_ERROR',
|
||||
UserCreated = 'USER_CREATED',
|
||||
UserLoggedIn = 'USER_LOGGED_IN'
|
||||
}
|
||||
|
||||
export const getMessageFromCode = (resultCode: string) => {
|
||||
switch (resultCode) {
|
||||
case ResultCode.InvalidCredentials:
|
||||
return 'Invalid credentials!'
|
||||
case ResultCode.InvalidSubmission:
|
||||
return 'Invalid submission, please try again!'
|
||||
case ResultCode.UserAlreadyExists:
|
||||
return 'User already exists, please log in!'
|
||||
case ResultCode.UserCreated:
|
||||
return 'User created, welcome!'
|
||||
case ResultCode.UnknownError:
|
||||
return 'Something went wrong, please try again!'
|
||||
case ResultCode.UserLoggedIn:
|
||||
return 'Logged in!'
|
||||
export function getLocalStorage(key: string) {
|
||||
if (typeof window !== "undefined") {
|
||||
return JSON.parse(localStorage.getItem(key) || "[]");
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export function format(date: Date, formatString: string) {
|
||||
const year = date.getFullYear()
|
||||
const month = date.getMonth()
|
||||
const day = date.getDate()
|
||||
const hours = String(date.getHours()).padStart(2, '0')
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0')
|
||||
const seconds = String(date.getSeconds()).padStart(2, '0')
|
||||
|
||||
const monthNames = [
|
||||
'Jan',
|
||||
'Feb',
|
||||
'Mar',
|
||||
'Apr',
|
||||
'May',
|
||||
'Jun',
|
||||
'Jul',
|
||||
'Aug',
|
||||
'Sep',
|
||||
'Oct',
|
||||
'Nov',
|
||||
'Dec'
|
||||
]
|
||||
|
||||
return formatString
|
||||
.replace('yyyy', year.toString())
|
||||
.replace('yy', String(year).slice(-2))
|
||||
.replace('LLL', monthNames[month])
|
||||
.replace('MM', String(month + 1).padStart(2, '0'))
|
||||
.replace('dd', String(day).padStart(2, '0'))
|
||||
.replace('d', day.toString())
|
||||
.replace('HH', hours)
|
||||
.replace('mm', minutes)
|
||||
.replace('ss', seconds)
|
||||
}
|
||||
|
||||
export function parseISO(dateString: string) {
|
||||
return new Date(dateString)
|
||||
}
|
||||
|
||||
export function subMonths(date: Date, amount: number) {
|
||||
const newDate: Date = new Date(date)
|
||||
newDate.setMonth(newDate.getMonth() - amount)
|
||||
return newDate
|
||||
export function generateUUID(): string {
|
||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
||||
const r = (Math.random() * 16) | 0;
|
||||
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue