Skip to content
Merged
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
57 changes: 9 additions & 48 deletions src/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ref, provide, onMounted, onUnmounted, getCurrentInstance } from "vue";
import { useI18n } from "vue-i18n";
import semver from "semver";

import { DeviceInfo, PlausibleEvents } from "./types";
import { getDeviceInfo, openURL } from "./util";
import { DeviceInfo, PlausibleEvents, UpdateStatus } from "./types";
import { getDeviceInfo } from "./util";
import CydAPIClient, {
APIErrorResponse,
GetVersionAPIResponse,
Expand All @@ -18,6 +18,7 @@ import AutomationErrorReportModal from "./modals/AutomationErrorReportModal.vue"
import AdvancedSettingsModal from "./modals/AdvancedSettingsModal.vue";

import TabsView from "./views/TabsView.vue";
import UpdatesBar from "./views/shared_components/UpdatesBar.vue";

// Get the global emitter
const vueInstance = getCurrentInstance();
Expand Down Expand Up @@ -123,15 +124,6 @@ emitter?.on("signed-out", () => {
});

// Check for updates
enum UpdateStatus {
Unknown,
Error,
Checking,
Available,
NotAvailable,
Downloaded,
}

const updatesAvailable = ref(false);
const updateStatus = ref(UpdateStatus.Unknown);
let checkForUpdatesInterval: ReturnType<typeof setTimeout> | null = null;
Expand Down Expand Up @@ -325,46 +317,15 @@ onUnmounted(() => {
<template v-else>
<TabsView
v-if="!shouldHideTabsView"
:updates-available="updatesAvailable"
@check-for-updates-clicked="checkForUpdates(true)"
/>

<div v-if="updatesAvailable" class="updates-bar">
<p>
<strong>{{ t("app.updates.updateAvailable") }}</strong>
{{ t("app.updates.shouldUseLatestVersion") }}
</p>
<p class="text-muted">
<template v-if="platform === 'linux'">
{{ t("app.updates.installViaPackageManager") }}
</template>
<template v-else>
<template v-if="updateStatus == UpdateStatus.Checking">
{{ t("app.updates.loadingUpdateStatus") }}
</template>
<template v-else-if="updateStatus == UpdateStatus.Available">
{{ t("app.updates.downloadingUpdate") }}
</template>
<template v-else-if="updateStatus == UpdateStatus.Downloaded">
<button class="btn btn-primary" @click="restartToUpdateClicked">
{{ t("app.updates.restartToUpdate") }}
</button>
</template>
<template
v-else-if="
updateStatus == UpdateStatus.Error ||
updateStatus == UpdateStatus.NotAvailable
"
>
{{ t("app.updates.errorWithAutomaticUpdate") }}
<a href="#" @click="openURL('https://cyd.social/download/')">{{
t("app.updates.fromWebsite")
}}</a
>.
</template>
</template>
</p>
</div>
<UpdatesBar
v-if="updatesAvailable"
:update-status="updateStatus"
:platform="platform"
@restart-to-update-clicked="restartToUpdateClicked"
/>
</template>

<!-- Sign in modal -->
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
},
"updates": {
"updateAvailable": "Cyd update available.",
"dismiss": "Dismiss",
"shouldUseLatestVersion": "You should always use the latest version of Cyd.",
"installViaPackageManager": "Install updates using your operating system's package manager.",
"loadingUpdateStatus": "Loading update status...",
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ body {
position: absolute;
right: 10px;
bottom: 10px;
padding: 10px 20px;
/* Leaves room for the dismiss button, which the right-aligned text would
otherwise run into. */
padding: 10px 34px 10px 20px;
border: 1px solid #ffc56e;
border-radius: 1em;
background-color: #ffe289;
Expand All @@ -91,6 +93,13 @@ body {
margin: 0;
}

.updates-bar-close {
position: absolute;
top: 10px;
right: 10px;
font-size: 0.7em;
}

/* Modal styles */

.modal-backdrop.show {
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export enum UpdateStatus {
Unknown,
Error,
Checking,
Available,
NotAvailable,
Downloaded,
}

export type DeviceInfo = {
userEmail: string;
deviceDescription: string;
Expand Down
4 changes: 1 addition & 3 deletions src/renderer/src/views/TabsView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ describe("TabsView", () => {

const mountComponent = async () => {
wrapper = mount(TabsView, {
props: {
updatesAvailable: false,
},
props: {},
global: {
plugins: [i18n],
provide: {
Expand Down
4 changes: 0 additions & 4 deletions src/renderer/src/views/TabsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import { openURL } from "../util";

const { t } = useI18n();

defineProps<{
updatesAvailable: boolean;
}>();

const emit = defineEmits<{
checkForUpdatesClicked: [];
}>();
Expand Down
126 changes: 126 additions & 0 deletions src/renderer/src/views/shared_components/UpdatesBar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { mount } from "@vue/test-utils";
import UpdatesBar from "./UpdatesBar.vue";
import { UpdateStatus } from "../../types";
import { mockElectronAPI } from "../../test_util";
import i18n from "../../i18n";

const mountBar = (
updateStatus: UpdateStatus = UpdateStatus.Unknown,
platform: string = "linux",
) =>
mount(UpdatesBar, {
props: { updateStatus, platform },
global: {
plugins: [i18n],
},
});

const allStatuses = [
UpdateStatus.Unknown,
UpdateStatus.Error,
UpdateStatus.Checking,
UpdateStatus.Available,
UpdateStatus.NotAvailable,
UpdateStatus.Downloaded,
];

describe("UpdatesBar", () => {
beforeEach(() => {
vi.clearAllMocks();
mockElectronAPI();
});

it("shows the update message", () => {
const wrapper = mountBar();

expect(wrapper.find(".updates-bar").exists()).toBe(true);
expect(wrapper.text()).toContain("Cyd update available.");
expect(wrapper.text()).toContain(
"You should always use the latest version of Cyd.",
);
});

describe("dismissing", () => {
it("hides the bar when the dismiss button is clicked", async () => {
const wrapper = mountBar();

(wrapper.find(".updates-bar-close").element as HTMLElement).click();
await wrapper.vm.$nextTick();

expect(wrapper.find(".updates-bar").exists()).toBe(false);
});

it("stays hidden when the update status changes", async () => {
const wrapper = mountBar(UpdateStatus.Checking, "darwin");

(wrapper.find(".updates-bar-close").element as HTMLElement).click();
await wrapper.vm.$nextTick();
await wrapper.setProps({ updateStatus: UpdateStatus.Downloaded });

expect(wrapper.find(".updates-bar").exists()).toBe(false);
});

// Dismissing is unconditional: a bar the user closed stays closed even
// when it's showing something actionable.
it.each(allStatuses)(
"offers a dismiss button in update status %i",
(updateStatus) => {
for (const platform of ["linux", "darwin", "win32"]) {
const wrapper = mountBar(updateStatus, platform);
expect(wrapper.find(".updates-bar-close").exists()).toBe(true);
}
},
);
});

describe("on Linux", () => {
it("points at the package manager and never offers a restart", () => {
const wrapper = mountBar(UpdateStatus.Downloaded, "linux");

expect(wrapper.text()).toContain(
"Install updates using your operating system's package manager.",
);
expect(wrapper.find(".btn-primary").exists()).toBe(false);
});
});

describe("on other platforms", () => {
it("reports that it's checking", () => {
expect(mountBar(UpdateStatus.Checking, "darwin").text()).toContain(
"Loading update status...",
);
});

it("reports that it's downloading", () => {
expect(mountBar(UpdateStatus.Available, "darwin").text()).toContain(
"Downloading update...",
);
});

it("emits when the restart button is clicked", async () => {
const wrapper = mountBar(UpdateStatus.Downloaded, "darwin");

expect(wrapper.text()).toContain("Restart to Update");
(wrapper.find(".btn-primary").element as HTMLElement).click();
await wrapper.vm.$nextTick();

expect(wrapper.emitted("restartToUpdateClicked")).toBeTruthy();
});

it.each([UpdateStatus.Error, UpdateStatus.NotAvailable])(
"falls back to the website in update status %i",
async (updateStatus) => {
const wrapper = mountBar(updateStatus, "win32");

expect(wrapper.text()).toContain("Error with automatic update.");
(wrapper.find("a").element as HTMLElement).click();
await wrapper.vm.$nextTick();

expect(window.electron.openURL).toHaveBeenCalledWith(
"https://cyd.social/download/",
);
},
);
});
});
74 changes: 74 additions & 0 deletions src/renderer/src/views/shared_components/UpdatesBar.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<script setup lang="ts">
import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { openURL } from "../../util";
import { UpdateStatus } from "../../types";

const { t } = useI18n();

defineProps<{
updateStatus: UpdateStatus;
platform: string;
}>();

const emit = defineEmits<{
restartToUpdateClicked: [];
}>();

// Dismissing hides the bar for the rest of the session, whatever it's
// showing. Update information is still reachable from the hamburger menu's
// "check for updates". App unmounts this component when updates stop being
// available, which intentionally resets the dismissal, so a new update is
// shown even if an earlier one was dismissed.
const dismissed = ref(false);
</script>

<template>
<div v-if="!dismissed" class="updates-bar">
<button
type="button"
class="btn-close updates-bar-close"
:aria-label="t('app.updates.dismiss')"
@click="dismissed = true"
></button>
<p>
<strong>{{ t("app.updates.updateAvailable") }}</strong>
{{ t("app.updates.shouldUseLatestVersion") }}
</p>
<p class="text-muted">
<template v-if="platform === 'linux'">
{{ t("app.updates.installViaPackageManager") }}
</template>
<template v-else>
<template v-if="updateStatus == UpdateStatus.Checking">
{{ t("app.updates.loadingUpdateStatus") }}
</template>
<template v-else-if="updateStatus == UpdateStatus.Available">
{{ t("app.updates.downloadingUpdate") }}
</template>
<template v-else-if="updateStatus == UpdateStatus.Downloaded">
<button
class="btn btn-primary"
@click="emit('restartToUpdateClicked')"
>
{{ t("app.updates.restartToUpdate") }}
</button>
</template>
<template
v-else-if="
updateStatus == UpdateStatus.Error ||
updateStatus == UpdateStatus.NotAvailable
"
>
{{ t("app.updates.errorWithAutomaticUpdate") }}
<a href="#" @click="openURL('https://cyd.social/download/')">{{
t("app.updates.fromWebsite")
}}</a
>.
</template>
</template>
</p>
</div>
</template>

<style scoped></style>
Loading