diff --git a/desktop/src/components/AppSidebar.svelte b/desktop/src/components/AppSidebar.svelte index 7cb0153ca..4c6fa1cf3 100644 --- a/desktop/src/components/AppSidebar.svelte +++ b/desktop/src/components/AppSidebar.svelte @@ -1,6 +1,7 @@ diff --git a/desktop/src/views/profile/Keys.svelte b/desktop/src/views/profile/Keys.svelte index 091c757ab..98b089082 100644 --- a/desktop/src/views/profile/Keys.svelte +++ b/desktop/src/views/profile/Keys.svelte @@ -95,7 +95,7 @@ revokeError = undefined; try { - await change(write, "keys"); + await change(write); } catch (error) { if (onerror) onerror(error); else @@ -140,10 +140,10 @@ rid: actor.rid, majority: actor.majority, confirm: (code: string, controller: boolean) => - change( - { command: "approve_enrollment", args: { code, controller } }, - "keys", - ), + change({ + command: "approve_enrollment", + args: { code, controller }, + }), }, }); } diff --git a/desktop/src/views/profile/Profile.svelte b/desktop/src/views/profile/Profile.svelte index de27ef11d..0e7e2b428 100644 --- a/desktop/src/views/profile/Profile.svelte +++ b/desktop/src/views/profile/Profile.svelte @@ -12,6 +12,7 @@ import Topbar from "@app/components/Topbar.svelte"; import Layout from "@app/views/repo/Layout.svelte"; + import { createActor } from "./actor"; import JoinActor from "./JoinActor.svelte"; import Keys from "./Keys.svelte"; import Notice from "./Notice.svelte"; @@ -24,7 +25,8 @@ } const { sidebarData, tab }: Props = $props(); - const actor = $derived(sidebarData.actor); + const current = createActor(() => sidebarData); + const actor = $derived(current.actor); const localDid = $derived(didFromPublicKey(sidebarData.config.publicKey)); const tabs = $derived< @@ -148,7 +150,7 @@ {#if error.code === "ActorError.Ambiguous"}
- +
{:else} @@ -215,8 +217,8 @@
- {#if sidebarData.actorError} - {@render failed(sidebarData.actorError)} + {#if current.actorError} + {@render failed(current.actorError)} {:else if tab === "profile"} {#if actor || editing} diff --git a/desktop/src/views/profile/ProfileForm.svelte b/desktop/src/views/profile/ProfileForm.svelte index 5857442fd..c2fb5f9dc 100644 --- a/desktop/src/views/profile/ProfileForm.svelte +++ b/desktop/src/views/profile/ProfileForm.svelte @@ -117,8 +117,8 @@ const shown = $derived(actor?.profile); const shownHead = $derived(actor?.head); - // The parent reuses this component across routes, so the form is rebuilt - // when the route brings a different profile, as after a save. + // The actor comes from the cache and changes in place, so the form is + // rebuilt when a different profile head arrives, as after a save. // svelte-ignore state_referenced_locally let form = $state
(formFrom(actor?.profile)); @@ -391,19 +391,16 @@ const updating = actor !== null; try { - await change( - { - command: updating ? "update_actor_profile" : "create_actor", - args: { - profile: profileFrom(form), - images: { - avatar: picked.avatar?.base64, - banner: picked.banner?.base64, - }, + await change({ + command: updating ? "update_actor_profile" : "create_actor", + args: { + profile: profileFrom(form), + images: { + avatar: picked.avatar?.base64, + banner: picked.banner?.base64, }, }, - "profile", - ); + }); } catch (error) { submitError = describeActorError( error, diff --git a/desktop/src/views/profile/SeveralProfiles.svelte b/desktop/src/views/profile/SeveralProfiles.svelte index b03c80f24..baed35fd2 100644 --- a/desktop/src/views/profile/SeveralProfiles.svelte +++ b/desktop/src/views/profile/SeveralProfiles.svelte @@ -3,7 +3,6 @@ import { invoke } from "@app/lib/invoke"; import { show } from "@app/lib/modal"; - import type { ProfileTab } from "@app/lib/router/definitions"; import Button from "@app/components/Button.svelte"; import CopyableId from "@app/components/CopyableId.svelte"; @@ -23,12 +22,11 @@ interface Props { localDid: string; - tab: ProfileTab; // What the backend said when it found the key in several profiles. message: string; } - const { localDid, tab, message }: Props = $props(); + const { localDid, message }: Props = $props(); let actors = $state(); let loadError = $state(); @@ -81,13 +79,13 @@ others: actor.keys.length - 1, confirm: async () => { try { - await change( - { command: "retire_actor", args: { rid: actor.rid } }, - tab, - ); + await change({ + command: "retire_actor", + args: { rid: actor.rid }, + }); } finally { - // Not waited for: the page has reloaded by now, and this only - // refreshes the list where the device is still in several. + // Not waited for: this only refreshes the list where the device + // is still in several. void load(); } }, @@ -105,7 +103,7 @@ acting = true; try { - await change({ command, args: { rid: actor.rid } }, tab); + await change({ command, args: { rid: actor.rid } }); } catch (e) { actionError = { rid: actor.rid, diff --git a/desktop/src/views/profile/actor.ts b/desktop/src/views/profile/actor.ts index 193cf70be..0ef23bccd 100644 --- a/desktop/src/views/profile/actor.ts +++ b/desktop/src/views/profile/actor.ts @@ -3,13 +3,18 @@ import type { ActorKey } from "@bindings/actor/ActorKey"; import type { ActorProfile } from "@bindings/actor/ActorProfile"; import type { ActorRevision } from "@bindings/actor/ActorRevision"; import type { ProfileImages } from "@bindings/actor/ProfileImages"; +import type { Key } from "@radicle-gui/core/keys"; +import { isJobsKey, keys } from "@radicle-gui/core/keys"; +import { createQuery, invalidate } from "@radicle-gui/ui/lib/query.svelte"; import { get } from "svelte/store"; +import { localActor } from "@app/lib/cobQueries"; import { nodeRunning } from "@app/lib/events"; -import { invalidateReposSummary, invoke, InvokeError } from "@app/lib/invoke"; -import * as router from "@app/lib/router"; -import type { ProfileTab } from "@app/lib/router/definitions"; +import { invoke, InvokeError } from "@app/lib/invoke"; +import { labs } from "@app/lib/labs"; +import { queryClient } from "@app/lib/query"; +import type { SidebarData } from "@app/lib/router/definitions"; import { formatTimestamp, publicKeyFromDid, truncateId } from "@app/lib/utils"; import { announce } from "@app/components/AnnounceSwitch.svelte"; @@ -32,11 +37,41 @@ export function describeActorError( return error instanceof Error ? error.message : fallback; } -// Reloads the profile page on `tab` from storage, so it shows what was -// written. -export async function reload(tab: ProfileTab): Promise { - invalidateReposSummary(); - await router.replace({ resource: "profile", tab }); +// The local actor as the cache has it, so that writes and the node's updates +// show in place, and as the route loaded it until the cache has an answer. +// Create it during component initialisation. +export function createActor( + loaded: () => Pick, +): Pick { + const query = createQuery(queryClient, () => ({ + ...localActor(), + enabled: labs.actorRepos, + })); + + // The query's own error, when it has one the page can show. + const queryError = () => { + const { error } = query.current; + return error instanceof InvokeError + ? { code: error.code, message: error.message } + : undefined; + }; + + // A disabled query still holds what it last fetched, so with the lab off + // the route's values count. + return { + get actor() { + if (!labs.actorRepos) return loaded().actor; + const { data } = query.current; + if (queryError()) return null; + return data !== undefined ? data : loaded().actor; + }, + get actorError() { + if (!labs.actorRepos) return loaded().actorError; + const error = queryError(); + if (error) return error; + return query.current.data !== undefined ? undefined : loaded().actorError; + }, + }; } // The actor writes and what each takes; the options are added by `change`. @@ -60,26 +95,30 @@ export type ActorWrite = // The votes on an open identity change. export type Vote = Extract["command"]; -// Runs an actor write, then reloads `tab` whether or not it went through: a -// failure may come from state the page has not seen, and a write that went -// through is not undone by a reload that did not. The write announces when -// the node runs and the user has not turned announcing off. -export async function change( - { command, args }: ActorWrite, - tab: ProfileTab, -): Promise { +// Runs an actor write, which announces when the node runs and the user has +// not turned announcing off. It returns once the actor is refetched, whether +// or not the write went through, so callers see what it left. The actor's +// repository is refetched too, since its identity and code change; most +// writes don't name it, so it comes from the cache. +export async function change({ command, args }: ActorWrite): Promise { const opts = { announce: get(nodeRunning) && get(announce) }; - const reloadQuietly = () => - reload(tab).catch((e: unknown) => - console.warn("Could not reload the profile", e), - ); + const rid = + "rid" in args + ? args.rid + : queryClient.getQueryData(keys.actor())?.rid; try { await invoke(command, { ...args, opts }); - } catch (error) { - await reloadQuietly(); - throw error; + } finally { + await Promise.all([ + invalidate(queryClient, { queryKey: keys.actor() }), + rid !== undefined && + invalidate(queryClient, { + queryKey: keys.repo(rid), + // Job lists don't depend on the identity, and listing them is slow. + predicate: query => !isJobsKey(query.queryKey as Key), + }), + ]); } - await reloadQuietly(); } // The keys that govern the actor's identity. diff --git a/desktop/tests/unit/writeCoverage.test.ts b/desktop/tests/unit/writeCoverage.test.ts index 54f5104fe..62b417673 100644 --- a/desktop/tests/unit/writeCoverage.test.ts +++ b/desktop/tests/unit/writeCoverage.test.ts @@ -72,24 +72,13 @@ const outsideCache = [ "pick_artifact_directory", "pick_artifact_files", "startup", - // Actor writes, after which the profile page reloads itself. A join code - // only changes the actor once another device approves it. - "approve_actor_revision", - "approve_enrollment", - "cancel_leave", - "create_actor", - "join_actor", - "leave_actor", - "reject_actor_revision", - "remove_actor_key", - "retire_actor", - "update_actor_profile", - "withdraw_actor_revision", // Writes outside the cache. The board shows its own writes as they land, // and a reload after each would make cards jump while the user moves more. - // Artifact seeding, embeds and files aren't in the cache. + // Artifact seeding, embeds and files aren't in the cache. A join code only + // changes the actor once another device approves it. "create_board", "download_artifact", + "join_actor", "rename_board", "save_diff_to_disk", "save_embed_by_bytes", diff --git a/shared/core/src/keys.ts b/shared/core/src/keys.ts index 4b9628369..614eb2462 100644 --- a/shared/core/src/keys.ts +++ b/shared/core/src/keys.ts @@ -34,6 +34,8 @@ export const keys = { // The repository list for one scope, with the seeded repositories that // haven't arrived yet. repoList: (scope: string) => ["repos", scope] as const, + // The local actor, which is found by scanning every repository's identity. + actor: () => ["actor"] as const, notifications: () => ["notifications"] as const, notificationCount: () => ["notifications", "count"] as const, notificationList: () => ["notifications", "list"] as const,