Skip to content

Commit

Permalink
Separate notifications and downloading logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccxzhang committed Aug 22, 2024
1 parent cecc1d0 commit c8091b7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
57 changes: 40 additions & 17 deletions src/renderer/src/pages/Modals/NotificationModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import { useSelector, useDispatch } from 'react-redux'
import { clearNotification, handleExit, toggleNotifications } from '../../reducers/statusReducer'

// eslint-disable-next-line react/display-name
const Notification = React.memo(() => {
const { isLoading, notification } = useSelector((state) => state.status)
const Logs = React.memo(() => {
const { logs } = useSelector((state) => state.status)
console.log(logs)

const renderedMessages = useMemo(() => {
return notification.map((msg, index) => (
return logs.map((msg, index) => (
<p
key={index}
className={`${msg?.type === 'error' ? 'text-red-600' : 'text-blue-600'} mb-2 whitespace-pre-wrap`}
>
{msg?.message}
</p>
))
}, [notification])
}, [logs])

if (notification.length === 0) return null
if (logs.length === 0) return null

return <div>{renderedMessages}</div>
})

const NotificationModal = () => {
const dispatch = useDispatch()
const { isLoading, notification, showNotifications } = useSelector((state) => state.status)
const { isLoading, notification, showLogs, logs } = useSelector((state) => state.status)

const handleClose = () => {
dispatch(handleExit())
Expand All @@ -44,18 +45,40 @@ const NotificationModal = () => {
</div>
)}

<button
onClick={() => dispatch(toggleNotifications())}
className="text-blue-500 hover:text-blue-700 transition duration-150 ease-in-out focus:outline-none"
>
{showNotifications ? 'Hide Details' : 'Show Details ▼'}
</button>
{/* Notification Content - Always shown */}
<div className="w-full mb-4">
{notification.length > 0
? notification.map((notification, index) => (
<div
key={index}
className={`p-4 mb-2 rounded-lg ${
notification.type === 'error'
? 'bg-red-100 text-red-600'
: 'bg-blue-100 text-blue-600'
}`}
>
{notification.message}
</div>
))
: null}
</div>

{/* Conditional rendering based on toggle */}
{showNotifications && (
<div className="flex-grow w-full p-4 bg-gray-100 rounded-lg border border-gray-300 overflow-y-auto max-h-48">
<Notification />
</div>
{!isLoading && logs.length > 0 && (
<>
<button
onClick={() => dispatch(toggleNotifications())}
className="text-blue-500 hover:text-blue-700 transition duration-150 ease-in-out focus:outline-none"
>
{showLogs ? 'Hide Logs' : 'View Logs ▼'}
</button>

{/* Conditional rendering based on toggle */}
{showLogs && (
<div className="flex-grow w-full p-4 bg-gray-100 rounded-lg border border-gray-300 overflow-y-auto max-h-48 mt-4">
<Logs />
</div>
)}
</>
)}

<button
Expand Down
30 changes: 24 additions & 6 deletions src/renderer/src/reducers/statusReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { throttle } from 'lodash'

const initialState = {
isLoading: false,
showNotifications: false,
notification: []
showLogs: false,
notification: [],
logs: []
}

const statusSlice = createSlice({
Expand All @@ -21,8 +22,14 @@ const statusSlice = createSlice({
clearNotification: (state) => {
state.notification = []
},
addLog: (state, action) => {
state.logs.push(action.payload)
},
clearLogs: (state) => {
state.logs = []
},
toggleNotifications: (state) => {
state.showNotifications = !state.showNotifications
state.showLogs = !state.showLogs
},
handleExit: (state) => {
return initialState
Expand All @@ -32,7 +39,7 @@ const statusSlice = createSlice({

const throttledDispatch = throttle((dispatch, notification) => {
dispatch(addNotification(notification))
}, 500) // Throttle interval is 500 ms
}, 500)

export const triggerNotification = (payload) => (dispatch) => {
dispatch(openModal({ type: 'NOTIFICATION' }))
Expand All @@ -50,6 +57,17 @@ export const triggerLoading = (isLoading) => (dispatch) => {
}
}

export const { setLoading, addNotification, clearNotification, toggleNotifications, handleExit } =
statusSlice.actions
export const logActivity = (payload) => (dispatch) => {
dispatch(addLog({ message: payload.message, type: payload.type }))
}

export const {
setLoading,
addNotification,
clearNotification,
addLog,
clearLogs,
toggleNotifications,
handleExit
} = statusSlice.actions
export default statusSlice.reducer

0 comments on commit c8091b7

Please sign in to comment.