Skip to content

feat: UX improvements to collections with single URL #2325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions frontend/src/features/collections/collection-replay-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { SelectSnapshotDetail } from "./select-collection-start-page";

import { BtrixElement } from "@/classes/BtrixElement";
import type { Dialog } from "@/components/ui/dialog";
import type { Collection } from "@/types/collection";

/**
* @fires btrix-change
Expand All @@ -40,14 +41,8 @@ export class CollectionStartPageDialog extends BtrixElement {
@property({ type: String })
collectionId?: string;

@property({ type: String })
homeUrl?: string | null = null;

@property({ type: String })
homePageId?: string | null = null;

@property({ type: String })
homeTs?: string | null = null;
@property({ type: Object })
collection?: Collection;

@property({ type: Boolean })
open = false;
Expand Down Expand Up @@ -77,8 +72,8 @@ export class CollectionStartPageDialog extends BtrixElement {
private readonly thumbnailPreview?: CollectionSnapshotPreview | null;

willUpdate(changedProperties: PropertyValues<this>) {
if (changedProperties.has("homeUrl")) {
this.homeView = this.homeUrl ? HomeView.URL : HomeView.Pages;
if (changedProperties.has("collection") && this.collection) {
this.homeView = this.collection.homeUrl ? HomeView.URL : HomeView.Pages;
}
}

Expand All @@ -92,7 +87,12 @@ export class CollectionStartPageDialog extends BtrixElement {
class="[--width:60rem]"
@sl-show=${() => (this.showContent = true)}
@sl-after-hide=${() => {
this.homeView = this.homeUrl ? HomeView.URL : HomeView.Pages;
if (this.collection) {
this.homeView = this.collection.homeUrl
? HomeView.URL
: HomeView.Pages;
}

this.isSubmitting = false;
this.selectedSnapshot = null;
this.showContent = false;
Expand Down Expand Up @@ -142,11 +142,11 @@ export class CollectionStartPageDialog extends BtrixElement {
private renderPreview() {
const snapshot =
this.selectedSnapshot ||
(this.homeUrl
(this.collection?.homeUrl
? {
url: this.homeUrl,
ts: this.homeTs,
pageId: this.homePageId,
url: this.collection.homeUrl,
ts: this.collection.homeUrlTs,
pageId: this.collection.homeUrlPageId,
status: 200,
}
: null);
Expand Down Expand Up @@ -207,8 +207,8 @@ export class CollectionStartPageDialog extends BtrixElement {

if (this.homeView === HomeView.Pages) {
if (
!this.homePageId ||
this.homePageId !== this.selectedSnapshot?.pageId
!this.collection?.homeUrlPageId ||
this.collection.homeUrlPageId !== this.selectedSnapshot?.pageId
) {
// Reset unsaved selected snapshot
this.selectedSnapshot = null;
Expand Down Expand Up @@ -244,8 +244,7 @@ export class CollectionStartPageDialog extends BtrixElement {
<section>
<btrix-select-collection-start-page
.collectionId=${this.collectionId}
.homeUrl=${this.homeUrl}
.homeTs=${this.homeTs}
.collection=${this.collection}
@btrix-select=${async (
e: CustomEvent<SelectSnapshotDetail>,
) => {
Expand Down Expand Up @@ -280,10 +279,10 @@ export class CollectionStartPageDialog extends BtrixElement {
const { homeView, useThumbnail } = serialize(form);

if (
(homeView === HomeView.Pages && !this.homePageId) ||
(homeView === HomeView.Pages && !this.collection?.homeUrlPageId) ||
(homeView === HomeView.URL &&
this.selectedSnapshot &&
this.homePageId === this.selectedSnapshot.pageId)
this.collection?.homeUrlPageId === this.selectedSnapshot.pageId)
) {
// No changes to save
this.open = false;
Expand All @@ -302,7 +301,7 @@ export class CollectionStartPageDialog extends BtrixElement {
homeView === HomeView.URL &&
useThumbnail === "on" &&
this.selectedSnapshot &&
this.homePageId !== this.selectedSnapshot.pageId;
this.collection?.homeUrlPageId !== this.selectedSnapshot.pageId;
// TODO get filename from rwp?
const fileName = `page-thumbnail_${this.selectedSnapshot?.pageId}.jpeg`;
let file: File | undefined;
Expand Down
129 changes: 70 additions & 59 deletions frontend/src/features/collections/select-collection-start-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import { html, type PropertyValues } from "lit";
import { customElement, property, query, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import debounce from "lodash/fp/debounce";
import sortBy from "lodash/fp/sortBy";
import filter from "lodash/fp/filter";
import flow from "lodash/fp/flow";
import orderBy from "lodash/fp/orderBy";
import queryString from "query-string";

import { BtrixElement } from "@/classes/BtrixElement";
import type { Combobox } from "@/components/ui/combobox";
import type { APIPaginatedList, APIPaginationQuery } from "@/types/api";
import type { Collection } from "@/types/collection";
import type { UnderlyingFunction } from "@/types/utils";
import { tw } from "@/utils/tailwind";

Expand All @@ -39,7 +42,11 @@ export type SelectSnapshotDetail = {

const DEFAULT_PROTOCOL = "http";

const sortByTs = sortBy<Snapshot>("ts");
// TODO Check if backend can sort and filter snapshots instead
const sortByTs = flow(
filter<Snapshot>(({ status }) => status < 300),
orderBy<Snapshot>("ts")("desc"),
) as (snapshots: Snapshot[]) => Snapshot[];

/**
* @fires btrix-select
Expand All @@ -50,11 +57,8 @@ export class SelectCollectionStartPage extends BtrixElement {
@property({ type: String })
collectionId?: string;

@property({ type: String })
homeUrl?: string | null = null;

@property({ type: String })
homeTs?: string | null = null;
@property({ type: Object })
collection?: Collection;

@state()
private searchQuery = "";
Expand Down Expand Up @@ -82,14 +86,13 @@ export class SelectCollectionStartPage extends BtrixElement {
return this.selectedSnapshot;
}

updated(changedProperties: PropertyValues<this>) {
if (changedProperties.has("homeUrl") && this.homeUrl) {
if (this.input) {
this.input.value = this.homeUrl;
}
this.searchQuery = this.homeUrl;
void this.initSelection();
protected willUpdate(changedProperties: PropertyValues<this>) {
if (changedProperties.has("collection") && this.collection) {
void this.initSelection(this.collection);
}
}

updated(changedProperties: PropertyValues<this>) {
if (changedProperties.has("selectedSnapshot")) {
this.dispatchEvent(
new CustomEvent<SelectSnapshotDetail>("btrix-select", {
Expand All @@ -106,34 +109,58 @@ export class SelectCollectionStartPage extends BtrixElement {
}
}

private async initSelection() {
await this.updateComplete;
await this.searchResults.taskComplete;
private async initSelection(collection: Collection) {
if (!collection.homeUrl && collection.pageCount !== 1) {
return;
}

if (this.homeUrl && this.searchResults.value) {
this.selectedPage = this.searchResults.value.items.find(
({ url }) => url === this.homeUrl,
);
const pageUrls = await this.getPageUrls({
id: collection.id,
urlPrefix: collection.homeUrl || "",
pageSize: 1,
});

if (this.selectedPage && this.homeTs) {
this.selectedSnapshot = this.selectedPage.snapshots.find(
({ ts }) => ts === this.homeTs,
);
}
if (!pageUrls.total) {
return;
}

const startPage = pageUrls.items[0];

if (this.input) {
this.input.value = startPage.url;
}

this.selectedPage = this.formatPage(startPage);

const homeTs = collection.homeUrlTs;

this.selectedSnapshot = homeTs
? this.selectedPage.snapshots.find(({ ts }) => ts === homeTs)
: this.selectedPage.snapshots[0];
}

/**
* Format page for display
* @TODO Check if backend can sort and filter snapshots instead
*/
private formatPage(page: Page) {
return {
...page,
snapshots: sortByTs(page.snapshots),
};
}

