feat: Allow changing instance URL when not logged in

This commit is contained in:
Xyphyn committed 2023-07-17 10:40:06 -07:00
1 parent f127b5f645
commit fed5cc2263
11 files changed
+133 -76

No files matched your search

+10 -2
View File
@@ -69,6 +69,14 @@
>
{postRes.post.name}
</a>
{#if postRes.post.url && !postRes.post.thumbnail_url}
<a
href={postRes.post.url}
class="max-w-[40ch] overflow-ellipsis whitespace-nowrap text-sky-400 hover:underline text-xs"
>
{postRes.post.url}
</a>
{/if}
{#if isImage(postRes.post.url)}
<div class="self-start" class:blur-3xl={postRes.post.nsfw}>
<picture
@@ -109,7 +117,7 @@
/>
<span
class="w-full px-4 py-2 overflow-hidden
whitespace-nowrap text-ellipsis text-sm group-hover:underline bg-slate-100 dark:bg-zinc-800 border dark:border-transparent
whitespace-nowrap text-ellipsis text-sm group-hover:underline bg-slate-100 border
absolute bottom-0 rounded-b-md flex flex-row gap-1 items-center"
>
<Icon src={Link} width={16} mini />
@@ -120,7 +128,7 @@
{#if postRes.post.body && !postRes.post.nsfw}
<p
class="text-sm max-h-[74px] line-clamp-3 bg-slate-100 dark:bg-zinc-800
border border-slate-200 dark:border-zinc-700 rounded-md p-2 mt-2"
border border-slate-200 dark:border-zinc-700 rounded-md p-2"
>
{postRes.post.body}
</p>
+1 -1
View File
@@ -59,7 +59,7 @@
}
</script>
<div class="flex flex-row gap-2 items-center pt-2">
<div class="flex flex-row gap-2 items-center">
<PostVote post={post.post} vote={post.my_vote} score={post.counts.score} />
<Link color={Color.border} href="/post/{getInstance()}/{post.post.id}">
+10 -14
View File
@@ -5,13 +5,13 @@
import Logo from '$lib/components/ui/Logo.svelte'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { DEFAULT_INSTANCE_URL, authData, user } from '$lib/lemmy.js'
import { authData, getInstance, instance, user } from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
import {
ArrowLeftOnRectangle,
ArrowRightOnRectangle,
Beaker,
Bookmark,
Cog6Tooth,
GlobeAlt,
Icon,
Inbox,
@@ -37,11 +37,7 @@
class="text-xs opacity-50
transition-opacity"
>
{#if $authData?.instance}
{$authData.instance}
{:else}
{DEFAULT_INSTANCE_URL}
{/if}
{$instance}
</span>
</div>
</a>
@@ -124,13 +120,13 @@
>
<Icon src={ArrowRightOnRectangle} mini width={16} />Log out
</MenuButton>
<hr class="dark:opacity-10 w-[90%] my-2 mx-auto" />
<!-- svelte-ignore missing-declaration -->
<li class="text-xs px-4 py-1 my-1 opacity-80">v{__VERSION__}</li>
<MenuButton link href="/experiments">
<Icon src={Beaker} mini width={16} />
Experiments
</MenuButton>
{/if}
<hr class="dark:opacity-10 w-[90%] my-2 mx-auto" />
<!-- svelte-ignore missing-declaration -->
<li class="text-xs px-4 py-1 my-1 opacity-80">v{__VERSION__}</li>
<MenuButton link href="/settings">
<Icon src={Cog6Tooth} mini width={16} />
Settings
</MenuButton>
</Menu>
</nav>
+28 -11
View File
@@ -8,32 +8,33 @@ import {
import { get, writable } from 'svelte/store'
import { PUBLIC_PROXY_URL } from '$env/static/public'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { userSettings } from '$lib/settings.js'
export const DEFAULT_INSTANCE_URL = 'lemmy.world'
export let instance_url = writable(DEFAULT_INSTANCE_URL)
export let instance = writable(DEFAULT_INSTANCE_URL)
export let corsSupported = writable(true)
export function buildBaseUrl(instance?: string) {
if (!instance) {
instance = get(authData)?.instance ?? DEFAULT_INSTANCE_URL
export function buildBaseUrl(instanceURL?: string) {
if (!instanceURL) {
instanceURL = get(instance)!
}
return `${PUBLIC_PROXY_URL}/cors/${instance}`
return `${PUBLIC_PROXY_URL}/cors/${instanceURL}`
}
export function getClient(instance?: string): LemmyHttp {
if (!instance) {
instance = get(authData)?.instance ?? DEFAULT_INSTANCE_URL
export function getClient(instanceURL?: string): LemmyHttp {
if (!instanceURL) {
instanceURL = get(instance)
}
return new LemmyHttp(
get(corsSupported)
? `https://${instance}`
: `${PUBLIC_PROXY_URL}/cors/${instance}`
? `https://${instanceURL}`
: `${PUBLIC_PROXY_URL}/cors/${instanceURL}`
)
}
export const getInstance = () => get(authData)?.instance ?? DEFAULT_INSTANCE_URL
export const getInstance = () => get(instance)
export interface AuthData {
token: string
@@ -73,7 +74,11 @@ if (typeof localStorage != 'undefined') {
}
authData.subscribe(async (data) => {
instance.set(
data?.instance ?? get(userSettings).instance ?? DEFAULT_INSTANCE_URL
)
if (!data?.token) return
try {
const site = await getClient().getSite({
auth: data.token,
@@ -92,3 +97,15 @@ authData.subscribe(async (data) => {
authData.set(undefined)
}
})
export async function validateInstance(instance: string): Promise<boolean> {
if (instance == '') return false
try {
await getClient(instance).getSite({})
return true
} catch (err) {
return false
}
}
+2
View File
@@ -1,8 +1,10 @@
import { DEFAULT_INSTANCE_URL } from '$lib/lemmy.js'
import { writable } from 'svelte/store'
interface Settings {
newComments: boolean
expandableImages: boolean
instance?: string
}
const defaultSettings: Settings = {