import { useMemo, useEffect, useRef } from 'react' import PixelartIcon, { pixelartIcons } from './PixelartIcon' import './IndicatorEffects.css' function formatTime (seconds: number): string { if (seconds < 0) return '' const minutes = Math.floor(seconds / 60) const remainingSeconds = Math.floor(seconds % 60) const formattedMinutes = String(minutes).padStart(2, '0') const formattedSeconds = String(remainingSeconds) return `${formattedMinutes}:${formattedSeconds}` } export type EffectType = { image: string, time: number, level: number, removeEffect: (image: string) => void, reduceTime: (image: string) => void } const EffectBox = ({ image, time, level }: Pick) => { const formattedTime = useMemo(() => formatTime(time), [time]) return
{formattedTime ? ( // if time is negative then effect is shown without time. // Component should be removed manually with time = 0
{formattedTime}
) : null} {level > 0 && level < 256 ? (
{level + 1}
) : null}
} export const defaultIndicatorsState = { chunksLoading: false, readingFiles: false, readonlyFiles: false, writingFiles: false, // saving appHasErrors: false, connectionIssues: 0, preventSleep: false, } const indicatorIcons: Record = { chunksLoading: 'add-grid', readingFiles: 'arrow-bar-down', writingFiles: 'arrow-bar-up', appHasErrors: 'alert', readonlyFiles: 'file-off', connectionIssues: pixelartIcons['cellular-signal-off'], preventSleep: pixelartIcons.moon, } const colorOverrides = { connectionIssues: { 0: false, 1: 'orange', 2: 'red' } } export default ({ indicators, effects }: { indicators: typeof defaultIndicatorsState, effects: readonly EffectType[] }) => { const effectsRef = useRef(effects) useEffect(() => { effectsRef.current = effects }, [effects]) useEffect(() => { // todo use more precise timer for each effect const interval = setInterval(() => { for (const [index, effect] of effectsRef.current.entries()) { if (effect.time === 0) { // effect.removeEffect(effect.image) return } effect.reduceTime(effect.image) } }, 1000) return () => { clearInterval(interval) } }, []) const indicatorsMapped = Object.entries(defaultIndicatorsState).map(([key]) => { const state = indicators[key] return { icon: indicatorIcons[key], // preserve order state, key } }) return
{ indicatorsMapped.map((indicator) =>
) }
{ effects.map((effect) => ) }
}