feat: Add post uploading

This commit is contained in:
Xyphyn committed 2023-07-05 12:58:36 -07:00
1 parent 5f674d4691
commit 1842139af9
31 files changed
+560 -225

No files matched your search

+19 -11
View File
@@ -23,6 +23,10 @@
export let data
</script>
<svelte:head>
<title>Xylemmy</title>
</svelte:head>
<div class="flex flex-row gap-4">
<div class="flex flex-col gap-4 max-w-full">
<div class="flex flex-col sm:flex-row gap-4 max-w-full">
@@ -60,18 +64,20 @@
</div>
<div>
<Link
href={(function () {
<Button
on:click={() => {
$page.url.searchParams.set(
'page',
((Number($page.url.searchParams.get('page')) || 1) + 1).toString()
)
return $page.url.toString()
})()}
goto($page.url.toString(), {
invalidateAll: true,
})
}}
>
Next
</Link>
</Button>
</div>
</div>
<div class="ml-auto hidden lg:block max-w-sm w-full">
@@ -104,12 +110,14 @@
</Card>
{:then site}
<Card>
<Avatar
width={64}
url={site.site_view.site.icon ?? ''}
alt={site.site_view.site.name}
/>
<span class="flex flex-row items-center gap-1 text-sm">
{#if site.site_view.site.icon}
<Avatar
width={64}
url={site.site_view.site.icon}
alt={site.site_view.site.name}
/>
{/if}
<span class="flex flex-row items-center gap-1 text-sm capitalize">
<Icon src={Calendar} width={16} height={16} mini />
<RelativeDate date={new Date(site.site_view.site.published)} />
</span>
+10 -38
View File
@@ -17,6 +17,7 @@
} from 'svelte-hero-icons'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import Card from '$lib/components/ui/StickyCard.svelte'
import CommunityCard from '$lib/components/community/CommunityCard.svelte'
export let data
</script>
@@ -56,8 +57,8 @@
</Link>
</div>
</div>
<Card>
{#await data.streamed.community}
{#await data.streamed.community}
<Card>
<div
class="w-[64px] h-[64px] rounded-full bg-slate-200
dark:bg-zinc-700 animate-pulse"
@@ -82,41 +83,12 @@
class="w-24 h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
rounded-md"
/>
{:then community}
<Avatar
width={48}
url={community.community_view.community.icon ??
`https://api.dicebear.com/6.x/initials/svg?seed=${community.community_view.community.name}`}
alt={community.community_view.community.name}
/>
<span class="flex flex-row items-center gap-1 text-sm">
<Icon src={Calendar} width={16} height={16} mini />
<RelativeDate
date={new Date(community.community_view.community.published)}
/>
</span>
<div class="text-sm flex flex-row flex-wrap gap-3">
<span class="flex flex-row items-center gap-1">
<Icon src={UserGroup} width={16} height={16} mini />
<FormattedNumber
number={community.community_view.counts.subscribers}
/>
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={PencilSquare} width={16} height={16} mini />
<FormattedNumber number={community.community_view.counts.posts} />
</span>
<span class="flex flex-row items-center gap-1">
<Icon src={ChatBubbleOvalLeftEllipsis} width={16} height={16} mini />
<FormattedNumber number={community.community_view.counts.comments} />
</span>
</div>
<h1 class="font-bold text-lg">
{community.community_view.community.name}
</h1>
<SvelteMarkdown source={community.community_view.community.description} />
{:catch}
</Card>
{:then community}
<CommunityCard community_view={community.community_view} />
{:catch}
<Card>
<p>Failed to load community. This is likely an issue on Lemmy's end.</p>
{/await}
</Card>
</Card>
{/await}
</div>
+2 -2
View File
@@ -2,14 +2,14 @@ import { authData, getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
export function load(req: any) {
export async function load(req: any) {
const page = Number(req.url.searchParams.get('page') || 1) || 1
const sort: SortType =
(req.url.searchParams.get('sort') as SortType) || 'Active'
return {
posts: getClient(req.params).getPosts({
posts: getClient().getPosts({
limit: 40,
community_name: req.params.name,
page: page,
+10 -12
View File
@@ -35,20 +35,18 @@
}}
/>
<div class="flex flex-col sm:flex-row gap-2 sm:ml-auto">
<TextInput
class="bg-slate-100 border border-slate-200 dark:border-zinc-800
dark:bg-zinc-900 rounded-md"
bind:value={search}
/>
<Link
href={(function () {
<TextInput class="rounded-md" bind:value={search} />
<Button
on:click={() => {
$page.url.searchParams.set('q', search)
return $page.url.toString()
})()}
goto($page.url.toString(), {
invalidateAll: true,
})
}}
color={Color.ghost}
>
Search
</Link>
</Button>
</div>
</div>
<ul class="flex flex-col">
@@ -92,11 +90,11 @@
</div>
</div>
{#if !community.community.local}
<span class="opacity-80">
<span class="opacity-80 mb-2">
{new URL(community.community.actor_id).hostname}
</span>
{/if}
<div class="flex flex-row gap-3 items-center mt-2">
<div class="flex flex-row gap-3 items-center">
<div class="flex flex-row gap-1 items-center">
<Icon src={UserGroup} width={16} mini />
<span>
+6 -3
View File
@@ -1,5 +1,6 @@
import { getClient } from '$lib/lemmy.js'
import { authData, getClient } from '$lib/lemmy.js'
import type { ListingType } from 'lemmy-js-client'
import { get } from 'svelte/store'
export function load({ url }) {
const type = url.searchParams.get('listingType') as ListingType
@@ -10,17 +11,19 @@ export function load({ url }) {
return getClient().search({
limit: 40,
page: page,
sort: 'Active',
sort: 'TopAll',
type_: 'Communities',
listing_type: type,
q: query,
auth: get(authData)?.token,
})
} else {
return getClient().listCommunities({
limit: 40,
page: page,
sort: 'Active',
sort: 'TopAll',
type_: type,
auth: get(authData)?.token,
})
}
}
+153
View File
@@ -0,0 +1,153 @@
<script lang="ts">
import { goto } from '$app/navigation'
import Button from '$lib/components/input/Button.svelte'
import FileInput from '$lib/components/input/FileInput.svelte'
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import Card from '$lib/components/ui/Card.svelte'
import Loading from '$lib/components/ui/Loading.svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import { authData, buildBaseUrl, getClient } from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
import type { Community } from 'lemmy-js-client'
import { list } from 'postcss'
import { onMount } from 'svelte'
let data: {
community: number | null
title: string
body: string
image: FileList | null
loading: boolean
} = {
community: null,
title: '',
body: '',
image: null,
loading: false,
}
async function uploadImage(
image: FileList | null | undefined
): Promise<string | undefined> {
if (!image || !$authData) return
try {
const res = await getClient().uploadImage({
image: image[0],
auth: $authData.token,
})
if (!res.url) {
throw new Error('Failed to upload image')
}
return res.url
} catch (err) {
throw new Error('Failed to upload image.')
}
}
async function submit() {
if (!data.community || !data.title || !$authData) return
data.loading = true
try {
let image = data.image ? await uploadImage(data.image) : undefined
const post = await getClient().createPost({
auth: $authData.token,
community_id: data.community,
name: data.title,
body: data.body,
url: image,
})
if (!post) throw new Error('Failed to upload post')
console.log(`Uploading post ${post?.post_view.post.id}`)
goto(`/post/${post?.post_view.post.id}`)
} catch (err) {
addToast('Error', err as any, ToastType.error)
}
data.loading = false
}
const placeholders = [
'Is starting nuclear warfare illegal?',
'A cool photo I took before the destruction of mankind!',
'AITA for ending all contention and causing world peace?',
'BREAKING NEWS: Police break into gun shop, find weapons',
]
const placeholder =
placeholders[Math.floor(Math.random() * placeholders.length)]
const placeholder2 =
placeholders[Math.floor(Math.random() * placeholders.length)]
let communities: Community[] | undefined
onMount(async () => {
if (!$authData) {
goto('/login')
return
}
const list = await getClient().listCommunities({
auth: $authData.token,
type_: 'Subscribed',
sort: 'New',
limit: 40,
})
communities = list.communities.map((c) => c.community)
})
</script>
<svelte:head>
<title>Create post</title>
</svelte:head>
<Card class="p-6 max-w-xl mx-auto">
<form on:submit|preventDefault={submit} class="flex flex-col gap-4">
<h1 class="font-bold text-xl">Create Post</h1>
<div>
<span class="block my-1 font-bold text-sm">
Community <span class="text-red-500">*</span>
</span>
{#if communities}
<SelectMenu
bind:selected={data.community}
options={communities.map((c) => c.id)}
optionNames={new Map(communities.map((c) => [c.id, c.title]))}
/>
{:else}
<Loading />
{/if}
</div>
<TextInput required label="Title" bind:value={data.title} {placeholder} />
<TextArea label="Body" bind:value={data.body} placeholder={placeholder2} />
<div>
<span class="block my-1 font-bold text-sm">Image</span>
<FileInput
accept="image/jpeg,image/png,image/webp"
image
bind:files={data.image}
/>
</div>
<Button
submit
color={Color.accent}
large
loading={data.loading}
disabled={data.loading}
>
Submit
</Button>
</form>
</Card>
+4
View File
@@ -4,6 +4,10 @@
export let data
</script>
<svelte:head>
<title>Inbox</title>
</svelte:head>
<h1 class="font-bold text-3xl">Inbox</h1>
<div class="flex flex-col gap-4 list-none my-4">
{#if !data.data || (data.data?.length ?? 0) == 0}
-28
View File
@@ -1,28 +0,0 @@
<script lang="ts">
import Button from '$lib/components/input/Button.svelte'
let url = ''
async function changeInstance() {
try {
new URL(`https://${url}`)
} catch (error) {
return
}
await fetch('/instance_url', {
method: 'POST',
body: url,
})
}
</script>
<form
class="bg-slate-100 border border-slate-200 dark:border-zinc-800
dark:bg-zinc-900 p-8 flex flex-col gap-4 rounded-md"
>
<h1 class="font-bold text-lg">Change instance</h1>
<p class="text-xs">(Must be exact URL )</p>
<input bind:value={url} type="text" />
<Button on:click={changeInstance}>change</Button>
</form>
-15
View File
@@ -1,15 +0,0 @@
import { getClient } from '$lib/lemmy.js'
import { error } from '@sveltejs/kit'
export async function POST(event) {
try {
const text = await event.request.text()
const instance = new URL(`https://${text}`)
event.cookies.set('instance_url', instance.hostname)
return new Response(instance.hostname)
} catch (err) {
throw error(400, 'Invalid URL')
}
}
+3
View File
@@ -57,6 +57,8 @@
}
</script>
<svelte:head>Login</svelte:head>
<Card class="mx-auto p-6">
<form on:submit|preventDefault={logIn} class="flex flex-col gap-4">
<h1 class="font-bold text-lg">Log in</h1>
@@ -76,6 +78,7 @@
disabled={data.loading}
large
color={Color.accent}
submit
>
Submit
</Button>
+6 -2
View File
@@ -12,6 +12,7 @@
import { page } from '$app/stores'
import { onMount } from 'svelte'
import UserLink from '$lib/components/user/UserLink.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
export let data
@@ -85,7 +86,10 @@
}
</script>
<title>{data.post.post_view.post.name}</title>
<svelte:head>
<title>{data.post.post_view.post.name}</title>
</svelte:head>
<div class="flex flex-col gap-2">
<span class="flex flex-row gap-2 text-sm opacity-80 items-center">
<CommunityLink avatar community={postData.community} />
@@ -118,7 +122,7 @@
class="bg-slate-100 border border-slate-200 dark:border-zinc-800
dark:bg-zinc-900 p-2 text-sm rounded-md leading-5"
>
<SvelteMarkdown source={post.body} />
<Markdown source={post.body} />
</p>
{/if}
<div class="w-max">
+6 -1
View File
@@ -2,6 +2,7 @@
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import Link from '$lib/components/input/Link.svelte'
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import Card from '$lib/components/ui/Card.svelte'
import StickyCard from '$lib/components/ui/StickyCard.svelte'
@@ -21,6 +22,10 @@
}
</script>
<svelte:head>
{data.user.person_view.person.name}
</svelte:head>
<div class="flex flex-row gap-4 max-w-full">
<div class="flex flex-col gap-4 max-w-full">
{#each data.user.comments as item}
@@ -79,7 +84,7 @@
{data.user.person_view.person.name}
</h1>
{#if data.user.person_view.person.bio}
<SvelteMarkdown source={data.user.person_view.person.bio} />
<Markdown source={data.user.person_view.person.bio} />
{/if}
</StickyCard>
</div>