Skip to content

Fix toast crashes #1154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions fission/src/ui/ToastContext.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { createContext, useState, useContext, useCallback, ReactNode } from "react"
import React, { createContext, ReactNode, useCallback, useContext, useState } from "react"
import Toast from "@/components/Toast"
import { AnimatePresence, motion } from "framer-motion"

export type ToastType = "info" | "warning" | "error"

export type ToastData = {
id: string
type: ToastType
toastType: ToastType
title: string
description: string
}
Expand All @@ -19,7 +19,7 @@

const ToastContext = createContext<ToastContextType | null>(null)

export const useToastContext = () => {

Check warning on line 22 in fission/src/ui/ToastContext.tsx

View workflow job for this annotation

GitHub Actions / ESLint Format Validation

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components
const context = useContext(ToastContext)
if (!context) throw new Error("useToastContext must be used within a ToastProvider")
return context
Expand All @@ -28,12 +28,12 @@
export const ToastProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [toasts, setToasts] = useState<ToastData[]>([])

const addToast = useCallback((type: ToastType, title: string, description: string) => {
const addToast = useCallback((toastType: ToastType, title: string, description: string) => {
// divide by 10 so that it's harder to have duplicates? could make smaller or remove
const id = "toast-" + Math.floor(Date.now() / 10).toString()
const newToast: ToastData = {
id,
type,
toastType,
title,
description,
}
Expand Down Expand Up @@ -80,7 +80,13 @@
key={t.id}
className="w-fit"
>
<Toast key={t.id} id={t.id} type={t.type} title={t.title} description={t.description} />
<Toast
key={t.id}
id={t.id}
toastType={t.toastType}
title={t.title}
description={t.description}
/>
</motion.div>
))}
</AnimatePresence>
Expand Down
6 changes: 3 additions & 3 deletions fission/src/ui/components/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BiSolidErrorCircle } from "react-icons/bi"

const TOAST_TIMEOUT: number = 5_000

const Toast: React.FC<ToastData> = ({ id, type, title, description }) => {
const Toast: React.FC<ToastData> = ({ id, toastType, title, description }) => {
const { removeToast } = useToastContext()

useEffect(() => {
Expand All @@ -22,7 +22,7 @@ const Toast: React.FC<ToastData> = ({ id, type, title, description }) => {
let icon: ReactElement
let className: string

switch (type) {
switch (toastType) {
case "info":
icon = <AiOutlineInfoCircle size={48} className="h-full w-full text-main-text" />
className = "bg-toast-info"
Expand All @@ -39,7 +39,7 @@ const Toast: React.FC<ToastData> = ({ id, type, title, description }) => {

return (
<div
className={`toast toast-${type.toLowerCase()} aspect-toast relative flex flex-row ${className} px-4 py-2 content-center justify-between items-center rounded-lg shadow-md shadow-[rgba(0,0,0,0.5)]`}
className={`toast toast-${toastType.toLowerCase()} aspect-toast relative flex flex-row ${className} px-4 py-2 content-center justify-between items-center rounded-lg shadow-md shadow-[rgba(0,0,0,0.5)]`}
>
<div className="w-10 h-10 mr-1">{icon}</div>
<div className="toast-content w-auto ml-2 text-main-text">
Expand Down
Loading