2024-01-30 00:33:19 -05:00
|
|
|
<script lang="ts">
|
|
|
|
|
import Services from '$lib/services.json'
|
|
|
|
|
import JellyfinAuthBox from './jellyfinAuthBox.svelte'
|
2024-02-01 18:10:15 -05:00
|
|
|
import { newestAlert } from '$lib/stores.js'
|
|
|
|
|
import type { PageServerData } from './$types.js'
|
2024-01-30 00:33:19 -05:00
|
|
|
import type { SubmitFunction } from '@sveltejs/kit'
|
2024-02-01 19:50:30 -05:00
|
|
|
import { getDeviceUUID } from '$lib/utils'
|
2024-02-02 03:05:42 -05:00
|
|
|
import { SvelteComponent, type ComponentType } from 'svelte'
|
|
|
|
|
import ConnectionProfile from './connectionProfile.svelte'
|
2024-01-30 00:33:19 -05:00
|
|
|
|
2024-02-01 18:10:15 -05:00
|
|
|
export let data: PageServerData
|
|
|
|
|
let connections = data.userConnections
|
2024-01-30 00:33:19 -05:00
|
|
|
|
|
|
|
|
const submitCredentials: SubmitFunction = ({ formData, action, cancel }) => {
|
|
|
|
|
switch (action.search) {
|
|
|
|
|
case '?/authenticateJellyfin':
|
|
|
|
|
const { serverUrl, username, password } = Object.fromEntries(formData)
|
|
|
|
|
|
|
|
|
|
if (!(serverUrl && username && password)) {
|
|
|
|
|
$newestAlert = ['caution', 'All fields must be filled out']
|
|
|
|
|
return cancel()
|
|
|
|
|
}
|
|
|
|
|
try {
|
2024-02-02 03:05:42 -05:00
|
|
|
formData.set('serverUrl', new URL(serverUrl.toString()).origin)
|
2024-01-30 00:33:19 -05:00
|
|
|
} catch {
|
|
|
|
|
$newestAlert = ['caution', 'Server URL is invalid']
|
|
|
|
|
return cancel()
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 19:50:30 -05:00
|
|
|
const deviceId = getDeviceUUID()
|
|
|
|
|
formData.append('deviceId', deviceId)
|
2024-01-30 00:33:19 -05:00
|
|
|
break
|
|
|
|
|
case '?/deleteConnection':
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return async ({ result }) => {
|
2024-02-03 02:47:23 -05:00
|
|
|
if (result.type === 'failure') {
|
|
|
|
|
return ($newestAlert = ['warning', result.data?.message])
|
|
|
|
|
} else if (result.type === 'success') {
|
|
|
|
|
if (result.data?.newConnection) {
|
|
|
|
|
const newConnection: Connection = result.data.newConnection
|
|
|
|
|
connections = [newConnection, ...connections]
|
2024-01-30 00:33:19 -05:00
|
|
|
|
2024-02-03 02:47:23 -05:00
|
|
|
newConnectionModal = null
|
|
|
|
|
return ($newestAlert = ['success', `Added ${Services[newConnection.service.type].displayName}`])
|
|
|
|
|
} else if (result.data?.deletedConnectionId) {
|
|
|
|
|
const id = result.data.deletedConnectionId
|
|
|
|
|
const indexToDelete = connections.findIndex((connection) => connection.id === id)
|
|
|
|
|
const serviceType = connections[indexToDelete].service.type
|
2024-01-30 00:33:19 -05:00
|
|
|
|
2024-02-03 02:47:23 -05:00
|
|
|
connections.splice(indexToDelete, 1)
|
|
|
|
|
connections = connections
|
2024-01-30 00:33:19 -05:00
|
|
|
|
2024-02-03 02:47:23 -05:00
|
|
|
return ($newestAlert = ['success', `Deleted ${Services[serviceType].displayName}`])
|
|
|
|
|
}
|
2024-01-30 00:33:19 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-01 19:50:30 -05:00
|
|
|
|
2024-02-02 03:05:42 -05:00
|
|
|
let newConnectionModal: ComponentType<SvelteComponent<{ submitFunction: SubmitFunction }>> | null = null
|
2024-01-30 00:33:19 -05:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<main>
|
|
|
|
|
<section class="mb-8 rounded-lg px-4" style="background-color: rgba(82, 82, 82, 0.25);">
|
|
|
|
|
<h1 class="py-2 text-xl">Add Connection</h1>
|
|
|
|
|
<div class="flex flex-wrap gap-2 pb-4">
|
2024-02-02 03:05:42 -05:00
|
|
|
<button class="add-connection-button h-14 rounded-md" on:click={() => (newConnectionModal = JellyfinAuthBox)}>
|
2024-02-01 19:50:30 -05:00
|
|
|
<img src={Services.jellyfin.icon} alt="{Services.jellyfin.displayName} icon" class="aspect-square h-full p-2" />
|
|
|
|
|
</button>
|
2024-02-02 03:05:42 -05:00
|
|
|
<button class="add-connection-button h-14 rounded-md">
|
2024-02-01 19:50:30 -05:00
|
|
|
<img src={Services['youtube-music'].icon} alt="{Services['youtube-music'].displayName} icon" class="aspect-square h-full p-2" />
|
|
|
|
|
</button>
|
2024-01-30 00:33:19 -05:00
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
<div class="grid gap-8">
|
2024-02-01 18:10:15 -05:00
|
|
|
{#each connections as connection}
|
2024-02-02 03:05:42 -05:00
|
|
|
<ConnectionProfile {connection} submitFunction={submitCredentials} />
|
2024-01-30 00:33:19 -05:00
|
|
|
{/each}
|
|
|
|
|
</div>
|
2024-02-03 02:47:23 -05:00
|
|
|
{#if newConnectionModal !== null}
|
|
|
|
|
<svelte:component this={newConnectionModal} submitFunction={submitCredentials} on:close={() => (newConnectionModal = null)} />
|
|
|
|
|
{/if}
|
2024-01-30 00:33:19 -05:00
|
|
|
</main>
|
2024-02-01 19:50:30 -05:00
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
.add-connection-button {
|
|
|
|
|
background-image: linear-gradient(to bottom, rgb(30, 30, 30), rgb(10, 10, 10));
|
|
|
|
|
}
|
2024-02-02 03:05:42 -05:00
|
|
|
</style>
|