PHP 8.2.30
Preview: notice.tsx Size: 3.27 KB
//proc/thread-self/root/home/byroehnu/.trash/node_modules11/adminjs/src/frontend/components/app/notice.tsx

import { Box, MessageBox, MessageBoxProps } from '@adminjs/design-system'
import React, { useEffect, useMemo, useRef, useState } from 'react'
import { connect } from 'react-redux'

import allowOverride from '../../hoc/allow-override.js'
import { useTranslation } from '../../hooks/index.js'
import { dropNotice } from '../../store/actions/drop-notice.js'
import { SetNoticeProgress, setNoticeProgress } from '../../store/actions/set-notice-progress.js'
import { ReduxState, type NoticeMessageInState } from '../../store/index.js'

const TIME_TO_DISAPPEAR = 3

export type NotifyProgress = (options: SetNoticeProgress) => void

export type NoticeElementProps = {
  notice: NoticeMessageInState
  drop: () => void
  notifyProgress: NotifyProgress
}

export type NoticeElementState = {
  progress: number
}

const NoticeElement: React.FC<NoticeElementProps> = (props) => {
  const { drop, notice, notifyProgress } = props
  const [progress, setProgress] = useState(0)
  const intervalRef = useRef<number>()
  const {
    tm,
    i18n: { language },
  } = useTranslation()
  const message = useMemo(
    () => tm(notice.message, notice.resourceId, notice.options),
    [notice.id, language],
  )
  const variant: MessageBoxProps['variant'] = notice.type === 'error' ? 'danger' : notice.type ?? 'info'

  useEffect(() => {
    intervalRef.current = window.setInterval(() => {
      const _progress = progress + 100 / TIME_TO_DISAPPEAR
      notifyProgress({ noticeId: notice.id, progress })
      setProgress(_progress)
      return _progress
    }, 1000)

    return () => {
      clearInterval(intervalRef.current)
    }
  }, [notice])

  useEffect(() => {
    if (progress >= 100) {
      drop()
    }
  }, [drop, progress])

  return (
    <MessageBox
      style={{ minWidth: '480px' }}
      message={message}
      variant={variant}
      onCloseClick={drop}
    >
      {notice.body}
    </MessageBox>
  )
}

type NoticeBoxPropsFromState = {
  notices: Array<NoticeMessageInState>
}

type NoticeBoxDispatchFromState = {
  drop: (noticeId: string) => void
  notifyProgress: NotifyProgress
}

const NoticeBox: React.FC<NoticeBoxPropsFromState & NoticeBoxDispatchFromState> = (props) => {
  const { drop, notices, notifyProgress } = props
  if (!notices.length) return null

  return (
    <Box
      as="div"
      data-testid="notice-wrapper"
      data-css="notice-wrapper"
      flex
      flexDirection="column"
      p="sm"
      style={{ gap: 4 }}
    >
      {notices.map((notice) => (
        <NoticeElement
          key={notice.id}
          notice={notice}
          drop={() => drop(notice.id)}
          notifyProgress={notifyProgress}
        />
      ))}
    </Box>
  )
}

const mapStateToProps = (state: ReduxState): NoticeBoxPropsFromState => ({
  notices: state.notices,
})

const mapDispatchToProps = (dispatch): NoticeBoxDispatchFromState => ({
  drop: (noticeId: string): void => dispatch(dropNotice(noticeId)),
  notifyProgress: ({ noticeId, progress }) => dispatch(setNoticeProgress({ noticeId, progress })),
})

const ConnectedNoticeBox = connect(mapStateToProps, mapDispatchToProps)(NoticeBox)
const OverridableConnectedNoticeBox = allowOverride(ConnectedNoticeBox, 'NoticeBox')

export {
  OverridableConnectedNoticeBox as NoticeBox,
  OverridableConnectedNoticeBox as default,
  ConnectedNoticeBox as OriginalNoticeBox,
}

Directory Contents

Dirs: 6 × Files: 17

Name Size Perms Modified Actions
- drwxr-xr-x 2026-02-28 00:32:46
Edit Download
- drwxr-xr-x 2026-02-28 00:32:50
Edit Download
- drwxr-xr-x 2026-02-28 00:32:46
Edit Download
- drwxr-xr-x 2026-02-28 00:32:48
Edit Download
sidebar DIR
- drwxr-xr-x 2026-02-28 00:32:50
Edit Download
utils DIR
- drwxr-xr-x 2026-02-28 00:32:48
Edit Download
383 B lrw-r--r-- 2026-02-28 00:32:46
Edit Download
235 B lrw-r--r-- 2026-02-28 00:32:46
Edit Download
525 B lrw-r--r-- 2026-02-28 00:32:46
Edit Download
2.47 KB lrw-r--r-- 2026-02-28 00:32:46
Edit Download
3.21 KB lrw-r--r-- 2026-02-28 00:32:46
Edit Download
5.74 KB lrw-r--r-- 2026-02-28 00:32:46
Edit Download
3.51 KB lrw-r--r-- 2026-02-28 00:32:46
Edit Download
966 B lrw-r--r-- 2026-02-28 00:32:46
Edit Download
2.25 KB lrw-r--r-- 2026-02-28 00:32:46
Edit Download
4.08 KB lrw-r--r-- 2026-02-28 00:32:46
Edit Download
282 B lrw-r--r-- 2026-02-28 00:32:46
Edit Download
755 B lrw-r--r-- 2026-02-28 00:32:28
Edit Download
1.15 KB lrw-r--r-- 2026-02-28 00:32:48
Edit Download
3.27 KB lrw-r--r-- 2026-02-28 00:32:48
Edit Download
1.55 KB lrw-r--r-- 2026-02-28 00:32:50
Edit Download
1.71 KB lrw-r--r-- 2026-02-28 00:32:50
Edit Download
1.22 KB lrw-r--r-- 2026-02-28 00:32:50
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).