UNPKG

@roadiehq/backstage-plugin-jira

Version:
85 lines (82 loc) 2.72 kB
import { useCallback, useEffect } from 'react'; import convert from 'xml-js'; import { DateTime } from 'luxon'; import { v4 } from 'uuid'; import { useApi } from '@backstage/core-plugin-api'; import { useAsyncFn } from 'react-use'; import { handleError } from './utils.esm.js'; import { jiraApiRef } from '../api/index.esm.js'; const getPropertyValue = (entry, property) => entry[property]?._text; const getElapsedTime = (start) => DateTime.fromISO(start).toRelative(); const decodeHtml = (html) => { const txt = document.createElement("textarea"); txt.innerHTML = html; return txt.value; }; const useActivityStream = (size, projectKey, componentName, ticketIds, label, isBearerAuth) => { const api = useApi(jiraApiRef); const getActivityStream = useCallback(async () => { try { const response = await api.getActivityStream( size, projectKey, componentName, ticketIds, label, isBearerAuth ); const parsedData = JSON.parse( convert.xml2json(response, { compact: true, spaces: 2 }) ); const mappedData = parsedData.feed.entry.map( (entry) => { const id = `urn:uuid:${v4()}`; const time = getPropertyValue(entry, "updated"); const iconLink = entry.link?.find( (link) => link._attributes.rel === "http://streams.atlassian.com/syndication/icon" ); let icon; if (iconLink) { icon = { url: iconLink._attributes.href, title: iconLink._attributes.title }; } return { id, time: { elapsed: getElapsedTime(time), value: new Date(time).toLocaleTimeString("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric" }) }, title: decodeHtml(getPropertyValue(entry, "title")), icon, summary: decodeHtml(getPropertyValue(entry, "summary") || ""), content: decodeHtml(getPropertyValue(entry, "content") || "") }; } ); return mappedData; } catch (err) { return handleError(err); } }, [api, size, projectKey, componentName, ticketIds, label, isBearerAuth]); const [state, fetchActivityStream] = useAsyncFn( () => getActivityStream(), [size] ); useEffect(() => { fetchActivityStream(); }, [size, fetchActivityStream]); return { activitiesLoading: state.loading, activities: state.value, activitiesError: state.error }; }; export { useActivityStream }; //# sourceMappingURL=useActivityStream.esm.js.map