Loggest thine Stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3 KiB

  1. <script lang="ts" context="module">
  2. const contextKey = {ctx: "scopeListCtx"};
  3. interface ScopeListContextData {
  4. scopes: Readable<Scope[]>,
  5. reloadScopeList(): Promise<void>,
  6. };
  7. const fallback: ScopeListContextData = {
  8. scopes: readable([]),
  9. reloadScopeList: () => Promise.resolve()
  10. };
  11. export function getScopeListContext() {
  12. return getContext(contextKey) as ScopeListContextData || fallback
  13. }
  14. </script>
  15. <script lang="ts">
  16. import { readable, writable, type Readable } from "svelte/store";
  17. import { getContext, setContext } from "svelte";
  18. import { sl3 } from "$lib/clients/sl3";
  19. import type Scope from "$lib/models/scope";
  20. import { getStores } from "$app/stores";
  21. export let scopes: Scope[];
  22. const {page} = getStores();
  23. let scopesWritable = writable<Scope[]>(scopes);
  24. let loading = false;
  25. let lastSet = scopes;
  26. setContext<ScopeListContextData>(contextKey, {
  27. scopes: {subscribe: scopesWritable.subscribe},
  28. reloadScopeList,
  29. });
  30. async function reloadScopeList() {
  31. if (loading) {
  32. return
  33. }
  34. try {
  35. const newScopes = await sl3(fetch, $page.stuff.idToken).listScopes()
  36. scopesWritable.set(newScopes);
  37. } catch(_) {}
  38. loading = false;
  39. }
  40. $: {
  41. if (lastSet !== scopes) {
  42. scopesWritable.set(scopes);
  43. lastSet = scopes;
  44. }
  45. }
  46. </script>
  47. <slot></slot>