feat: add guest instance
This commit is contained in:
1 parent
2da415b670
commit
6bb47c2b1d
3 files changed
+114
-7
No files matched your search
+9
-6
@@ -1,13 +1,8 @@
|
|||||||
import { env } from '$env/dynamic/public'
|
|
||||||
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||||
import { getClient, instance } from '$lib/lemmy.js'
|
import { DEFAULT_INSTANCE_URL, getClient, instance } from '$lib/lemmy.js'
|
||||||
import { userSettings } from '$lib/settings.js'
|
|
||||||
import type { MyUserInfo } from 'lemmy-js-client'
|
import type { MyUserInfo } from 'lemmy-js-client'
|
||||||
import { get, writable } from 'svelte/store'
|
import { get, writable } from 'svelte/store'
|
||||||
|
|
||||||
const LINKED_INSTANCE_URL = env.PUBLIC_INSTANCE_URL
|
|
||||||
const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemmy.ml'
|
|
||||||
|
|
||||||
function getFromStorage<T>(key: string): T | undefined {
|
function getFromStorage<T>(key: string): T | undefined {
|
||||||
if (typeof localStorage == 'undefined') return undefined
|
if (typeof localStorage == 'undefined') return undefined
|
||||||
const lc = localStorage.getItem(key)
|
const lc = localStorage.getItem(key)
|
||||||
@@ -35,6 +30,7 @@ interface Profile {
|
|||||||
interface ProfileData {
|
interface ProfileData {
|
||||||
profiles: Profile[]
|
profiles: Profile[]
|
||||||
profile: number
|
profile: number
|
||||||
|
defaultInstance?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PersonData extends MyUserInfo {
|
interface PersonData extends MyUserInfo {
|
||||||
@@ -48,11 +44,18 @@ export const profileData = writable<ProfileData>(
|
|||||||
|
|
||||||
profileData.subscribe((pd) => {
|
profileData.subscribe((pd) => {
|
||||||
setFromStorage('profileData', pd)
|
setFromStorage('profileData', pd)
|
||||||
|
|
||||||
|
if (pd.profile == -1) {
|
||||||
|
instance.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export const profile = writable<Profile | undefined>(getProfile())
|
export const profile = writable<Profile | undefined>(getProfile())
|
||||||
|
|
||||||
profile.subscribe(async (p) => {
|
profile.subscribe(async (p) => {
|
||||||
|
if (p?.id == -1) {
|
||||||
|
instance.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
||||||
|
}
|
||||||
if (!p || !p.jwt) {
|
if (!p || !p.jwt) {
|
||||||
profileData.update((pd) => ({ ...pd, profile: -1 }))
|
profileData.update((pd) => ({ ...pd, profile: -1 }))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -54,7 +54,7 @@
|
|||||||
{item.post.removed ? 'Restore' : 'Remove'}
|
{item.post.removed ? 'Restore' : 'Remove'}
|
||||||
{/if}
|
{/if}
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
{#if $profile.user && $profile.user?.local_user_view.person.id != item.creator.id}
|
{#if $profile?.user && $profile.user?.local_user_view.person.id != item.creator.id}
|
||||||
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
|
<span class="px-4 py-1 my-1 text-xs text-slate-600 dark:text-zinc-400">
|
||||||
User
|
User
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -7,11 +7,21 @@
|
|||||||
deleteProfile,
|
deleteProfile,
|
||||||
} from '$lib/auth.js'
|
} from '$lib/auth.js'
|
||||||
import Button from '$lib/components/input/Button.svelte'
|
import Button from '$lib/components/input/Button.svelte'
|
||||||
|
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||||
import EditableList from '$lib/components/ui/list/EditableList.svelte'
|
import EditableList from '$lib/components/ui/list/EditableList.svelte'
|
||||||
|
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||||
|
import { instance, validateInstance } from '$lib/lemmy.js'
|
||||||
import { ArrowLeftOnRectangle, Icon, Plus, Trash } from 'svelte-hero-icons'
|
import { ArrowLeftOnRectangle, Icon, Plus, Trash } from 'svelte-hero-icons'
|
||||||
import { flip } from 'svelte/animate'
|
import { flip } from 'svelte/animate'
|
||||||
|
|
||||||
|
let newInstance: string = $instance
|
||||||
|
let loading = false
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<svelte:head>
|
||||||
|
<title>Accounts</title>
|
||||||
|
</svelte:head>
|
||||||
|
|
||||||
{#if $profileData.profiles.length == 0}
|
{#if $profileData.profiles.length == 0}
|
||||||
<div class="h-full flex items-center justify-center">
|
<div class="h-full flex items-center justify-center">
|
||||||
<div
|
<div
|
||||||
@@ -25,6 +35,52 @@
|
|||||||
<Icon slot="icon" src={ArrowLeftOnRectangle} size="16" mini />
|
<Icon slot="icon" src={ArrowLeftOnRectangle} size="16" mini />
|
||||||
Log in
|
Log in
|
||||||
</Button>
|
</Button>
|
||||||
|
<div class="flex flex-row font-normal gap-2">
|
||||||
|
<TextInput
|
||||||
|
label="Guest instance"
|
||||||
|
on:change={async () => {
|
||||||
|
loading = true
|
||||||
|
try {
|
||||||
|
const valid = await validateInstance(newInstance.trim())
|
||||||
|
|
||||||
|
if (!valid) {
|
||||||
|
throw new Error('invalid instance')
|
||||||
|
}
|
||||||
|
|
||||||
|
toast({
|
||||||
|
content: 'Changed guest instance.',
|
||||||
|
type: ToastType.success,
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
toast({
|
||||||
|
content: 'Failed to contact that instance URL. Is it down?',
|
||||||
|
type: ToastType.error,
|
||||||
|
})
|
||||||
|
|
||||||
|
loading = false
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$profileData.defaultInstance = newInstance
|
||||||
|
if ($currentProfile && $currentProfile.id == -1) {
|
||||||
|
$instance = newInstance
|
||||||
|
}
|
||||||
|
loading = false
|
||||||
|
}}
|
||||||
|
placeholder="Instance URL"
|
||||||
|
bind:value={newInstance}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
color="primary"
|
||||||
|
class="h-max self-end"
|
||||||
|
size="lg"
|
||||||
|
{loading}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
Change
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{:else}
|
{:else}
|
||||||
@@ -79,6 +135,54 @@
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
|
<div class="flex flex-row gap-4 items-center py-4">
|
||||||
|
<span class="font-bold flex flex-col">
|
||||||
|
Guest
|
||||||
|
<div class="flex flex-row font-normal gap-2">
|
||||||
|
<TextInput
|
||||||
|
on:change={async () => {
|
||||||
|
loading = true
|
||||||
|
try {
|
||||||
|
const valid = await validateInstance(newInstance.trim())
|
||||||
|
|
||||||
|
if (!valid) {
|
||||||
|
throw new Error('invalid instance')
|
||||||
|
}
|
||||||
|
|
||||||
|
toast({
|
||||||
|
content: 'Changed guest instance.',
|
||||||
|
type: ToastType.success,
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
toast({
|
||||||
|
content: 'Failed to contact that instance URL. Is it down?',
|
||||||
|
type: ToastType.error,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
$profileData.defaultInstance = newInstance
|
||||||
|
if ($currentProfile && $currentProfile.id == -1) {
|
||||||
|
$instance = newInstance
|
||||||
|
}
|
||||||
|
loading = false
|
||||||
|
}}
|
||||||
|
placeholder="Instance URL"
|
||||||
|
bind:value={newInstance}
|
||||||
|
/>
|
||||||
|
<Button color="primary" {loading} disabled={loading}>Change</Button>
|
||||||
|
</div>
|
||||||
|
</span>
|
||||||
|
<div class="ml-auto" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
on:click={() => {
|
||||||
|
setUserID(-1)
|
||||||
|
}}
|
||||||
|
color={$currentProfile?.id == -1 ? 'primary' : 'secondary'}
|
||||||
|
>
|
||||||
|
{$currentProfile?.id == -1 ? 'Current' : 'Switch to'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</EditableList>
|
</EditableList>
|
||||||
<Button href="/login" size="lg" class="mt-auto">
|
<Button href="/login" size="lg" class="mt-auto">
|
||||||
<Icon slot="icon" src={Plus} size="16" mini />
|
<Icon slot="icon" src={Plus} size="16" mini />
|
||||||
|
|||||||
Reference in new issue
Block a user