Add ESLint and fix all linting errors

This commit is contained in:
Torjus Håkestad 2023-12-06 03:11:20 +01:00
parent bbd3adc4f7
commit dc5d75a2dd
8 changed files with 1264 additions and 131 deletions

7
.eslintrc.cjs Normal file
View File

@ -0,0 +1,7 @@
/* eslint-env node */
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
};

1247
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,9 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"eslint": "^8.55.0",
"parcel": "^2.10.3", "parcel": "^2.10.3",
"process": "^0.11.10", "process": "^0.11.10",
"ts-loader": "^9.5.1", "ts-loader": "^9.5.1",

View File

@ -20,21 +20,16 @@ export class MinistreamApiClient {
} }
async listStreams(): Promise<StreamInfo[]> { async listStreams(): Promise<StreamInfo[]> {
var data: StreamInfo[] = []; let data: StreamInfo[] = [];
var url = "/whip" let url = "/whip"
if (this.ENV !== "production") { if (this.ENV !== "production") {
url = "http://localhost:8080/whip" url = "http://localhost:8080/whip"
} }
try {
const resp = await fetch( const resp = await fetch(
url, url,
); );
data = await resp.json() as unknown as StreamInfo[]; data = await resp.json() as unknown as StreamInfo[];
}
catch (e) {
throw e;
}
const sortedStreams = data.sort((a, b) => { const sortedStreams = data.sort((a, b) => {
if (a.streamKey > b.streamKey) { if (a.streamKey > b.streamKey) {
@ -49,7 +44,7 @@ export class MinistreamApiClient {
} }
async postOffer(streamKey: string, offer_sdp: RTCSessionDescription): Promise<RTCSessionDescription> { async postOffer(streamKey: string, offer_sdp: RTCSessionDescription): Promise<RTCSessionDescription> {
var url = "/whip/" + streamKey let url = "/whip/" + streamKey
if (this.ENV !== "production") { if (this.ENV !== "production") {
url = "http://localhost:8080/whip/" + streamKey url = "http://localhost:8080/whip/" + streamKey
} }
@ -66,7 +61,7 @@ export class MinistreamApiClient {
} }
async siteInfo(): Promise<SiteInfo> { async siteInfo(): Promise<SiteInfo> {
var url = "/api/siteinfo" let url = "/api/siteinfo"
if (this.ENV !== "production") { if (this.ENV !== "production") {
url = "http://localhost:8080/api/siteinfo" url = "http://localhost:8080/api/siteinfo"
} }

View File

@ -1,10 +1,10 @@
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import { Menu } from "./menu"; import { Menu } from "./menu";
import { createContext, useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { MediaContainer } from "./media"; import { MediaContainer } from "./media";
import { MinistreamApiClient, StreamInfo } from "./api"; import { MinistreamApiClient, StreamInfo } from "./api";
import React from "react"; import React from "react";
import { Log } from "./log"; import * as Log from "./log";
interface AppProps { interface AppProps {
api: MinistreamApiClient api: MinistreamApiClient
@ -13,7 +13,7 @@ interface AppProps {
const titleKey = "ministream.title" const titleKey = "ministream.title"
function setTitleFromLocalstorage() { function setTitleFromLocalstorage() {
var title = localStorage.getItem(titleKey) const title = localStorage.getItem(titleKey)
if (title) { if (title) {
setTitle(title) setTitle(title)
} }

View File

@ -1,8 +1,20 @@
export namespace Log { function levelToNumber(level: Level): number {
var currentLevel: Level = "INFO" switch (level) {
case "DEBUG":
return 0
case "INFO":
return 1
case "ERROR":
return 2
case "WARN":
return 3
}
}
let currentLevel: Level = "INFO"
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
currentLevel = "DEBUG" currentLevel = "DEBUG"
@ -14,11 +26,24 @@ export namespace Log {
currentLevel = level currentLevel = level
} }
export interface LogArgs { export interface LogArgs {
[key: string]: string [key: string]: string
} }
const doLog = (level: Level, message: string, extras?: LogArgs) => {
let logLine = `[${level}] ${message}`
if (extras) {
Object.keys(extras).forEach((key) => {
logLine = logLine + ` ${key}=${extras[key]}`
})
}
if (levelToNumber(level) >= levelToNumber(currentLevel)) {
console.log(logLine)
}
}
export function Error(message: string, extras?: LogArgs) { export function Error(message: string, extras?: LogArgs) {
doLog("ERROR", message, extras) doLog("ERROR", message, extras)
} }
@ -31,31 +56,3 @@ export namespace Log {
export function Debug(message: string, extras?: LogArgs) { export function Debug(message: string, extras?: LogArgs) {
doLog("DEBUG", message, extras) doLog("DEBUG", message, extras)
} }
function doLog(level: Level, message: string, extras?: LogArgs) {
var logLine = `[${level}] ${message}`
if (extras) {
Object.keys(extras).forEach((key) => {
logLine = logLine + ` ${key}=${extras[key]}`
})
}
if (levelToNumber(level) >= levelToNumber(currentLevel)) {
console.log(logLine)
}
}
function levelToNumber(level: Level): number {
switch (level) {
case "DEBUG":
return 0
case "INFO":
return 1
case "ERROR":
return 2
case "WARN":
return 3
}
}
}

View File

@ -1,8 +1,7 @@
import { ComponentProps, useRef, useEffect, useState } from "react" import { useRef, useEffect, useState } from "react"
import { MinistreamApiClient } from "./api" import { MinistreamApiClient } from "./api"
import { resolve } from "path"
import React from "react" import React from "react"
import { Log } from "./log" import * as Log from "./log";
type MediaContainerProps = { type MediaContainerProps = {
selectedStream: string | null selectedStream: string | null
@ -151,7 +150,7 @@ interface LoadingText {
} }
export function LoadingText({ text, spinner = false }: LoadingText) { export function LoadingText({ text, spinner = false }: LoadingText) {
var spinnerElement = (<></>) let spinnerElement = (<></>)
if (spinner) { if (spinner) {
spinnerElement = (<Spinner />) spinnerElement = (<Spinner />)
} }

View File

@ -1,4 +1,3 @@
import { createRoot } from "react-dom/client";
import React from "react"; import React from "react";
import { StreamInfo } from "./api"; import { StreamInfo } from "./api";