This commit is contained in:
Michael Freno
2025-12-18 12:02:00 -05:00
parent a19ad0cb36
commit 09c064eba3
7 changed files with 334 additions and 283 deletions

View File

@@ -1,15 +1,13 @@
import { Show, Suspense, For } from "solid-js";
import { useParams, A, Navigate } from "@solidjs/router";
import { useParams, A, Navigate, query } from "@solidjs/router";
import { Title } from "@solidjs/meta";
import { createAsync } from "@solidjs/router";
import { cache } from "@solidjs/router";
import {
ConnectionFactory,
getUserID,
getPrivilegeLevel
} from "~/server/utils";
import { getRequestEvent } from "solid-js/web";
import { HttpStatusCode } from "@solidjs/start";
import SessionDependantLike from "~/components/blog/SessionDependantLike";
import CommentIcon from "~/components/icons/CommentIcon";
import CommentSectionWrapper from "~/components/blog/CommentSectionWrapper";
@@ -18,7 +16,7 @@ import type { Comment, CommentReaction, UserPublicData } from "~/types/comment";
import { useBars } from "~/context/bars";
// Server function to fetch post by title
const getPostByTitle = cache(async (title: string) => {
const getPostByTitle = query(async (title: string) => {
"use server";
const event = getRequestEvent()!;

View File

@@ -1,19 +1,18 @@
import { Show, createSignal } from "solid-js";
import { useSearchParams, useNavigate } from "@solidjs/router";
import { useSearchParams, useNavigate, query } from "@solidjs/router";
import { Title } from "@solidjs/meta";
import { cache, createAsync } from "@solidjs/router";
import { createAsync } from "@solidjs/router";
import { getRequestEvent } from "solid-js/web";
import { getPrivilegeLevel, getUserID } from "~/server/utils";
import { api } from "~/lib/api";
// Server function to get auth state
const getAuthState = cache(async () => {
const getAuthState = query(async () => {
"use server";
const event = getRequestEvent()!;
const privilegeLevel = await getPrivilegeLevel(event.nativeEvent);
const userID = await getUserID(event.nativeEvent);
return { privilegeLevel, userID };
}, "auth-state");
@@ -130,7 +129,7 @@ export default function CreatePost() {
rows={15}
value={body()}
onInput={(e) => setBody(e.currentTarget.value)}
class="w-full rounded-md border border-surface2 bg-surface0 px-4 py-2 font-mono text-sm"
class="border-surface2 bg-surface0 w-full rounded-md border px-4 py-2 font-mono text-sm"
placeholder="Enter post content (HTML)"
/>
</div>

View File

@@ -1,15 +1,14 @@
import { Show, createSignal, createEffect } from "solid-js";
import { useParams, useNavigate } from "@solidjs/router";
import { useParams, useNavigate, query } from "@solidjs/router";
import { Title } from "@solidjs/meta";
import { createAsync } from "@solidjs/router";
import { cache } from "@solidjs/router";
import { getRequestEvent } from "solid-js/web";
import { getPrivilegeLevel, getUserID } from "~/server/utils";
import { api } from "~/lib/api";
import { ConnectionFactory } from "~/server/utils";
// Server function to fetch post for editing
const getPostForEdit = cache(async (id: string) => {
const getPostForEdit = query(async (id: string) => {
"use server";
const event = getRequestEvent()!;
@@ -140,7 +139,7 @@ export default function EditPost() {
required
value={title()}
onInput={(e) => setTitle(e.currentTarget.value)}
class="w-full rounded-md border border-surface2 bg-surface0 px-4 py-2"
class="border-surface2 bg-surface0 w-full rounded-md border px-4 py-2"
placeholder="Enter post title"
/>
</div>
@@ -155,7 +154,7 @@ export default function EditPost() {
type="text"
value={subtitle()}
onInput={(e) => setSubtitle(e.currentTarget.value)}
class="w-full rounded-md border border-surface2 bg-surface0 px-4 py-2"
class="border-surface2 bg-surface0 w-full rounded-md border px-4 py-2"
placeholder="Enter post subtitle"
/>
</div>
@@ -170,7 +169,7 @@ export default function EditPost() {
rows={15}
value={body()}
onInput={(e) => setBody(e.currentTarget.value)}
class="w-full rounded-md border border-surface2 bg-surface0 px-4 py-2 font-mono text-sm"
class="border-surface2 bg-surface0 w-full rounded-md border px-4 py-2 font-mono text-sm"
placeholder="Enter post content (HTML)"
/>
</div>
@@ -185,7 +184,7 @@ export default function EditPost() {
type="text"
value={bannerPhoto()}
onInput={(e) => setBannerPhoto(e.currentTarget.value)}
class="w-full rounded-md border border-surface2 bg-surface0 px-4 py-2"
class="border-surface2 bg-surface0 w-full rounded-md border px-4 py-2"
placeholder="Enter banner photo URL"
/>
</div>
@@ -207,7 +206,7 @@ export default function EditPost() {
.filter(Boolean)
)
}
class="w-full rounded-md border border-surface2 bg-surface0 px-4 py-2"
class="border-surface2 bg-surface0 w-full rounded-md border px-4 py-2"
placeholder="tag1, tag2, tag3"
/>
</div>

View File

@@ -1,66 +1,81 @@
import { createSignal, Show, Suspense } from "solid-js";
import { useSearchParams, A } from "@solidjs/router";
import { Show, Suspense } from "solid-js";
import { useSearchParams, A, query } from "@solidjs/router";
import { Title } from "@solidjs/meta";
import { createAsync } from "@solidjs/router";
import { cache } from "@solidjs/router";
import { getRequestEvent } from "solid-js/web";
import { ConnectionFactory, getPrivilegeLevel } from "~/server/utils";
import PostSortingSelect from "~/components/blog/PostSortingSelect";
import TagSelector from "~/components/blog/TagSelector";
import PostSorting from "~/components/blog/PostSorting";
// Simple in-memory cache for blog posts to reduce DB load
let cachedPosts: {
posts: any[];
tagMap: Record<string, number>;
privilegeLevel: string;
} | null = null;
let cacheTimestamp: number = 0;
// Server function to fetch posts
const getPosts = cache(async () => {
const getPosts = query(async () => {
"use server";
const event = getRequestEvent()!;
const privilegeLevel = await getPrivilegeLevel(event.nativeEvent);
// Check if we have fresh cached data (cache duration: 30 seconds)
const now = Date.now();
if (cachedPosts && now - cacheTimestamp < 30000) {
return cachedPosts;
}
// Single optimized query using JOINs instead of subqueries and separate queries
let query = `
SELECT
Post.id,
Post.title,
Post.subtitle,
Post.body,
Post.banner_photo,
Post.date,
Post.published,
Post.category,
Post.author_id,
Post.reads,
Post.attachments,
(SELECT COUNT(*) FROM PostLike WHERE Post.id = PostLike.post_id) AS total_likes,
(SELECT COUNT(*) FROM Comment WHERE Post.id = Comment.post_id) AS total_comments
FROM
Post
LEFT JOIN
PostLike ON Post.id = PostLike.post_id
LEFT JOIN
Comment ON Post.id = Comment.post_id`;
SELECT
p.id,
p.title,
p.subtitle,
p.body,
p.banner_photo,
p.date,
p.published,
p.category,
p.author_id,
p.reads,
p.attachments,
COUNT(DISTINCT pl.user_id) as total_likes,
COUNT(DISTINCT c.id) as total_comments,
GROUP_CONCAT(t.value) as tags
FROM Post p
LEFT JOIN PostLike pl ON p.id = pl.post_id
LEFT JOIN Comment c ON p.id = c.post_id
LEFT JOIN Tag t ON p.id = t.post_id`;
if (privilegeLevel !== "admin") {
query += ` WHERE Post.published = TRUE`;
query += ` WHERE p.published = TRUE`;
}
query += ` GROUP BY Post.id, Post.title, Post.subtitle, Post.body, Post.banner_photo, Post.date, Post.published, Post.category, Post.author_id, Post.reads, Post.attachments ORDER BY Post.date DESC;`;
query += ` GROUP BY p.id, p.title, p.subtitle, p.body, p.banner_photo, p.date, p.published, p.category, p.author_id, p.reads, p.attachments ORDER BY p.date DESC;`;
const conn = ConnectionFactory();
const results = await conn.execute(query);
const posts = results.rows;
const postIds = posts.map((post: any) => post.id);
const tagQuery =
postIds.length > 0
? `SELECT * FROM Tag WHERE post_id IN (${postIds.join(", ")})`
: "SELECT * FROM Tag WHERE 1=0";
const tagResults = await conn.execute(tagQuery);
const tags = tagResults.rows;
// Process tags into a map for the UI
let tagMap: Record<string, number> = {};
tags.forEach((tag: any) => {
tagMap[tag.value] = (tagMap[tag.value] || 0) + 1;
posts.forEach((post: any) => {
if (post.tags) {
const postTags = post.tags.split(",");
postTags.forEach((tag: string) => {
tagMap[tag] = (tagMap[tag] || 0) + 1;
});
}
});
return { posts, tags, tagMap, privilegeLevel };
// Cache the results
cachedPosts = { posts, tagMap, privilegeLevel };
cacheTimestamp = now;
return cachedPosts;
}, "blog-posts");
export default function BlogIndex() {