feat: Add instance changing

This commit is contained in:
Xyphyn committed 2023-07-02 14:27:04 -07:00
1 parent f201123291
commit 3159583654
17 files changed
+142 -35

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
import { instance_url } from '$lib/lemmy.js'
export function load({ cookies, locals }) {
return {
instance_url: cookies.get('instance_url') || instance_url,
}
}
+3 -1
View File
@@ -19,9 +19,11 @@
nProgress.done()
}
}
export let data
</script>
<Navbar />
<Navbar instance_url={data.instance_url} />
<div class="mx-auto max-w-5xl p-4">
<slot />
</div>
+6 -7
View File
@@ -1,15 +1,14 @@
import { lemmy } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
export function load(req) {
const page = Number(req.url.searchParams.get('page') || 1) || 1
export function load({ url, cookies }) {
const page = Number(url.searchParams.get('page') || 1) || 1
const sort: SortType =
(req.url.searchParams.get('sort') as SortType) || 'Active'
const sort: SortType = (url.searchParams.get('sort') as SortType) || 'Active'
const listingType: ListingType =
(req.url.searchParams.get('type') as ListingType) || 'Local'
(url.searchParams.get('type') as ListingType) || 'Local'
return lemmy.getPosts({
return getClient(cookies.get('instance_url')).getPosts({
limit: 40,
page: page,
sort: sort,
-5
View File
@@ -1,5 +0,0 @@
import { lemmy } from '$lib/lemmy.js';
export function load({ params }) {
return lemmy.getCommunity({ name: params.name })
}
+5
View File
@@ -0,0 +1,5 @@
import { getClient } from '$lib/lemmy.js'
export function load({ params }) {
return getClient().getCommunity({ name: params.name })
}
+2 -2
View File
@@ -1,4 +1,4 @@
import { lemmy } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
export function load(req: any) {
@@ -7,7 +7,7 @@ export function load(req: any) {
const sort: SortType =
(req.url.searchParams.get('sort') as SortType) || 'Active'
return lemmy.getPosts({
return getClient(req.cookies.get('instance_url')).getPosts({
limit: 40,
community_name: req.params.name,
page: page,
+7 -7
View File
@@ -1,13 +1,13 @@
import { lemmy } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
import type { ListingType } from 'lemmy-js-client'
export function load(req) {
const type = req.url.searchParams.get('listingType') as ListingType
const page = Number(req.url.searchParams.get('page')) || 1
const query = req.url.searchParams.get('q')
export function load({ url, cookies }) {
const type = url.searchParams.get('listingType') as ListingType
const page = Number(url.searchParams.get('page')) || 1
const query = url.searchParams.get('q')
if (query) {
return lemmy.search({
return getClient(cookies.get('instance_url')).search({
limit: 40,
page: page,
sort: 'Active',
@@ -16,7 +16,7 @@ export function load(req) {
q: query,
})
} else {
return lemmy.listCommunities({
return getClient(cookies.get('instance_url')).listCommunities({
limit: 40,
page: page,
sort: 'Active',
+28
View File
@@ -0,0 +1,28 @@
<script lang="ts">
import Button from '$lib/components/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
@@ -0,0 +1,15 @@
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')
}
}
+6 -4
View File
@@ -1,9 +1,11 @@
import { lemmy } from '$lib/lemmy.js'
import { getClient } from '$lib/lemmy.js'
export async function load({ params }) {
export async function load({ params, cookies }) {
return {
post: await lemmy.getPost({ id: Number(params.id) }),
comments: await lemmy.getComments({
post: await getClient(cookies.get('instance_url')).getPost({
id: Number(params.id),
}),
comments: await getClient(cookies.get('instance_url')).getComments({
post_id: Number(params.id),
type_: 'All',
limit: 250,
+3 -4
View File
@@ -14,9 +14,6 @@
<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">
<!-- <button class="-z-10">
<Icon src={open ? Minus : Plus} width={12} mini />
</button> -->
<Avatar
url={node.comment_view.creator.avatar ??
`https://api.dicebear.com/6.x/initials/svg?seed=${node.comment_view.creator.name}`}
@@ -38,7 +35,9 @@
class="flex flex-col gap-1 whitespace-pre-wrap
max-w-full"
>
<SvelteMarkdown source={node.comment_view.comment.content} />
<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 />
+5 -3
View File
@@ -16,6 +16,7 @@
import type { CommentNodeI } from './comments.js'
import RelativeDate from '$lib/components/RelativeDate.svelte'
import CommunityLink from '$lib/components/CommunityLink.svelte'
import { isImage } from '$lib/ui/image.js'
let postRes: PostView
export { postRes as post }
@@ -45,12 +46,13 @@
{postRes.post.url}
</a>
{/if}
{#if postRes.post.thumbnail_url}
{#if isImage(postRes.post.url)}
<a href="/post/{postRes.post.id}" class="inline self-start">
<img
src={postRes.post.thumbnail_url}
src={postRes.post.url}
alt={postRes.post.name}
class="rounded-md max-h-64 max-w-max"
class="rounded-md max-h-64 max-w-full"
loading="lazy"
/>
</a>
{/if}