Toast refactoring

main
Antonio De Lucreziis 2 years ago
parent 70ab93de0a
commit f893f0e751

@ -6,6 +6,15 @@ let globalToastId = 0
const ToastContext = createContext([])
export const ToastMessage = ({ icon, children }) => {
return (
<div class="row">
{icon && <Icon name={icon} />}
<p>{children}</p>
</div>
)
}
export const ToastProvider = ({ children }) => {
const [toasts, setToasts] = useState([])
@ -18,7 +27,7 @@ export const ToastProvider = ({ children }) => {
{children}
<div class="toasts">
{toasts.map(({ uid, removed, message }) => (
<div key={uid} class={removed ? 'toast removed' : 'toast'}>
<div class={'toast' + (removed ? ' removed' : '')}>
<p>{message}</p>
<div class="toast-close" onClick={() => removeToast(uid)}>
<Icon name="close" />
@ -43,7 +52,7 @@ export const useToasts = () => {
}, duration)
setTimeout(() => {
setToasts(toasts => toasts.filter(t => !t.removed))
setToasts(toasts => toasts.filter(t => t !== toast))
}, duration + 1000)
setToasts(ts => [...ts.slice(-5), toast])

@ -1,6 +1,9 @@
import { useEffect, useState } from 'preact/hooks'
import { ToastMessage, useToasts } from '../components/Toasts.jsx'
export const AdminPanel = () => {
const [showToast] = useToasts()
const [text, setText] = useState('')
useEffect(() => {
@ -19,10 +22,12 @@ export const AdminPanel = () => {
})
if (!res.ok) {
alert('An error occurred while updating the state!')
showToast(`Errore "${await res.text()}"`)
}
setText(formatted)
showToast(<ToastMessage icon="info">Dashboard aggiornata</ToastMessage>)
}
return (

@ -1,6 +1,6 @@
import { useEffect, useRef, useState } from 'preact/hooks'
import { Icon } from '../components/Icon.jsx'
import { useToasts } from '../components/Toasts.jsx'
import { ToastMessage, useToasts } from '../components/Toasts.jsx'
import { useStore } from '../context.jsx'
import { useRemoteState } from '../util.jsx'
@ -38,9 +38,10 @@ export const Bucket = () => {
const { id } = await res.json()
showToast(
<>
File caricato con id <code>{id}</code>
</>
<ToastMessage icon="info">
File caricato con ID: <code>{id}</code>
</ToastMessage>,
{ duration: 10e3 }
)
await updateBucketObjects()

@ -1,5 +1,5 @@
import { toChildArray } from 'preact'
import { useCallback, useEffect, useState } from 'preact/hooks'
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
export function hashCode(s) {
s = s.toString() + "seed iniziale dell'hash"
@ -82,6 +82,45 @@ export function useRemoteState(url, initialValue = null) {
return [value, error, refresh]
}
// Toggle CSS Class
// export function useClasses(classes) {
// const elementRef = useRef(null)
// if (Array.isArray(classes)) {
// classes = Object.fromEntries(classes.filter(Boolean).map(className => [className, true]))
// }
// const updateClasses = () => {
// if (elementRef.current) {
// const $el = elementRef.current
// console.log($el.classList)
// console.log('Applying classes:', classes)
// for (const [className, present] of Object.entries(classes)) {
// if (present) {
// if (!$el.classList.contains(className)) {
// console.log('Adding class:', className, present)
// $el.classList.add(className)
// }
// } else {
// if ($el.classList.contains(className)) {
// console.log('Removing class:', className, present)
// $el.classList.remove(className)
// }
// }
// }
// }
// }
// useEffect(() => {
// updateClasses()
// })
// return [elementRef]
// }
//
// Change Case Utility
//

@ -298,7 +298,7 @@ main {
background: #38383d;
color: var(--fg-dark-500);
padding: 0.5rem 0.5rem 0.5rem 1rem;
padding: 0.5rem;
border-radius: 0.5rem;
opacity: 0.95;
@ -309,7 +309,7 @@ main {
display: flex;
align-items: center;
gap: 0.5rem;
gap: 1rem;
.toast-close {
display: flex;

Loading…
Cancel
Save