From e5367e9a1a5e58bc062df2ce32e0f47e8578205f Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Sat, 18 May 2024 15:19:11 +0700 Subject: [PATCH] prettier format --- src/components/ui/toaster.tsx | 30 +++----- src/components/ui/use-toast.ts | 133 ++++++++++++++++----------------- src/context/layoutContext.tsx | 6 +- src/context/themeContext.tsx | 6 +- src/hooks/useLocalStorage.ts | 3 +- src/types/schema.ts | 15 +--- src/utils/encryptionHelper.ts | 24 ++---- tailwind.config.ts | 7 +- 8 files changed, 89 insertions(+), 135 deletions(-) diff --git a/src/components/ui/toaster.tsx b/src/components/ui/toaster.tsx index 8b67a2d..1c0e5ed 100644 --- a/src/components/ui/toaster.tsx +++ b/src/components/ui/toaster.tsx @@ -1,35 +1,29 @@ -"use client" +"use client"; -import { - Toast, - ToastClose, - ToastDescription, - ToastProvider, - ToastTitle, - ToastViewport, -} from "~/components/ui/toast" -import { useToast } from "~/components/ui/use-toast" +import { Toast, ToastClose, ToastDescription, ToastProvider, ToastTitle, ToastViewport } from "~/components/ui/toast"; +import { useToast } from "~/components/ui/use-toast"; export function Toaster() { - const { toasts } = useToast() + const { toasts } = useToast(); return ( {toasts.map(function ({ id, title, description, action, ...props }) { return ( - -
+ +
{title && {title}} - {description && ( - {description} - )} + {description && {description}}
{action}
- ) + ); })} - ) + ); } diff --git a/src/components/ui/use-toast.ts b/src/components/ui/use-toast.ts index d6698ef..84c8ad7 100644 --- a/src/components/ui/use-toast.ts +++ b/src/components/ui/use-toast.ts @@ -1,78 +1,75 @@ -"use client" +"use client"; // Inspired by react-hot-toast library -import * as React from "react" +import * as React from "react"; -import type { - ToastActionElement, - ToastProps, -} from "~/components/ui/toast" +import type { ToastActionElement, ToastProps } from "~/components/ui/toast"; -const TOAST_LIMIT = 1 -const TOAST_REMOVE_DELAY = 1000000 +const TOAST_LIMIT = 1; +const TOAST_REMOVE_DELAY = 1000000; type ToasterToast = ToastProps & { - id: string - title?: React.ReactNode - description?: React.ReactNode - action?: ToastActionElement -} + id: string; + title?: React.ReactNode; + description?: React.ReactNode; + action?: ToastActionElement; +}; const actionTypes = { ADD_TOAST: "ADD_TOAST", UPDATE_TOAST: "UPDATE_TOAST", DISMISS_TOAST: "DISMISS_TOAST", REMOVE_TOAST: "REMOVE_TOAST", -} as const +} as const; -let count = 0 +let count = 0; function genId() { - count = (count + 1) % Number.MAX_SAFE_INTEGER - return count.toString() + count = (count + 1) % Number.MAX_SAFE_INTEGER; + return count.toString(); } -type ActionType = typeof actionTypes +type ActionType = typeof actionTypes; type Action = | { - type: ActionType["ADD_TOAST"] - toast: ToasterToast + type: ActionType["ADD_TOAST"]; + toast: ToasterToast; } | { - type: ActionType["UPDATE_TOAST"] - toast: Partial + type: ActionType["UPDATE_TOAST"]; + toast: Partial; } | { - type: ActionType["DISMISS_TOAST"] - toastId?: ToasterToast["id"] + type: ActionType["DISMISS_TOAST"]; + toastId?: ToasterToast["id"]; } | { - type: ActionType["REMOVE_TOAST"] - toastId?: ToasterToast["id"] - } + type: ActionType["REMOVE_TOAST"]; + toastId?: ToasterToast["id"]; + }; interface State { - toasts: ToasterToast[] + toasts: ToasterToast[]; } -const toastTimeouts = new Map>() +const toastTimeouts = new Map>(); const addToRemoveQueue = (toastId: string) => { if (toastTimeouts.has(toastId)) { - return + return; } const timeout = setTimeout(() => { - toastTimeouts.delete(toastId) + toastTimeouts.delete(toastId); dispatch({ type: "REMOVE_TOAST", toastId: toastId, - }) - }, TOAST_REMOVE_DELAY) + }); + }, TOAST_REMOVE_DELAY); - toastTimeouts.set(toastId, timeout) -} + toastTimeouts.set(toastId, timeout); +}; export const reducer = (state: State, action: Action): State => { switch (action.type) { @@ -80,27 +77,25 @@ export const reducer = (state: State, action: Action): State => { return { ...state, toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT), - } + }; case "UPDATE_TOAST": return { ...state, - toasts: state.toasts.map((t) => - t.id === action.toast.id ? { ...t, ...action.toast } : t - ), - } + toasts: state.toasts.map((t) => (t.id === action.toast.id ? { ...t, ...action.toast } : t)), + }; case "DISMISS_TOAST": { - const { toastId } = action + const { toastId } = action; // ! Side effects ! - This could be extracted into a dismissToast() action, // but I'll keep it here for simplicity if (toastId) { - addToRemoveQueue(toastId) + addToRemoveQueue(toastId); } else { state.toasts.forEach((toast) => { - addToRemoveQueue(toast.id) - }) + addToRemoveQueue(toast.id); + }); } return { @@ -111,46 +106,46 @@ export const reducer = (state: State, action: Action): State => { ...t, open: false, } - : t + : t, ), - } + }; } case "REMOVE_TOAST": if (action.toastId === undefined) { return { ...state, toasts: [], - } + }; } return { ...state, toasts: state.toasts.filter((t) => t.id !== action.toastId), - } + }; } -} +}; -const listeners: Array<(state: State) => void> = [] +const listeners: Array<(state: State) => void> = []; -let memoryState: State = { toasts: [] } +let memoryState: State = { toasts: [] }; function dispatch(action: Action) { - memoryState = reducer(memoryState, action) + memoryState = reducer(memoryState, action); listeners.forEach((listener) => { - listener(memoryState) - }) + listener(memoryState); + }); } -type Toast = Omit +type Toast = Omit; function toast({ ...props }: Toast) { - const id = genId() + const id = genId(); const update = (props: ToasterToast) => dispatch({ type: "UPDATE_TOAST", toast: { ...props, id }, - }) - const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id }) + }); + const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id }); dispatch({ type: "ADD_TOAST", @@ -159,36 +154,36 @@ function toast({ ...props }: Toast) { id, open: true, onOpenChange: (open) => { - if (!open) dismiss() + if (!open) dismiss(); }, }, - }) + }); return { id: id, dismiss, update, - } + }; } function useToast() { - const [state, setState] = React.useState(memoryState) + const [state, setState] = React.useState(memoryState); React.useEffect(() => { - listeners.push(setState) + listeners.push(setState); return () => { - const index = listeners.indexOf(setState) + const index = listeners.indexOf(setState); if (index > -1) { - listeners.splice(index, 1) + listeners.splice(index, 1); } - } - }, [state]) + }; + }, [state]); return { ...state, toast, dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }), - } + }; } -export { useToast, toast } +export { useToast, toast }; diff --git a/src/context/layoutContext.tsx b/src/context/layoutContext.tsx index c3552ae..9203684 100644 --- a/src/context/layoutContext.tsx +++ b/src/context/layoutContext.tsx @@ -37,10 +37,6 @@ const LayoutProvider = ({ children }: TLayoutProvider) => { localStorage.setItem("layout", layout); }; - return ( - - {children} - - ); + return {children}; }; export default LayoutProvider; diff --git a/src/context/themeContext.tsx b/src/context/themeContext.tsx index 7413e0e..4395fd6 100644 --- a/src/context/themeContext.tsx +++ b/src/context/themeContext.tsx @@ -35,9 +35,5 @@ const ThemeProvider = ({ children }: TThemeProvider) => { setTheme(getThemeFromLocalStorage()); }, []); - return ( - - {children} - - ); + return {children}; }; diff --git a/src/hooks/useLocalStorage.ts b/src/hooks/useLocalStorage.ts index 450a1f9..dc26a4b 100644 --- a/src/hooks/useLocalStorage.ts +++ b/src/hooks/useLocalStorage.ts @@ -23,8 +23,7 @@ export default function useLocalStorage(key: string, initialValue: T) { // Create dispatch function to set or remove localStorage const setValue: SetValue = (value) => { try { - const valueToStore = - value instanceof Function ? value(storedValue) : value; + const valueToStore = value instanceof Function ? value(storedValue) : value; setStoredValue(valueToStore); window.localStorage.setItem(key, JSON.stringify(valueToStore)); } catch (error) { diff --git a/src/types/schema.ts b/src/types/schema.ts index 2e38049..91abdee 100644 --- a/src/types/schema.ts +++ b/src/types/schema.ts @@ -133,12 +133,7 @@ export const Schema_Config_Site = z.object({ toaster: z .object({ - position: z.enum([ - "top-left", - "top-right", - "bottom-left", - "bottom-right", - ]), + position: z.enum(["top-left", "top-right", "bottom-left", "bottom-right"]), duration: z.number().positive(), }) .optional() @@ -201,12 +196,10 @@ export const Schema_App_Configuration = z.object({ site: Schema_Config_Site, }); -export type ConfigurationCategory = keyof z.infer< +export type ConfigurationCategory = keyof z.infer; +export type ConfigurationKeys> = keyof z.infer< typeof Schema_App_Configuration ->; -export type ConfigurationKeys< - T extends keyof z.infer, -> = keyof z.infer[T]; +>[T]; export type ConfigurationValue< T extends keyof z.infer, K extends keyof z.infer[T], diff --git a/src/utils/encryptionHelper.ts b/src/utils/encryptionHelper.ts index 64c0a56..520d373 100644 --- a/src/utils/encryptionHelper.ts +++ b/src/utils/encryptionHelper.ts @@ -15,17 +15,11 @@ const generateKey = () => { }; const key = generateKey(); -export async function encryptData( - data: string, - encryptKey: string = key, -): Promise { +export async function encryptData(data: string, encryptKey: string = key): Promise { try { const ivKey = Buffer.from(key); const cipher = crypto.createCipheriv("aes-128-cbc", encryptKey, ivKey); - return Buffer.concat([ - cipher.update(data, "utf-8"), - cipher.final(), - ]).toString("hex"); + return Buffer.concat([cipher.update(data, "utf-8"), cipher.final()]).toString("hex"); } catch (error) { const e = error as Error; console.error(e.message); @@ -33,23 +27,15 @@ export async function encryptData( } } -export async function decryptData( - hash: string, - encryptKey: string = key, -): Promise { +export async function decryptData(hash: string, encryptKey: string = key): Promise { try { const ivKey = Buffer.from(key); const decipher = crypto.createDecipheriv("aes-128-cbc", encryptKey, ivKey); - return Buffer.concat([ - decipher.update(hash, "hex"), - decipher.final(), - ]).toString("utf-8"); + return Buffer.concat([decipher.update(hash, "hex"), decipher.final()]).toString("utf-8"); } catch (error) { const e = error as Error; console.error(e.message); - throw new Error( - "Failed to decrypt data, either invalid hash or encryption key.", - ); + throw new Error("Failed to decrypt data, either invalid hash or encryption key."); } } diff --git a/tailwind.config.ts b/tailwind.config.ts index 83d0966..445dca2 100644 --- a/tailwind.config.ts +++ b/tailwind.config.ts @@ -3,12 +3,7 @@ import tw from "tailwindcss/defaultTheme"; const config: Config = { darkMode: ["class"], - content: [ - "./pages/**/*.{ts,tsx}", - "./components/**/*.{ts,tsx}", - "./app/**/*.{ts,tsx}", - "./src/**/*.{ts,tsx}", - ], + content: ["./pages/**/*.{ts,tsx}", "./components/**/*.{ts,tsx}", "./app/**/*.{ts,tsx}", "./src/**/*.{ts,tsx}"], prefix: "", theme: { container: {