Foundry and discord bot
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
This commit is contained in:
356
frontend/app/components/windows/CampaignSettingsWindow.vue
Normal file
356
frontend/app/components/windows/CampaignSettingsWindow.vue
Normal file
@@ -0,0 +1,356 @@
|
||||
<script setup>
|
||||
import { onBeforeUnmount, 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 pairingCode = ref('');
|
||||
const requestId = ref('');
|
||||
const linking = ref(false);
|
||||
const id = data.id;
|
||||
let statusPoll;
|
||||
let checkingStatus = false;
|
||||
|
||||
const rows = [
|
||||
{ id: 'basic', value: 'campaigns.settings.tabs.basic' },
|
||||
{ id: 'integrations', value: 'campaigns.settings.tabs.integrations' }
|
||||
];
|
||||
|
||||
onMounted(() => {
|
||||
Top(wrapper);
|
||||
SetupHandle(id, handle);
|
||||
SetSize(id, { width: 560, height: 430 });
|
||||
ResetPosition(id, 'center');
|
||||
SetResizable(id, true);
|
||||
SetMinSize(id, { width: 440, height: 340 });
|
||||
ResumePendingFoundryLink();
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
clearInterval(statusPoll);
|
||||
});
|
||||
|
||||
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 StartFoundryLink() {
|
||||
linking.value = true;
|
||||
try {
|
||||
const response = await Server().post('/campaign/foundry-link/start', { campaignId: campaign._id });
|
||||
if (response.data.status !== 'ok') {
|
||||
DisplayToast('red', $t(response.data.msg || 'errors.internal'), 3000);
|
||||
return;
|
||||
}
|
||||
|
||||
pairingCode.value = response.data.code;
|
||||
requestId.value = response.data.requestId;
|
||||
clearInterval(statusPoll);
|
||||
statusPoll = setInterval(CheckFoundryLinkStatus, 2500);
|
||||
} catch {
|
||||
DisplayToast('red', $t('errors.internal'), 3000);
|
||||
} finally {
|
||||
linking.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// The pairing code lives server-side until it expires, so re-attach to it when
|
||||
// the window opens again after a reload, restart or closing the window.
|
||||
async function ResumePendingFoundryLink() {
|
||||
try {
|
||||
const response = await Server().get(`/campaign/foundry-link/pending?campaignId=${campaign._id}`);
|
||||
if (response.data.status !== 'ok' || !response.data.request) return;
|
||||
|
||||
pairingCode.value = response.data.request.code || '';
|
||||
requestId.value = response.data.request.requestId;
|
||||
clearInterval(statusPoll);
|
||||
statusPoll = setInterval(CheckFoundryLinkStatus, 2500);
|
||||
} catch {
|
||||
// Showing the stored pairing code is best-effort; ignore resume failures.
|
||||
}
|
||||
}
|
||||
|
||||
async function CheckFoundryLinkStatus() {
|
||||
if (!requestId.value || checkingStatus) return;
|
||||
checkingStatus = true;
|
||||
try {
|
||||
const response = await Server().get(`/campaign/foundry-link/status/${requestId.value}`);
|
||||
if (response.data.status !== 'ok' || response.data.state === 'expired') {
|
||||
clearInterval(statusPoll);
|
||||
pairingCode.value = '';
|
||||
requestId.value = '';
|
||||
const error = response.data.state === 'expired'
|
||||
? 'campaigns.settings.errors.link-expired'
|
||||
: response.data.msg || 'campaigns.settings.errors.link-expired';
|
||||
DisplayToast('red', $t(error), 3000);
|
||||
return;
|
||||
}
|
||||
if (response.data.state === 'complete') {
|
||||
ApplyCampaignUpdate(response.data.campaign);
|
||||
clearInterval(statusPoll);
|
||||
pairingCode.value = '';
|
||||
requestId.value = '';
|
||||
DisplayToast('green', $t('campaigns.settings.linked'), 3000);
|
||||
}
|
||||
} catch {
|
||||
// Keep polling while the Foundry module completes the pairing.
|
||||
} finally {
|
||||
checkingStatus = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function UnlinkFoundry() {
|
||||
linking.value = true;
|
||||
try {
|
||||
const response = await Server().post('/campaign/foundry-unlink', { campaignId: campaign._id });
|
||||
if (response.data.status !== 'ok') {
|
||||
DisplayToast('red', $t(response.data.msg || 'errors.internal'), 3000);
|
||||
return;
|
||||
}
|
||||
|
||||
ApplyCampaignUpdate(response.data.campaign);
|
||||
DisplayToast('green', $t('campaigns.settings.unlinked'), 3000);
|
||||
} catch {
|
||||
DisplayToast('red', $t('errors.internal'), 3000);
|
||||
} finally {
|
||||
linking.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function CopyPairingCode() {
|
||||
try {
|
||||
await navigator.clipboard.writeText(pairingCode.value);
|
||||
DisplayToast('green', $t('campaigns.settings.code-copied'), 2000);
|
||||
} catch {
|
||||
DisplayToast('red', $t('campaigns.settings.errors.copy-code'), 3000);
|
||||
}
|
||||
}
|
||||
</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 class="world-link">
|
||||
<div v-if="campaign.foundryWorldId" class="linked-world">
|
||||
<strong>{{ campaign.foundryWorldTitle || campaign.foundryWorldId }}</strong>
|
||||
<span v-if="campaign.foundrySystem">{{ campaign.foundrySystem }}</span>
|
||||
<button class="btn-primary sound-click" type="button" :disabled="linking" @click="UnlinkFoundry">
|
||||
{{ $t('campaigns.settings.unlink-foundry') }}
|
||||
</button>
|
||||
</div>
|
||||
<p v-else class="help-text standalone">{{ $t('campaigns.settings.no-foundry-world') }}</p>
|
||||
|
||||
<button class="btn-primary sound-click" type="button" :disabled="linking" @click="StartFoundryLink">
|
||||
{{ $t('campaigns.settings.start-foundry-link') }}
|
||||
</button>
|
||||
<p class="help-text standalone">{{ $t('campaigns.settings.start-foundry-link-help') }}</p>
|
||||
<div v-if="pairingCode" class="pairing-code">
|
||||
<code>{{ pairingCode }}</code>
|
||||
<button class="btn-primary sound-click" type="button" @click="CopyPairingCode">
|
||||
{{ $t('campaigns.settings.copy-code') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<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;
|
||||
}
|
||||
|
||||
.world-link {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
padding-bottom: 14px;
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.linked-world {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.linked-world span {
|
||||
opacity: 0.75;
|
||||
}
|
||||
|
||||
.pairing-code {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 10px;
|
||||
padding: 10px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.pairing-code code {
|
||||
user-select: text;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
padding: 12px 18px;
|
||||
border-top: 1px solid var(--color-border);
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user