315 lines
9.7 KiB
Vue
315 lines
9.7 KiB
Vue
<script setup>
|
|
import { onMounted, ref } from 'vue';
|
|
import WindowHandle from './partials/WindowHandle.vue';
|
|
import Tabs from '../layouts/Tabs.vue';
|
|
import { SetupHandle, SetSize, ResetPosition, Top, SetResizable, SetMinSize } from '@/services/Windows';
|
|
import Server from '~/services/Server';
|
|
import { DisplayToast } from '~/services/Toaster';
|
|
|
|
const props = defineProps(['data']);
|
|
const data = props.data;
|
|
const campaign = data.campaign;
|
|
|
|
const handle = ref(null);
|
|
const wrapper = ref(null);
|
|
const saving = ref(false);
|
|
const name = ref(campaign.name || '');
|
|
const color = ref(campaign.color || '#6b6b6b');
|
|
const settings = ref(campaign.settings && typeof campaign.settings === 'object' ? campaign.settings : {});
|
|
const foundryUrl = ref(settings.value.integrations?.foundryUrl || '');
|
|
const id = data.id;
|
|
|
|
// Els serveis s'enllacen tots igual: ells mostren un enllaç de Dragonroll i aquí
|
|
// només es mira què té connectat la campanya i se'n desenllaça el que calgui.
|
|
const services = ref([]);
|
|
const unlinked = ref('');
|
|
|
|
const rows = [
|
|
{ id: 'basic', value: 'campaigns.settings.tabs.basic' },
|
|
{ id: 'integrations', value: 'campaigns.settings.tabs.integrations' }
|
|
];
|
|
|
|
const serviceKeys = ['foundry', 'discord', 'obsidian'];
|
|
|
|
onMounted(() => {
|
|
Top(wrapper);
|
|
SetupHandle(id, handle);
|
|
SetSize(id, { width: 560, height: 430 });
|
|
ResetPosition(id, 'center');
|
|
SetResizable(id, true);
|
|
SetMinSize(id, { width: 440, height: 340 });
|
|
LoadServices();
|
|
});
|
|
|
|
function ApplyCampaignUpdate(updatedCampaign) {
|
|
Object.assign(campaign, updatedCampaign);
|
|
settings.value = updatedCampaign.settings || {};
|
|
foundryUrl.value = settings.value.integrations?.foundryUrl || foundryUrl.value;
|
|
}
|
|
|
|
function validFoundryUrl(value) {
|
|
if (!value.trim()) return true;
|
|
try {
|
|
const url = new URL(value.trim());
|
|
return url.protocol === 'http:' || url.protocol === 'https:';
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function SaveSettings() {
|
|
if (!name.value.trim()) {
|
|
DisplayToast('red', $t('campaigns.settings.errors.name-required'), 3000);
|
|
return;
|
|
}
|
|
if (!validFoundryUrl(foundryUrl.value)) {
|
|
DisplayToast('red', $t('campaigns.settings.errors.invalid-foundry-url'), 3000);
|
|
return;
|
|
}
|
|
|
|
saving.value = true;
|
|
const updatedSettings = {
|
|
...settings.value,
|
|
integrations: {
|
|
...(settings.value.integrations || {}),
|
|
foundryUrl: foundryUrl.value.trim()
|
|
}
|
|
};
|
|
|
|
try {
|
|
const response = await Server().post('/campaign/update', {
|
|
id: campaign._id,
|
|
name: name.value.trim(),
|
|
color: color.value,
|
|
settings: updatedSettings
|
|
});
|
|
|
|
if (response.data.status !== 'ok') {
|
|
DisplayToast('red', $t(response.data.msg || 'errors.internal'), 3000);
|
|
return;
|
|
}
|
|
|
|
ApplyCampaignUpdate(response.data.campaign);
|
|
DisplayToast('green', $t('campaigns.settings.saved'), 3000);
|
|
} catch {
|
|
DisplayToast('red', $t('errors.internal'), 3000);
|
|
} finally {
|
|
saving.value = false;
|
|
}
|
|
}
|
|
|
|
async function LoadServices() {
|
|
try {
|
|
const response = await Server().get(`/campaign/services?campaignId=${campaign._id}`);
|
|
services.value = response.data.status === 'ok' && Array.isArray(response.data.services)
|
|
? response.data.services
|
|
: [];
|
|
} catch {
|
|
services.value = [];
|
|
}
|
|
}
|
|
|
|
function LinkFor(service) {
|
|
return services.value.find((link) => link.service === service) || null;
|
|
}
|
|
|
|
// El món de Foundry es descriu amb el que la campanya ja en sap, per si el món
|
|
// es va enllaçar abans que els serveis tinguessin un registre comú.
|
|
function ServiceTarget(service, link) {
|
|
if (link?.targetName) return link.targetName;
|
|
if (service === 'foundry' && campaign.foundryWorldTitle) return campaign.foundryWorldTitle;
|
|
if (service === 'discord' && campaign.discordGuildId) return campaign.discordGuildId;
|
|
return '';
|
|
}
|
|
|
|
function WhoConnected(link) {
|
|
return link?.dragonrollUserName ? $t('campaigns.settings.services.connected-by', { user: link.dragonrollUserName }) : '';
|
|
}
|
|
|
|
async function Unlink(service) {
|
|
unlinked.value = service;
|
|
try {
|
|
const response = await Server().post('/campaign/service-unlink', {
|
|
campaignId: campaign._id,
|
|
service
|
|
});
|
|
|
|
if (response.data.status !== 'ok') {
|
|
DisplayToast('red', $t(response.data.msg || 'errors.internal'), 3000);
|
|
return;
|
|
}
|
|
|
|
ApplyCampaignUpdate(response.data.campaign);
|
|
services.value = Array.isArray(response.data.services) ? response.data.services : [];
|
|
DisplayToast('green', $t('campaigns.settings.services.unlinked', {
|
|
service: $t(`campaigns.settings.services.${service}.name`)
|
|
}), 3000);
|
|
} catch {
|
|
DisplayToast('red', $t('errors.internal'), 3000);
|
|
} finally {
|
|
unlinked.value = '';
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="window-wrapper" :id="'window-wrapper-' + id" ref="wrapper">
|
|
<WindowHandle :window="id" ref="handle"></WindowHandle>
|
|
|
|
<div class="settings-body">
|
|
<Tabs :rows="rows">
|
|
<template #basic>
|
|
<div class="settings-fields">
|
|
<label class="field">
|
|
<span>{{ $t('campaigns.settings.name') }}</span>
|
|
<input v-model="name" type="text" maxlength="120" autocomplete="off">
|
|
</label>
|
|
<label class="field color-field">
|
|
<span>{{ $t('campaigns.settings.color') }}</span>
|
|
<input v-model="color" type="color">
|
|
</label>
|
|
</div>
|
|
</template>
|
|
<template #integrations>
|
|
<div class="settings-fields">
|
|
<div v-for="service in serviceKeys" :key="service" class="service">
|
|
<div class="service-head">
|
|
<strong>{{ $t(`campaigns.settings.services.${service}.name`) }}</strong>
|
|
<button
|
|
v-if="LinkFor(service)"
|
|
class="btn-primary sound-click"
|
|
type="button"
|
|
:disabled="unlinked !== ''"
|
|
@click="Unlink(service)"
|
|
>
|
|
{{ $t('campaigns.settings.services.unlink') }}
|
|
</button>
|
|
</div>
|
|
<p class="service-state" :class="{ linked: LinkFor(service) }">
|
|
{{ ServiceTarget(service, LinkFor(service)) || $t('campaigns.settings.services.not-connected') }}
|
|
<span v-if="WhoConnected(LinkFor(service))" class="who">
|
|
{{ WhoConnected(LinkFor(service)) }}
|
|
</span>
|
|
</p>
|
|
<p class="help-text standalone">
|
|
{{ $t(`campaigns.settings.services.${service}.how`) }}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div class="settings-fields">
|
|
<label class="field">
|
|
<span>{{ $t('campaigns.settings.foundry-url') }}</span>
|
|
<input
|
|
v-model="foundryUrl"
|
|
type="url"
|
|
placeholder="https://foundry.example.com"
|
|
autocomplete="url"
|
|
>
|
|
</label>
|
|
<p class="help-text">{{ $t('campaigns.settings.foundry-url-help') }}</p>
|
|
</div>
|
|
</template>
|
|
</Tabs>
|
|
<div class="actions">
|
|
<button class="btn-primary sound-click" type="button" :disabled="saving" @click="SaveSettings">
|
|
{{ saving ? $t('campaigns.settings.saving') : $t('general.save') }}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped lang="scss">
|
|
.window-wrapper {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
}
|
|
|
|
.settings-body {
|
|
display: flex;
|
|
flex: 1;
|
|
flex-direction: column;
|
|
min-height: 0;
|
|
width: 100%;
|
|
}
|
|
|
|
.settings-fields {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 18px;
|
|
padding: 22px;
|
|
}
|
|
|
|
.field {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 6px;
|
|
}
|
|
|
|
input {
|
|
width: 100%;
|
|
box-sizing: border-box;
|
|
padding: 8px 10px;
|
|
border: 1px solid var(--color-border);
|
|
border-radius: 6px;
|
|
background: var(--color-background-soft);
|
|
color: var(--color-text);
|
|
font-size: 14px;
|
|
}
|
|
|
|
.color-field input {
|
|
width: 72px;
|
|
height: 38px;
|
|
padding: 3px;
|
|
}
|
|
|
|
.help-text {
|
|
margin: -10px 22px 0;
|
|
color: var(--color-text);
|
|
font-size: 13px;
|
|
opacity: 0.75;
|
|
}
|
|
|
|
.help-text.standalone {
|
|
margin: 0;
|
|
}
|
|
|
|
.service {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 6px;
|
|
padding-bottom: 14px;
|
|
border-bottom: 1px solid var(--color-border);
|
|
}
|
|
|
|
.service-head {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 10px;
|
|
}
|
|
|
|
.service-state {
|
|
margin: 0;
|
|
font-size: 14px;
|
|
overflow-wrap: anywhere;
|
|
}
|
|
|
|
.service-state.linked {
|
|
color: var(--color-green);
|
|
}
|
|
|
|
.who {
|
|
opacity: 0.75;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.actions {
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
padding: 12px 18px;
|
|
border-top: 1px solid var(--color-border);
|
|
}
|
|
</style>
|