MapComplete/src/UI/Base/LoginToggle.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.6 KiB
Svelte
Raw Normal View History

<script lang="ts">
import Loading from "./Loading.svelte"
import type { OsmServiceState } from "../../Logic/Osm/OsmConnection"
import { OsmConnection } from "../../Logic/Osm/OsmConnection"
import { Translation } from "../i18n/Translation"
import Translations from "../i18n/Translations"
import Tr from "./Tr.svelte"
2024-12-15 22:39:22 +01:00
import { ImmutableStore, Store, UIEventSource } from "../../Logic/UIEventSource"
2023-12-01 15:23:28 +01:00
import Invalid from "../../assets/svg/Invalid.svelte"
import ArrowPath from "@babeard/svelte-heroicons/mini/ArrowPath"
export let state: {
osmConnection: OsmConnection
featureSwitches?: { featureSwitchEnableLogin?: UIEventSource<boolean> }
}
/**
* If set, 'loading' will act as if we are already logged in.
*/
export let ignoreLoading: boolean = false
/**
* Only show the 'successful' state, don't show loading or error messages
*/
2024-06-16 16:06:26 +02:00
export let silentFail: boolean = false
2024-12-15 22:39:22 +01:00
/**
* If set and the OSM-api fails, do _not_ show any error messages nor the successful state, just hide
*/
export let hiddenFail: boolean = false
let loadingStatus = state?.osmConnection?.loadingStatus ?? new ImmutableStore("logged-in")
let badge = state?.featureSwitches?.featureSwitchEnableLogin ?? new ImmutableStore(true)
const t = Translations.t.general
const offlineModes: Partial<Record<OsmServiceState, Translation>> = {
offline: t.loginFailedOfflineMode,
unreachable: t.loginFailedUnreachableMode,
2023-04-07 02:13:57 +02:00
unknown: t.loginFailedUnreachableMode,
readonly: t.loginFailedReadonlyMode,
}
2024-12-15 22:39:22 +01:00
const apiState: Store<string> =
2023-11-09 16:30:26 +01:00
state?.osmConnection?.apiIsOnline ?? new ImmutableStore<OsmServiceState>("online")
</script>
{#if $badge}
{#if !ignoreLoading && !silentFail && $loadingStatus === "loading"}
<slot name="loading">
<Loading />
</slot>
2024-12-15 22:39:22 +01:00
{:else if !silentFail && ($loadingStatus === "error" || $apiState === "readonly" || $apiState === "offline")}
{#if !hiddenFail}
<slot name="error">
<div class="alert flex flex-col items-center">
<div class="max-w-64 flex items-center">
<Invalid class="m-2 h-8 w-8 shrink-0" />
<Tr t={offlineModes[$apiState] ?? t.loginFailedUnreachableMode} />
</div>
<button class="h-fit" on:click={() => state.osmConnection.AttemptLogin()}>
<ArrowPath class="h-6 w-6" />
<Tr t={t.retry} />
</button>
2024-06-24 13:11:35 +02:00
</div>
2024-12-15 22:39:22 +01:00
</slot>
{/if}
{:else if $loadingStatus === "logged-in"}
<slot />
{:else if !silentFail && $loadingStatus === "not-attempted"}
<slot name="not-logged-in" />
2023-10-30 13:45:44 +01:00
{/if}
{/if}