Skip to content

Commit

Permalink
feat: hub page entities and search results now have links.workspaceRe…
Browse files Browse the repository at this point in the history
…lative and links.layoutRelative

affects: @esri/hub-common
  • Loading branch information
mjuniper committed Nov 19, 2024
1 parent 1915be1 commit af50353
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
18 changes: 4 additions & 14 deletions packages/common/src/pages/HubPages.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { getFamily } from "../content/get-family";
import {
deriveLocationFromItem,
getHubRelativeUrl,
} from "../content/_internal/internalContentUtils";
import { deriveLocationFromItem } from "../content/_internal/internalContentUtils";
import { fetchItemEnrichments } from "../items/_enrichments";
import { getProp } from "../objects";
import { getItemThumbnailUrl } from "../resources";
import { IHubSearchResult } from "../search";
import { parseInclude } from "../search/_internal/parseInclude";
import { IHubRequestOptions, IModel } from "../types";
import { getItemHomeUrl } from "../urls";
import { IRequestOptions } from "@esri/arcgis-rest-request";
import { getItem, IItem } from "@esri/arcgis-rest-portal";
import { cloneObject, unique } from "../util";
Expand All @@ -29,6 +24,7 @@ import { IUserRequestOptions } from "@esri/arcgis-rest-auth";
import { IUserItemOptions, removeItem } from "@esri/arcgis-rest-portal";
import { DEFAULT_PAGE, DEFAULT_PAGE_MODEL } from "./defaults";
import { ensureUniqueEntitySlug } from "../items/_internal/ensureUniqueEntitySlug";
import { computeLinks } from "./_internal/computeLinks";

/**
* @private
Expand Down Expand Up @@ -232,14 +228,8 @@ export async function enrichPageSearchResult(
result[spec.prop] = getProp(enriched, spec.path);
});

// Handle Links
result.links.thumbnail = getItemThumbnailUrl(item, requestOptions);
result.links.self = getItemHomeUrl(result.id, requestOptions);
result.links.siteRelative = getHubRelativeUrl(
result.type,
result.id,
item.typeKeywords
);
// Handle links
result.links = computeLinks(item, requestOptions);

return result;
}
36 changes: 36 additions & 0 deletions packages/common/src/pages/_internal/computeLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { IItem } from "@esri/arcgis-rest-types";
import { IRequestOptions } from "@esri/arcgis-rest-request";
import { UserSession } from "@esri/arcgis-rest-auth";
import { IHubEntityLinks } from "../../core/types";
import { getItemIdentifier } from "../../items";
import { getRelativeWorkspaceUrl } from "../../core/getRelativeWorkspaceUrl";
import { getItemThumbnailUrl } from "../../resources/get-item-thumbnail-url";
import { getHubRelativeUrl } from "../../content/_internal/internalContentUtils";
import { getItemHomeUrl } from "../../urls";

/**
* Compute the links that get appended to a Hub Site
* search result and entity
*
* @param item
* @param requestOptions
*/
export function computeLinks(
item: IItem,
requestOptions: IRequestOptions
): IHubEntityLinks {
let token: string;
if (requestOptions.authentication) {
const session: UserSession = requestOptions.authentication as UserSession;
token = session.token;
}

return {
self: getItemHomeUrl(item.id, requestOptions),
siteRelative: getHubRelativeUrl(item.type, item.id, item.typeKeywords),
siteRelativeEntityType: getHubRelativeUrl("page"),
layoutRelative: `/pages/${item.id}/edit`,
workspaceRelative: getRelativeWorkspaceUrl(item.type, item.id),
thumbnail: getItemThumbnailUrl(item, requestOptions, token),
};
}
10 changes: 2 additions & 8 deletions packages/common/src/pages/_internal/computeProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IHubPage } from "../../core/types/IHubPage";
import { getRelativeWorkspaceUrl } from "../../core/getRelativeWorkspaceUrl";
import { computeItemProps } from "../../core/_internal/computeItemProps";
import { getHubRelativeUrl } from "../../content/_internal/internalContentUtils";
import { computeLinks } from "./computeLinks";

/**
* Given a model and a page, set various computed properties that can't be directly mapped
Expand All @@ -35,14 +36,7 @@ export function computeProps(
const thumbnailUrl = getItemThumbnailUrl(model.item, requestOptions, token);
// TODO: Remove this once opendata-ui starts using `links.thumbnail` instead
page.thumbnailUrl = thumbnailUrl;
page.links = {
self: getItemHomeUrl(page.id, requestOptions),
siteRelative: `/pages/${page.id}`,
siteRelativeEntityType: getHubRelativeUrl("page"),
workspaceRelative: getRelativeWorkspaceUrl("page", page.id),
layoutRelative: `/pages/${page.id}/edit`,
thumbnail: thumbnailUrl,
};
page.links = computeLinks(model.item, requestOptions);

/**
* Features that can be disabled by the entity owner
Expand Down
5 changes: 1 addition & 4 deletions packages/common/src/sites/_internal/computeLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export function computeLinks(
siteRelative: getHubRelativeUrl(item.type, item.id, item.typeKeywords),
siteRelativeEntityType: getHubRelativeUrl(item.type),
layoutRelative: "/edit",
workspaceRelative: getRelativeWorkspaceUrl(
item.type,
getItemIdentifier(item)
),
workspaceRelative: getRelativeWorkspaceUrl(item.type, item.id),
thumbnail: getItemThumbnailUrl(item, requestOptions, token),
};
}
1 change: 1 addition & 0 deletions packages/common/test/pages/_internal/computeProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("Pages: computeProps:", () => {
item: {
created: new Date().getTime(),
modified: new Date().getTime(),
type: "Hub Page",
},
data: {},
} as IModel;
Expand Down

0 comments on commit af50353

Please sign in to comment.