Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/web/src/components/settings/UpdateTrackSwitchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ function SwitchPlan({
// After a partial switch this app may already be on the track; then only remotes move.
const switchesLocal = crossesLocal && !updatesOff;
const [checking, setChecking] = useState(false);
const everything = useUpdateEverything(switchesLocal ? release.version : null, release);
const everything = useUpdateEverything(switchesLocal ? release.version : null, {
channelSwitch: release,
});
const label = channelSwitchLabel(release.channel, m);
const idle = nothingToMove(
{
Expand Down
153 changes: 142 additions & 11 deletions apps/web/src/components/sidebar/SidebarUpdatePill.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const mocks = vi.hoisted(() => {
return {
update: null as DesktopUpdateState | null,
hasUpdates: false,
hasProviderUpdates: false,
hasAppUpdates: false,
rows: [] as ReadonlyArray<UpdateEverythingRow>,
subscribe: (listener: () => void) => {
listeners.add(listener);
Expand All @@ -35,16 +37,46 @@ vi.mock("../../env", async (importOriginal) => ({
...(await importOriginal<typeof import("../../env")>()),
isElectron: true,
}));
vi.mock("./useUpdateEverything", () => ({
useUpdateEverything: () => ({
hasUpdates: mocks.hasUpdates,
summary: "Update everything",
version: null,
providerCount: 0,
rows: mocks.rows,
restarts: [],
}),
}));
vi.mock("./useUpdateEverything", async () => {
const { useSyncExternalStore } = await import("react");
const { selectProviderUpdates } = await import("./updateEverything.logic");
return {
useUpdateEverything: (
_version: string | null,
options?: { includeProviderUpdates: boolean },
) => {
const hasAppUpdates = useSyncExternalStore(mocks.subscribe, () => mocks.hasAppUpdates);
const appUpdates = _version !== null || hasAppUpdates;
const selected = selectProviderUpdates(
{ machines: mocks.hasProviderUpdates ? ["Claude"] : [], updateAll: async () => [] },
{ hasAppUpdates: appUpdates, ...options },
);
return {
hasUpdates: mocks.hasUpdates,
hasAppUpdates: appUpdates,
canChooseProviderUpdates: selected.canChooseProviderUpdates,
summary: "Update everything",
version: null,
providerCount: selected.machines.length,
rows:
mocks.rows.length > 0
? mocks.rows
: mocks.hasProviderUpdates
? [
{
environmentId: null,
label: "Test computer",
app: appUpdates ? "this-app" : null,
skipped: null,
providers: selected.machines,
},
]
: [],
restarts: [],
};
},
};
});

function nightlyUpdate(releaseNotes: DesktopUpdateState["releaseNotes"]): DesktopUpdateState {
return {
Expand Down Expand Up @@ -75,11 +107,20 @@ function setup(
update: DesktopUpdateState | null,
{
hasUpdates = false,
hasProviderUpdates = false,
hasAppUpdates = false,
rows = [],
}: { hasUpdates?: boolean; rows?: ReadonlyArray<UpdateEverythingRow> } = {},
}: {
hasUpdates?: boolean;
hasProviderUpdates?: boolean;
hasAppUpdates?: boolean;
rows?: ReadonlyArray<UpdateEverythingRow>;
} = {},
) {
mocks.update = update;
mocks.hasUpdates = hasUpdates;
mocks.hasProviderUpdates = hasProviderUpdates;
mocks.hasAppUpdates = hasAppUpdates;
mocks.rows = rows;
}

Expand Down Expand Up @@ -126,6 +167,96 @@ describe("SidebarUpdatePill release notes", () => {
});

describe("SidebarUpdatePill update everything", () => {
it.each([true, false])(
"requires a new confirmation if shown app-update targets disappear (present on open: %s)",
async (presentOnOpen) => {
setup(null, { hasUpdates: true, hasProviderUpdates: true, hasAppUpdates: presentOnOpen });
const key = "t3code:include-providers-in-app-updates";
const previous = localStorage.getItem(key);
using _storage = {
[Symbol.dispose]() {
if (previous === null) localStorage.removeItem(key);
else localStorage.setItem(key, previous);
},
};
localStorage.setItem(key, "false");
await using screen = await render(<SidebarUpdatePill />);
const button = screen.getByRole("button", { name: "Update everything" });
await userEvent.click(button);
const checkbox = page.getByRole("checkbox", { name: "Include provider updates" });
const provider = page.getByRole("cell", { name: "Claude", exact: true });
if (!presentOnOpen) {
await expect.element(checkbox).not.toBeInTheDocument();
await expect.element(provider).toBeVisible();
await expect.element(page.getByRole("alertdialog")).toBeVisible();
await act(() => {
mocks.hasAppUpdates = true;
mocks.notify();
});
}
await expect.element(checkbox).not.toBeChecked();
await expect.element(provider).not.toBeInTheDocument();

await act(() => {
mocks.hasAppUpdates = false;
mocks.notify();
});
await expect.element(page.getByRole("alertdialog")).not.toBeInTheDocument();
await userEvent.click(button);
await expect.element(page.getByRole("alertdialog")).toBeVisible();
await expect.element(checkbox).not.toBeInTheDocument();
await expect.element(provider).toBeVisible();
},
);

it.each([false, true])(
"remembers the provider choice in another client (already mounted: %s)",
async (alreadyMounted) => {
setup(nightlyUpdate([]), { hasUpdates: true, hasProviderUpdates: true });
const key = "t3code:include-providers-in-app-updates";
const previous = localStorage.getItem(key);
using _storage = {
[Symbol.dispose]() {
if (previous === null) localStorage.removeItem(key);
else localStorage.setItem(key, previous);
},
};
localStorage.removeItem(key);
const renderClient = (id: string) => {
const container = document.createElement("div");
container.id = id;
return render(<SidebarUpdatePill />, { container });
};
await using clients = new AsyncDisposableStack();
const screen = clients.use(await renderClient("update-client-first"));
const other = alreadyMounted ? clients.use(await renderClient("update-client-second")) : null;
await userEvent.click(screen.getByRole("button", { name: "Update everything" }));
const checkbox = page.getByRole("checkbox", { name: "Include provider updates" });
await expect.element(checkbox).toBeChecked();
await userEvent.click(checkbox);
await expect.element(checkbox).not.toBeChecked();
expect(localStorage.getItem(key)).toBe("false");
await screen.unmount();

const restored = other ?? clients.use(await renderClient("update-client-second"));
await userEvent.click(restored.getByRole("button", { name: "Update everything" }));
await expect.element(checkbox).not.toBeChecked();
await act(() => {
localStorage.setItem(key, "true");
window.dispatchEvent(new StorageEvent("storage", { key, newValue: "true" }));
});
await expect.element(checkbox).not.toBeChecked();
await userEvent.click(page.getByRole("button", { name: "Cancel" }));
await userEvent.click(restored.getByRole("button", { name: "Update everything" }));
await expect.element(checkbox).toBeChecked();
await userEvent.click(checkbox);
expect(localStorage.getItem(key)).toBe("false");
await userEvent.click(checkbox);
await expect.element(checkbox).toBeChecked();
expect(localStorage.getItem(key)).toBe("true");
},
);

it("does not refocus the update button after a pointer cancel", async () => {
setup(null, { hasUpdates: true });
await using screen = await render(<SidebarUpdatePill />);
Expand Down
35 changes: 34 additions & 1 deletion apps/web/src/components/sidebar/SidebarUpdatePill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
import { managedUpdatesDenial } from "@t3tools/client-runtime/contract-labels";
import { type DesktopBridge, type DesktopUpdateState } from "@t3tools/contracts";
import { TriangleAlertIcon } from "lucide-react";
import * as Schema from "effect/Schema";
import { msg, T, useGT, useMessages } from "gt-react";
import { type ComponentProps, useId, useRef, useState } from "react";
import { flushSync } from "react-dom";
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
import { isElectron } from "../../env";
import { m } from "../../i18n/messages";
import { useMediaQuery } from "../../hooks/useMediaQuery";
import { useLocalStorage } from "../../hooks/useLocalStorage";
import { cn } from "../../lib/utils";
import { ensureLocalApi } from "../../localApi";
import { useDesktopUpdateState } from "../../state/desktopUpdate";
Expand Down Expand Up @@ -50,6 +52,8 @@ import {
} from "./updateEverythingRun";
import { type ReportLocalUpdateError, useUpdateEverything } from "./useUpdateEverything";

const INCLUDE_PROVIDER_UPDATES_KEY = "t3code:include-providers-in-app-updates";

type SidebarUpdatePopoverChangeDetails = Parameters<
NonNullable<ComponentProps<typeof Popover>["onOpenChange"]>
>[1];
Expand Down Expand Up @@ -210,6 +214,7 @@ function SidebarUpdateControl() {
const [providerCheck, setProviderCheck] = useState<"button" | "dialog" | null>(null);
const isCheckingProviders = providerCheck === "button";
const [isConfirmingEverything, setIsConfirmingEverything] = useState(false);
const [hasShownAppUpdates, setHasShownAppUpdates] = useState(false);
const [checkAnimationKey, setCheckAnimationKey] = useState(0);
const [releaseNotesPopoverHandle] = useState(() => PopoverCreateHandle());
const suppressReleaseNotesFocusOpen = useRef(false);
Expand Down Expand Up @@ -238,7 +243,22 @@ function SidebarUpdateControl() {
const isDownloading = state?.status === "downloading";
const localUpdateVersion =
state && action !== "none" ? getDesktopUpdateDownloadedVersion(state) : null;
const everything = useUpdateEverything(localUpdateVersion);
const [savedIncludeProviderUpdates, saveIncludeProviderUpdates] = useLocalStorage(
INCLUDE_PROVIDER_UPDATES_KEY,
true,
Schema.Boolean,
);
const [includeProviderUpdates, setIncludeProviderUpdates] = useState(savedIncludeProviderUpdates);
const everything = useUpdateEverything(localUpdateVersion, {
includeProviderUpdates: isConfirmingEverything
? includeProviderUpdates
: savedIncludeProviderUpdates,
});
// App targets that disappear require a new confirmation before providers can re-enter the plan.
if (isConfirmingEverything) {
if (everything.hasAppUpdates && !hasShownAppUpdates) setHasShownAppUpdates(true);
else if (!everything.hasAppUpdates && hasShownAppUpdates) setIsConfirmingEverything(false);
}
const checkProviderUpdates = useCheckProviderUpdates();
const runsEverything = everything.hasUpdates && !isDownloading;
const run = useUpdateEverythingRun();
Expand Down Expand Up @@ -294,6 +314,8 @@ function SidebarUpdateControl() {
if (isInteractionDisabled) return;

if (runsEverything) {
setIncludeProviderUpdates(savedIncludeProviderUpdates);
setHasShownAppUpdates(everything.hasAppUpdates);
setIsConfirmingEverything(true);
// Machines that still cache an older latest version add their rows while the dialog is open.
setProviderCheck("dialog");
Expand Down Expand Up @@ -428,6 +450,17 @@ function SidebarUpdateControl() {
providerCount={everything.providerCount}
rows={everything.rows}
restarts={everything.restarts}
{...(everything.canChooseProviderUpdates
? {
providerUpdates: {
included: includeProviderUpdates,
onIncludedChange: (included: boolean) => {
setIncludeProviderUpdates(included);
saveIncludeProviderUpdates(included);
},
},
}
: {})}
checking={providerCheck === "dialog"}
onConfirm={() => void runEverything()}
/>
Expand Down
79 changes: 51 additions & 28 deletions apps/web/src/components/sidebar/UpdateEverythingDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AlertDialogTitle,
} from "../ui/alert-dialog";
import { Button } from "../ui/button";
import { Checkbox } from "../ui/checkbox";
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
import { MiddleTruncate } from "../ui/middle-truncate";
import { ScrollArea } from "../ui/scroll-area";
Expand Down Expand Up @@ -48,6 +49,10 @@ interface UpdateEverythingDialogProps {
readonly rows: ReadonlyArray<UpdateEverythingRow>;
readonly restarts: ReadonlyArray<RestartingMachine>;
readonly onConfirm: () => void;
readonly providerUpdates?: {
readonly included: boolean;
readonly onIncludedChange: (included: boolean) => void;
};
/** Overrides the "Update N machines?" title, for an update-track switch. */
readonly title?: string;
readonly confirmLabel?: string;
Expand Down Expand Up @@ -99,6 +104,7 @@ function UpdateEverythingDialogContent({
rows,
restarts,
onConfirm,
providerUpdates,
cancelRef,
title,
confirmLabel,
Expand Down Expand Up @@ -246,37 +252,54 @@ function UpdateEverythingDialogContent({
</div>
</ScrollArea>
<AlertDialogFooter>
{disabledReason !== null ? (
<p className="self-center text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
{disabledReason}
</p>
) : running > 0 ? (
<p className="self-center text-muted-foreground text-sm max-sm:order-1 max-sm:text-center sm:mr-auto">
{stopped === 0
? gt(
"{count, plural, one {{count} running thread will be interrupted and can resume after the restart.} other {{count} running threads will be interrupted and can resume after the restart.}}",
{ count: running },
)
: continuing === 0
<div className="flex w-full min-w-0 flex-col gap-2">
{disabledReason !== null ? (
<p className="text-sm">{disabledReason}</p>
) : running > 0 ? (
<p className="text-muted-foreground text-sm">
{stopped === 0
? gt(
"{count, plural, one {{count} running thread will be interrupted.} other {{count} running threads will be interrupted.}}",
"{count, plural, one {{count} running thread will be interrupted and can resume after the restart.} other {{count} running threads will be interrupted and can resume after the restart.}}",
{ count: running },
)
: `${gt(
"{count, plural, one {{count} running thread will be interrupted.} other {{count} running threads will be interrupted.}}",
{ count: running },
)} ${gt(
"{count, plural, one {{count} can resume after the restart.} other {{count} can resume after the restart.}}",
{ count: continuing },
)}`}
</p>
) : null}
<AlertDialogClose ref={cancelRef} render={<Button variant="outline" disabled={busy} />}>
<T>Cancel</T>
</AlertDialogClose>
<Button disabled={disabledReason !== null || busy} onClick={onConfirm}>
{confirmLabel ?? gt("Update", { $context: "verb, confirms the update" })}
</Button>
: continuing === 0
? gt(
"{count, plural, one {{count} running thread will be interrupted.} other {{count} running threads will be interrupted.}}",
{ count: running },
)
: `${gt(
"{count, plural, one {{count} running thread will be interrupted.} other {{count} running threads will be interrupted.}}",
{ count: running },
)} ${gt(
"{count, plural, one {{count} can resume after the restart.} other {{count} can resume after the restart.}}",
{ count: continuing },
)}`}
</p>
) : null}
<div className="flex flex-wrap items-center justify-end gap-3">
{providerUpdates ? (
<label className="mr-auto flex min-w-0 items-center gap-2 text-sm">
<Checkbox
checked={providerUpdates.included}
onCheckedChange={providerUpdates.onIncludedChange}
disabled={busy}
/>
<T>Include provider updates</T>
</label>
) : null}
<div className="flex gap-2">
<AlertDialogClose
ref={cancelRef}
render={<Button variant="outline" disabled={busy} />}
>
<T>Cancel</T>
</AlertDialogClose>
<Button disabled={disabledReason !== null || busy} onClick={onConfirm}>
{confirmLabel ?? gt("Update", { $context: "verb, confirms the update" })}
</Button>
</div>
</div>
</div>
</AlertDialogFooter>
</>
);
Expand Down
Loading
Loading