impr: rewrite PostForm component to be stabler and more sustainable (#684)

* impr: rewrite PostForm component into extracted state class

* fix: use base community as PostFormState

* fix: add else clause to ensure 'unset' unsets the language
This commit is contained in:
xylight authored and GitHub committed 2025-11-15 23:12:52 +00:00
1 parent 6f7501ffc5
commit e4507cbf91
16 files changed
+506 -586

No files matched your search

+22 -22
View File
@@ -1,23 +1,35 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { page } from '$app/state'
import type { CommunityView } from '$lib/api/types'
import { t } from '$lib/app/i18n'
import { getSessionStorage, setSessionStorage } from '$lib/app/session'
import PostForm from '$lib/feature/post/PostForm.svelte'
import PostForm from '$lib/feature/post/form/PostForm.svelte'
import {
PostFormState,
type PostFormInit,
} from '$lib/feature/post/form/postform.svelte.js'
import { postLink } from '$lib/feature/post/helpers.js'
import { onDestroy } from 'svelte'
let { data } = $props()
let community = getSessionStorage('lastSeenCommunity') as
| { id: number; name: string }
| CommunityView
| undefined
onDestroy(() => {
setSessionStorage('lastSeenCommunity', undefined)
})
// TODO remove this
let post = getSessionStorage('postDraft') as any
let crosspost = $derived.by<PostFormInit>(() => {
const crosspostParam = page.url.searchParams.get('crosspost')
try {
return crosspostParam
? JSON.parse(atob(crosspostParam) || '{}')
: undefined
} catch {
return undefined
}
})
</script>
<svelte:head>
@@ -25,20 +37,8 @@
</svelte:head>
<PostForm
passedData={data.crosspost == true
? post
: {
body: '',
community: null,
image: null,
loading: false,
nsfw: false,
title: '',
url: undefined,
flair_list: [],
}}
passedCommunity={community}
onsubmit={(e) => goto(`/post/${e.post.id}`)}
init={new PostFormState(crosspost ?? { community: community?.community })}
onsubmit={(post) => goto(postLink(post.post))}
>
{#snippet formtitle()}{/snippet}
{#snippet title()}{/snippet}
</PostForm>
+1 -3
View File
@@ -1,8 +1,6 @@
import { profile } from '$lib/app/auth.svelte'
import { redirect } from '@sveltejs/kit'
export const load = ({ url }) => {
export const load = () => {
if (!profile.current.jwt) redirect(302, '/login')
return { crosspost: Boolean(url.searchParams.get('crosspost')) }
}