Files
raucao 4c9d6093c0 Skip the Sockethub server step in calendar setup when already configured
Track whether a Sockethub URL has been stored and start the ICS and CalDAV add flows past the server step for returning users, while keeping it for first-timers. Back navigation stops before the removed step, and the step's helper text now links Sockethub to its website.
2026-11-07 13:38:33 +04:00

568 lines
18 KiB
Plaintext

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { fn } from '@ember/helper';
import { on } from '@ember/modifier';
import { classifyCalendarError } from '../../../config/calendar';
import {
normalizeSockethubUrl,
DEFAULT_SOCKETHUB_URL,
} from '../../../config/sockethub';
import { normalizeCaldavUrl } from '../../../config/caldav';
// Multi-step setup for a CalDAV account: server URL, account credentials, then
// the calendars to display. On edit it opens on the calendar selection and
// refreshes the list with the stored credentials.
export default class SettingsIntegrationsCalendarCaldavForm extends Component {
@service calendar;
@service sockethub;
@tracked step = null;
@tracked serverUrlDraft = null;
@tracked caldavUrlDraft = null;
@tracked usernameDraft = null;
@tracked password = '';
@tracked calendars = [];
@tracked selectedIds = [];
@tracked error = null;
@tracked submitting = false;
@tracked refreshing = false;
@tracked sockethubReady = false;
@tracked hasStoredSockethub = false;
constructor(owner, args) {
super(owner, args);
const integration = args.props?.integration;
if (args.props?.mode === 'edit' && integration) {
this.calendars = integration.calendars ?? [];
this.selectedIds = integration.selectedCalendarIds ?? [];
this.step = 3;
this.refreshCalendars();
}
this.loadSockethubState();
}
async loadSockethubState() {
let hasStored = false;
try {
await this.sockethub.loadUrl();
hasStored = Boolean(this.sockethub.hasStoredUrl);
} catch (err) {
console.error('Error loading the Sockethub URL:', err);
}
this.hasStoredSockethub = hasStored;
this.sockethubReady = true;
// Add mode starts with no step; edit mode (with an integration) already
// chose one. Anything still undecided falls back to the normal first step.
if (this.step === null) {
this.step = hasStored ? 2 : 1;
}
}
get integration() {
return this.args.props?.integration;
}
get isEdit() {
return this.args.props?.mode === 'edit';
}
get title() {
return this.isEdit ? 'Edit CalDAV Calendar' : 'Add CalDAV Calendar';
}
get serverUrl() {
return this.serverUrlDraft ?? this.sockethub.url ?? DEFAULT_SOCKETHUB_URL;
}
get caldavUrl() {
return this.caldavUrlDraft ?? this.integration?.url ?? '';
}
get username() {
return this.usernameDraft ?? this.integration?.username ?? '';
}
// Calendars that can hold events (unknown component lists are kept).
get eventCalendars() {
return this.calendars.filter(
(calendar) =>
calendar.components.length === 0 ||
calendar.components.includes('event')
);
}
get isServerStep() {
return this.step === 1;
}
get isAccountStep() {
return this.step === 2;
}
get isSelectionStep() {
return this.step === 3;
}
// Returning users already chose a Sockethub server, so the server step is
// skipped and Back must not walk into it.
get firstStep() {
return this.hasStoredSockethub ? 2 : 1;
}
get canGoBack() {
return this.sockethubReady && this.step > this.firstStep;
}
@action
isSelected(id) {
return this.selectedIds.includes(id);
}
@action
updateServerUrl(event) {
this.serverUrlDraft = event.target.value;
this.error = null;
}
@action
updateCaldavUrl(event) {
this.caldavUrlDraft = event.target.value;
this.error = null;
}
@action
updateUsername(event) {
this.usernameDraft = event.target.value;
this.error = null;
}
@action
updatePassword(event) {
this.password = event.target.value;
this.error = null;
}
@action
toggle(id) {
this.selectedIds = this.selectedIds.includes(id)
? this.selectedIds.filter((selected) => selected !== id)
: [...this.selectedIds, id];
this.error = null;
}
@action
back() {
this.error = null;
this.step = Math.max(this.step - 1, this.firstStep);
}
@action
async submitServer(event) {
event.preventDefault();
this.error = null;
const url = normalizeSockethubUrl(this.serverUrl);
if (!url) {
this.error = 'Please enter a valid Sockethub server URL';
return;
}
this.submitting = true;
try {
await this.sockethub.setUrl(url);
this.step = 2;
} catch (err) {
this.error = classifyCalendarError(err);
} finally {
this.submitting = false;
}
}
@action
async submitAccount(event) {
event.preventDefault();
this.error = null;
const url = normalizeCaldavUrl(this.caldavUrl);
if (!url) {
this.error = 'Please enter a valid CalDAV URL';
return;
}
const username = this.username.trim();
if (!username) {
this.error = 'Please enter your username';
return;
}
// On edit a blank password keeps the stored one.
const password = this.password.trim() || this.integration?.password;
if (!password) {
this.error = 'Please enter your password';
return;
}
this.submitting = true;
try {
const calendars = await this.calendar.fetchCalendars({
url,
username,
password,
});
this.calendars = calendars;
const ids = new Set(calendars.map((calendar) => calendar.id));
this.selectedIds = this.selectedIds.filter((id) => ids.has(id));
this.step = 3;
} catch (err) {
this.error = classifyCalendarError(err);
} finally {
this.submitting = false;
}
}
@action
async finish(event) {
event.preventDefault();
this.error = null;
if (!this.selectedIds.length) {
this.error = 'Select at least one calendar';
return;
}
this.submitting = true;
try {
await this.calendar.configureCaldav(
{
url: normalizeCaldavUrl(this.caldavUrl),
username: this.username.trim(),
password: this.password.trim() || this.integration?.password,
calendars: this.calendars,
selectedCalendarIds: this.selectedIds,
},
this.integration?.id
);
this.args.close?.();
} catch (err) {
this.error = classifyCalendarError(err);
} finally {
this.submitting = false;
}
}
async refreshCalendars() {
const integration = this.integration;
if (!integration) {
return;
}
this.refreshing = true;
try {
const calendars = await this.calendar.fetchCalendars({
url: integration.url,
username: integration.username,
password: integration.password,
});
if (calendars.length) {
this.calendars = calendars;
const ids = new Set(calendars.map((calendar) => calendar.id));
this.selectedIds = this.selectedIds.filter((id) => ids.has(id));
}
} catch {
// Keep the stored calendars if the refresh fails.
} finally {
this.refreshing = false;
}
}
<template>
<div class="p-6">
<h3
id={{if @titleId @titleId "modal-title"}}
class="text-base font-semibold leading-6 text-gray-900"
>{{this.title}}</h3>
<div class="mt-2">
<p class="text-sm text-gray-500">
Connect a CalDAV account to show upcoming events on your dashboard.
</p>
</div>
{{#if this.isServerStep}}
<form {{on "submit" this.submitServer}} class="mt-5">
<div>
<label
for="caldav-server-url"
class="block text-sm font-medium leading-6 text-gray-900"
>
Sockethub server URL
</label>
<div class="mt-2">
<input
type="text"
inputmode="url"
id="caldav-server-url"
name="server-url"
value={{this.serverUrl}}
{{on "input" this.updateServerUrl}}
placeholder={{DEFAULT_SOCKETHUB_URL}}
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
<p class="mt-2 text-sm text-gray-500">
The
<a
href="https://sockethub.org"
target="_blank"
rel="noopener noreferrer"
class="text-indigo-600 hover:text-indigo-500"
>Sockethub</a>
instance Dashtab talks to. Leave as-is unless you self-host it.
</p>
</div>
{{#if this.error}}
<p class="mt-4 text-sm text-red-600">{{this.error}}</p>
{{/if}}
<div class="mt-5 sm:mt-6 flex gap-3">
<button
type="submit"
disabled={{this.submitting}}
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 disabled:opacity-50"
>
Continue
</button>
<button
type="button"
{{on "click" @close}}
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Cancel
</button>
</div>
</form>
{{else if this.isAccountStep}}
<form {{on "submit" this.submitAccount}} class="mt-5">
<div class="space-y-5">
<div>
<label
for="caldav-url"
class="block text-sm font-medium leading-6 text-gray-900"
>
CalDAV URL
</label>
<div class="mt-2">
<input
type="text"
inputmode="url"
id="caldav-url"
name="caldav-url"
value={{this.caldavUrl}}
{{on "input" this.updateCaldavUrl}}
placeholder="https://calendar.example/dav/"
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
</div>
<div>
<label
for="caldav-username"
class="block text-sm font-medium leading-6 text-gray-900"
>
Username
</label>
<div class="mt-2">
<input
type="text"
id="caldav-username"
name="username"
autocomplete="username"
value={{this.username}}
{{on "input" this.updateUsername}}
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
</div>
<div>
<label
for="caldav-password"
class="block text-sm font-medium leading-6 text-gray-900"
>
Password
</label>
<div class="mt-2">
<input
type="password"
id="caldav-password"
name="password"
autocomplete="current-password"
value={{this.password}}
{{on "input" this.updatePassword}}
placeholder={{if
this.isEdit
"Leave blank to keep current"
""
}}
class="block w-full rounded-md border-0 px-3 py-2 text-sm text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600"
/>
</div>
<p class="mt-2 text-sm text-gray-500">
Use an app password where your provider supports one.
</p>
</div>
</div>
{{#if this.error}}
<p class="mt-4 text-sm text-red-600">{{this.error}}</p>
{{/if}}
<div class="mt-5 sm:mt-6 flex gap-3">
<button
type="submit"
disabled={{this.submitting}}
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 disabled:opacity-50"
>
Continue
</button>
{{#if this.canGoBack}}
<button
type="button"
data-test-back
{{on "click" this.back}}
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Back
</button>
{{/if}}
<button
type="button"
{{on "click" @close}}
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Cancel
</button>
</div>
</form>
{{else if this.isSelectionStep}}
<form {{on "submit" this.finish}} class="mt-5">
{{#if this.eventCalendars.length}}
<fieldset>
<legend class="text-base font-semibold text-gray-900">
Calendars
</legend>
<div
class="mt-4 divide-y divide-gray-200 border-t border-b border-gray-200"
>
{{#each this.eventCalendars as |calendar index|}}
<div class="relative flex gap-3 py-4">
<div class="min-w-0 flex-1 text-sm/6">
<label
for="caldav-calendar-{{index}}"
class="font-medium text-gray-900 select-none"
>
{{calendar.name}}
</label>
{{#if calendar.components.length}}
<p class="text-xs text-gray-500">
{{calendar.components}}
</p>
{{/if}}
</div>
<div class="flex h-6 shrink-0 items-center">
<div class="group grid size-4 grid-cols-1">
<input
id="caldav-calendar-{{index}}"
type="checkbox"
name="calendar-{{index}}"
checked={{this.isSelected calendar.id}}
{{on "change" (fn this.toggle calendar.id)}}
class="col-start-1 row-start-1 appearance-none rounded-sm border border-gray-300 bg-white checked:border-indigo-600 checked:bg-indigo-600 indeterminate:border-indigo-600 indeterminate:bg-indigo-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:border-gray-300 disabled:bg-gray-100 disabled:checked:bg-gray-100 forced-colors:appearance-auto"
/>
<svg
viewBox="0 0 14 14"
fill="none"
class="pointer-events-none col-start-1 row-start-1 size-3.5 self-center justify-self-center stroke-white group-has-disabled:stroke-gray-950/25"
>
<path
d="M3 8L6 11L11 3.5"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="opacity-0 group-has-checked:opacity-100"
/>
<path
d="M3 7H11"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="opacity-0 group-has-indeterminate:opacity-100"
/>
</svg>
</div>
</div>
</div>
{{/each}}
</div>
</fieldset>
{{else}}
<p class="text-sm text-gray-500">
No calendars with events were found for this account.
</p>
{{/if}}
{{#if this.error}}
<p class="mt-4 text-sm text-red-600">{{this.error}}</p>
{{/if}}
<div class="mt-5 sm:mt-6 flex gap-3">
<button
type="submit"
disabled={{this.submitting}}
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 disabled:opacity-50"
>
{{if this.isEdit "Save" "Connect"}}
</button>
{{#if this.canGoBack}}
<button
type="button"
data-test-back
{{on "click" this.back}}
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Back
</button>
{{/if}}
<button
type="button"
{{on "click" @close}}
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Cancel
</button>
</div>
</form>
{{/if}}
</div>
</template>
}