From d45a0d2fa2e35f3bb26be29d0915502a1625ab15 Mon Sep 17 00:00:00 2001 From: Amoghavarsha Kudaligi Date: Thu, 12 Dec 2024 13:46:30 +0530 Subject: [PATCH 01/47] Add callbacks to call for fetching data from backend. --- .../components/webStories/contentPanel.tsx | 98 ++++++++++++++++++- 1 file changed, 94 insertions(+), 4 deletions(-) diff --git a/packages/extension/src/view/devtools/components/webStories/contentPanel.tsx b/packages/extension/src/view/devtools/components/webStories/contentPanel.tsx index c17507d93..c6da0c962 100644 --- a/packages/extension/src/view/devtools/components/webStories/contentPanel.tsx +++ b/packages/extension/src/view/devtools/components/webStories/contentPanel.tsx @@ -16,15 +16,104 @@ /** * External dependencies. */ -import React from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; /** * Internal dependencies. */ -import { getStoryMarkup } from './createStoryIframe'; -import { STORY_JSON } from './story'; +import { getStoryMarkup, type SingleStoryJSON } from './createStoryIframe'; const WebStories = () => { - const storyMarkup = getStoryMarkup(STORY_JSON); + const [storyMarkup, setStoryMarkup] = useState(''); + + const getAuthorsAndPublisherLogo = useCallback( + async (mediaAuthorSet: Record) => { + const response = await fetch( + 'https://privacysandbox-stories.com/wp-json/web-stories/v1/users' + ); + const responseJSON = await response.json(); + const transformedMediaAuthorMap: Record< + number, + Record + > = {}; + + const authorNameIdMap: Record = {}; + + responseJSON.forEach((singleResponse: Record) => { + authorNameIdMap[singleResponse.id] = singleResponse.name; + }); + + await Promise.all( + Object.keys(mediaAuthorSet).map(async (key: string) => { + const mediaResponse = await fetch( + 'https://privacysandbox-stories.com/wp-json/web-stories/v1/media/' + + mediaAuthorSet[Number(key)] + ); + + //Check the media response and get the avif/webp image if available else use source_url. + const mediaResponseJSON = await mediaResponse.json(); + const sourceUrl = mediaResponseJSON.source_url; + const splittedUrl = sourceUrl.split('/'); + const urlWithoutName = sourceUrl.substring( + 0, + sourceUrl.length - splittedUrl[splittedUrl.length - 1].length + ); + + const avifResource = + urlWithoutName + + mediaResponseJSON?.media_details?.sources?.['image/avif']?.file; + const webpResource = + urlWithoutName + + mediaResponseJSON?.media_details?.sources?.['image/webp']?.file; + + transformedMediaAuthorMap[Number(key)] = { + name: authorNameIdMap[Number(key)], + publisherLogo: avifResource ?? webpResource ?? sourceUrl, + }; + }) + ); + return transformedMediaAuthorMap; + }, + [] + ); + + useEffect(() => { + (async () => { + const response = await fetch( + 'https://privacysandbox-stories.com/wp-json/web-stories/v1/web-story/' + ); + + const responseJSON = await response.json(); + let storyJSON: SingleStoryJSON[] = []; + const mediaAuthorSet: Record = {}; + responseJSON.forEach((singleResponse: any) => { + if (singleResponse?.status === 'publish') { + mediaAuthorSet[singleResponse.author] = + singleResponse?.meta?.web_stories_publisher_logo; + + storyJSON.push({ + heroImage: singleResponse?.story_poster?.url ?? '', + publisherLogo: singleResponse?.meta?.web_stories_publisher_logo, + publisherName: singleResponse?.author, + storyTitle: singleResponse?.title?.rendered, + storyUrl: `${singleResponse?.link}#embedMode=2`, + }); + } + }); + + const authorsAndPublisherLogoMap = await getAuthorsAndPublisherLogo( + mediaAuthorSet + ); + + storyJSON = storyJSON.map((story) => { + const key = Number(story.publisherName); + story.publisherName = authorsAndPublisherLogoMap[key]?.name; + story.publisherLogo = authorsAndPublisherLogoMap[key]?.publisherLogo; + return story; + }); + + setStoryMarkup(getStoryMarkup(storyJSON)); + })(); + }, [getAuthorsAndPublisherLogo]); return (
{ className="h-full w-full text-raisin-black dark:text-bright-gray overflow-y-auto" >