This commit is contained in:
MuslemRahimi 2024-06-05 08:15:37 +02:00
parent 05a14b91be
commit 226d21c6e7
5 changed files with 979 additions and 982 deletions

View File

@ -74,7 +74,7 @@ export const oauthProvider = writable(<string> (""));
export const switchWatchList = writable(<boolean>(false));
export const cachedPosts = writable(<Array<any>> []);
export const cachedPosts = writable(<Array<any>> {});
export const currentPagePosition = writable(<Number> (0));
export const similarTickerClicked = writable(<boolean>(false));

View File

@ -163,7 +163,7 @@ async function getPost() {
let postData;
if ($cachedPosts?.length === 0)
if (Object?.keys($cachedPosts)?.length === 0)
{
postData = {
//userId: data?.user.id,
@ -176,8 +176,8 @@ async function getPost() {
{
postData = {
//userId: data?.user.id,
startPage: $cachedPosts[0].currentPage,
seenPostId: $cachedPosts[0].seenPostId.length === 0 ? [] : $cachedPosts[0].seenPostId,
startPage: $cachedPosts?.currentPage,
seenPostId: $cachedPosts?.seenPostId.length === 0 ? [] : $cachedPosts?.seenPostId,
sortingPosts: sortingPosts,
};
}
@ -227,9 +227,9 @@ let LoginPopup;
let BottomNavigation;
onMount(async () => {
if ($cachedPosts?.length === 0) {
if (Object?.keys($cachedPosts)?.length === 0) {
// Only make API requests if cached posts are not available
[communityStats, moderators, posts, discordData] = await Promise.all([
[communityStats, moderators, posts, discordData] = await Promise?.all([
getCommunityStats(),
getModerators(),
getPost(),
@ -244,7 +244,7 @@ onMount(async () => {
else {
// Use cached data if available
posts = $cachedPosts?.at(0)?.posts;
posts = $cachedPosts?.posts;
communityStats = getCache('', 'getCommunityStats');
moderators = getCache('', 'getModerators');
discordData = getCache('','getDiscordWidget');
@ -269,7 +269,7 @@ onDestroy(async () => {
let sortingPosts = $cachedPosts?.at(0)?.sortingPosts?.length > 0 ? $cachedPosts?.at(0)?.sortingPosts : 'hot';
let sortingPosts = $cachedPosts?.sortingPosts?.length > 0 ? $cachedPosts?.sortingPosts : 'hot';
async function handleCategoryOfPosts(state) {
loading = true;
@ -280,7 +280,7 @@ async function handleCategoryOfPosts(state) {
noPostMore = false;
sortingPosts = state;
$cachedPosts = [];
$cachedPosts = {};
posts = await getPost();
loading = false;
}
@ -319,7 +319,7 @@ $: {
$: {
if(posts)
{
$cachedPosts = [{"sortingPosts": sortingPosts,'currentPage': currentPage, 'seenPostId': seenPostId, 'posts': posts}];
$cachedPosts = {"sortingPosts": sortingPosts,'currentPage': currentPage, 'seenPostId': seenPostId, 'posts': posts};
}
}

View File

@ -1,38 +1,7 @@
import { error, fail, redirect } from "@sveltejs/kit";
import { validateData } from "$lib/utils";
import { loginUserSchema, registerUserSchema } from "$lib/schemas";
import { userRegion, oauthState, oauthVerifier, oauthProvider } from '$lib/store';
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
userRegion.subscribe(value => {
if (usRegion.includes(value)) {
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
} else {
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
}
});
export const load = async ({ params }) => {
const getPostId = async () => {
return params.postId;
};
return {
getPostId: await getPostId(),
};
};
import { oauthState, oauthVerifier, oauthProvider } from '$lib/store';
export const actions = {

View File

@ -1,26 +1,28 @@
<script lang='ts'>
import {getImageURL, formatDate} from '$lib/utils';
import CommentSection from '$lib/components/CommentSection.svelte';
import TextEditor from '$lib/components/TextEditor.svelte';
import Downvote from '$lib/components/Downvote.svelte';
import Upvote from '$lib/components/Upvote.svelte';
import VideoPlayer from '$lib/components/VideoPlayer.svelte';
import {getImageURL, formatDate} from '$lib/utils';
import CommentSection from '$lib/components/CommentSection.svelte';
import TextEditor from '$lib/components/TextEditor.svelte';
import Downvote from '$lib/components/Downvote.svelte';
import Upvote from '$lib/components/Upvote.svelte';
import VideoPlayer from '$lib/components/VideoPlayer.svelte';
import { onMount, onDestroy } from 'svelte';
import {cachedPosts, userRegion, screenWidth, scrollToComment, postIdDeleted, setCache, getCache, tagList, numberOfUnreadNotification, commentAdded, commentUpdated, commentIdDeleted } from '$lib/store';
import { goto, afterNavigate } from '$app/navigation';
import { base } from '$app/paths'
import toast from 'svelte-french-toast';
export let data;
export let form;
import { onMount, onDestroy } from 'svelte';
import {userRegion, screenWidth, scrollToComment, postIdDeleted, setCache, getCache, tagList, numberOfUnreadNotification, commentAdded, commentUpdated, commentIdDeleted } from '$lib/store';
import { goto, afterNavigate } from '$app/navigation';
import { base } from '$app/paths'
const usRegion = ['cle1','iad1','pdx1','sfo1'];
import toast from 'svelte-french-toast';
let fastifyURL;
export let data;
export let form;
userRegion.subscribe(value => {
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let fastifyURL;
userRegion.subscribe(value => {
if (usRegion.includes(value)) {
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
@ -30,35 +32,19 @@ userRegion.subscribe(value => {
});
let post = {'id': 'test', 'upvote': 0, 'downvote': 0};
let isScrolled = false;
let isLoaded = false;
let post = data?.getOnePost;
let isScrolled = false;
let isLoaded = false;
let upvoteButtonClicked = {};
let downvoteButtonClicked = {};
let upvoteCounter = {};
let downvoteCounter = {};
let userAlreadyVoted;
async function getOnePost() {
const postData = {'postId': data?.getPostId};
const response = await fetch(fastifyURL+'/get-one-post', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData),
});
const output = await response.json()
return output?.items
}
let upvoteButtonClicked = {};
let downvoteButtonClicked = {};
let upvoteCounter = {};
let downvoteCounter = {};
let userAlreadyVoted;
const handleUpvote = async (event) => {
const handleUpvote = async (event) => {
event.preventDefault(); // prevent the default form submission behavior
@ -90,9 +76,9 @@ const handleUpvote = async (event) => {
body: JSON.stringify(postData)
}); // make a POST request to the server with the FormData object
};
};
const handleDownvote = async (event) => {
const handleDownvote = async (event) => {
event.preventDefault(); // prevent the default form submission behavior
const postId = event.target.postId.value;
@ -125,9 +111,9 @@ const handleDownvote = async (event) => {
body: JSON.stringify(postData)
}); // make a POST request to the server with the FormData object
};
};
function handleScroll() {
function handleScroll() {
// Check the scroll position
isScrolled = window.scrollY > 0;
@ -135,7 +121,7 @@ function handleScroll() {
}
const sortCommentsByDate = (comments) => {
const sortCommentsByDate = (comments) => {
return comments.sort(function(a, b) {
return new Date(b?.created) - new Date(a?.created);
});
@ -144,33 +130,33 @@ const sortCommentsByDate = (comments) => {
let dropdownOpen = false; // State variable to control dropdown visibility
let dropdownOpen = false; // State variable to control dropdown visibility
function toggleDropdown() {
function toggleDropdown() {
dropdownOpen = !dropdownOpen;
}
const handleCopyLink = async () => {
const handleCopyLink = async () => {
dropdownOpen = !dropdownOpen;
await navigator.clipboard.writeText("https://stocknear.com/community/post/"+post.id);
toast.success('Link copied', {
style: 'border-radius: 200px; background: #333; color: #fff;'
});
};
};
const handleReport = async () => {
const handleReport = async () => {
dropdownOpen = !dropdownOpen;
toast.success('Post has been reported. Thank you!', {
style: 'border-radius: 200px; background: #333; color: #fff;'
});
};
};
const handleDeletePost = async () => {
const handleDeletePost = async () => {
dropdownOpen = !dropdownOpen;
@ -206,26 +192,26 @@ const handleDeletePost = async () => {
style: 'border-radius: 200px; background: #333; color: #fff;'
});
}
};
};
let numberOfComments = 0;
let comments = [];
//let replyComments = data?.allReplyComments?.items.reverse() || [];
let numberOfComments = 0;
let comments = [];
//let replyComments = data?.allReplyComments?.items.reverse() || [];
let moderators;
let videoId = null;
let moderators;
let videoId = null;
let loadTextEditor = false;
let loadTextEditor = false;
async function getAllComments()
{
async function getAllComments()
{
const postData = {'postId': data?.getPostId};
@ -242,10 +228,10 @@ async function getAllComments()
return output;
};
};
const getModerators = async () => {
const getModerators = async () => {
let output;
// Get cached data for the specific tickerID
@ -272,48 +258,28 @@ const getModerators = async () => {
function isModerator(userId) {
function isModerator(userId) {
return moderators?.some(moderator => userId === moderator?.user);
}
}
function countAllComments(comments) {
// Helper function to recursively count comments
function countComments(commentsList) {
let count = 0;
for (let comment of commentsList) {
function countAllComments(comments) {
// Helper function to recursively count comments
function countComments(commentsList) {
let count = 0;
for (let comment of commentsList) {
count += 1; // Count this comment
if (comment.children && comment.children.length > 0) {
count += countComments(comment.children); // Recursively count nested comments
}
}
return count;
}
return countComments(comments);
}
let LoginPopup;
onMount(async () => {
post = $cachedPosts?.at(0)?.posts?.find(item => item?.id === data?.getPostId) ?? {};
if (Object?.keys(post)?.length !== 0) {
[moderators, comments] = await Promise?.all([
getModerators(),
getAllComments(),
]);
} else {
[post, moderators, comments] = await Promise?.all([
getOnePost(),
getModerators(),
getAllComments(),
]);
}
return count;
}
return countComments(comments);
}
numberOfComments = countAllComments(comments) || 0;
let LoginPopup;
onMount(async () => {
upvoteCounter[post.id] = post?.upvote;
downvoteCounter[post.id] = post?.downvote;
@ -334,10 +300,14 @@ onMount(async () => {
LoginPopup = (await import('$lib/components/LoginPopup.svelte')).default;
}
[moderators, comments] = await Promise.all([
getModerators(),
getAllComments(),
]);
numberOfComments = countAllComments(comments) || 0;
if (post?.postType === 'link') {
if (post?.postType === 'link') {
const url = new URL(post.link);
if (url.hostname === "www.youtube.com") {
const videoIdMatch = url.search.match(/v=([^&]+)/);
@ -346,7 +316,7 @@ if (post?.postType === 'link') {
}
}
}
}
loadTextEditor = true;
isLoaded = true;
@ -361,16 +331,16 @@ if (post?.postType === 'link') {
window.removeEventListener('scroll', handleScroll);
};
});
});
let previousPage : string = base || '/community/';
afterNavigate(({from}) => {
let previousPage : string = base || '/community/';
afterNavigate(({from}) => {
previousPage = from?.url.pathname || '/community/'
})
function closePost(event) {
function closePost(event) {
if (event.target.classList.contains('cursor-zoom-out')) {
@ -378,16 +348,18 @@ function closePost(event) {
}
}
}
onDestroy( () => {
onDestroy( () => {
$commentAdded = '';
$commentIdDeleted = '';
$scrollToComment = '';
})
})
function addCommentToParent(comments, newComment) {
function addCommentToParent(comments, newComment) {
// Helper function to handle the recursion
function findAndAddParent(commentsList, newComment) {
for (let comment of commentsList) {
@ -421,9 +393,9 @@ function addCommentToParent(comments, newComment) {
}
return comments;
}
}
function updateCommentInList(newObject, list) {
function updateCommentInList(newObject, list) {
// Helper function to handle the recursion
function findAndReplaceComment(commentsList, newComment) {
for (let comment of commentsList) {
@ -446,12 +418,12 @@ function updateCommentInList(newObject, list) {
// Return the updated list
return list;
}
}
// Function to recursively find and remove a comment by id
function removeCommentById(comments, idToRemove) {
// Function to recursively find and remove a comment by id
function removeCommentById(comments, idToRemove) {
for (let i = 0; i < comments?.length; i++) {
let comment = comments[i];
// If the current comment has the id to remove
@ -466,40 +438,40 @@ function removeCommentById(comments, idToRemove) {
}
}
return comments; // Return the original comments list if the comment is not found
}
}
$: {
$: {
if($commentAdded?.length !== 0)
{
comments = sortCommentsByDate(addCommentToParent(comments, $commentAdded))
numberOfComments = countAllComments(comments)
}
}
}
$: {
$: {
if($commentUpdated?.length !== 0)
{
comments = updateCommentInList($commentUpdated, comments);
}
}
}
$: {
$: {
if($commentIdDeleted?.length !== 0)
{
comments = removeCommentById(comments, $commentIdDeleted)
numberOfComments = countAllComments(comments)
}
}
}
</script>
</script>
<svelte:head>
<svelte:head>
<title> {$numberOfUnreadNotification > 0 ? `(${$numberOfUnreadNotification})` : ''} {post.title} · stocknear</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
@ -518,16 +490,15 @@ $: {
<meta name="twitter:description" content="Your daily dose of stock market funny memes, GIFs, videos and weird news stories. We deliver hundreds of new stock market memes daily.">
<meta name="twitter:image" content="https://stocknear-pocketbase.s3.amazonaws.com/logo/meta_logo.jpg"/>
<!-- Add more Twitter meta tags as needed -->
</svelte:head>
</svelte:head>
<!--<section on:click={closePost} class="bg-[#0F0F0F] cursor-zoom-out min-h-screen">-->
<!--<section on:click={closePost} class="bg-[#0F0F0F] cursor-zoom-out min-h-screen">-->
<!--in:pageTransitionIn={{ duration: 250, screenWidth: $screenWidth }}-->
{#if isLoaded}
<div class="overflow-hidden flex flex-row item-start w-full lg:mt-5 relative max-w-6xl m-auto lg:px-5 sm:pb-40">
<!--in:pageTransitionIn={{ duration: 250, screenWidth: $screenWidth }}-->
<div class="overflow-hidden flex flex-row item-start w-full lg:mt-5 relative max-w-6xl m-auto lg:px-5 sm:pb-40">
<!--Start Voting-->
<div style="top: 4rem;" class="hidden lg:flex flex-col items-center relative h-fit sticky z-20 lg:mr-3 ">
<!--Start Upvote-->
@ -579,10 +550,10 @@ $: {
<!--End Voting-->
<div class="w-full bg-[#202020] max-w-5xl m-auto border sm:hover:border-slate-800 border-slate-800 rounded-none sm:rounded-xl ">
<div class="w-full bg-[#202020] max-w-5xl m-auto border sm:hover:border-slate-800 border-slate-800 rounded-none sm:rounded-xl ">
<!-- Start Header -->
<div style="top: 0.8rem;" class="sm:rounded-xl absolute h-12 sticky z-20 bg-[#202020] w-full {isScrolled && $screenWidth < 640 ? 'border-b border-gray-700 ease-in' : 'ease-out'}">
<!-- Start Header -->
<div style="top: 0.8rem;" class="sm:rounded-xl absolute h-12 sticky z-20 bg-[#202020] w-full {isScrolled && $screenWidth < 640 ? 'border-b border-gray-700 ease-in' : 'ease-out'}">
<div class="flex flex-row items-center justify-between w-full pt-3">
<a href={previousPage} class="absolute left-2 sm:left-4 sm:top-4">
<svg class="w-5 h-5 inline-block sm:mr-1 sm:-mt-0.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
@ -598,9 +569,9 @@ $: {
Post
</span>
</div>
</div>
</div>
<!-- Page wrapper -->
<!-- Page wrapper -->
<div class="flex flex-col overflow-hidden pb-40">
@ -924,33 +895,29 @@ $: {
</div>
</div>
{:else}
<div class="flex justify-center items-center m-auto h-full w-full max-w-6xl">
<div class="loader">Loading...</div>
</div>
{/if}
</div>
<!--Start Login Modal-->
{#if LoginPopup}
<!--Start Login Modal-->
{#if LoginPopup}
<LoginPopup form={form}/>
{/if}
<!--End Login Modal-->
{/if}
<!--End Login Modal-->
{#if data?.user}
{#if data?.user}
<!--Start Delete Strategy Modal-->
<!--Start Delete Strategy Modal-->
<input type="checkbox" id="deletePostModal" class="modal-toggle" />
<input type="checkbox" id="deletePostModal" class="modal-toggle" />
<dialog id="deletePostModal" class="modal modal-bottom sm:modal-middle">
<dialog id="deletePostModal" class="modal modal-bottom sm:modal-middle">
<label for="deletePostModal" class="cursor-pointer modal-backdrop bg-[#000] bg-opacity-[0.5]"></label>
<label for="deletePostModal" class="cursor-pointer modal-backdrop bg-[#000] bg-opacity-[0.5]"></label>
<div class="modal-box w-full bg-[#202020] sm:border sm:border-slate-600 overflow-hidden">
<div class="modal-box w-full bg-[#202020] sm:border sm:border-slate-600 overflow-hidden">
<h3 class="font-bold text-xl mb-5 pt-5 text-white m-auto w-3/4 text-center">
Are you sure you want to delete the post?
@ -964,21 +931,21 @@ $: {
</div>
</div>
</dialog>
</dialog>
<!--End Delete Strategy Modal-->
<!--End Delete Strategy Modal-->
{/if}
{/if}
<style>
<style>
.info-card {
.info-card {
position: fixed;
top: 50%;
left: 50%;
@ -989,4 +956,4 @@ $: {
}
</style>
</style>

View File

@ -0,0 +1,61 @@
import { userRegion, cachedPosts } from '$lib/store';
import { get } from 'svelte/store';
export const load = async ({ params }) => {
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
userRegion.subscribe(value => {
if (usRegion.includes(value)) {
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
} else {
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
}
});
async function getOnePost() {
// Get the current value of cachedPosts
const cachedValue = get(cachedPosts);
// Try to find the post in the cached value
const output = cachedValue?.posts?.find(item => item?.id === params.postId) ?? {};
// If the post is found in the cache, return it
if (Object.keys(output).length !== 0) {
return output;
}
// If the post is not found in the cache, fetch it from the endpoint
const postData = { postId: params.postId };
const response = await fetch(fastifyURL + '/get-one-post', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData),
});
const result = await response.json();
// Assuming the result contains an 'items' array
return result.items;
}
const getPostId = async () => {
return params.postId;
};
return {
getPostId: await getPostId(),
getOnePost: await getOnePost(),
};
};