Skip to content

Commit

Permalink
Merge pull request #92 from nebulabroadcast/martastain/channel-switcher
Browse files Browse the repository at this point in the history
Add channel switcher to Navbar
  • Loading branch information
martastain authored Nov 6, 2024
2 parents 417a635 + ccd6b4a commit aa239e3
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 9 deletions.
9 changes: 9 additions & 0 deletions frontend/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const initialState = {
selectedAssets: [],
focusedAsset: null,
pageTitle: '',
currentChannel: JSON.parse(localStorage.getItem('currentChannel') || 'null'),
}

const contextSlice = createSlice({
Expand Down Expand Up @@ -66,6 +67,13 @@ const contextSlice = createSlice({
state.sendToIds = undefined
state.sendToDialogVisible = false
},

setCurrentChannel: (state, action) => {
console.debug('setCurrentChannel', action.payload)
state.currentChannel = action.payload
localStorage.setItem('currentChannel', JSON.stringify(action.payload))
return state
},
},
})

Expand All @@ -79,6 +87,7 @@ export const {
setPageTitle,
showSendToDialog,
hideSendToDialog,
setCurrentChannel,
} = contextSlice.actions

export default contextSlice.reducer
20 changes: 20 additions & 0 deletions frontend/src/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import ServicesPage from '/src/pages/ServicesPage'
import ToolPage from '/src/pages/ToolPage'
import ProfilePage from '/src/pages/ProfilePage'
import UsersPage from '/src/pages/UsersPage'
import Dropdown from '/src/components/Dropdown'

const App = () => {
const [accessToken, setAccessToken] = useLocalStorage('accessToken', null)
const [errorCode, setErrorCode] = useState(null)
const [loading, setLoading] = useState(true)
const [initData, setInitData] = useState(null)
const [channels, setChannels] = useState([])

// Ensure server connection

Expand Down Expand Up @@ -56,6 +58,24 @@ const App = () => {
.finally(() => setLoading(false))
}, [accessToken])

useEffect(() => {
if (initData?.settings?.channels) {
setChannels(initData.settings.channels)
const mostRecentChannel = JSON.parse(
localStorage.getItem('currentChannel')
)
if (mostRecentChannel) {
setCurrentChannel(mostRecentChannel)
} else if (initData.settings.channels.length > 0) {
setCurrentChannel(initData.settings.channels[0])
}
}
}, [initData])

const handleChannelChange = (channel) => {
setCurrentChannel(channel)
}

// Render

