2022-04-22 16:56:41 -04:00
// @ts-check
/// <reference path="./types.d.ts"/>
2022-04-25 17:23:23 -04:00
import 'https://js.boxcast.com/v3.min.js' ;
2022-04-26 13:49:43 -04:00
import { html } from "https://esm.sh/htm/react" ;
2022-04-26 17:10:25 -04:00
import styled from 'https://esm.sh/styled-components?deps=react@18' ;
import { createElement as h , useState , useEffect , useRef } from 'https://esm.sh/react@18' ;
2022-04-26 13:49:43 -04:00
import { createRoot } from "https://esm.sh/react-dom/client" ;
const StyledRoot = styled . div `
. Partition
{
margin : 10 px 0 0 0 ;
}
. Broadcast
{
position : relative ;
display : flex ;
padding : 5 px 0 5 px 0 ;
2022-04-26 16:00:04 -04:00
. Pointer
{
width : 75 px ;
. Badge
{
display : inline - block ;
border - radius : 20 px ;
padding : 2 px 8 px ;
font - size : 12 px ;
font - weight : 900 ;
font - family : sans - serif ;
letter - spacing : 0.1 em ;
& . Next
{
background : yellow ;
color : black ;
}
& . Live
{
background : limegreen ;
color : white ;
}
}
}
2022-04-26 13:49:43 -04:00
. Time
2022-04-25 17:23:23 -04:00
{
2022-04-26 13:49:43 -04:00
width : 75 px ;
margin - right : 5 px ;
font - size : 16 px ;
text - align : right ;
}
2022-04-26 16:00:04 -04:00
. Title
2022-04-25 17:23:23 -04:00
{
2022-04-26 16:00:04 -04:00
flex : 1 ;
}
. Control
{
button
{
appearance : none ;
display : block ;
width : 80 px ;
padding : 5 px 10 px 5 px 10 px ;
background : black ;
cursor : pointer ;
border : none ;
color : white ;
font - size : 10 px ;
font - weight : 900 ;
}
button [ disabled ]
{
background : grey ;
}
2022-04-26 13:49:43 -04:00
}
}
. Alert
{
position : fixed ;
right : 20 px ;
bottom : - 300 px ;
width : 300 px ;
height : 200 px ;
background : # 333 ;
border - radius : 5 px ;
transition : bottom 0.4 s ;
color : # fff ;
text - align : center ;
& . Show
2022-04-25 17:23:23 -04:00
{
2022-04-26 13:49:43 -04:00
bottom : 20 px ;
}
. Close
2022-04-25 17:23:23 -04:00
{
2022-04-26 13:49:43 -04:00
display : inline - block ;
position : absolute ;
padding : 5 px 10 px 5 px 10 px ;
border - radius : 20 px ;
top : - 15 px ;
right : 10 px ;
background : # 000000 ;
cursor : pointer ;
color : # fff ;
2022-04-25 17:23:23 -04:00
}
2022-04-26 13:49:43 -04:00
}
` ;
const PlayerID = "boxcast-player" ;
2022-04-22 16:56:41 -04:00
/** @type {(props:{channel:string, interval:number})=>any} */
const App = props =>
{
/** @type {Boxcast.StateBinding<Array<Boxcast.Broadcast>>} */
const [ ListGet , ListSet ] = useState ( [ ] ) ;
2022-04-26 16:00:04 -04:00
/** @type {Boxcast.StateBinding<string|null>} */
2022-04-26 10:47:45 -04:00
const [ SelectedGet , SelectedSet ] = useState ( null ) ;
2022-04-22 16:56:41 -04:00
2022-04-26 10:47:45 -04:00
/** @type {Boxcast.StateBinding<Boxcast.Broadcast|null>} */
2022-04-26 12:04:39 -04:00
const [ LeadingGet , LeadingSet ] = useState ( null ) ;
/** @type {Boxcast.StateBinding<boolean>} */
const [ AlertGet , AlertSet ] = useState ( false ) ;
2022-04-22 17:19:25 -04:00
2022-04-25 17:23:23 -04:00
/** @type {(inList:Array<Boxcast.Broadcast>)=>Array<Boxcast.Broadcast>} */
const SortStart = ( inList ) => {
inList . sort ( ( a , b ) => a . starts _at > b . starts _at ? 1 : - 1 ) ;
inList . forEach ( item => item . start = DateParse ( item . starts _at ) ) ;
return inList ;
}
2022-04-22 16:56:41 -04:00
2022-04-25 17:23:23 -04:00
const Player = useRef ( null ) ;
2022-04-22 16:56:41 -04:00
2022-04-25 17:23:23 -04:00
// on mount
2022-04-22 17:19:25 -04:00
useEffect ( ( ) =>
{
2022-04-25 17:23:23 -04:00
Player . current = boxcast ( ` # ${ PlayerID } ` ) ;
2022-04-25 10:36:55 -04:00
/** @type {()=>Promise} */
const Ping = async ( ) =>
{
const response = await fetch ( ` https://rest.boxcast.com/channels/ ${ props . channel } /broadcasts?l=50 ` ) ;
/** @type {Array<Boxcast.Broadcast>} */
const json = await response . json ( ) ;
2022-04-25 17:23:23 -04:00
ListSet ( SortStart ( json ) ) ;
2022-04-25 10:36:55 -04:00
} ;
2022-04-22 17:19:25 -04:00
Ping ( ) ;
const timer = setInterval ( Ping , props . interval ) ;
return ( ) => clearInterval ( timer ) ;
}
, [ ] ) ;
2022-04-26 17:10:25 -04:00
// on new list
useEffect ( ( ) =>
{
let leading ;
for ( let i = 0 ; i < ListGet . length ; i ++ )
{
if ( ListGet [ i ] . timeframe != "past" )
{
leading = ListGet [ i ] ;
2022-04-26 12:04:39 -04:00
2022-04-26 17:10:25 -04:00
if ( ( leading . timeframe == "current" || leading . timeframe == "preroll" ) && ( leading . id != LeadingGet ? . id ) && ( SelectedGet != leading . id ) ) // if something is selected other than the leading event, alert the user
{
AlertSet ( true ) ;
}
2022-04-26 12:04:39 -04:00
2022-04-26 17:10:25 -04:00
if ( SelectedGet == null ) // if nothing is selected select the leading event
{
SelectedSet ( leading . id ) ;
}
LeadingSet ( leading ) ;
return ;
}
}
2022-04-26 12:04:39 -04:00
if ( ListGet . length ) // if there are events but theres no leading event, clear leading and select the first event
2022-04-25 10:36:55 -04:00
{
2022-04-26 12:04:39 -04:00
LeadingSet ( null ) ;
2022-04-26 16:00:04 -04:00
if ( SelectedGet == null )
{
SelectedSet ( ListGet [ 0 ] . id ) ;
}
2022-04-26 12:04:39 -04:00
AlertSet ( false ) ;
2022-04-25 10:36:55 -04:00
}
}
, [ ListGet ] ) ;
2022-04-22 17:19:25 -04:00
2022-04-25 17:23:23 -04:00
// on new video selected
useEffect ( ( ) =>
{
2022-04-26 12:04:39 -04:00
const settings = {
2022-04-26 16:00:04 -04:00
selectedBroadcastId : SelectedGet ,
2022-04-25 17:23:23 -04:00
showTitle : true ,
showDescription : true ,
showCountdown : true ,
showRelated : false ,
autoplay : true ,
defaultVideo : "next"
2022-04-26 12:04:39 -04:00
} ;
Player . current . loadChannel ( props . channel , settings ) ;
2022-04-22 16:56:41 -04:00
}
2022-04-26 16:00:04 -04:00
, [ SelectedGet ] ) ;
2022-04-25 17:23:23 -04:00
return html `
2022-04-26 13:49:43 -04:00
< $ { StyledRoot } >
2022-04-25 17:23:23 -04:00
< div id = $ { PlayerID } > < / d i v >
< div class = "Boxcast-Playlist" >
$ {
2022-04-26 16:00:04 -04:00
ListGet . map ( ( item , index ) =>
{
return h ( BroadcastItem ,
2022-04-25 17:23:23 -04:00
{
item : item ,
previous : ListGet [ index - 1 ] ,
2022-04-26 12:04:39 -04:00
priority : item . id == LeadingGet ? . id ,
2022-04-26 16:00:04 -04:00
selected : item . id == SelectedGet ,
select : ( ) => SelectedSet ( item . id )
} ) ;
} )
2022-04-25 17:23:23 -04:00
}
< / d i v >
2022-04-26 13:49:43 -04:00
< div class = $ { ` Alert ${ AlertGet ? " Show" : null } ` } >
< span class = "Close" onClick = $ { ( ) => { AlertSet ( false ) ; } } > Dismiss ✕ < / s p a n >
< h4 > A new session is starting : < / h 4 >
< p > $ { LeadingGet ? . name } < / p >
2022-04-26 16:00:04 -04:00
< button onClick = $ { ( ) => { SelectedSet ( LeadingGet . id ) ; AlertSet ( false ) ; } } > Watch Now < / b u t t o n >
2022-04-26 13:49:43 -04:00
< / d i v >
< //>
2022-04-22 16:56:41 -04:00
` ;
}
2022-04-25 17:23:23 -04:00
/** @type {(props:{item:Boxcast.Broadcast, previous: false | Boxcast.Broadcast, priority:boolean, selected:boolean, select:()=>void})=>any} */
const BroadcastItem = ( { item , previous , priority , selected , select } ) =>
2022-04-22 16:56:41 -04:00
{
2022-04-25 17:23:23 -04:00
let pointerText ;
if ( item . timeframe == "current" || item . timeframe == "preroll" )
{
2022-04-26 16:00:04 -04:00
pointerText = html ` <div class="Badge Live">Live</div> ` ;
2022-04-25 17:23:23 -04:00
}
else if ( priority )
{
2022-04-26 16:00:04 -04:00
pointerText = html ` <div class="Badge Next">Next</div> ` ;
2022-04-25 17:23:23 -04:00
}
let partition ;
if ( ! previous || ( previous . start . Date !== item . start . Date ) )
{
2022-04-26 16:00:04 -04:00
partition = html ` <h3 class="Partition" key= ${ item . id + item . start . Day } >
$ { item . start . Day } , $ { item . start . Month } $ { item . start . Date }
< / h 3 > ` ;
2022-04-25 17:23:23 -04:00
}
let buttonText ;
if ( item . timeframe == "past" )
{
buttonText = "Recording" ;
}
if ( item . timeframe == "current" || item . timeframe == "preroll" )
{
buttonText = "Watch" ;
}
if ( item . timeframe == "future" )
{
buttonText = "Preview" ;
}
if ( selected )
{
buttonText = "(Viewing)" ;
}
2022-04-22 16:56:41 -04:00
return html `
2022-04-25 17:23:23 -04:00
$ { partition }
2022-04-26 16:00:04 -04:00
< div class = "Broadcast" key = $ { item . id } onClick = $ { select } >
2022-04-26 13:49:43 -04:00
< div class = "Pointer" > $ { pointerText } < / d i v >
< div class = "Time" > $ { item . start . Hours } : $ { item . start . Minutes } $ { item . start . M } < / d i v >
< div class = "Title" > $ { item . name } < / s t r o n g >
< div class = "Control" >
< button onClick = $ { select } disabled = $ { selected } > $ { buttonText } < / b u t t o n >
< / d i v >
2022-04-25 17:23:23 -04:00
< / d i v > ` ;
2022-04-22 16:56:41 -04:00
} ;
/** @type {(inDate:string)=>Boxcast.Date} */
const DateParse = ( inDateString ) =>
{
let date = new Date ( inDateString ) ;
/** @type {Boxcast.Date} */
let obj = {
Zone : date . toString ( ) . match ( /\(([A-Za-z\s].*)\)/ ) ,
Day : [ "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" ] [ date . getDay ( ) ] ,
Month : [ "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" ] [ date . getMonth ( ) ] ,
Date : date . getDate ( ) ,
Hours : date . getHours ( ) ,
Minutes : date . getMinutes ( ) ,
2022-04-26 10:39:25 -04:00
Epoch : date . valueOf ( )
2022-04-22 16:56:41 -04:00
} ;
obj . Zone = obj . Zone ? obj . Zone [ 1 ] : "local time" ;
obj . M = obj . Hours >= 12 ? "PM" : "AM" ;
obj . Hours %= 12 ;
if ( obj . Hours == 0 ) { obj . Hours = 12 ; }
if ( obj . Minutes < 10 ) { obj . Minutes = "0" + obj . Minutes ; }
return obj ;
} ;
2022-04-26 10:39:25 -04:00
/** @type {(inChannel:string, inSelector:string)=>void} */
2022-04-26 16:00:04 -04:00
export default ( inChannel , inSelector ) => createRoot ( document . querySelector ( inSelector ) ) . render ( h ( App , { channel : inChannel , interval : 5000 } ) ) ;