diff --git a/src/hooks.server.ts b/src/hooks.server.ts
index f1800a07..f6a13b19 100644
--- a/src/hooks.server.ts
+++ b/src/hooks.server.ts
@@ -1,4 +1,3 @@
-import { DEFAULT_INSTANCE_URL } from '$lib/lemmy.js'
import { error } from '@sveltejs/kit'
export async function handle({ event, resolve }) {
@@ -14,21 +13,30 @@ export async function handle({ event, resolve }) {
event.request.headers.delete('host')
try {
- console.log(`https://${instance}/${url.toString()}`)
const data = await fetch(`https://${instance}/${url.toString()}`, {
method: event.request.method,
body: event.request.body,
headers: event.request.headers,
+ // @ts-ignore this works, idk why typescript complains
+ duplex: 'half',
signal: AbortSignal.timeout(20 * 1000),
})
- return new Response(JSON.stringify(await data.json()))
- } catch (err) {
- console.error(err)
- throw error(500, { message: 'The proxy failed to fetch from the server' })
+ const json = await data.json()
+
+ return new Response(JSON.stringify(json))
+ } catch (error) {
+ console.error(error)
+ return new Response(
+ JSON.stringify({
+ message: 'the proxy failed to fetch from server',
+ }),
+ {
+ status: 500,
+ }
+ )
}
}
-
const response = await resolve(event)
return response
}
diff --git a/src/lib/components/input/Button.svelte b/src/lib/components/input/Button.svelte
index f070fd1b..79e9ef53 100644
--- a/src/lib/components/input/Button.svelte
+++ b/src/lib/components/input/Button.svelte
@@ -1,23 +1,31 @@
diff --git a/src/lib/components/input/TextInput.svelte b/src/lib/components/input/TextInput.svelte
new file mode 100644
index 00000000..e438de1c
--- /dev/null
+++ b/src/lib/components/input/TextInput.svelte
@@ -0,0 +1,39 @@
+
+
+
diff --git a/src/lib/components/ui/Card.svelte b/src/lib/components/ui/Card.svelte
index 074e23be..f64813f3 100644
--- a/src/lib/components/ui/Card.svelte
+++ b/src/lib/components/ui/Card.svelte
@@ -1,7 +1,12 @@
+
+
diff --git a/src/lib/components/ui/Loading.svelte b/src/lib/components/ui/Loading.svelte
new file mode 100644
index 00000000..657c9395
--- /dev/null
+++ b/src/lib/components/ui/Loading.svelte
@@ -0,0 +1,46 @@
+
+
+
diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte
index d9f070e5..4ebad951 100644
--- a/src/lib/components/ui/Navbar.svelte
+++ b/src/lib/components/ui/Navbar.svelte
@@ -1,7 +1,15 @@
diff --git a/src/lib/components/ui/StickyCard.svelte b/src/lib/components/ui/StickyCard.svelte
new file mode 100644
index 00000000..074e23be
--- /dev/null
+++ b/src/lib/components/ui/StickyCard.svelte
@@ -0,0 +1,7 @@
+
+
+
diff --git a/src/lib/components/ui/menu/Menu.svelte b/src/lib/components/ui/menu/Menu.svelte
new file mode 100644
index 00000000..59eba320
--- /dev/null
+++ b/src/lib/components/ui/menu/Menu.svelte
@@ -0,0 +1,58 @@
+
+
+ {
+ let path = event.composedPath()
+
+ if (!path.includes(element)) {
+ if (open) {
+ open = false
+ }
+ }
+ }}
+/>
+
diff --git a/src/lib/components/ui/menu/MenuButton.svelte b/src/lib/components/ui/menu/MenuButton.svelte
new file mode 100644
index 00000000..ed6146a9
--- /dev/null
+++ b/src/lib/components/ui/menu/MenuButton.svelte
@@ -0,0 +1,27 @@
+
+
+{#if link}
+
+
+
+{:else}
+
+{/if}
diff --git a/src/lib/lemmy.ts b/src/lib/lemmy.ts
index 441cae18..3a45cb20 100644
--- a/src/lib/lemmy.ts
+++ b/src/lib/lemmy.ts
@@ -1,13 +1,51 @@
-import { LemmyHttp, type LoginResponse } from 'lemmy-js-client'
+import {
+ type GetPersonDetailsResponse,
+ LemmyHttp,
+ type LoginResponse,
+} from 'lemmy-js-client'
import { writable } from 'svelte/store'
import { PUBLIC_PROXY_URL } from '$env/static/public'
-export const DEFAULT_INSTANCE_URL = 'lemmy.world'
-
+export const DEFAULT_INSTANCE_URL = 'programming.dev'
export let instance_url = writable(DEFAULT_INSTANCE_URL)
-export function getClient(instance: string = `lemmy.world`): LemmyHttp {
+export function getClient(instance: string = `programming.dev`): LemmyHttp {
return new LemmyHttp(`${PUBLIC_PROXY_URL}/cors/${instance}`)
}
-export const token = writable()
+export interface AuthData {
+ token: string
+ username: string
+ instance: string
+}
+
+export const authData = writable()
+export const user = writable()
+
+if (typeof localStorage != 'undefined') {
+ if (localStorage.getItem('user')) {
+ try {
+ authData.set(JSON.parse(localStorage.getItem('user') ?? ''))
+ } catch (error) {
+ localStorage.removeItem('user')
+ }
+ }
+}
+
+authData.subscribe(async (data) => {
+ if (!data) return
+ try {
+ const person = await getClient().getPersonDetails({
+ auth: data?.token,
+ username: `${data?.username}@${data?.instance}`,
+ })
+
+ user.set(person)
+
+ if (typeof localStorage != 'undefined') {
+ localStorage.setItem('user', JSON.stringify(data))
+ }
+ } catch (error) {
+ authData.set(undefined)
+ }
+})
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index d0f09f7e..e26f4258 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -2,7 +2,7 @@
import { goto } from '$app/navigation'
import { page } from '$app/stores'
import Button from '$lib/components/input/Button.svelte'
- import Card from '$lib/components/ui/Card.svelte'
+ import Card from '$lib/components/ui/StickyCard.svelte'
import Link from '$lib/components/input/Link.svelte'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import {
@@ -90,7 +90,7 @@
rounded-md"
/>
diff --git a/src/routes/login/+page.svelte b/src/routes/login/+page.svelte
new file mode 100644
index 00000000..2e750c04
--- /dev/null
+++ b/src/routes/login/+page.svelte
@@ -0,0 +1,54 @@
+
+
+
+
+
diff --git a/src/routes/post/[id]/+page.svelte b/src/routes/post/[id]/+page.svelte
index 800f90a0..f0e5065b 100644
--- a/src/routes/post/[id]/+page.svelte
+++ b/src/routes/post/[id]/+page.svelte
@@ -7,18 +7,18 @@
CommentView,
} from 'lemmy-js-client'
import type { CommentNodeI } from './comments.js'
- import Comments from './Comments.svelte'
+ import Comments from './comment/Comments.svelte'
import SvelteMarkdown from 'svelte-markdown'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
+ import { user } from '$lib/lemmy.js'
+ import CommentForm from './comment/CommentForm.svelte'
- export let data: { post: GetPostResponse; comments: GetCommentsResponse }
+ export let data
const postData = data.post.post_view
const post = postData.post
- const comments = data.comments.comments
-
function getCommentParentId(comment?: Comment): number | undefined {
const split = comment?.path.split('.')
// remove the 0
@@ -124,10 +124,39 @@
{postData.counts.comments}
-
-
+ {#await data.streamed.comments}
+
+ {#each new Array(10) as comment}
+
+ {/each}
+
+ {:then comments}
+ {#if $user}
+ comments.comments.unshift(comment.detail)}
+ />
+ {/if}
+
+
+ {:catch}
+
+ Failed to load posts
+
+ {/await}
diff --git a/src/routes/post/[id]/+page.ts b/src/routes/post/[id]/+page.ts
index fc7e5905..135cbac6 100644
--- a/src/routes/post/[id]/+page.ts
+++ b/src/routes/post/[id]/+page.ts
@@ -1,18 +1,23 @@
-import { getClient } from '$lib/lemmy.js'
+import { authData, getClient } from '$lib/lemmy.js'
+import { get } from 'svelte/store'
export async function load({ params }) {
return {
- post: await getClient().getPost({
+ post: getClient().getPost({
id: Number(params.id),
+ auth: get(authData)?.token,
}),
- comments: await getClient().getComments({
- post_id: Number(params.id),
- type_: 'All',
- limit: 250,
- page: 1,
- max_depth: 6,
- saved_only: false,
- sort: 'Hot',
- }),
+ streamed: {
+ comments: getClient().getComments({
+ post_id: Number(params.id),
+ type_: 'All',
+ limit: 250,
+ page: 1,
+ max_depth: 6,
+ saved_only: false,
+ sort: 'Hot',
+ auth: get(authData)?.token,
+ }),
+ },
}
}
diff --git a/src/routes/post/[id]/Comment.svelte b/src/routes/post/[id]/Comment.svelte
deleted file mode 100644
index c5e138ef..00000000
--- a/src/routes/post/[id]/Comment.svelte
+++ /dev/null
@@ -1,77 +0,0 @@
-
-
-
-
-
-
- {node.comment_view.creator.name}
-
- {node.comment_view.counts.score} point{node.comment_view.counts.score ==
- 1
- ? ''
- : 's'}
-
-
-
-
-
- {#if open}
-
-
-
- Collapse
- {:else}
-
- Expand
- {#if node.children.length > 0}
- +{node.children.length}
- {/if}
- {/if}
-
-
-
-
-
-
-
-
-
-
- {node.comment_view.counts.score}
-
-
-
-
-
-
-
-
diff --git a/src/routes/post/[id]/Post.svelte b/src/routes/post/[id]/Post.svelte
index d2dedf6c..1c928b99 100644
--- a/src/routes/post/[id]/Post.svelte
+++ b/src/routes/post/[id]/Post.svelte
@@ -15,13 +15,43 @@
ChevronUp,
Icon,
} from 'svelte-hero-icons'
- import type { CommentNodeI } from './comments.js'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
+ import { authData, getClient } from '$lib/lemmy.js'
let postRes: PostView
export { postRes as post }
+
+ let vote = postRes.my_vote
+
+ async function upvote() {
+ if (!$authData) return
+
+ const upvoted = postRes.my_vote == 1
+
+ await getClient()
+ .likePost({
+ score: upvoted ? 0 : 1,
+ auth: $authData.token,
+ post_id: postRes.post.id,
+ })
+ .catch((_) => undefined)
+ }
+
+ async function downvote() {
+ if (!$authData) return
+
+ const upvoted = postRes.my_vote == -1
+
+ await getClient()
+ .likePost({
+ score: upvoted ? 0 : -1,
+ auth: $authData.token,
+ post_id: postRes.post.id,
+ })
+ .catch((_) => undefined)
+ }
{/if}
-
+
-
diff --git a/src/routes/post/[id]/comment/Comment.svelte b/src/routes/post/[id]/comment/Comment.svelte
new file mode 100644
index 00000000..8c29e213
--- /dev/null
+++ b/src/routes/post/[id]/comment/Comment.svelte
@@ -0,0 +1,150 @@
+
+
+
+
+
+
+ {node.comment_view.creator.name}
+
+ {node.comment_view.counts.score} point{node.comment_view.counts.score ==
+ 1
+ ? ''
+ : 's'}
+
+
+
+
+
+ {#if open}
+
+
+
+ Collapse
+ {:else}
+
+ Expand
+ {#if node.children.length > 0}
+ +{node.children.length}
+ {/if}
+ {/if}
+
+
+
+
+
+
+
+
+
+
+
+ {node.comment_view.counts.score}
+
+
+
+
(replying = !replying)}
+ >
+
+ Reply
+
+
+
+
+ {#if replying}
+
+
Reply
+ {
+ node.children.unshift(e.detail)
+ replying = false
+ }}
+ />
+
+ {/if}
+
+
+
diff --git a/src/routes/post/[id]/comment/CommentForm.svelte b/src/routes/post/[id]/comment/CommentForm.svelte
new file mode 100644
index 00000000..688562e1
--- /dev/null
+++ b/src/routes/post/[id]/comment/CommentForm.svelte
@@ -0,0 +1,58 @@
+
+
+
diff --git a/src/routes/post/[id]/Comments.svelte b/src/routes/post/[id]/comment/Comments.svelte
similarity index 62%
rename from src/routes/post/[id]/Comments.svelte
rename to src/routes/post/[id]/comment/Comments.svelte
index 6552e4cc..852d6c7a 100644
--- a/src/routes/post/[id]/Comments.svelte
+++ b/src/routes/post/[id]/comment/Comments.svelte
@@ -1,11 +1,13 @@
{#each nodes.slice(0, maxComments) as node}
-
+
{#if node.children?.length > 0}
-
+
{/if}
{/each}