if (loading) return <LoadingPage />
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/Dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const Dropdown = ({
contentStyle = {},
value = null,
disabled = false,
defaultValue = null,
}) => {
if (align === 'right') contentStyle['right'] = 0

Expand Down
36 changes: 34 additions & 2 deletions frontend/src/containers/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import nebula from '/src/nebula'
import styled from 'styled-components'
import { useMemo } from 'react'
import { useSelector } from 'react-redux'
import { useSelector, useDispatch } from 'react-redux'
import { NavLink, useNavigate } from 'react-router-dom'
import { setCurrentChannel } from '/src/actions'

import { Navbar, Dropdown } from '/src/components'

Expand Down Expand Up @@ -83,8 +84,9 @@ const logout = () => {

const NavBar = () => {
const navigate = useNavigate()
const dispatch = useDispatch()
const focusedAsset = useSelector((state) => state.context.focusedAsset)

const currentChannel = useSelector((state) => state.context.currentChannel)
const mamSuffix = focusedAsset ? `?asset=${focusedAsset}` : ''

const mainMenuOptions = useMemo(() => {
Expand Down Expand Up @@ -114,6 +116,35 @@ const NavBar = () => {
return result
}, [])

const channelSwitcher = useMemo(() => {
if ((nebula.settings?.playout_channels || []).length < 2) {
if (nebula.settings?.playout_channels.length === 1) {
dispatch(setCurrentChannel(nebula.settings.playout_channels[0].id))
}
return null
}

if (!nebula.experimental) return null

const channelOptions = nebula.settings?.playout_channels.map((channel) => ({
label: channel.name,
onClick: () => dispatch(setCurrentChannel(channel.id)),
}))

const currentChannelName = nebula.settings?.playout_channels.find(
(channel) => channel.id === currentChannel
)?.name

return (
<Dropdown
align="right"
options={channelOptions}
buttonStyle={{ background: 'none' }}
label={currentChannelName}
/>
)
}, [nebula.settings?.playout_channels, currentChannel])

return (
<Navbar>
<div className="left">
Expand All @@ -136,6 +167,7 @@ const NavBar = () => {
<PageTitle />
</div>
<div className="right">
{channelSwitcher}
<Dropdown
icon="apps"
align="right"
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ const useLocalStorage = (key, initialValue) => {
console.error(error)
}
}

useEffect(() => {
const handleStorageChange = () => {
const item = window.localStorage.getItem(key)
setStoredValue(item ? JSON.parse(item) : initialValue)
}

window.addEventListener('storage', handleStorageChange)
return () => {
window.removeEventListener('storage', handleStorageChange)
}
}, [key, initialValue])

return [storedValue, setValue]
}

Expand Down
6 changes: 6 additions & 0 deletions frontend/src/nebula.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const nebula = {
return result
},

getPlayoutChannel(id_channel) {
for (const channel of this.settings?.playoutChannels || []) {
if (channel.id === id_channel) return channel
}
},

getFolderName(id_folder) {
for (const folder of this.settings?.folders || []) {
if (folder.id === id_folder) return folder.name
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/pages/Rundown/Rundown.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useState, useEffect, useMemo } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import nebula from '/src/nebula'

import RundownNav from './RundownNav'
import RundownTable from './RundownTable'

const Rundown = () => {
const [startTime, setStartTime] = useState(null)
const currentChannel = useSelector((state) => state.context.currentChannel)
const [rundown, setRundown] = useState(null)

const onResponse = (response) => {
Expand All @@ -20,10 +22,10 @@ const Rundown = () => {
if (!startTime) return
const requestParams = {
date: startTime.toISOString().split('T')[0],
id_channel: 1,
id_channel: currentChannel,
}
nebula.request('rundown', requestParams).then(onResponse).catch(onError)
}, [startTime])
}, [startTime, currentChannel])

return (
<main className="column">
Expand Down
15 changes: 10 additions & 5 deletions frontend/src/pages/Scheduler/Scheduler.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect, useMemo } from 'react'
import { useDispatch } from 'react-redux'
import { useDispatch, useSelector } from 'react-redux'
import { setPageTitle } from '/src/actions'
import { toast } from 'react-toastify'

Expand All @@ -15,6 +15,11 @@ const Scheduler = ({ draggedAsset }) => {
const [startTime, setStartTime] = useState(getWeekStart())
const [events, setEvents] = useState([])
const [editorData, setEditorData] = useState(null)
const currentChannel = useSelector((state) => state.context.currentChannel)

const channelConfig = useMemo(() => {
return nebula.getPlayoutChannel(currentChannel)
}, [currentChannel])

const onResponse = (response) => {
const events = response.data.events
Expand All @@ -33,7 +38,7 @@ const Scheduler = ({ draggedAsset }) => {
}

const requestParams = {
id_channel: 1,
id_channel: currentChannel,
date: DateTime.fromJSDate(startTime).toFormat('yyyy-MM-dd'),
}

Expand Down Expand Up @@ -66,7 +71,7 @@ const Scheduler = ({ draggedAsset }) => {
setEditorData(null)
}

for (const field of nebula.settings.playout_channels[0].fields) {
for (const field of channelConfig?.fields || []) {
const key = field.name
if (event[key] === undefined) continue
payload.meta[key] = event[key]
Expand All @@ -87,13 +92,13 @@ const Scheduler = ({ draggedAsset }) => {

useEffect(() => {
loadEvents()
}, [startTime])
}, [startTime, currentChannel])

useEffect(() => {
// console.log('Week start time changed', startTime)
const pageTitle = createTitle(startTime)
dispatch(setPageTitle({ title: pageTitle }))
}, [startTime])
}, [startTime, currentChannel])

//
// Context menu
Expand Down

0 comments on commit aa239e3

Please sign in to comment.