@@ -245,53 +245,43 @@ export const LiveBlogEpic = ({
245245
246246 const [ pageUrl , setPageUrl ] = useState < string | undefined > ( ) ;
247247
248- const [ epicPlaceholder , setEpicPlaceholder ] = useState < HTMLElement | null > (
249- null ,
250- ) ;
251-
252248 useEffect ( ( ) => {
253249 setPageUrl ( window . location . origin + window . location . pathname ) ;
254250 } , [ ] ) ;
255251
256- useEffect ( ( ) => {
257- /**
258- * Here we decide where to insert the epic.
259- *
260- * If the url contains a permalink then we
261- * want to insert it immediately after that block to prevent any CLS issues.
262- *
263- * Otherwise, we choose a random position near the top of the blog
264- */
265- const placeholder = document . createElement ( 'article' ) ;
266- if ( window . location . href . includes ( '#block-' ) ) {
267- // Because we're using a permalink there's a possibility the epic will render in
268- // view. To prevent confusing layout shift we initially hide the message so that
269- // we can reveal (animate in) it once it has been rendered
270- placeholder . classList . add ( 'pending' ) ;
271- const blockId = window . location . hash . slice ( 1 ) ;
272- const blockLinkedTo = document . getElementById ( blockId ) ;
273- if ( blockLinkedTo ) {
274- insertAfter ( blockLinkedTo , placeholder ) ;
275- }
276- placeholder . classList . add ( 'reveal' ) ;
277- placeholder . classList . remove ( 'pending' ) ;
278- } else {
279- // This is a simple page load so we simply insert the epic somewhere near the top
280- // of the blog
281- const randomPosition = Math . floor ( Math . random ( ) * 3 ) + 1 ; // 1, 2 or 3
282- const aBlockNearTheTop =
283- document . querySelectorAll < HTMLElement > ( 'article.block' ) [
284- randomPosition
285- ] ;
286- if ( aBlockNearTheTop ) {
287- insertAfter ( aBlockNearTheTop , placeholder ) ;
288- }
252+ /**
253+ * Here we decide where to insert the epic.
254+ *
255+ * If the url contains a permalink then we
256+ * want to insert it immediately after that block to prevent any CLS issues.
257+ *
258+ * Otherwise, we choose a random position near the top of the blog
259+ */
260+ const epicPlaceholder = document . createElement ( 'article' ) ;
261+ if ( window . location . href . includes ( '#block-' ) ) {
262+ // Because we're using a permalink there's a possibility the epic will render in
263+ // view. To prevent confusing layout shift we initially hide the message so that
264+ // we can reveal (animate in) it once it has been rendered
265+ epicPlaceholder . classList . add ( 'pending' ) ;
266+ const blockId = window . location . hash . slice ( 1 ) ;
267+ const blockLinkedTo = document . getElementById ( blockId ) ;
268+ if ( blockLinkedTo ) {
269+ insertAfter ( blockLinkedTo , epicPlaceholder ) ;
289270 }
290- setEpicPlaceholder ( placeholder ) ;
291- return ( ) => {
292- placeholder . remove ( ) ;
293- } ;
294- } , [ ] ) ;
271+ epicPlaceholder . classList . add ( 'reveal' ) ;
272+ epicPlaceholder . classList . remove ( 'pending' ) ;
273+ } else {
274+ // This is a simple page load so we simply insert the epic somewhere near the top
275+ // of the blog
276+ const randomPosition = Math . floor ( Math . random ( ) * 3 ) + 1 ; // 1, 2 or 3
277+ const aBlockNearTheTop =
278+ document . querySelectorAll < HTMLElement > ( 'article.block' ) [
279+ randomPosition
280+ ] ;
281+ if ( aBlockNearTheTop ) {
282+ insertAfter ( aBlockNearTheTop , epicPlaceholder ) ;
283+ }
284+ }
295285
296286 // First construct the payload
297287 const payload = usePayload ( {
@@ -302,7 +292,7 @@ export const LiveBlogEpic = ({
302292 pageId,
303293 keywordIds,
304294 } ) ;
305- if ( ! ophanPageViewId || ! payload || ! pageUrl || ! epicPlaceholder ) {
295+ if ( ! ophanPageViewId || ! payload || ! pageUrl ) {
306296 return null ;
307297 }
308298
0 commit comments