misc: add account logging in
This commit is contained in:
1 parent
7d3f29d9eb
commit
f71ad4be8e
21 files changed
+686
-151
No files matched your search
+15
-7
@@ -1,4 +1,3 @@
|
|||||||
import { DEFAULT_INSTANCE_URL } from '$lib/lemmy.js'
|
|
||||||
import { error } from '@sveltejs/kit'
|
import { error } from '@sveltejs/kit'
|
||||||
|
|
||||||
export async function handle({ event, resolve }) {
|
export async function handle({ event, resolve }) {
|
||||||
@@ -14,21 +13,30 @@ export async function handle({ event, resolve }) {
|
|||||||
event.request.headers.delete('host')
|
event.request.headers.delete('host')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`https://${instance}/${url.toString()}`)
|
|
||||||
const data = await fetch(`https://${instance}/${url.toString()}`, {
|
const data = await fetch(`https://${instance}/${url.toString()}`, {
|
||||||
method: event.request.method,
|
method: event.request.method,
|
||||||
body: event.request.body,
|
body: event.request.body,
|
||||||
headers: event.request.headers,
|
headers: event.request.headers,
|
||||||
|
// @ts-ignore this works, idk why typescript complains
|
||||||
|
duplex: 'half',
|
||||||
signal: AbortSignal.timeout(20 * 1000),
|
signal: AbortSignal.timeout(20 * 1000),
|
||||||
})
|
})
|
||||||
|
|
||||||
return new Response(JSON.stringify(await data.json()))
|
const json = await data.json()
|
||||||
} catch (err) {
|
|
||||||
console.error(err)
|
return new Response(JSON.stringify(json))
|
||||||
throw error(500, { message: 'The proxy failed to fetch from the server' })
|
} 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)
|
const response = await resolve(event)
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,31 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { createEventDispatcher } from 'svelte'
|
import { createEventDispatcher } from 'svelte'
|
||||||
import { Color } from '$lib/ui/colors.js'
|
import { Color } from '$lib/ui/colors.js'
|
||||||
|
import Loading from '$lib/components/ui/Loading.svelte'
|
||||||
|
|
||||||
export let color: Color = Color.secondary
|
export let color: Color = Color.secondary
|
||||||
export let loading = false
|
export let loading = false
|
||||||
|
export let large = false
|
||||||
|
export let disabled = false
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
on:click={(e) => dispatch('click', e)}
|
{disabled}
|
||||||
class="px-3 py-1.5 rounded-md {color} text-sm transition-all"
|
on:click={(e) => dispatch('click', e)}
|
||||||
|
class="{large
|
||||||
|
? 'w-full px-4 py-2'
|
||||||
|
: 'px-3 py-1.5'} rounded-md {color} text-sm transition-all {disabled
|
||||||
|
? 'opacity-50 pointer-events-none'
|
||||||
|
: ''}"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="flex flex-row items-center justify-center gap-2"
|
class="flex flex-row items-center justify-center gap-2"
|
||||||
class:opacity-0={loading}
|
class:hidden={loading}
|
||||||
>
|
>
|
||||||
<slot name="icon" />
|
<slot name="icon" />
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
<div class="mx-auto my-auto" class:hidden={!loading}>loading...</div>
|
<div class="mx-auto py-2" class:hidden={!loading}><Loading /></div>
|
||||||
</button>
|
</button>
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { createEventDispatcher } from 'svelte'
|
||||||
|
|
||||||
|
export let label: string = ''
|
||||||
|
export let type: 'text' | 'password' | 'email' | null | undefined = 'text'
|
||||||
|
export let placeholder: string = ''
|
||||||
|
export let value: string = ''
|
||||||
|
export let maxlength: number = -1
|
||||||
|
|
||||||
|
function typeAction(node: Node) {
|
||||||
|
// @ts-ignore
|
||||||
|
node.type = type
|
||||||
|
}
|
||||||
|
|
||||||
|
let clazz = ''
|
||||||
|
export { clazz as class }
|
||||||
|
|
||||||
|
const dispatcher = createEventDispatcher()
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<label class="flex flex-col items-center {clazz}">
|
||||||
|
{#if label != ''}
|
||||||
|
<span
|
||||||
|
class="font-bold text-sm text-left mb-1 cursor-pointer w-max self-start"
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</span>
|
||||||
|
{/if}
|
||||||
|
<input
|
||||||
|
use:typeAction
|
||||||
|
bind:value
|
||||||
|
on:input={(e) => dispatcher('input', e)}
|
||||||
|
{placeholder}
|
||||||
|
{maxlength}
|
||||||
|
class="w-full px-3 text-sm py-2.5 bg-transparent border border-black/20
|
||||||
|
dark:border-white/20 dark:focus:border-white focus:outline-none
|
||||||
|
focus:border-black transition-colors rounded-md {clazz}"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
let clazz = ''
|
||||||
|
export { clazz as class }
|
||||||
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
class="sticky top-4 bg-white border border-slate-200
|
class="bg-white border border-slate-200
|
||||||
dark:border-zinc-800 dark:bg-zinc-900 break-words hidden lg:flex
|
dark:border-zinc-800 dark:bg-zinc-900 break-words
|
||||||
flex-col gap-4 p-8 max-w-sm rounded-md h-max max-h-[calc(100vh-2rem)] overflow-auto"
|
flex-col gap-4 p-4 max-w-lg rounded-md {clazz}"
|
||||||
>
|
>
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<div class="loading">
|
||||||
|
<div class="dot dot1" />
|
||||||
|
<div class="dot dot2" />
|
||||||
|
<div class="dot dot3" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.loading {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dot {
|
||||||
|
width: 5px;
|
||||||
|
height: 5px;
|
||||||
|
border-radius: 50%;
|
||||||
|
background-color: #555;
|
||||||
|
margin: 0px 1px;
|
||||||
|
animation: blink 750ms infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dot1 {
|
||||||
|
animation-delay: 0s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dot2 {
|
||||||
|
animation-delay: 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.dot3 {
|
||||||
|
animation-delay: 0.4s;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes blink {
|
||||||
|
0% {
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
20% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
100% {
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,7 +1,15 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import Link from '$lib/components/input/Link.svelte'
|
import Link from '$lib/components/input/Link.svelte'
|
||||||
import Logo from '$lib/components/ui/Logo.svelte'
|
import Logo from '$lib/components/ui/Logo.svelte'
|
||||||
import { instance_url } from '$lib/lemmy.js'
|
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||||
|
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||||
|
import { instance_url, user } from '$lib/lemmy.js'
|
||||||
|
import {
|
||||||
|
ArrowLeftOnRectangle,
|
||||||
|
ArrowRightOnRectangle,
|
||||||
|
Cog6Tooth,
|
||||||
|
Icon,
|
||||||
|
} from 'svelte-hero-icons'
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav
|
<nav
|
||||||
@@ -14,19 +22,37 @@
|
|||||||
<div class="flex flex-col">
|
<div class="flex flex-col">
|
||||||
<span class="font-bold">Xylemmy</span>
|
<span class="font-bold">Xylemmy</span>
|
||||||
{#if $instance_url}
|
{#if $instance_url}
|
||||||
<a
|
<span
|
||||||
class="text-xs opacity-50 hover:opacity-100 hover:underline
|
class="text-xs opacity-50
|
||||||
transition-opacity"
|
transition-opacity"
|
||||||
href="/instance"
|
|
||||||
>
|
>
|
||||||
{new URL(`https://${$instance_url}`).hostname}
|
{new URL(`https://${$instance_url}`).hostname}
|
||||||
</a>
|
</span>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<Link href="/communities">Communities</Link>
|
<Link href="/communities">Communities</Link>
|
||||||
<div
|
<Menu>
|
||||||
class="w-8 h-8 rounded-full ring-1 ring-slate-300 bg-slate-100 dark:bg-zinc-800"
|
<button
|
||||||
/>
|
class="w-8 h-8 rounded-full ring-1 ring-slate-300 bg-slate-100 dark:bg-zinc-800"
|
||||||
|
aria-label="Profile"
|
||||||
|
slot="button"
|
||||||
|
>
|
||||||
|
{#if $user}
|
||||||
|
<img
|
||||||
|
src={$user.person_view.person.avatar ??
|
||||||
|
'https://xylo.xylight.dev/img/logo-background.svg'}
|
||||||
|
alt=""
|
||||||
|
width={32}
|
||||||
|
height={32}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
</button>
|
||||||
|
<span class="text-xs opacity-80 text-left mx-4 my-2">Profile</span>
|
||||||
|
<MenuButton link href="/login">
|
||||||
|
<Icon src={ArrowLeftOnRectangle} mini width={16} /> Log in
|
||||||
|
</MenuButton>
|
||||||
|
<MenuButton><Icon src={Cog6Tooth} mini width={16} /> Settings</MenuButton>
|
||||||
|
</Menu>
|
||||||
</nav>
|
</nav>
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<div
|
||||||
|
class="sticky top-4 bg-white border border-slate-200
|
||||||
|
dark:border-zinc-800 dark:bg-zinc-900 break-words hidden lg:flex
|
||||||
|
flex-col gap-4 p-8 max-w-sm rounded-md h-max max-h-[calc(100vh-2rem)] overflow-auto"
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { expoOut } from 'svelte/easing'
|
||||||
|
import { scale } from 'svelte/transition'
|
||||||
|
|
||||||
|
export let open = false
|
||||||
|
|
||||||
|
let clazz = ''
|
||||||
|
export { clazz as class }
|
||||||
|
|
||||||
|
export let itemsClass = ''
|
||||||
|
|
||||||
|
export let absolute = false
|
||||||
|
export let rtl = true
|
||||||
|
export let top = false
|
||||||
|
|
||||||
|
let element: any
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:body
|
||||||
|
on:click={(event) => {
|
||||||
|
let path = event.composedPath()
|
||||||
|
|
||||||
|
if (!path.includes(element)) {
|
||||||
|
if (open) {
|
||||||
|
open = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
class="overflow-visible z-30 {absolute
|
||||||
|
? 'absolute'
|
||||||
|
: 'relative'} cursor-auto {clazz}"
|
||||||
|
on:click={() => (open = !open)}
|
||||||
|
bind:this={element}
|
||||||
|
>
|
||||||
|
<slot name="button" />
|
||||||
|
{#if open}
|
||||||
|
<div
|
||||||
|
transition:scale|local={{
|
||||||
|
duration: 200,
|
||||||
|
start: 0.95,
|
||||||
|
easing: expoOut,
|
||||||
|
}}
|
||||||
|
class="absolute {rtl
|
||||||
|
? 'origin-top-right right-0'
|
||||||
|
: 'origin-top-left left-0'}
|
||||||
|
{top ? 'bottom-[100%] origin-bottom-left' : 'top-[100%]'}
|
||||||
|
rounded-md py-2 min-w-[16rem] my-2 flex flex-col shadow-lg {itemsClass
|
||||||
|
? itemsClass
|
||||||
|
: 'bg-white dark:bg-zinc-900'}"
|
||||||
|
>
|
||||||
|
{#if open}
|
||||||
|
<slot />
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</button>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Color } from '$lib/ui/colors'
|
||||||
|
import { createEventDispatcher } from 'svelte'
|
||||||
|
|
||||||
|
export let color: Color = Color.secondary
|
||||||
|
export let link = false
|
||||||
|
export let href = ''
|
||||||
|
|
||||||
|
const click = createEventDispatcher()
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if link}
|
||||||
|
<a
|
||||||
|
{href}
|
||||||
|
class="flex flex-row gap-2 items-center px-4 py-2 w-full text-sm transition-colors {color}"
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</a>
|
||||||
|
{:else}
|
||||||
|
<button
|
||||||
|
on:click={(e) => click('click', e)}
|
||||||
|
type="button"
|
||||||
|
class="flex flex-row gap-2 items-center px-4 py-2 w-full text-sm transition-colors {color}"
|
||||||
|
>
|
||||||
|
<slot />
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
+43
-5
@@ -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 { writable } from 'svelte/store'
|
||||||
import { PUBLIC_PROXY_URL } from '$env/static/public'
|
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 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}`)
|
return new LemmyHttp(`${PUBLIC_PROXY_URL}/cors/${instance}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const token = writable<LoginResponse>()
|
export interface AuthData {
|
||||||
|
token: string
|
||||||
|
username: string
|
||||||
|
instance: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const authData = writable<AuthData | undefined>()
|
||||||
|
export const user = writable<GetPersonDetailsResponse>()
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/stores'
|
import { page } from '$app/stores'
|
||||||
import Button from '$lib/components/input/Button.svelte'
|
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 Link from '$lib/components/input/Link.svelte'
|
||||||
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
||||||
import {
|
import {
|
||||||
@@ -90,7 +90,7 @@
|
|||||||
rounded-md"
|
rounded-md"
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
class="w-36 h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
|
class="w-36 mt-4 h-4 bg-slate-200 dark:bg-zinc-700 animate-pulse
|
||||||
rounded-md"
|
rounded-md"
|
||||||
/>
|
/>
|
||||||
<div
|
<div
|
||||||
|
|||||||
+4
-2
@@ -1,7 +1,8 @@
|
|||||||
import { getClient } from '$lib/lemmy.js'
|
import { authData, getClient } from '$lib/lemmy.js'
|
||||||
import type { ListingType, SortType } from 'lemmy-js-client'
|
import type { ListingType, SortType } from 'lemmy-js-client'
|
||||||
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
export function load({ url }) {
|
export async function load({ url }) {
|
||||||
const page = Number(url.searchParams.get('page') || 1) || 1
|
const page = Number(url.searchParams.get('page') || 1) || 1
|
||||||
|
|
||||||
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'Active'
|
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'Active'
|
||||||
@@ -14,6 +15,7 @@ export function load({ url }) {
|
|||||||
page: page,
|
page: page,
|
||||||
sort: sort,
|
sort: sort,
|
||||||
type_: listingType,
|
type_: listingType,
|
||||||
|
auth: get(authData)?.token,
|
||||||
}),
|
}),
|
||||||
streamed: {
|
streamed: {
|
||||||
site: getClient().getSite({}),
|
site: getClient().getSite({}),
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
UserGroup,
|
UserGroup,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
||||||
import Card from '$lib/components/ui/Card.svelte'
|
import Card from '$lib/components/ui/StickyCard.svelte'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import Button from '$lib/components/input/Button.svelte'
|
||||||
|
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||||
|
import Card from '$lib/components/ui/Card.svelte'
|
||||||
|
import { DEFAULT_INSTANCE_URL, authData, getClient } from '$lib/lemmy.js'
|
||||||
|
import { Color } from '$lib/ui/colors.js'
|
||||||
|
|
||||||
|
let data = {
|
||||||
|
username: '',
|
||||||
|
password: '',
|
||||||
|
loading: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
async function logIn() {
|
||||||
|
data.loading = true
|
||||||
|
try {
|
||||||
|
const response = await getClient().login({
|
||||||
|
username_or_email: data.username,
|
||||||
|
password: data.password,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (response.jwt) {
|
||||||
|
authData.set({
|
||||||
|
instance: DEFAULT_INSTANCE_URL,
|
||||||
|
token: response.jwt,
|
||||||
|
username: data.username,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
alert('failed')
|
||||||
|
}
|
||||||
|
data.loading = false
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
<TextInput
|
||||||
|
bind:value={data.username}
|
||||||
|
label="Username"
|
||||||
|
placeholder="Example"
|
||||||
|
/>
|
||||||
|
<TextInput bind:value={data.password} label="Password" type="password" />
|
||||||
|
<Button
|
||||||
|
loading={data.loading}
|
||||||
|
disabled={data.loading}
|
||||||
|
large
|
||||||
|
color={Color.accent}
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</Card>
|
||||||
@@ -7,18 +7,18 @@
|
|||||||
CommentView,
|
CommentView,
|
||||||
} from 'lemmy-js-client'
|
} from 'lemmy-js-client'
|
||||||
import type { CommentNodeI } from './comments.js'
|
import type { CommentNodeI } from './comments.js'
|
||||||
import Comments from './Comments.svelte'
|
import Comments from './comment/Comments.svelte'
|
||||||
import SvelteMarkdown from 'svelte-markdown'
|
import SvelteMarkdown from 'svelte-markdown'
|
||||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||||
import { isImage } from '$lib/ui/image.js'
|
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 postData = data.post.post_view
|
||||||
const post = postData.post
|
const post = postData.post
|
||||||
|
|
||||||
const comments = data.comments.comments
|
|
||||||
|
|
||||||
function getCommentParentId(comment?: Comment): number | undefined {
|
function getCommentParentId(comment?: Comment): number | undefined {
|
||||||
const split = comment?.path.split('.')
|
const split = comment?.path.split('.')
|
||||||
// remove the 0
|
// remove the 0
|
||||||
@@ -124,10 +124,39 @@
|
|||||||
{postData.counts.comments}
|
{postData.counts.comments}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<MultiSelect
|
{#await data.streamed.comments}
|
||||||
options={['hot', 'new', 'top']}
|
<div class="flex flex-col gap-8">
|
||||||
optionNames={['Hot', 'New', 'Top']}
|
{#each new Array(10) as comment}
|
||||||
selected="hot"
|
<div class="flex flex-col gap-2">
|
||||||
/>
|
<div
|
||||||
<Comments nodes={buildCommentsTree(comments, false)} isParent={true} />
|
class="dark:bg-zinc-700 bg-slate-200 animate-pulse w-64 h-3 rounded-md"
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
class="dark:bg-zinc-700 bg-slate-200 animate-pulse w-128 h-4 rounded-md"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
{:then comments}
|
||||||
|
{#if $user}
|
||||||
|
<CommentForm
|
||||||
|
postId={post.id}
|
||||||
|
on:comment={(comment) => comments.comments.unshift(comment.detail)}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
<MultiSelect
|
||||||
|
options={['hot', 'new', 'top']}
|
||||||
|
optionNames={['Hot', 'New', 'Top']}
|
||||||
|
selected="hot"
|
||||||
|
/>
|
||||||
|
<Comments
|
||||||
|
{post}
|
||||||
|
nodes={buildCommentsTree(comments.comments, false)}
|
||||||
|
isParent={true}
|
||||||
|
/>
|
||||||
|
{:catch}
|
||||||
|
<div class="bg-red-500/10 border border-red-500 rounded-md p-4">
|
||||||
|
Failed to load posts
|
||||||
|
</div>
|
||||||
|
{/await}
|
||||||
</div>
|
</div>
|
||||||
@@ -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 }) {
|
export async function load({ params }) {
|
||||||
return {
|
return {
|
||||||
post: await getClient().getPost({
|
post: getClient().getPost({
|
||||||
id: Number(params.id),
|
id: Number(params.id),
|
||||||
|
auth: get(authData)?.token,
|
||||||
}),
|
}),
|
||||||
comments: await getClient().getComments({
|
streamed: {
|
||||||
post_id: Number(params.id),
|
comments: getClient().getComments({
|
||||||
type_: 'All',
|
post_id: Number(params.id),
|
||||||
limit: 250,
|
type_: 'All',
|
||||||
page: 1,
|
limit: 250,
|
||||||
max_depth: 6,
|
page: 1,
|
||||||
saved_only: false,
|
max_depth: 6,
|
||||||
sort: 'Hot',
|
saved_only: false,
|
||||||
}),
|
sort: 'Hot',
|
||||||
|
auth: get(authData)?.token,
|
||||||
|
}),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
<script lang="ts">
|
|
||||||
import { ArrowDown, ArrowUp, Icon, Minus, Plus } from 'svelte-hero-icons'
|
|
||||||
import Avatar from './Avatar.svelte'
|
|
||||||
import type { CommentNodeI } from './comments.js'
|
|
||||||
import SvelteMarkdown from 'svelte-markdown'
|
|
||||||
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
|
|
||||||
import Button from '$lib/components/input/Button.svelte'
|
|
||||||
import { Color } from '$lib/ui/colors.js'
|
|
||||||
|
|
||||||
export let node: CommentNodeI
|
|
||||||
export let open = true
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<li class="py-3">
|
|
||||||
<details bind:open class="flex flex-col gap-1">
|
|
||||||
<summary
|
|
||||||
class="flex flex-row cursor-pointer arrow gap-2 items-center group"
|
|
||||||
>
|
|
||||||
<Avatar
|
|
||||||
url={node.comment_view.creator.avatar ??
|
|
||||||
`https://api.dicebear.com/6.x/initials/svg?seed=${node.comment_view.creator.name}`}
|
|
||||||
width={24}
|
|
||||||
/>
|
|
||||||
<span class="text-sm">{node.comment_view.creator.name}</span>
|
|
||||||
<span class="text-sm opacity-60">
|
|
||||||
{node.comment_view.counts.score} point{node.comment_view.counts.score ==
|
|
||||||
1
|
|
||||||
? ''
|
|
||||||
: 's'}
|
|
||||||
</span>
|
|
||||||
<span class="text-sm opacity-60">
|
|
||||||
<RelativeDate date={new Date(node.comment_view.comment.published)} />
|
|
||||||
</span>
|
|
||||||
<span
|
|
||||||
class="ml-auto translate-x-1 opacity-0
|
|
||||||
group-hover:translate-x-0 group-hover:opacity-100 text-xs transition-all
|
|
||||||
flex flex-row items-center gap-1 hover:dark:bg-white/10 p-1 rounded-md
|
|
||||||
hover:bg-black/10 px-2"
|
|
||||||
>
|
|
||||||
{#if open}
|
|
||||||
<div class="inline">
|
|
||||||
<Icon src={Minus} width={16} height={16} mini />
|
|
||||||
</div>
|
|
||||||
Collapse
|
|
||||||
{:else}
|
|
||||||
<Icon src={Plus} width={16} height={16} mini />
|
|
||||||
Expand
|
|
||||||
{#if node.children.length > 0}
|
|
||||||
<span class="text-xs opacity-50">+{node.children.length}</span>
|
|
||||||
{/if}
|
|
||||||
{/if}
|
|
||||||
</span>
|
|
||||||
</summary>
|
|
||||||
<div class="mt-1">
|
|
||||||
<div
|
|
||||||
class="flex flex-col gap-1 whitespace-pre-wrap
|
|
||||||
max-w-full"
|
|
||||||
>
|
|
||||||
<div class="max-w-full break-words">
|
|
||||||
<SvelteMarkdown source={node.comment_view.comment.content} />
|
|
||||||
</div>
|
|
||||||
<div class="flex flex-row gap-2">
|
|
||||||
<button>
|
|
||||||
<Icon src={ArrowUp} width={16} mini />
|
|
||||||
</button>
|
|
||||||
<span class="text-sm font-medium">
|
|
||||||
{node.comment_view.counts.score}
|
|
||||||
</span>
|
|
||||||
<button>
|
|
||||||
<Icon src={ArrowDown} width={16} mini />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<slot />
|
|
||||||
</details>
|
|
||||||
</li>
|
|
||||||
@@ -15,13 +15,43 @@
|
|||||||
ChevronUp,
|
ChevronUp,
|
||||||
Icon,
|
Icon,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
import type { CommentNodeI } from './comments.js'
|
|
||||||
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
|
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
|
||||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||||
import { isImage } from '$lib/ui/image.js'
|
import { isImage } from '$lib/ui/image.js'
|
||||||
|
import { authData, getClient } from '$lib/lemmy.js'
|
||||||
|
|
||||||
let postRes: PostView
|
let postRes: PostView
|
||||||
export { postRes as post }
|
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)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
@@ -65,13 +95,26 @@
|
|||||||
</p>
|
</p>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
<div class="flex flex-row gap-2 p-2">
|
<div class="flex flex-row gap-2 pb-4 px-4">
|
||||||
<div
|
<div
|
||||||
class="flex flex-row items-center gap-1 rounded-md
|
class="flex flex-row items-center gap-1 rounded-md transition-colors cursor-pointer
|
||||||
bg-slate-100 dark:bg-zinc-800 hover:bg-slate-200
|
{// upvote
|
||||||
dark:hover:bg-zinc-700 transition-colors cursor-pointer"
|
vote == 1
|
||||||
|
? 'bg-orange-600/20 text-orange-500'
|
||||||
|
: // downvote
|
||||||
|
vote == -1
|
||||||
|
? 'bg-blue-600/40 text-blue-400'
|
||||||
|
: // no vote
|
||||||
|
'bg-slate-100 dark:bg-zinc-800 hover:bg-slate-200 dark:hover:bg-zinc-700'}"
|
||||||
>
|
>
|
||||||
<button aria-label="Upvote" class="p-1 px-1.5">
|
<button
|
||||||
|
aria-label="Upvote"
|
||||||
|
class="p-1 px-1.5"
|
||||||
|
on:click={() => {
|
||||||
|
vote = vote == 1 ? 0 : 1
|
||||||
|
upvote()
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Icon src={ChevronUp} mini width={20} height={20} />
|
<Icon src={ChevronUp} mini width={20} height={20} />
|
||||||
</button>
|
</button>
|
||||||
<span class="text-sm">
|
<span class="text-sm">
|
||||||
@@ -79,7 +122,14 @@
|
|||||||
postRes.counts.score
|
postRes.counts.score
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
<button aria-label="Downvote" class="p-1 px-1.5">
|
<button
|
||||||
|
aria-label="Downvote"
|
||||||
|
class="p-1 px-1.5"
|
||||||
|
on:click={() => {
|
||||||
|
vote = vote == -1 ? 0 : -1
|
||||||
|
downvote()
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Icon src={ChevronDown} mini width={20} height={20} />
|
<Icon src={ChevronDown} mini width={20} height={20} />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,150 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import {
|
||||||
|
ArrowDown,
|
||||||
|
ArrowUp,
|
||||||
|
ChatBubbleOvalLeft,
|
||||||
|
Icon,
|
||||||
|
Minus,
|
||||||
|
Plus,
|
||||||
|
} from 'svelte-hero-icons'
|
||||||
|
import Avatar from '../Avatar.svelte'
|
||||||
|
import type { CommentNodeI } from '../comments.js'
|
||||||
|
import SvelteMarkdown from 'svelte-markdown'
|
||||||
|
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
|
||||||
|
import { getClient, authData } from '$lib/lemmy.js'
|
||||||
|
import { Color } from '$lib/ui/colors.js'
|
||||||
|
import CommentForm from './CommentForm.svelte'
|
||||||
|
|
||||||
|
export let node: CommentNodeI
|
||||||
|
|
||||||
|
export let postId: number
|
||||||
|
|
||||||
|
export let open = true
|
||||||
|
export let replying = false
|
||||||
|
|
||||||
|
async function upvote() {
|
||||||
|
if (!$authData) return
|
||||||
|
|
||||||
|
const upvoted = node.comment_view.my_vote == 1
|
||||||
|
|
||||||
|
await getClient()
|
||||||
|
.likeComment({
|
||||||
|
score: upvoted ? 0 : 1,
|
||||||
|
auth: $authData.token,
|
||||||
|
comment_id: node.comment_view.comment.id,
|
||||||
|
})
|
||||||
|
.catch((_) => undefined)
|
||||||
|
|
||||||
|
node.comment_view.my_vote = Number(!upvoted)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function downvote() {
|
||||||
|
if (!$authData) return
|
||||||
|
|
||||||
|
const upvoted = node.comment_view.my_vote == -1
|
||||||
|
|
||||||
|
await getClient()
|
||||||
|
.likeComment({
|
||||||
|
score: upvoted ? 0 : -1,
|
||||||
|
auth: $authData.token,
|
||||||
|
comment_id: node.comment_view.comment.id,
|
||||||
|
})
|
||||||
|
.catch((_) => undefined)
|
||||||
|
|
||||||
|
node.comment_view.my_vote = -Number(!upvoted)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<li class="py-3">
|
||||||
|
<details bind:open class="flex flex-col gap-1">
|
||||||
|
<summary
|
||||||
|
class="flex flex-row cursor-pointer arrow gap-2 items-center group"
|
||||||
|
>
|
||||||
|
<Avatar
|
||||||
|
url={node.comment_view.creator.avatar ??
|
||||||
|
`https://api.dicebear.com/6.x/initials/svg?seed=${node.comment_view.creator.name}`}
|
||||||
|
width={24}
|
||||||
|
/>
|
||||||
|
<span class="text-sm">{node.comment_view.creator.name}</span>
|
||||||
|
<span class="text-sm opacity-60">
|
||||||
|
{node.comment_view.counts.score} point{node.comment_view.counts.score ==
|
||||||
|
1
|
||||||
|
? ''
|
||||||
|
: 's'}
|
||||||
|
</span>
|
||||||
|
<span class="text-sm opacity-60">
|
||||||
|
<RelativeDate date={new Date(node.comment_view.comment.published)} />
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
class="ml-auto translate-x-1 opacity-0
|
||||||
|
group-hover:translate-x-0 group-hover:opacity-100 text-xs transition-all
|
||||||
|
flex flex-row items-center gap-1 hover:dark:bg-white/10 p-1 rounded-md
|
||||||
|
hover:bg-black/10 px-2"
|
||||||
|
>
|
||||||
|
{#if open}
|
||||||
|
<div class="inline">
|
||||||
|
<Icon src={Minus} width={16} height={16} mini />
|
||||||
|
</div>
|
||||||
|
Collapse
|
||||||
|
{:else}
|
||||||
|
<Icon src={Plus} width={16} height={16} mini />
|
||||||
|
Expand
|
||||||
|
{#if node.children.length > 0}
|
||||||
|
<span class="text-xs opacity-50">+{node.children.length}</span>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
</span>
|
||||||
|
</summary>
|
||||||
|
<div class="mt-1">
|
||||||
|
<div
|
||||||
|
class="flex flex-col gap-1 whitespace-pre-wrap
|
||||||
|
max-w-full"
|
||||||
|
>
|
||||||
|
<div class="max-w-full break-words">
|
||||||
|
<SvelteMarkdown source={node.comment_view.comment.content} />
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-row gap-2 items-center">
|
||||||
|
<div class="flex flex-row gap-2 items-center">
|
||||||
|
<button
|
||||||
|
on:click={upvote}
|
||||||
|
class={node.comment_view.my_vote == 1 ? 'text-orange-500' : ''}
|
||||||
|
>
|
||||||
|
<Icon src={ArrowUp} width={16} mini />
|
||||||
|
</button>
|
||||||
|
<span class="text-sm font-medium">
|
||||||
|
{node.comment_view.counts.score}
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
on:click={downvote}
|
||||||
|
class={node.comment_view.my_vote == -1 ? 'text-blue-500' : ''}
|
||||||
|
>
|
||||||
|
<Icon src={ArrowDown} width={16} mini />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
class="flex flex-row gap-1 items-center transition-colors px-2 py-1
|
||||||
|
rounded-md {Color.secondary}"
|
||||||
|
on:click={() => (replying = !replying)}
|
||||||
|
>
|
||||||
|
<Icon src={ChatBubbleOvalLeft} width={16} height={16} mini />
|
||||||
|
<span class="text-xs">Reply</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{#if replying}
|
||||||
|
<div class="max-w-sm my-2">
|
||||||
|
<h1 class="font-bold text-sm mb-2">Reply</h1>
|
||||||
|
<CommentForm
|
||||||
|
{postId}
|
||||||
|
parentId={node.comment_view.comment.id}
|
||||||
|
on:comment={(e) => {
|
||||||
|
node.children.unshift(e.detail)
|
||||||
|
replying = false
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
<slot />
|
||||||
|
</details>
|
||||||
|
</li>
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import Button from '$lib/components/input/Button.svelte'
|
||||||
|
import { Color } from '$lib/ui/colors.js'
|
||||||
|
import type { Comment, Post } from 'lemmy-js-client'
|
||||||
|
import { authData, getClient, user } from '$lib/lemmy.js'
|
||||||
|
import { createEventDispatcher } from 'svelte'
|
||||||
|
|
||||||
|
export let postId: number
|
||||||
|
export let parentId: number | undefined = undefined
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher()
|
||||||
|
|
||||||
|
let newComment = ''
|
||||||
|
let loading = false
|
||||||
|
|
||||||
|
async function submit() {
|
||||||
|
if (!$user || !$authData || newComment == '') return
|
||||||
|
|
||||||
|
loading = true
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await getClient().createComment({
|
||||||
|
auth: $authData.token,
|
||||||
|
content: newComment,
|
||||||
|
post_id: postId,
|
||||||
|
parent_id: parentId,
|
||||||
|
})
|
||||||
|
dispatch('comment', response)
|
||||||
|
|
||||||
|
newComment = ''
|
||||||
|
} catch (err) {
|
||||||
|
alert('Failed to comment')
|
||||||
|
}
|
||||||
|
|
||||||
|
loading = false
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="max-w-lg flex flex-col gap-2">
|
||||||
|
<textarea
|
||||||
|
rows="3"
|
||||||
|
class="w-full bg-slate-100 dark:bg-zinc-900 border border-slate-200
|
||||||
|
dark:border-zinc-800 p-4 outline-none focus:border-black
|
||||||
|
focus:dark:border-white transition-colors rounded-md"
|
||||||
|
bind:value={newComment}
|
||||||
|
/>
|
||||||
|
<div class="w-24 ml-auto">
|
||||||
|
<Button
|
||||||
|
large
|
||||||
|
on:click={submit}
|
||||||
|
color={Color.accent}
|
||||||
|
{loading}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import Comment from './Comment.svelte'
|
import Comment from './Comment.svelte'
|
||||||
import type { CommentNodeI } from './comments.js'
|
import type { CommentNodeI } from '../comments.js'
|
||||||
|
import type { Post } from 'lemmy-js-client'
|
||||||
|
|
||||||
const maxComments = 250
|
const maxComments = 250
|
||||||
|
|
||||||
export let nodes: CommentNodeI[]
|
export let nodes: CommentNodeI[]
|
||||||
export let isParent: boolean
|
export let isParent: boolean
|
||||||
|
export let post: Post
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ul
|
<ul
|
||||||
@@ -14,9 +16,9 @@
|
|||||||
: 'ml-2.5 mt-2 px-2.5 border-l-2 border-black/10 dark:border-white/10'}
|
: 'ml-2.5 mt-2 px-2.5 border-l-2 border-black/10 dark:border-white/10'}
|
||||||
>
|
>
|
||||||
{#each nodes.slice(0, maxComments) as node}
|
{#each nodes.slice(0, maxComments) as node}
|
||||||
<Comment {node} open={true}>
|
<Comment postId={post.id} {node} open={true}>
|
||||||
{#if node.children?.length > 0}
|
{#if node.children?.length > 0}
|
||||||
<svelte:self nodes={node.children} isParent={false} />
|
<svelte:self {post} nodes={node.children} isParent={false} />
|
||||||
{/if}
|
{/if}
|
||||||
</Comment>
|
</Comment>
|
||||||
{/each}
|
{/each}
|
||||||
Reference in new issue
Block a user