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

295 lines
8.6 KiB
Plaintext

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { on } from '@ember/modifier';
import { classifyCalendarError } from '../../../config/calendar';
import {
DEFAULT_SOCKETHUB_URL,
normalizeSockethubUrl,
} from '../../../config/sockethub';
import { normalizeFeedUrl } from '../../../config/ics';
// Setup for a published ICS/WebCal feed: the Sockethub server URL, then the
// feed URL. The feed URL is often a secret link, so there are no credentials.
export default class SettingsIntegrationsCalendarIcsForm extends Component {
@service calendar;
@service sockethub;
@tracked step = null;
@tracked serverUrlDraft = null;
@tracked feedUrlDraft = null;
@tracked error = null;
@tracked submitting = false;
@tracked sockethubReady = false;
@tracked hasStoredSockethub = false;
constructor(owner, args) {
super(owner, args);
if (args.props?.mode === 'edit' && args.props?.integration) {
this.step = 2;
}
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 ICS / WebCal Calendar'
: 'Add ICS / WebCal Calendar';
}
get serverUrl() {
return this.serverUrlDraft ?? this.sockethub.url ?? DEFAULT_SOCKETHUB_URL;
}
get feedUrl() {
return this.feedUrlDraft ?? this.integration?.url ?? '';
}
get isServerStep() {
return this.step === 1;
}
get isFeedStep() {
return this.step === 2;
}
// 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
updateServerUrl(event) {
this.serverUrlDraft = event.target.value;
this.error = null;
}
@action
updateFeedUrl(event) {
this.feedUrlDraft = event.target.value;
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 submitFeed(event) {
event.preventDefault();
this.error = null;
const url = normalizeFeedUrl(this.feedUrl);
if (!url) {
this.error = 'Please enter a valid .ics or webcal:// link';
return;
}
this.submitting = true;
try {
const { name } = await this.calendar.verifyFeed(url);
await this.calendar.configureIcs({ url, name }, this.integration?.id);
this.args.close?.();
} catch (err) {
this.error = classifyCalendarError(err);
} finally {
this.submitting = 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">
Add a published calendar link to show upcoming events on your
dashboard.
</p>
</div>
{{#if this.isServerStep}}
<form {{on "submit" this.submitServer}} class="mt-5">
<div>
<label
for="ics-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="ics-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.isFeedStep}}
<form {{on "submit" this.submitFeed}} class="mt-5">
<div>
<label
for="ics-feed-url"
class="block text-sm font-medium leading-6 text-gray-900"
>
Calendar link
</label>
<div class="mt-2">
<input
type="text"
inputmode="url"
id="ics-feed-url"
name="feed-url"
value={{this.feedUrl}}
{{on "input" this.updateFeedUrl}}
placeholder="https://example.org/holidays.ics"
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">
A read-only .ics link or webcal:// address. If it is private, keep
the secret link safe.
</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"
>
{{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>
}