mirror of
https://github.com/pockethost/pockethost.git
synced 2025-03-30 15:08:30 +00:00
Bugfixes: navbar and realtime provisioning status
This commit is contained in:
parent
f5b9ebabea
commit
a149df0d58
@ -1,13 +1,13 @@
|
|||||||
<script>
|
<script lang="ts">
|
||||||
// Documentation Source
|
// Documentation Source
|
||||||
// https://svelte.dev/repl/26eb44932920421da01e2e21539494cd?version=3.51.0
|
// https://svelte.dev/repl/26eb44932920421da01e2e21539494cd?version=3.51.0
|
||||||
|
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
|
|
||||||
export let query
|
export let query = ''
|
||||||
|
|
||||||
let mql
|
let mql: MediaQueryList | undefined = undefined
|
||||||
let mqlListener
|
let mqlListener: (e: MediaQueryListEvent) => void
|
||||||
let wasMounted = false
|
let wasMounted = false
|
||||||
let matches = false
|
let matches = false
|
||||||
|
|
||||||
@ -25,7 +25,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addNewListener(query) {
|
function addNewListener(query: string) {
|
||||||
mql = window.matchMedia(query)
|
mql = window.matchMedia(query)
|
||||||
mqlListener = (v) => (matches = v.matches)
|
mqlListener = (v) => (matches = v.matches)
|
||||||
mql.addListener(mqlListener)
|
mql.addListener(mqlListener)
|
||||||
|
@ -1,15 +1,29 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { client } from '$src/pocketbase'
|
|
||||||
import MediaQuery from '$components/MediaQuery.svelte'
|
import MediaQuery from '$components/MediaQuery.svelte'
|
||||||
import ThemeToggle from '$components/ThemeToggle.svelte'
|
import ThemeToggle from '$components/ThemeToggle.svelte'
|
||||||
|
import { client } from '$src/pocketbase'
|
||||||
|
import { createCleanupManagerSync } from '$util/CleanupManager'
|
||||||
|
import { onDestroy, onMount } from 'svelte'
|
||||||
|
const { isLoggedIn, logOut, onAuthChange } = client
|
||||||
|
|
||||||
const { isLoggedIn, logOut } = client
|
const cm = createCleanupManagerSync()
|
||||||
|
|
||||||
|
let _isLoggedIn = isLoggedIn()
|
||||||
|
onMount(() => {
|
||||||
|
_isLoggedIn = isLoggedIn()
|
||||||
|
const unsub = onAuthChange(() => {
|
||||||
|
_isLoggedIn = isLoggedIn()
|
||||||
|
})
|
||||||
|
cm.add(unsub)
|
||||||
|
})
|
||||||
|
|
||||||
|
onDestroy(cm.cleanupAll)
|
||||||
|
|
||||||
const handleLogout = (e: Event) => {
|
const handleLogout = (e: Event) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
logOut()
|
logOut()
|
||||||
// Hard refresh to make sure any remaining data is cleared
|
// Hard refresh to make sure any remaining data is cleared
|
||||||
window.location = '/'
|
window.location.href = '/'
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -34,7 +48,7 @@
|
|||||||
|
|
||||||
<div class="collapse navbar-collapse" id="nav-links">
|
<div class="collapse navbar-collapse" id="nav-links">
|
||||||
<ul class="navbar-nav ms-auto mb-2 mb-md-0">
|
<ul class="navbar-nav ms-auto mb-2 mb-md-0">
|
||||||
{#if isLoggedIn()}
|
{#if _isLoggedIn}
|
||||||
<li class="nav-item text-md-start text-center">
|
<li class="nav-item text-md-start text-center">
|
||||||
<a class="nav-link" href="/dashboard">Dashboard</a>
|
<a class="nav-link" href="/dashboard">Dashboard</a>
|
||||||
</li>
|
</li>
|
||||||
@ -67,7 +81,7 @@
|
|||||||
</MediaQuery>
|
</MediaQuery>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if !isLoggedIn()}
|
{#if !_isLoggedIn}
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link text-md-start text-center" href="/signup">Sign up</a>
|
<a class="nav-link text-md-start text-center" href="/signup">Sign up</a>
|
||||||
</li>
|
</li>
|
||||||
|
@ -4,18 +4,21 @@ import { keys, map } from '@s-libs/micro-dash'
|
|||||||
import PocketBase, { BaseAuthStore, ClientResponseError, Record } from 'pocketbase'
|
import PocketBase, { BaseAuthStore, ClientResponseError, Record } from 'pocketbase'
|
||||||
import type { Unsubscriber } from 'svelte/store'
|
import type { Unsubscriber } from 'svelte/store'
|
||||||
|
|
||||||
|
export type AuthChangeHandler = (user: BaseAuthStore) => void
|
||||||
|
|
||||||
export const createPocketbaseClient = (url: string) => {
|
export const createPocketbaseClient = (url: string) => {
|
||||||
const client = new PocketBase(url)
|
const client = new PocketBase(url)
|
||||||
|
|
||||||
const { authStore } = client
|
const { authStore } = client
|
||||||
|
|
||||||
const { onChange } = authStore
|
|
||||||
|
|
||||||
const user = () => authStore.model
|
const user = () => authStore.model
|
||||||
|
|
||||||
const isLoggedIn = () => authStore.isValid
|
const isLoggedIn = () => authStore.isValid
|
||||||
|
|
||||||
const onAuthChange = (cb: (user: BaseAuthStore) => Unsubscriber) => onChange(() => cb(authStore))
|
const onAuthChange = (cb: AuthChangeHandler): Unsubscriber =>
|
||||||
|
authStore.onChange(() => {
|
||||||
|
cb(authStore)
|
||||||
|
})
|
||||||
|
|
||||||
const logOut = () => authStore.clear()
|
const logOut = () => authStore.clear()
|
||||||
|
|
||||||
|
@ -4,10 +4,11 @@
|
|||||||
import RetroBoxContainer from '$components/RetroBoxContainer.svelte'
|
import RetroBoxContainer from '$components/RetroBoxContainer.svelte'
|
||||||
import { PUBLIC_PB_DOMAIN } from '$src/env'
|
import { PUBLIC_PB_DOMAIN } from '$src/env'
|
||||||
import { client } from '$src/pocketbase'
|
import { client } from '$src/pocketbase'
|
||||||
|
import { createCleanupManagerSync } from '$util/CleanupManager'
|
||||||
import type { Instance_Out_ByIdCollection } from '@pockethost/common/src/schema'
|
import type { Instance_Out_ByIdCollection } from '@pockethost/common/src/schema'
|
||||||
import { forEach, values } from '@s-libs/micro-dash'
|
import { forEach, values } from '@s-libs/micro-dash'
|
||||||
import { onDestroy, onMount } from 'svelte'
|
import { onDestroy, onMount } from 'svelte'
|
||||||
import type { Unsubscriber } from 'svelte/store'
|
import { fade } from 'svelte/transition'
|
||||||
|
|
||||||
// Wait for the instance call to complete before rendering the UI
|
// Wait for the instance call to complete before rendering the UI
|
||||||
let hasPageLoaded = false
|
let hasPageLoaded = false
|
||||||
@ -18,8 +19,7 @@
|
|||||||
// This will update when the `apps` value changes
|
// This will update when the `apps` value changes
|
||||||
$: isFirstApplication = values(apps).length === 0
|
$: isFirstApplication = values(apps).length === 0
|
||||||
|
|
||||||
let unsubs: Unsubscriber[] = []
|
const cm = createCleanupManagerSync()
|
||||||
|
|
||||||
let _touch = 0 // This is a fake var because without it the watcher callback will not update UI when the apps object changes
|
let _touch = 0 // This is a fake var because without it the watcher callback will not update UI when the apps object changes
|
||||||
const _update = (_apps: Instance_Out_ByIdCollection) => {
|
const _update = (_apps: Instance_Out_ByIdCollection) => {
|
||||||
apps = _apps
|
apps = _apps
|
||||||
@ -37,7 +37,7 @@
|
|||||||
console.log(`got a record`, r)
|
console.log(`got a record`, r)
|
||||||
_update({ ...apps, [r.id]: r })
|
_update({ ...apps, [r.id]: r })
|
||||||
})
|
})
|
||||||
unsubs.push(unsub)
|
cm.add(unsub)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
@ -48,9 +48,7 @@
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(cm.cleanupAll)
|
||||||
unsubs.forEach((u) => u())
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Protected>
|
<Protected>
|
||||||
|
42
packages/pockethost.io/src/util/CleanupManager.ts
Normal file
42
packages/pockethost.io/src/util/CleanupManager.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { forEach, reduce } from '@s-libs/micro-dash'
|
||||||
|
|
||||||
|
export type CleanupFuncSync = () => void
|
||||||
|
export const createCleanupManagerSync = () => {
|
||||||
|
let i = 0
|
||||||
|
const cleanups: any = {}
|
||||||
|
const add = (cb: CleanupFuncSync) => {
|
||||||
|
const idx = i++
|
||||||
|
const cleanup = () => {
|
||||||
|
cb()
|
||||||
|
delete cleanups[idx]
|
||||||
|
}
|
||||||
|
cleanups[idx] = cleanup
|
||||||
|
return cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanupAll = () => {
|
||||||
|
forEach(cleanups, (c) => c())
|
||||||
|
}
|
||||||
|
|
||||||
|
return { add, cleanupAll }
|
||||||
|
}
|
||||||
|
|
||||||
|
export type CleanupFuncAsync = () => Promise<any>
|
||||||
|
export const createCleanupManagerAsync = () => {
|
||||||
|
let i = 0
|
||||||
|
const cleanups: any = {}
|
||||||
|
const add = (cb: CleanupFuncAsync) => {
|
||||||
|
const idx = i++
|
||||||
|
const cleanup = async () => {
|
||||||
|
await cb()
|
||||||
|
delete cleanups[idx]
|
||||||
|
}
|
||||||
|
cleanups[idx] = cleanup
|
||||||
|
return cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanupAll = () =>
|
||||||
|
reduce(cleanups, (c, v) => c.then(v()), Promise.resolve())
|
||||||
|
|
||||||
|
return { add, cleanupAll }
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user