🎉 feat: initialization
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import { removeCookie, setCookie } from "@workspace/ui/lib/cookies";
|
||||
import { isBrowser } from "@workspace/ui/utils/index";
|
||||
import { intlFormat } from "date-fns";
|
||||
|
||||
export function getPlatform(): string {
|
||||
if (typeof window === "undefined") return "unknown";
|
||||
|
||||
const userAgent = navigator.userAgent.toLowerCase();
|
||||
|
||||
if (userAgent.includes("win")) return "windows";
|
||||
if (userAgent.includes("mac")) return "macos";
|
||||
if (userAgent.includes("linux")) return "linux";
|
||||
if (userAgent.includes("android")) return "android";
|
||||
if (userAgent.includes("iphone") || userAgent.includes("ipad")) return "ios";
|
||||
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
export function differenceInDays(date1: Date, date2: Date): number {
|
||||
const diffTime = Math.abs(date1.getTime() - date2.getTime());
|
||||
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
|
||||
return diffDays;
|
||||
}
|
||||
|
||||
export function formatDate(date?: Date | number, showTime = true) {
|
||||
if (!date) return;
|
||||
const timeZone = localStorage.getItem("timezone") || "UTC";
|
||||
return intlFormat(date, {
|
||||
year: "numeric",
|
||||
month: "numeric",
|
||||
day: "numeric",
|
||||
...(showTime && {
|
||||
hour: "numeric",
|
||||
minute: "numeric",
|
||||
second: "numeric",
|
||||
}),
|
||||
hour12: false,
|
||||
timeZone,
|
||||
});
|
||||
}
|
||||
|
||||
export function setAuthorization(token: string): void {
|
||||
setCookie("Authorization", token);
|
||||
}
|
||||
|
||||
export function getRedirectUrl(): string {
|
||||
if (typeof window === "undefined") return "/dashboard";
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const redirect = params.get("redirect");
|
||||
return redirect?.startsWith("/") ? redirect : "/dashboard";
|
||||
}
|
||||
|
||||
export function setRedirectUrl(value?: string) {
|
||||
if (value) {
|
||||
sessionStorage.setItem("redirect-url", value);
|
||||
}
|
||||
}
|
||||
|
||||
export function Logout() {
|
||||
if (!isBrowser()) return;
|
||||
removeCookie("Authorization");
|
||||
const pathname = location.pathname;
|
||||
if (!["", "/"].includes(pathname)) {
|
||||
setRedirectUrl(location.pathname);
|
||||
location.href = "/";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user