safetwitch/src/mixins.ts

84 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-06-18 23:42:40 -04:00
export function truncate(value: string, length: number) {
2023-07-20 13:57:01 -04:00
if (value.length > length) {
return value.substring(0, length) + '...'
} else {
return value
}
2023-06-18 23:42:40 -04:00
}
2023-07-20 13:57:01 -04:00
const language = localStorage.getItem('language') || 'en-us'
2023-07-04 20:52:49 -04:00
2023-06-18 23:42:40 -04:00
export function abbreviate(text: number) {
2023-07-20 13:57:01 -04:00
return Intl.NumberFormat(language, {
//@ts-ignore
notation: 'compact',
maximumFractionDigits: 1
}).format(text)
2023-06-18 23:42:40 -04:00
}
2023-07-20 13:57:01 -04:00
const https = import.meta.env.SAFETWITCH_HTTPS.slice() === 'true'
2023-06-18 23:42:40 -04:00
const protocol = https ? 'https://' : 'http://'
2023-06-19 23:18:10 -04:00
const rootBackendUrl = `${protocol}${import.meta.env.SAFETWITCH_BACKEND_DOMAIN}/`
2023-06-18 23:42:40 -04:00
export async function getEndpoint(endpoint: string) {
2023-07-20 13:57:01 -04:00
const res = await fetch(rootBackendUrl + endpoint, {
method: 'GET',
headers: {
'Accept-Language': language
}
})
const rawData = await res.json()
2023-06-18 23:42:40 -04:00
2023-07-20 13:57:01 -04:00
if (!res.ok) {
throw res
}
if (rawData.status !== 'ok') {
throw rawData
}
2023-06-18 23:42:40 -04:00
2023-07-20 13:57:01 -04:00
const data = rawData.data
2023-06-18 23:42:40 -04:00
2023-07-20 13:57:01 -04:00
return data
}
2023-08-16 13:48:44 -04:00
2023-08-18 13:12:06 -04:00
export function getDefaultSettings() {
return {
"audioOnly": {
"name": "Audio Only",
"selected": false,
"type": "checkbox"
},
"defaultQuality": {
"name": "Default Quality",
"options": ["160p", "360p", "480p", "720p", "1080p"],
"selected": "480p",
"type": "option"
},
"chatVisible": {
"name": "Hide Chat",
"selected": false,
"type": "checkbox"
},
}
}
export function getSetting(key: string): boolean | string {
2023-08-16 13:48:44 -04:00
const storage = localStorage.getItem('settings')
2023-08-18 13:12:06 -04:00
let parsed
if (!storage) {
parsed = getDefaultSettings()
} else {
parsed = JSON.parse(storage)
}
return parsed[key].selected
}
2023-08-16 13:48:44 -04:00
2023-08-18 13:12:06 -04:00
export function chatVisible() {
const p = getSetting("chatVisible")
2023-08-16 13:48:44 -04:00
// Flip becuase on the setting page it's
// displayed as "Hide Chat", but the value
// is chatVisible
2023-08-18 13:12:06 -04:00
return !p
2023-08-16 13:48:44 -04:00
}