From c6ce13a5308fa71bc7479d1571d07adafd9ef258 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Wed, 11 Jun 2025 00:53:54 -0700 Subject: [PATCH] Simplify posts We had a lot of unnecessary complexity here due to post types that never ended up getting used. We also made the post slug field reactive and bound to the title field. We also fixed filters on the Universe admin page so we can filter by unpublished posts too (WIP) We also fixed the hover state of BackButton --- .../migration.sql | 1 + .../migration.sql | 3 + prisma/schema.prisma | 6 +- scripts/init-db.ts | 82 +++++++++---------- src/lib/components/BackButton.svelte | 4 +- src/lib/components/DynamicPostContent.svelte | 5 +- src/lib/components/admin/EssayForm.svelte | 10 ++- src/lib/components/admin/PostListItem.svelte | 5 +- .../admin/PostMetadataPopover.svelte | 7 +- .../components/admin/UniverseComposer.svelte | 16 ++-- src/routes/admin/posts/+page.svelte | 4 +- src/routes/admin/posts/[id]/edit/+page.svelte | 12 +-- src/routes/admin/posts/new/+page.svelte | 49 ++++++----- .../api/posts/by-slug/[slug]/+server.ts | 33 +------- 14 files changed, 109 insertions(+), 128 deletions(-) create mode 100644 prisma/migrations/20250611072926_update_post_types_to_post_and_essay/migration.sql create mode 100644 prisma/migrations/20250611073054_update_post_types/migration.sql diff --git a/prisma/migrations/20250611072926_update_post_types_to_post_and_essay/migration.sql b/prisma/migrations/20250611072926_update_post_types_to_post_and_essay/migration.sql new file mode 100644 index 0000000..af5102c --- /dev/null +++ b/prisma/migrations/20250611072926_update_post_types_to_post_and_essay/migration.sql @@ -0,0 +1 @@ +-- This is an empty migration. \ No newline at end of file diff --git a/prisma/migrations/20250611073054_update_post_types/migration.sql b/prisma/migrations/20250611073054_update_post_types/migration.sql new file mode 100644 index 0000000..467b168 --- /dev/null +++ b/prisma/migrations/20250611073054_update_post_types/migration.sql @@ -0,0 +1,3 @@ +-- Update existing postType values +UPDATE "Post" SET "postType" = 'essay' WHERE "postType" = 'blog'; +UPDATE "Post" SET "postType" = 'post' WHERE "postType" = 'microblog'; \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c62014c..cfa7d1f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -43,9 +43,9 @@ model Project { model Post { id Int @id @default(autoincrement()) slug String @unique @db.VarChar(255) - postType String @db.VarChar(50) // blog, microblog - title String? @db.VarChar(255) // Optional for microblog posts - content Json? // BlockNote JSON for blog/microblog + postType String @db.VarChar(50) // post, essay + title String? @db.VarChar(255) // Optional for post type + content Json? // JSON content for posts and essays featuredImage String? @db.VarChar(500) attachments Json? // Array of media IDs for photo attachments diff --git a/scripts/init-db.ts b/scripts/init-db.ts index 912d889..ec9a02c 100644 --- a/scripts/init-db.ts +++ b/scripts/init-db.ts @@ -4,54 +4,54 @@ import { execSync } from 'child_process' const prisma = new PrismaClient() async function isDatabaseInitialized(): Promise { - try { - // Check if we have any completed migrations - const migrationCount = await prisma.$queryRaw<[{ count: bigint }]>` + try { + // Check if we have any completed migrations + const migrationCount = await prisma.$queryRaw<[{ count: bigint }]>` SELECT COUNT(*) as count FROM _prisma_migrations WHERE finished_at IS NOT NULL ` - - return migrationCount[0].count > 0n - } catch (error: any) { - // If the table doesn't exist, database is not initialized - console.log('📊 Migration table check failed (expected on first deploy):', error.message) - return false - } + + return migrationCount[0].count > 0n + } catch (error: any) { + // If the table doesn't exist, database is not initialized + console.log('📊 Migration table check failed (expected on first deploy):', error.message) + return false + } } async function initializeDatabase() { - console.log('🔍 Checking database initialization status...') - - // Give the database a moment to be ready - await new Promise(resolve => setTimeout(resolve, 2000)) - - try { - const isInitialized = await isDatabaseInitialized() - - if (!isInitialized) { - console.log('📦 First time setup detected. Initializing database...') - - // Run migrations - console.log('🔄 Running database migrations...') - execSync('npx prisma migrate deploy', { stdio: 'inherit' }) - - // Run seeds - console.log('🌱 Seeding database...') - execSync('npx prisma db seed', { stdio: 'inherit' }) - - console.log('✅ Database initialization complete!') - } else { - console.log('✅ Database already initialized. Running migrations only...') - execSync('npx prisma migrate deploy', { stdio: 'inherit' }) - } - } catch (error) { - console.error('❌ Database initialization failed:', error) - process.exit(1) - } finally { - await prisma.$disconnect() - } + console.log('🔍 Checking database initialization status...') + + // Give the database a moment to be ready + await new Promise((resolve) => setTimeout(resolve, 2000)) + + try { + const isInitialized = await isDatabaseInitialized() + + if (!isInitialized) { + console.log('📦 First time setup detected. Initializing database...') + + // Run migrations + console.log('🔄 Running database migrations...') + execSync('npx prisma migrate deploy', { stdio: 'inherit' }) + + // Run seeds + console.log('🌱 Seeding database...') + execSync('npx prisma db seed', { stdio: 'inherit' }) + + console.log('✅ Database initialization complete!') + } else { + console.log('✅ Database already initialized. Running migrations only...') + execSync('npx prisma migrate deploy', { stdio: 'inherit' }) + } + } catch (error) { + console.error('❌ Database initialization failed:', error) + process.exit(1) + } finally { + await prisma.$disconnect() + } } // Run the initialization -initializeDatabase() \ No newline at end of file +initializeDatabase() diff --git a/src/lib/components/BackButton.svelte b/src/lib/components/BackButton.svelte index f72915e..eef1b7f 100644 --- a/src/lib/components/BackButton.svelte +++ b/src/lib/components/BackButton.svelte @@ -35,7 +35,7 @@ display: inline-flex; align-items: center; gap: $unit-half; - padding: $unit $unit-2x; + padding: $unit 0; font-size: 0.875rem; font-weight: 500; color: $red-60; @@ -46,8 +46,6 @@ transition: all 0.2s ease; &:hover { - background: rgba($red-60, 0.08); - :global(.arrow-icon) { transform: translateX(-3px); } diff --git a/src/lib/components/DynamicPostContent.svelte b/src/lib/components/DynamicPostContent.svelte index 7fae908..2af519b 100644 --- a/src/lib/components/DynamicPostContent.svelte +++ b/src/lib/components/DynamicPostContent.svelte @@ -25,7 +25,6 @@ {/if} - {#if post.album && post.album.photos && post.album.photos.length > 0}
@@ -78,7 +77,7 @@ .post-content { display: flex; flex-direction: column; - max-width: 784px; + width: 100%; gap: $unit-3x; margin: 0 auto; @@ -95,6 +94,8 @@ } &.essay { + max-width: 100%; // Full width for essays + .post-body { font-size: 1rem; line-height: 1.4; diff --git a/src/lib/components/admin/EssayForm.svelte b/src/lib/components/admin/EssayForm.svelte index 28122d3..4617cf3 100644 --- a/src/lib/components/admin/EssayForm.svelte +++ b/src/lib/components/admin/EssayForm.svelte @@ -98,7 +98,7 @@ const payload = { title, slug, - type: 'blog', // 'blog' is the database value for essays + type: 'essay', // No mapping needed anymore status, content, tags @@ -261,7 +261,13 @@ }} >
- + diff --git a/src/lib/components/admin/PostListItem.svelte b/src/lib/components/admin/PostListItem.svelte index fe18ba1..48bff7a 100644 --- a/src/lib/components/admin/PostListItem.svelte +++ b/src/lib/components/admin/PostListItem.svelte @@ -25,10 +25,7 @@ const postTypeLabels: Record = { post: 'Post', - essay: 'Essay', - // Map database types to display names - blog: 'Essay', - microblog: 'Post' + essay: 'Essay' } function handlePostClick() { diff --git a/src/lib/components/admin/PostMetadataPopover.svelte b/src/lib/components/admin/PostMetadataPopover.svelte index 19ebbf1..855fbd2 100644 --- a/src/lib/components/admin/PostMetadataPopover.svelte +++ b/src/lib/components/admin/PostMetadataPopover.svelte @@ -12,6 +12,7 @@ onRemoveTag: (tag: string) => void onDelete: () => void onClose?: () => void + onFieldUpdate?: (key: string, value: any) => void } let { @@ -24,12 +25,14 @@ onAddTag, onRemoveTag, onDelete, - onClose = () => {} + onClose = () => {}, + onFieldUpdate }: Props = $props() function handleFieldUpdate(key: string, value: any) { if (key === 'slug') { slug = value + onFieldUpdate?.(key, value) } else if (key === 'tagInput') { tagInput = value } @@ -92,4 +95,4 @@ {onAddTag} {onRemoveTag} {onClose} -/> \ No newline at end of file +/> diff --git a/src/lib/components/admin/UniverseComposer.svelte b/src/lib/components/admin/UniverseComposer.svelte index e943d32..19a9fa0 100644 --- a/src/lib/components/admin/UniverseComposer.svelte +++ b/src/lib/components/admin/UniverseComposer.svelte @@ -77,8 +77,11 @@ } function switchToEssay() { - const contentParam = content ? encodeURIComponent(JSON.stringify(content)) : '' - goto(`/admin/posts/new?type=essay${contentParam ? `&content=${contentParam}` : ''}`) + // Store content in sessionStorage to avoid messy URLs + if (content && content.content && content.content.length > 0) { + sessionStorage.setItem('draft_content', JSON.stringify(content)) + } + goto('/admin/posts/new?type=essay') } function generateSlug(title: string): string { @@ -92,7 +95,6 @@ essaySlug = generateSlug(essayTitle) } - function handlePhotoUpload() { fileInput.click() } @@ -201,7 +203,7 @@ if (postType === 'essay') { postData = { ...postData, - type: 'blog', // 'blog' is the database value for essays + type: 'essay', // No mapping needed anymore title: essayTitle, slug: essaySlug, excerpt: essayExcerpt, @@ -211,7 +213,7 @@ // All other content is just a "post" with attachments postData = { ...postData, - type: 'microblog' // 'microblog' is for shorter posts + type: 'post' // No mapping needed anymore } } @@ -301,7 +303,6 @@ class="composer-editor" /> - {#if attachedPhotos.length > 0}
{#each attachedPhotos as photo} @@ -335,7 +336,6 @@