private readonly searchResults = new Task(this, {
task: async ([searchValue], { signal }) => {
const searchResults = await this.getPageUrls(
const pageUrls = await this.getPageUrls(
{
id: this.collectionId!,
urlPrefix: searchValue,
},
signal,
);

return searchResults;
return pageUrls;
},
args: () => [this.searchQuery] as const,
});
Expand All @@ -150,6 +177,7 @@ export class SelectCollectionStartPage extends BtrixElement {
value=${this.selectedSnapshot?.pageId || ""}
?required=${this.selectedPage && !this.selectedSnapshot}
?disabled=${!this.selectedPage}
hoist
@sl-change=${async (e: SlChangeEvent) => {
const { value } = e.currentTarget as SlSelect;

Expand All @@ -160,35 +188,22 @@ export class SelectCollectionStartPage extends BtrixElement {
);
}}
>
${when(
this.selectedSnapshot,
(snapshot) => html`
<btrix-badge
slot="suffix"
variant=${snapshot.status < 300 ? "success" : "danger"}
>${snapshot.status}</btrix-badge
>
`,
)}
${when(this.selectedPage, (item) =>
item.snapshots.map(
({ pageId, ts, status }) => html`
<sl-option value=${pageId}>
${this.localize.date(ts)}
<btrix-badge
slot="suffix"
variant=${status < 300 ? "success" : "danger"}
>${status}</btrix-badge
>
Comment on lines -178 to -182
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we display the badge (or maybe just an icon with a tooltip) for non-2xx status codes? While I like that we're getting rid of it, I do think it could still be useful to highlight pages that didn't load successfully

Copy link
Member Author

@SuaYoo SuaYoo Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're being filtered out here, do you mean keep them in and highlight as errors?

</sl-option>
`,
),
)}
${when(this.selectedPage, this.renderSnapshotOptions)}
</sl-select>
</div>
`;
}

private readonly renderSnapshotOptions = ({ snapshots }: Page) => {
return html`
${snapshots.map(
({ pageId, ts }) => html`
<sl-option value=${pageId}> ${this.localize.date(ts)} </sl-option>
`,
)}
`;
};

private renderPageSearch() {
let prefix: {
icon: string;
Expand Down Expand Up @@ -223,7 +238,7 @@ export class SelectCollectionStartPage extends BtrixElement {
id="pageUrlInput"
label=${msg("Page URL")}
placeholder=${msg("Start typing a URL...")}
clearable
?clearable=${this.collection && this.collection.pageCount > 1}
@sl-focus=${() => {
this.resetInputValidity();
this.combobox?.show();
Expand Down Expand Up @@ -295,7 +310,7 @@ export class SelectCollectionStartPage extends BtrixElement {
this.selectedSnapshot = undefined;
} else if (results.total === 1) {
// Choose only option, e.g. for copy-paste
this.selectedPage = this.searchResults.value.items[0];
this.selectedPage = this.formatPage(this.searchResults.value.items[0]);
this.selectedSnapshot = this.selectedPage.snapshots[0];
}
};
Expand Down Expand Up @@ -326,11 +341,7 @@ export class SelectCollectionStartPage extends BtrixElement {
this.input.value = item.url;
}

this.selectedPage = {
...item,
// TODO check if backend can sort
snapshots: sortByTs(item.snapshots).reverse(),
};
this.selectedPage = this.formatPage(item);

this.combobox?.hide();

Expand Down
4 changes: 1 addition & 3 deletions frontend/src/pages/org/collection-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,7 @@ export class CollectionDetail extends BtrixElement {
}}
@sl-hide=${async () => (this.openDialogName = undefined)}
collectionId=${this.collectionId}
.homeUrl=${this.collection?.homeUrl}
.homePageId=${this.collection?.homeUrlPageId}
.homeTs=${this.collection?.homeUrlTs}
.collection=${this.collection}
?replayLoaded=${this.isRwpLoaded}
></btrix-collection-replay-dialog>

Expand Down
Loading