Files
raucao 303b26793f Reorder the custom link option and its icon controls
Sort the generic "Custom Link" entry last in the add-integration dialog so
it reads as an "add anything" action after the concrete integrations, and
move the icon controls below the URL and name fields in the custom link
form so the form flows from link details to its optional icon.
2026-11-13 21:09:21 +04:00

341 lines
9.8 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 { labelForUrl, normalizeSiteUrl } from '../../../utils/site-url';
// Add/edit a user-defined quick link. The site's own icon is discovered in the
// background (and previewed here); a custom upload can override it and is
// pinned until the user chooses "Use site's icon" again.
export default class SettingsIntegrationsCustomLinkForm extends Component {
@service sites;
@service siteIcons;
@service storage;
@tracked urlDraft = null;
@tracked nameDraft = null;
@tracked customIcon = null;
@tracked previewIcon = null;
@tracked previewLoading = false;
@tracked imgFailed = false;
@tracked error = null;
@tracked submitting = false;
constructor() {
super(...arguments);
this.customIcon = this.integration?.customIcon ?? null;
this.previewIcon = this.integration?.icon ?? null;
}
get integration() {
return this.args.props?.integration;
}
get isEdit() {
return this.args.props?.mode === 'edit';
}
get title() {
return this.isEdit ? 'Edit Link' : 'Add Custom Link';
}
get submitLabel() {
return this.isEdit ? 'Save' : 'Add';
}
get url() {
return this.urlDraft ?? this.integration?.url ?? '';
}
get name() {
return this.nameDraft ?? this.integration?.name ?? '';
}
get preview() {
return this.customIcon ?? this.previewIcon;
}
get hasPreview() {
return Boolean(this.preview?.dataUrl) && !this.imgFailed;
}
get imageClass() {
return this.preview?.maskable
? 'size-full object-cover'
: 'size-8 rounded-md object-contain';
}
get initial() {
const source = (this.name || labelForUrl(this.url) || '?').trim();
return source ? source[0].toUpperCase() : '?';
}
@action
updateUrl(event) {
this.urlDraft = event.target.value;
this.error = null;
}
@action
updateName(event) {
this.nameDraft = event.target.value;
}
// Preview the site's discovered icon after the user finishes typing a URL.
// Uses the cached resolver, so an unchanged edit never triggers a probe on
// its own.
@action
async previewFromUrl() {
const url = normalizeSiteUrl(this.url);
if (!url || this.customIcon) {
return;
}
this.previewLoading = true;
try {
const icon = await this.siteIcons.iconFor(url);
// The URL may have changed while discovery was in flight.
if (normalizeSiteUrl(this.url) !== url) {
return;
}
if (icon) {
this.previewIcon = icon;
this.imgFailed = false;
}
} finally {
this.previewLoading = false;
}
}
@action
async uploadIcon(event) {
const input = event.target;
const file = input.files?.[0];
// Reset so picking the same file again still fires `change`.
input.value = '';
if (!file) {
return;
}
this.error = null;
try {
this.customIcon = await this.siteIcons.iconFromFile(file);
this.imgFailed = false;
} catch (err) {
this.error = err?.message ?? 'Unable to use that image';
}
}
@action
useSiteIcon() {
this.customIcon = null;
this.imgFailed = false;
this.previewFromUrl();
}
@action
handleImageError() {
this.imgFailed = true;
}
@action
async handleSubmit(event) {
event.preventDefault();
this.error = null;
const url = normalizeSiteUrl(this.url);
if (!url) {
this.error = 'Please enter a valid website URL';
return;
}
this.submitting = true;
try {
const name = this.name.trim();
await this.storage.set('integrations', this.key(), {
type: 'custom',
status: 'active',
url,
...(name ? { name } : {}),
// Omitting `customIcon` clears a previously pinned icon.
...(this.customIcon ? { customIcon: this.customIcon } : {}),
createdAt: this.integration?.createdAt ?? Date.now(),
});
await this.sites.reload();
this.args.close?.();
} catch (err) {
console.error('Error saving custom link:', err);
this.error = 'Unable to save this link';
} finally {
this.submitting = false;
}
}
key() {
if (this.integration?.id) {
return this.integration.id;
}
const uuid =
globalThis.crypto?.randomUUID?.() ??
`${Date.now()}-${Math.random().toString(36).slice(2)}`;
return `custom-${uuid}`;
}
<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 any website to your dashboard. Its icon is fetched automatically,
and you can upload your own if you prefer.
</p>
</div>
<form {{on "submit" this.handleSubmit}} class="mt-5">
<div class="space-y-5">
<div>
<label
for="custom-link-url"
class="block text-sm font-medium leading-6 text-gray-900"
>
Website URL
</label>
<div class="mt-2">
<input
type="text"
inputmode="url"
id="custom-link-url"
name="url"
value={{this.url}}
{{on "input" this.updateUrl}}
{{on "blur" this.previewFromUrl}}
placeholder="example.com"
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 full address; if you leave out the scheme, https is assumed.
</p>
</div>
<div>
<label
for="custom-link-name"
class="block text-sm font-medium leading-6 text-gray-900"
>
Name
</label>
<div class="mt-2">
<input
type="text"
id="custom-link-name"
name="name"
value={{this.name}}
{{on "input" this.updateName}}
placeholder={{labelForUrl this.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">
Optional. Defaults to the site's hostname.
</p>
</div>
<div class="flex items-center gap-4">
<div class="shrink-0">
{{#if this.hasPreview}}
<span
data-test-preview
class="icon-edge flex size-12 items-center justify-center overflow-hidden rounded-xl bg-gray-100"
>
<img
src={{this.preview.dataUrl}}
alt=""
aria-hidden="true"
data-test-preview-image
class={{this.imageClass}}
{{on "error" this.handleImageError}}
/>
</span>
{{else}}
<span
class="flex size-12 items-center justify-center overflow-hidden rounded-xl bg-indigo-500 text-sm font-semibold text-white"
data-test-preview
>{{this.initial}}</span>
{{/if}}
</div>
<div class="flex flex-wrap items-center gap-2">
<label
class="cursor-pointer 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"
>
{{#if this.customIcon}}Replace icon{{else}}Upload icon{{/if}}
<input
type="file"
accept="image/*"
class="sr-only"
data-test-icon-upload
{{on "change" this.uploadIcon}}
/>
</label>
{{#if this.customIcon}}
<button
type="button"
data-test-use-site-icon
{{on "click" this.useSiteIcon}}
class="rounded-md px-3 py-2 text-sm font-semibold text-gray-600 hover:text-gray-900"
>
Use site's icon
</button>
{{/if}}
{{#if this.previewLoading}}
<span class="text-sm text-gray-500">Fetching icon…</span>
{{/if}}
</div>
</div>
{{#if this.error}}
<p class="text-sm text-red-600" data-test-error>{{this.error}}</p>
{{/if}}
</div>
<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 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:opacity-50"
>
{{this.submitLabel}}
</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>
</div>
</template>
}