From e87b203c527b81545808e66c2ca94e14b9077011 Mon Sep 17 00:00:00 2001 From: Talha Saeed Date: Wed, 11 Oct 2023 17:14:16 +0500 Subject: [PATCH 1/3] Thumbnails bugfix --- src/common/core/caches/entries-cache.tsx | 7 +++++++ src/common/pages/submit/api/update.ts | 3 ++- src/common/pages/submit/functions/build-metadata.ts | 8 +++++++- src/common/pages/submit/index.tsx | 12 +++++++++++- src/common/store/entries/types.ts | 1 + 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/common/core/caches/entries-cache.tsx b/src/common/core/caches/entries-cache.tsx index 351d31f6644..a5482e44d84 100644 --- a/src/common/core/caches/entries-cache.tsx +++ b/src/common/core/caches/entries-cache.tsx @@ -161,9 +161,12 @@ export function useEntryCache( async () => { let entry = getByLink(queryKey) as T; + console.log("Entry by link", entry); + if (!entry) { entry = getExistingEntryFromStore() as T; if (entry) { + console.log("Entry from store", entry); updateCache([entry]); } } @@ -173,6 +176,7 @@ export function useEntryCache( // update cache value to getting from there next time if (response) { + console.log("entry from api", response); updateCache([response]); } return response; @@ -188,10 +192,13 @@ export function useEntryCache( ); useEffect(() => { + console.log("Query returned that entry", query.data); if (query.data) { if (getExistingEntryFromStore()) { updateEntry(query.data); + console.log("Update entry in store", query.data); } else { + console.log("Add entry in store", query.data); addEntry(query.data); } } diff --git a/src/common/pages/submit/api/update.ts b/src/common/pages/submit/api/update.ts index 41bd71db406..9b92e764399 100644 --- a/src/common/pages/submit/api/update.ts +++ b/src/common/pages/submit/api/update.ts @@ -60,7 +60,8 @@ export function useUpdateApi(history: History, onClear: () => void) { tags, description, selectionTouched, - selectedThumbnail + selectedThumbnail, + images: json_metadata.image }), { tags }, { description } diff --git a/src/common/pages/submit/functions/build-metadata.ts b/src/common/pages/submit/functions/build-metadata.ts index b4245ea9acf..391e0e0c924 100644 --- a/src/common/pages/submit/functions/build-metadata.ts +++ b/src/common/pages/submit/functions/build-metadata.ts @@ -11,7 +11,8 @@ export function buildMetadata({ description, selectedThumbnail, selectionTouched, - videoMetadata + videoMetadata, + images }: { tags: string[]; title: string; @@ -20,10 +21,15 @@ export function buildMetadata({ selectedThumbnail?: string; selectionTouched: boolean; videoMetadata?: ThreeSpeakVideo; + images?: string[]; }) { const { thumbnails, ...meta } = extractMetaData(body); let localThumbnail = ls.get("draft_selected_image"); + if (images?.length) { + meta.image = [...images, ...(meta.image || [])]; + } + if (meta.image) { if (selectionTouched && selectedThumbnail) { meta.image = [selectedThumbnail, ...meta.image!.splice(0, 9)]; diff --git a/src/common/pages/submit/index.tsx b/src/common/pages/submit/index.tsx index 63852aa5a5a..cc8a0a0081d 100644 --- a/src/common/pages/submit/index.tsx +++ b/src/common/pages/submit/index.tsx @@ -59,6 +59,7 @@ import { useUpdateApi } from "./api/update"; import "./_index.scss"; import { SubmitVideoAttachments } from "./submit-video-attachments"; import { useThreeSpeakMigrationAdapter } from "./hooks/three-speak-migration-adapter"; +import { useEntryCache } from "../../core/caches/entries-cache"; interface MatchProps { match: MatchType; @@ -133,12 +134,19 @@ export function Submit(props: PageProps & MatchProps) { setTags([...tags, community]); }); + const { data } = useEntryCache( + "", + props.match.params.username?.replace("@", ""), + props.match.params.permlink + ); + useEntryDetector(props.match, props.history, (entry) => { if (entry) { setTitle(entry.title); setTags([...new Set(entry.json_metadata?.tags ?? [])]); setBody(entry.body); setDescription(entry.json_metadata?.description ?? postBodySummary(body, 200)); + data?.json_metadata?.image && setSelectedThumbnail(data?.json_metadata?.image[0]); setEditingEntry(entry); threeSpeakManager.setIsEditing(true); } else if (editingEntry) { @@ -244,7 +252,9 @@ export function Submit(props: PageProps & MatchProps) { _updateTimer = setTimeout(() => { const { thumbnails } = extractMetaData(body); setPreview({ title, tags, body, description }); - setThumbnails(thumbnails ?? []); + const existingImages = data.json_metadata.image ?? []; + const newThumbnails = thumbnails ? [...existingImages, ...thumbnails] : existingImages; + setThumbnails([...new Set(newThumbnails)]); if (editingEntry === null) { setLocalDraft({ title, tags, body, description }); } diff --git a/src/common/store/entries/types.ts b/src/common/store/entries/types.ts index 017a0e997ae..13dc32a814c 100644 --- a/src/common/store/entries/types.ts +++ b/src/common/store/entries/types.ts @@ -27,6 +27,7 @@ export interface JsonMetadata { format?: string; original_author?: string; original_permlink?: string; + image?: string[]; } export interface Entry { From 9088879e7b9b2c971493af8f83bc13c655dcb3a8 Mon Sep 17 00:00:00 2001 From: Talha Saeed Date: Wed, 11 Oct 2023 17:22:07 +0500 Subject: [PATCH 2/3] Removed consoles --- src/common/core/caches/entries-cache.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/common/core/caches/entries-cache.tsx b/src/common/core/caches/entries-cache.tsx index a5482e44d84..351d31f6644 100644 --- a/src/common/core/caches/entries-cache.tsx +++ b/src/common/core/caches/entries-cache.tsx @@ -161,12 +161,9 @@ export function useEntryCache( async () => { let entry = getByLink(queryKey) as T; - console.log("Entry by link", entry); - if (!entry) { entry = getExistingEntryFromStore() as T; if (entry) { - console.log("Entry from store", entry); updateCache([entry]); } } @@ -176,7 +173,6 @@ export function useEntryCache( // update cache value to getting from there next time if (response) { - console.log("entry from api", response); updateCache([response]); } return response; @@ -192,13 +188,10 @@ export function useEntryCache( ); useEffect(() => { - console.log("Query returned that entry", query.data); if (query.data) { if (getExistingEntryFromStore()) { updateEntry(query.data); - console.log("Update entry in store", query.data); } else { - console.log("Add entry in store", query.data); addEntry(query.data); } } From 1a98436a51aafb9d5ed85f391d82247923aebbc7 Mon Sep 17 00:00:00 2001 From: Talha Saeed Date: Tue, 17 Oct 2023 11:22:51 +0500 Subject: [PATCH 3/3] Use editing entry instead of entry from cache --- src/common/pages/submit/index.tsx | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/common/pages/submit/index.tsx b/src/common/pages/submit/index.tsx index cc8a0a0081d..6f79a68a489 100644 --- a/src/common/pages/submit/index.tsx +++ b/src/common/pages/submit/index.tsx @@ -59,7 +59,6 @@ import { useUpdateApi } from "./api/update"; import "./_index.scss"; import { SubmitVideoAttachments } from "./submit-video-attachments"; import { useThreeSpeakMigrationAdapter } from "./hooks/three-speak-migration-adapter"; -import { useEntryCache } from "../../core/caches/entries-cache"; interface MatchProps { match: MatchType; @@ -134,19 +133,13 @@ export function Submit(props: PageProps & MatchProps) { setTags([...tags, community]); }); - const { data } = useEntryCache( - "", - props.match.params.username?.replace("@", ""), - props.match.params.permlink - ); - useEntryDetector(props.match, props.history, (entry) => { if (entry) { setTitle(entry.title); setTags([...new Set(entry.json_metadata?.tags ?? [])]); setBody(entry.body); setDescription(entry.json_metadata?.description ?? postBodySummary(body, 200)); - data?.json_metadata?.image && setSelectedThumbnail(data?.json_metadata?.image[0]); + entry?.json_metadata?.image && setSelectedThumbnail(entry?.json_metadata?.image[0]); setEditingEntry(entry); threeSpeakManager.setIsEditing(true); } else if (editingEntry) { @@ -252,7 +245,7 @@ export function Submit(props: PageProps & MatchProps) { _updateTimer = setTimeout(() => { const { thumbnails } = extractMetaData(body); setPreview({ title, tags, body, description }); - const existingImages = data.json_metadata.image ?? []; + const existingImages = editingEntry?.json_metadata.image ?? []; const newThumbnails = thumbnails ? [...existingImages, ...thumbnails] : existingImages; setThumbnails([...new Set(newThumbnails)]); if (editingEntry === null) {