44 collection ,
55 deleteDoc ,
66 doc ,
7- getDocs ,
87 increment ,
98 onSnapshot ,
109 orderBy ,
@@ -26,6 +25,8 @@ let currentViewerIsAdmin = false;
2625const PUBLIC_PROFILE_DIRECTORY_KEY = "noctive_public_profile_directory" ;
2726const POST_VOTE_STORAGE_PREFIX = "noctive_post_votes_" ;
2827const BIO_MAX_LENGTH = 180 ;
28+ const ACCESS_MODE_SESSION_KEY = "noctive_access_mode" ;
29+ const GUEST_PREVIEW_MODE = "guest" ;
2930
3031const ADMIN_CONFIG = {
3132 uids : [
@@ -40,6 +41,15 @@ const ADMIN_CONFIG = {
4041 ]
4142} ;
4243
44+ function isGuestPreviewSession ( ) {
45+ try {
46+ return window . sessionStorage . getItem ( ACCESS_MODE_SESSION_KEY ) === GUEST_PREVIEW_MODE ;
47+ } catch ( error ) {
48+ console . warn ( "Could not read guest preview session:" , error ) ;
49+ return false ;
50+ }
51+ }
52+
4353function normalizeAdminValue ( value ) {
4454 return String ( value || "" ) . trim ( ) . toLowerCase ( ) ;
4555}
@@ -442,13 +452,15 @@ function renderPosts(snapshot) {
442452
443453 if ( snapshot . empty ) {
444454 container . innerHTML = "<p>No posts yet.</p>" ;
455+ window . noctiveFeedBridge ?. renderGuestPreviewPosts ?. ( container ) ;
445456 return ;
446457 }
447458
448459 snapshot . forEach ( ( doc ) => {
449460 container . appendChild ( buildPostCard ( doc ) ) ;
450461 } ) ;
451462
463+ window . noctiveFeedBridge ?. renderGuestPreviewPosts ?. ( container ) ;
452464 window . noctiveFeedBridge ?. applyGeneratedAvatars ?. ( ) ;
453465}
454466
@@ -461,7 +473,7 @@ function renderPostsError(error) {
461473 container . innerHTML = "<p>Could not load posts.</p>" ;
462474}
463475
464- function subscribeToPosts ( postsSource ) {
476+ function subscribeToPosts ( postsSource , fallbackSource = null ) {
465477 if ( stopPostsSubscription ) {
466478 stopPostsSubscription ( ) ;
467479 }
@@ -472,12 +484,18 @@ function subscribeToPosts(postsSource) {
472484 renderPosts ( snapshot ) ;
473485 } ,
474486 ( error ) => {
487+ if ( fallbackSource && fallbackSource !== postsSource ) {
488+ console . warn ( "Falling back to alternate posts source:" , error ) ;
489+ subscribeToPosts ( fallbackSource , null ) ;
490+ return ;
491+ }
492+
475493 renderPostsError ( error ) ;
476494 }
477495 ) ;
478496}
479497
480- async function loadPosts ( ) {
498+ function loadPosts ( ) {
481499 const container = getFeedContainer ( ) ;
482500
483501 if ( ! container ) return ;
@@ -486,21 +504,8 @@ async function loadPosts() {
486504 container . innerHTML = "<p>Loading posts...</p>" ;
487505
488506 const postsCollection = collection ( db , "Posts" ) ;
489-
490- try {
491- const orderedPostsQuery = query ( postsCollection , orderBy ( "createdAt" , "desc" ) ) ;
492- await getDocs ( orderedPostsQuery ) ;
493- subscribeToPosts ( orderedPostsQuery ) ;
494- } catch ( orderError ) {
495- console . warn ( "Falling back to unordered posts:" , orderError ) ;
496-
497- try {
498- await getDocs ( postsCollection ) ;
499- subscribeToPosts ( postsCollection ) ;
500- } catch ( error ) {
501- renderPostsError ( error ) ;
502- }
503- }
507+ const orderedPostsQuery = query ( postsCollection , orderBy ( "createdAt" , "desc" ) ) ;
508+ subscribeToPosts ( orderedPostsQuery , postsCollection ) ;
504509}
505510
506511async function createPost ( text ) {
@@ -512,8 +517,8 @@ async function createPost(text) {
512517
513518 const uid = window . noctiveViewerKey ;
514519
515- if ( ! uid || uid === "guest" ) {
516- throw new Error ( "Sign in before posting ." ) ;
520+ if ( isGuestPreviewSession ( ) || ! uid || uid === "guest" ) {
521+ throw new Error ( "Guest preview posts do not publish. Sign up to post for real ." ) ;
517522 }
518523
519524 const displayName =
@@ -538,7 +543,7 @@ window.noctiveCreatePost = createPost;
538543async function voteOnPost ( postId , direction ) {
539544 const normalizedDirection = direction === "down" ? "down" : "up" ;
540545
541- if ( ! currentViewer ?. uid ) {
546+ if ( isGuestPreviewSession ( ) || ! currentViewer ?. uid ) {
542547 throw new Error ( "Sign in before voting." ) ;
543548 }
544549
@@ -585,6 +590,16 @@ async function voteOnPost(postId, direction) {
585590window . noctiveVotePost = voteOnPost ;
586591
587592onAuthStateChanged ( getAuth ( ) , ( user ) => {
593+ if ( isGuestPreviewSession ( ) ) {
594+ currentViewer = null ;
595+ currentViewerIsAdmin = false ;
596+
597+ if ( latestSnapshot ) {
598+ renderPosts ( latestSnapshot ) ;
599+ }
600+ return ;
601+ }
602+
588603 currentViewer = user ;
589604 currentViewerIsAdmin = isAdminIdentity ( getViewerAdminIdentity ( user ) ) ;
590605
0 commit comments