misc: add account logging in

This commit is contained in:
Xyphyn committed 2023-07-03 14:57:53 -07:00
1 parent 7d3f29d9eb
commit f71ad4be8e
21 files changed
+686 -151

No files matched your search

+23 -15
View File
@@ -1,23 +1,31 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte'
import { Color } from '$lib/ui/colors.js'
import { createEventDispatcher } from 'svelte'
import { Color } from '$lib/ui/colors.js'
import Loading from '$lib/components/ui/Loading.svelte'
export let color: Color = Color.secondary
export let loading = false
export let color: Color = Color.secondary
export let loading = false
export let large = false
export let disabled = false
const dispatch = createEventDispatcher()
const dispatch = createEventDispatcher()
</script>
<button
on:click={(e) => dispatch('click', e)}
class="px-3 py-1.5 rounded-md {color} text-sm transition-all"
{disabled}
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
class="flex flex-row items-center justify-center gap-2"
class:opacity-0={loading}
>
<slot name="icon" />
<slot />
</div>
<div class="mx-auto my-auto" class:hidden={!loading}>loading...</div>
<div
class="flex flex-row items-center justify-center gap-2"
class:hidden={loading}
>
<slot name="icon" />
<slot />
</div>
<div class="mx-auto py-2" class:hidden={!loading}><Loading /></div>
</button>
+39
View File
@@ -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>
+8 -3
View File
@@ -1,7 +1,12 @@
<script lang="ts">
let clazz = ''
export { clazz as class }
</script>
<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"
class="bg-white border border-slate-200
dark:border-zinc-800 dark:bg-zinc-900 break-words
flex-col gap-4 p-4 max-w-lg rounded-md {clazz}"
>
<slot />
</div>
+46
View File
@@ -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>
+34 -8
View File
@@ -1,7 +1,15 @@
<script lang="ts">
import Link from '$lib/components/input/Link.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>
<nav
@@ -14,19 +22,37 @@
<div class="flex flex-col">
<span class="font-bold">Xylemmy</span>
{#if $instance_url}
<a
class="text-xs opacity-50 hover:opacity-100 hover:underline
<span
class="text-xs opacity-50
transition-opacity"
href="/instance"
>
{new URL(`https://${$instance_url}`).hostname}
</a>
</span>
{/if}
</div>
</a>
</div>
<Link href="/communities">Communities</Link>
<div
class="w-8 h-8 rounded-full ring-1 ring-slate-300 bg-slate-100 dark:bg-zinc-800"
/>
<Menu>
<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>
+7
View File
@@ -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>
+58
View File
@@ -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
View File
@@ -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<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)
}
})