-
Notifications
You must be signed in to change notification settings - Fork 49
feat(csm-portal-microapp): resolve inline .iix images in comments and update descriptions #1374
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
Rashmika998
merged 2 commits into
wso2-open-operations:main
from
2003dinijay:feat/resolve-inline-comment-images
Aug 5, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| // Ported from the webapp's identical utility (features/csm-cases/utils/inlineImages.ts) — both | ||
| // apps talk to the same csm-portal backend, which embeds inline comment/description images the | ||
| // same way regardless of client. | ||
|
|
||
| // Shared "img tag with quoted/bare src" grammar for both extraction and | ||
| // replacement, so the two stay in sync as the pattern evolves. Capture groups: | ||
| // 1=before-src attrs, 2=double-quoted src, 3=single-quoted src, 4=bare src, 5=after-src attrs. | ||
| const IMG_TAG_SRC = /<img([^>]*?)\s+src\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))([^>]*)>/gi; | ||
|
|
||
| /** | ||
| * Extracts the backing attachment id from an inline `<img>` `src` value. The | ||
| * backing data source embeds inline images as `.iix`-suffixed references | ||
| * (e.g. `.../<attachmentId>.iix`); this pulls the id out regardless of | ||
| * whether it appears as a full path or a bare token. | ||
| */ | ||
| export function extractInlineImageRefId(src: string): string { | ||
| const s = src.trim(); | ||
| const fromPath = s.match(/\/([a-f0-9]{32})\.iix(?:\?|#|$)/i); | ||
| if (fromPath) return fromPath[1]; | ||
| const tail = | ||
| s | ||
| .replace(/\.iix$/i, "") | ||
| .split("/") | ||
| .pop() | ||
| ?.trim() ?? ""; | ||
| if (/^[a-f0-9]{32}$/i.test(tail)) return tail; | ||
| return s | ||
| .replace(/^\//, "") | ||
| .replace(/\.iix$/i, "") | ||
| .trim(); | ||
| } | ||
|
|
||
| /** | ||
| * Formats a 32-character ServiceNow sysid (no hyphens) as a canonical UUID | ||
| * (`8-4-4-4-12`) — the shape the backend's `/attachments/{id}/content` | ||
| * endpoint requires. Ids already in another shape are returned unchanged. | ||
| */ | ||
| export function sysidToUuid(id: string): string { | ||
| if (!/^[a-f0-9]{32}$/i.test(id)) return id; | ||
| return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20, 32)}`; | ||
| } | ||
|
|
||
| /** Extracts every attachment id referenced by a `.iix` `<img>` src within an HTML string. */ | ||
| export function extractIixAttachmentIds(html: string): string[] { | ||
| const ids: string[] = []; | ||
| let match; | ||
| IMG_TAG_SRC.lastIndex = 0; | ||
| while ((match = IMG_TAG_SRC.exec(html)) !== null) { | ||
| const src = match[2] ?? match[3] ?? match[4] ?? ""; | ||
| if (src.includes(".iix")) { | ||
| const id = extractInlineImageRefId(src); | ||
| if (id && !ids.includes(id)) ids.push(id); | ||
| } | ||
| } | ||
| return ids; | ||
| } | ||
|
|
||
| /** | ||
| * Replaces every `.iix` `<img>` src in `html` with its resolved data URL from | ||
| * `dataUrls`. A `.iix` reference with no matching entry is stripped (rather | ||
| * than left pointing at an auth-gated URL the browser cannot fetch). | ||
| */ | ||
| export function replaceInlineImageSrcs(html: string, dataUrls: Map<string, string>): string { | ||
| return html.replace(IMG_TAG_SRC, (fullMatch, before, doubleSrc, singleSrc, bareSrc, after) => { | ||
| const src = (doubleSrc ?? singleSrc ?? bareSrc ?? "") as string; | ||
| if (!src.includes(".iix")) return fullMatch; | ||
| const refId = extractInlineImageRefId(src); | ||
| const dataUrl = dataUrls.get(refId); | ||
| const quote = doubleSrc !== undefined ? '"' : singleSrc !== undefined ? "'" : '"'; | ||
| if (!dataUrl) { | ||
| return `<img${before} src=${quote}${quote} data-unresolved="true"${after}>`; | ||
| } | ||
| return `<img${before} src=${quote}${dataUrl}${quote}${after}>`; | ||
| }); | ||
| } |
99 changes: 99 additions & 0 deletions
99
apps/csm-portal/microapp/src/utils/useResolvedInlineImageHtml.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright (c) 2026 WSO2 LLC. (https://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import { useMemo } from "react"; | ||
| import { useQueries } from "@tanstack/react-query"; | ||
| import { attachments } from "@src/services/attachments"; | ||
| import { extractIixAttachmentIds, replaceInlineImageSrcs, sysidToUuid } from "./inlineImages"; | ||
|
|
||
| const SAFE_IMAGE_SUBTYPES = /^(png|jpeg|jpg|gif|webp|svg\+xml|bmp|avif)$/i; | ||
|
|
||
| /** Returns the normalized image MIME type, or `null` if `raw` isn't an allowed image subtype. */ | ||
| function toSafeMimeType(raw: string): string | null { | ||
| const lower = raw.trim().toLowerCase(); | ||
| const fullMatch = lower.match(/^image\/(.+)$/); | ||
| if (fullMatch && SAFE_IMAGE_SUBTYPES.test(fullMatch[1])) return lower; | ||
| if (SAFE_IMAGE_SUBTYPES.test(lower)) return `image/${lower}`; | ||
| return null; | ||
| } | ||
|
|
||
| function blobToDataUrl(blob: Blob): Promise<string | null> { | ||
| return new Promise((resolve) => { | ||
| const reader = new FileReader(); | ||
| reader.onloadend = () => { | ||
| const result = reader.result; | ||
| resolve(typeof result === "string" ? result : null); | ||
| }; | ||
| reader.onerror = () => resolve(null); | ||
| reader.readAsDataURL(blob); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Fetches inline-image attachments referenced within comment/description HTML via the | ||
| * authenticated `GET /attachments/{id}/content` endpoint and resolves them into `data:` URLs so | ||
| * `<img>` tags can render them without the WebView making an unauthenticated request. Ported from | ||
| * the webapp's identical hook (features/csm-cases/api/useResolvedInlineImageHtml.ts) — both apps | ||
| * read the same csm-portal backend and the same `.iix` inline-image convention. | ||
| * | ||
| * @param html - Sanitized HTML that may contain `.iix` `<img>` src references. | ||
| */ | ||
| export function useResolvedInlineImageHtml(html: string): { resolvedHtml: string; isLoading: boolean } { | ||
| const attachmentIds = useMemo(() => extractIixAttachmentIds(html), [html]); | ||
|
|
||
| const queries = useQueries({ | ||
| queries: attachmentIds.map((id) => ({ | ||
| queryKey: ["attachment", "inline-preview", id], | ||
| queryFn: async (): Promise<string | null> => { | ||
| // The extracted id is a bare 32-char sysid; the content endpoint requires the canonical | ||
| // UUID shape (hyphens re-inserted), same as every other attachment id sent to this | ||
| // backend. | ||
| const blob = await attachments.getContentById(sysidToUuid(id)); | ||
| const mimeType = toSafeMimeType(blob.type); | ||
| if (!mimeType) return null; | ||
| return blobToDataUrl(blob); | ||
| }, | ||
| enabled: !!id, | ||
| // Attachment content is immutable once uploaded, so cache it indefinitely rather than | ||
| // refetching on every remount. | ||
| staleTime: Infinity, | ||
| retry: 1, | ||
| })), | ||
| }); | ||
|
|
||
| const isLoading = queries.some((q) => q.isLoading); | ||
|
|
||
| const dataUrls = new Map<string, string>(); | ||
| attachmentIds.forEach((id, i) => { | ||
| const result = queries[i]?.data; | ||
| if (result) dataUrls.set(id, result); | ||
| }); | ||
|
|
||
| // A fixed-length key derived from the resolved data URLs: useMemo's dependency array must stay | ||
| // the same length across renders, which `queries.map((q) => q.data)` cannot guarantee as | ||
| // attachmentIds changes. | ||
| const dataUrlsKey = Array.from(dataUrls.entries()) | ||
| .map(([id, url]) => `${id}:${url}`) | ||
| .join(","); | ||
|
|
||
| const resolvedHtml = useMemo( | ||
| () => replaceInlineImageSrcs(html, dataUrls), | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| [html, dataUrlsKey], | ||
| ); | ||
|
|
||
| return { resolvedHtml, isLoading: attachmentIds.length > 0 && isLoading }; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.