import type { DesktopBridge, DesktopUpdateState } from "@t3tools/contracts";
import { ArrowRightIcon } from "lucide-react";
import {
getDesktopUpdateDownloadedVersion,
getDesktopUpdateReleaseUrl,
} from "./desktopUpdate.logic";
import { toastManager } from "./ui/toast";
type DesktopUpdateShell = Pick<DesktopBridge, "openExternal">;
export async function openDesktopUpdateReleaseNotes(
shell: DesktopUpdateShell | undefined,
releaseUrl: string,
): Promise<void> {
try {
if (shell && (await shell.openExternal(releaseUrl))) return;
} catch {}
toastManager.add({ type: "error", title: "Unable to open release notes" });
}
function ReleaseNotesLink({
shell,
releaseUrl,
}: {
shell: DesktopUpdateShell;
releaseUrl: string;
}) {
return (
<button
className="ml-2 inline cursor-pointer text-muted-foreground underline decoration-dotted underline-offset-4 transition-colors hover:text-foreground"
onClick={() => {
void openDesktopUpdateReleaseNotes(shell, releaseUrl);
}}
type="button"
>
Read more
<ArrowRightIcon
aria-hidden
className="ml-1 inline size-3 -rotate-45 align-[-0.125em]"
strokeWidth={2.25}
/>
</button>
);
}
export function showDesktopUpdateDownloadedToast(
shell: DesktopUpdateShell,
state: DesktopUpdateState,
): void {
const releaseUrl = getDesktopUpdateReleaseUrl(getDesktopUpdateDownloadedVersion(state));
toastManager.add({
type: "success",
title: "Update downloaded",
description: (
<>
Restart the app from the update button to install it.
{releaseUrl ? <ReleaseNotesLink releaseUrl={releaseUrl} shell={shell} /> : null}
</>
),
});
}