safetwitch/src/mixins.ts
2023-08-18 13:39:26 -04:00

84 lines
1.8 KiB
TypeScript

export function truncate(value: string, length: number) {
if (value.length > length) {
return value.substring(0, length) + '...'
} else {
return value
}
}
const language = localStorage.getItem('language') || 'en-us'
export function abbreviate(text: number) {
return Intl.NumberFormat(language, {
//@ts-ignore
notation: 'compact',
maximumFractionDigits: 1
}).format(text)
}
const https = import.meta.env.SAFETWITCH_HTTPS.slice() === 'true'
const protocol = https ? 'https://' : 'http://'
const rootBackendUrl = `${protocol}${import.meta.env.SAFETWITCH_BACKEND_DOMAIN}/`
export async function getEndpoint(endpoint: string) {
const res = await fetch(rootBackendUrl + endpoint, {
method: 'GET',
headers: {
'Accept-Language': language
}
})
const rawData = await res.json()
if (!res.ok) {
throw res
}
if (rawData.status !== 'ok') {
throw rawData
}
const data = rawData.data
return data
}
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 {
const storage = localStorage.getItem('settings')
let parsed
if (!storage) {
parsed = getDefaultSettings()
} else {
parsed = JSON.parse(storage)
}
return parsed[key].selected
}
export function chatVisible() {
const p = getSetting('chatVisible')
// Flip becuase on the setting page it's
// displayed as "Hide Chat", but the value
// is chatVisible
return !p
}