132 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-05-23 11:42:14 +02:00
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
2023-08-08 17:15:05 +02:00
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
2023-09-26 14:21:32 +02:00
import { localizedHelpUrl } from '../../../utils/help'
import { helpButtonSVG } from '../../../videowatch/buttons'
2024-05-09 22:32:42 +02:00
import { TemplateResult, html } from 'lit';
interface HomeViewData {
title: string
description: string
please_select: string
channels: any[]
helpButton: TemplateResult
}
2023-08-08 17:15:05 +02:00
2023-08-08 18:26:40 +02:00
/**
* Renders the livechat configuration setup home page.
2023-08-08 18:26:40 +02:00
* @param registerClientOptions Peertube client options
* @returns The page content
*/
2024-05-09 22:32:42 +02:00
async function renderConfigurationHome (registerClientOptions: RegisterClientOptions): Promise<TemplateResult> {
2023-08-08 17:15:05 +02:00
const { peertubeHelpers } = registerClientOptions
try {
// Getting the current username in localStorage. Don't know any cleaner way to do.
const username = window.localStorage.getItem('username')
if (!username) {
throw new Error('Can\'t get the current username.')
}
2023-09-20 14:59:01 +02:00
// FIXME: if more than 100 channels, loop (or add a pagination)
2023-08-08 17:15:05 +02:00
const channels = await (await fetch(
2023-08-09 14:24:52 +02:00
'/api/v1/accounts/' + encodeURIComponent(username) + '/video-channels?start=0&count=100&sort=name',
2023-08-08 17:15:05 +02:00
{
method: 'GET',
headers: peertubeHelpers.getAuthHeader()
}
)).json()
if (!channels || !('data' in channels) || !Array.isArray(channels.data)) {
throw new Error('Can\'t get the channel list.')
}
for (const channel of channels.data) {
channel.livechatConfigurationUri = '/p/livechat/configuration/channel?channelId=' + encodeURIComponent(channel.id)
// Note: since Peertube v6.0.0, channel.avatar is dropped, and we have to use channel.avatars.
// So, if !channel.avatar, we will search a suitable one in channel.avatars, and fill channel.avatar.
if (!channel.avatar && channel.avatars && Array.isArray(channel.avatars)) {
for (const avatar of channel.avatars) {
if (avatar.width === 120) {
channel.avatar = avatar
break
}
}
}
2023-08-08 17:15:05 +02:00
}
2024-05-09 22:32:42 +02:00
const view : HomeViewData = {
title: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_TITLE),
description: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_DESC),
please_select: await peertubeHelpers.translate(LOC_LIVECHAT_CONFIGURATION_PLEASE_SELECT),
2024-05-09 22:32:42 +02:00
channels: channels.data,
helpButton: await _fillViewHelpButtons(registerClientOptions)
2023-08-08 17:15:05 +02:00
}
2023-09-26 14:21:32 +02:00
2024-05-09 22:32:42 +02:00
return renderConfigurationHomeFromTemplate(view)
2023-08-08 17:15:05 +02:00
} catch (err: any) {
peertubeHelpers.notifier.error(err.toString())
2024-05-09 22:32:42 +02:00
return html``
2023-08-08 17:15:05 +02:00
}
}
2023-09-26 14:21:32 +02:00
async function _fillViewHelpButtons ( // TODO: refactor with the similar function in channel.ts
2024-05-09 22:32:42 +02:00
registerClientOptions: RegisterClientOptions
): Promise<TemplateResult> {
2023-09-26 14:21:32 +02:00
const title = await registerClientOptions.peertubeHelpers.translate(LOC_ONLINE_HELP)
2024-05-09 22:32:42 +02:00
const button = async (page: string): Promise<TemplateResult> => {
2023-09-26 14:21:32 +02:00
const helpUrl = await localizedHelpUrl(registerClientOptions, {
page
})
const helpIcon = helpButtonSVG()
2024-05-09 22:32:42 +02:00
return html`<a
2023-09-26 14:21:32 +02:00
href="${helpUrl}"
target=_blank
title="${title}"
class="orange-button peertube-button-link"
>${helpIcon}</a>`
}
2024-05-09 22:32:42 +02:00
return button('documentation/user/streamers/channel')
}
function renderConfigurationHomeFromTemplate(view: HomeViewData) {
return html`
<div class="margin-content peertube-plugin-livechat-configuration peertube-plugin-livechat-configuration-home">
<h1>
${view.title}
${view.helpButton}
</h1>
<p>${view.description}</p>
<p>${view.please_select}</p>
<ul class="peertube-plugin-livechat-configuration-home-channels">
${view.channels.map((channel) => html`
<li>
<a href="${channel.livechatConfigurationUri}">
${channel.avatar ?
html`<img class="avatar channel" src="${channel.avatar.path}">`
:
html`<div class="avatar channel initial gray"></div>`
}
</a>
<div class="peertube-plugin-livechat-configuration-home-info">
<a href="${channel.livechatConfigurationUri}">
<div>${channel.displayName}</div>
<div>${channel.name}</div>
</a>
</div>
</li>
`)}
</ul>
</div>
`
2023-09-26 14:21:32 +02:00
}
2023-08-08 17:15:05 +02:00
export {
renderConfigurationHome
2023-08-08 17:15:05 +02:00
}