Skip to content

Commit

Permalink
fix: lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
PikiLee committed Aug 17, 2024
1 parent b8eee29 commit 4bbee7c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion components/GraphGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useForm } from 'react-hook-form'
import { Image, Tooltip } from '@nextui-org/react'
import { LuImagePlus } from 'react-icons/lu'
import { useEffect, useState } from 'react'
import { useState } from 'react'
import { IoCopyOutline } from 'react-icons/io5'
import AppCheckBox from './AppCheckBox'
import GraphColorSelector from './GraphColorSelector'
Expand Down
86 changes: 44 additions & 42 deletions components/ui/use-toast.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"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"
} from '~/components/ui/toast'

const TOAST_LIMIT = 1
const TOAST_REMOVE_DELAY = 1000000
Expand All @@ -18,11 +18,11 @@ type ToasterToast = ToastProps & {
action?: ToastActionElement
}

const actionTypes = {
ADD_TOAST: "ADD_TOAST",
UPDATE_TOAST: "UPDATE_TOAST",
DISMISS_TOAST: "DISMISS_TOAST",
REMOVE_TOAST: "REMOVE_TOAST",
const _actionTypes = {
ADD_TOAST: 'ADD_TOAST',
UPDATE_TOAST: 'UPDATE_TOAST',
DISMISS_TOAST: 'DISMISS_TOAST',
REMOVE_TOAST: 'REMOVE_TOAST',
} as const

let count = 0
Expand All @@ -32,90 +32,91 @@ function genId() {
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<ToasterToast>
}
type: ActionType['UPDATE_TOAST']
toast: Partial<ToasterToast>
}
| {
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[]
}

const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()

const addToRemoveQueue = (toastId: string) => {
function addToRemoveQueue(toastId: string) {
if (toastTimeouts.has(toastId)) {
return
}

const timeout = setTimeout(() => {
toastTimeouts.delete(toastId)
dispatch({
type: "REMOVE_TOAST",
toastId: toastId,
type: 'REMOVE_TOAST',
toastId,
})
}, TOAST_REMOVE_DELAY)

toastTimeouts.set(toastId, timeout)
}

export const reducer = (state: State, action: Action): State => {
export function reducer(state: State, action: Action): State {
switch (action.type) {
case "ADD_TOAST":
case 'ADD_TOAST':
return {
...state,
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
}

case "UPDATE_TOAST":
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": {
case 'DISMISS_TOAST': {
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)
} else {
}
else {
state.toasts.forEach((toast) => {
addToRemoveQueue(toast.id)
})
}

return {
...state,
toasts: state.toasts.map((t) =>
toasts: state.toasts.map(t =>
t.id === toastId || toastId === undefined
? {
...t,
open: false,
}
: t
: t,
),
}
}
case "REMOVE_TOAST":
case 'REMOVE_TOAST':
if (action.toastId === undefined) {
return {
...state,
Expand All @@ -124,7 +125,7 @@ export const reducer = (state: State, action: Action): State => {
}
return {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId),
toasts: state.toasts.filter(t => t.id !== action.toastId),
}
}
}
Expand All @@ -140,32 +141,33 @@ function dispatch(action: Action) {
})
}

type Toast = Omit<ToasterToast, "id">
type Toast = Omit<ToasterToast, 'id'>

function toast({ ...props }: Toast) {
const id = genId()

const update = (props: ToasterToast) =>
dispatch({
type: "UPDATE_TOAST",
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",
type: 'ADD_TOAST',
toast: {
...props,
id,
open: true,
onOpenChange: (open) => {
if (!open) dismiss()
if (!open)
dismiss()
},
},
})

return {
id: id,
id,
dismiss,
update,
}
Expand All @@ -187,7 +189,7 @@ function useToast() {
return {
...state,
toast,
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
dismiss: (toastId?: string) => dispatch({ type: 'DISMISS_TOAST', toastId }),
}
}

Expand Down
2 changes: 1 addition & 1 deletion index.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@
body {
@apply bg-background text-foreground;
}
}
}
4 changes: 2 additions & 2 deletions utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand Down

0 comments on commit 4bbee7c

Please sign in to comment.