Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
// @ts-ignore: Unused imports
import { Create as $Create } from "@wailsio/runtime";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import * as updater$0 from "../../../../focusd-so/focusd/internal/updater/models.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import * as usage$0 from "../../../../focusd-so/focusd/internal/usage/models.js";

function configure() {
Object.freeze(Object.assign($Create.Events, {
"protection:status": $$createType0,
"usage:update": $$createType1,
"update:available": $$createType2,
"usage:update": $$createType3,
}));
}

// Private type creation functions
const $$createType0 = usage$0.ProtectionPause.createFrom;
const $$createType1 = usage$0.ApplicationUsage.createFrom;
const $$createType1 = updater$0.UpdateInfo.createFrom;
const $$createType2 = $Create.Nullable($$createType1);
const $$createType3 = usage$0.ApplicationUsage.createFrom;

configure();
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// @ts-ignore: Unused imports
import type { Events } from "@wailsio/runtime";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import type * as updater$0 from "../../../../focusd-so/focusd/internal/updater/models.js";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import type * as usage$0 from "../../../../focusd-so/focusd/internal/usage/models.js";
Expand All @@ -14,6 +17,7 @@ declare module "@wailsio/runtime" {
interface CustomEvents {
"authctx:updated": any;
"protection:status": usage$0.ProtectionPause;
"update:available": updater$0.UpdateInfo | null;
"usage:update": usage$0.ApplicationUsage;
}
}
Expand Down
23 changes: 22 additions & 1 deletion frontend/src/components/settings/general-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSettingsStore } from "@/stores/settings-store";
import { SettingsKey } from "../../../bindings/github.com/focusd-so/focusd/internal/settings/models";

export function GeneralSettings() {
const { idleThreshold, historyRetention, distractionAllowance, updateSetting } = useSettingsStore();
const { idleThreshold, historyRetention, distractionAllowance, autoUpdate, updateSetting } = useSettingsStore();

return (
<div className="space-y-6">
Expand All @@ -18,6 +18,27 @@ export function GeneralSettings() {
</CardHeader>
<CardContent className="space-y-6">

<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label className="text-sm font-medium">App updates</Label>
<div className="text-sm text-muted-foreground">
Install new versions automatically or show a manual update prompt.
</div>
</div>
<Select
value={autoUpdate ? "automatic" : "manual"}
onValueChange={(val) => updateSetting(SettingsKey.SettingsKeyAutoUpdate, String(val === "automatic"))}
>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Select update mode" />
</SelectTrigger>
<SelectContent>
<SelectItem value="automatic">Automatic</SelectItem>
<SelectItem value="manual">Manual</SelectItem>
</SelectContent>
</Select>
</div>

<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label className="text-sm font-medium">Idle timeout</Label>
Expand Down
80 changes: 80 additions & 0 deletions frontend/src/components/update-status.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { useEffect, useState } from "react";
import { IconDownload } from "@tabler/icons-react";
import { Button } from "@/components/ui/button";
import { useSettingsStore } from "@/stores/settings-store";
import { ApplyUpdate, GetPendingUpdate, RefreshPendingUpdate } from "../../bindings/github.com/focusd-so/focusd/internal/updater/service";
import type { UpdateInfo } from "../../bindings/github.com/focusd-so/focusd/internal/updater/models";

export function UpdateStatus() {
const autoUpdate = useSettingsStore((state) => state.autoUpdate);
const [pendingUpdate, setPendingUpdate] = useState<UpdateInfo | null>(null);
const [isApplying, setIsApplying] = useState(false);

useEffect(() => {
if (autoUpdate) {
setPendingUpdate(null);
return;
}

let active = true;
const handlePendingUpdate = (event: Event) => {
if (!active) {
return;
}

setPendingUpdate((event as CustomEvent<UpdateInfo | null>).detail ?? null);
};

window.addEventListener("update:available", handlePendingUpdate);

const loadPendingUpdate = async () => {
try {
const currentPending = await GetPendingUpdate();
if (active) {
setPendingUpdate(currentPending);
}

const latestPending = await RefreshPendingUpdate();
if (active) {
setPendingUpdate(latestPending);
}
} catch (error) {
console.error("Failed to fetch pending update:", error);
}
};

void loadPendingUpdate();

return () => {
active = false;
window.removeEventListener("update:available", handlePendingUpdate);
};
}, [autoUpdate]);

if (autoUpdate || pendingUpdate == null) {
return null;
}

const handleApplyUpdate = async () => {
try {
setIsApplying(true);
await ApplyUpdate();
} catch (error) {
console.error("Failed to apply update:", error);
setIsApplying(false);
}
};

return (
<Button
variant="outline"
size="sm"
className="h-7 border-emerald-500/40 bg-emerald-500/10 text-emerald-400 hover:bg-emerald-500/20 hover:text-emerald-300 text-xs gap-1.5"
onClick={handleApplyUpdate}
disabled={isApplying}
>
<IconDownload className="w-3.5 h-3.5" />
<span>{isApplying ? "Updating..." : `Update ${pendingUpdate.version}`}</span>
</Button>
);
}
6 changes: 5 additions & 1 deletion frontend/src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useOnboardingStore } from "@/stores/onboarding-store";
import { useEffect } from "react";
import { StartObserver, EnableLoginItem } from "../../bindings/github.com/focusd-so/focusd/internal/native/nativeservice";
import { AccountStatus } from "@/components/account-status";
import { UpdateStatus } from "@/components/update-status";

const routeTitles: Record<string, string> = {
"/activity": "Smart Blocking",
Expand Down Expand Up @@ -81,7 +82,10 @@ function RootLayout() {
</h1>
)}
</div>
<AccountStatus />
<div className="flex items-center gap-2">
<UpdateStatus />
<AccountStatus />
</div>
</header>
<div className="flex flex-1 flex-col h-full overflow-hidden">
<Outlet />
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/stores/settings-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface SettingsState {
idleThreshold: string;
historyRetention: string;
distractionAllowance: string;
autoUpdate: boolean;
customRulesHistory: Settings[];
isLoading: boolean;
error: string | null;
Expand All @@ -23,12 +24,21 @@ interface SettingsState {
getSettingValue: (key: string) => string | undefined;
}

function parseBooleanSetting(value: string | undefined, fallback: boolean) {
if (value == null || value === "") {
return fallback;
}

return value.toLowerCase() !== "false";
}

export const useSettingsStore = create<SettingsState>()((set, get) => ({
settings: [],
customRules: "",
idleThreshold: "120", // default 120
historyRetention: "7", // default 7
distractionAllowance: "0", // default 0 / unlimited
autoUpdate: true,
customRulesHistory: [],
isLoading: false,
error: null,
Expand All @@ -49,13 +59,18 @@ export const useSettingsStore = create<SettingsState>()((set, get) => ({
const distractionAllowance =
settings?.find((s) => s.key === SettingsKey.SettingsKeyDistractionAllowance)
?.value || "0";
const autoUpdate = parseBooleanSetting(
settings?.find((s) => s.key === SettingsKey.SettingsKeyAutoUpdate)?.value,
true
);

set({
settings: settings || [],
customRules,
idleThreshold,
historyRetention,
distractionAllowance,
autoUpdate,
isLoading: false
});
} catch (error) {
Expand Down Expand Up @@ -90,6 +105,8 @@ export const useSettingsStore = create<SettingsState>()((set, get) => ({
set({ historyRetention: value });
} else if (key === SettingsKey.SettingsKeyDistractionAllowance) {
set({ distractionAllowance: value });
} else if (key === SettingsKey.SettingsKeyAutoUpdate) {
set({ autoUpdate: parseBooleanSetting(value, true) });
}

// Refresh to get the updated version from backend
Expand Down
1 change: 1 addition & 0 deletions internal/settings/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
SettingsKeyIdleThreshold SettingsKey = "idle_threshold"
SettingsKeyHistoryRetention SettingsKey = "history_retention"
SettingsKeyDistractionAllowance SettingsKey = "distraction_allowance"
SettingsKeyAutoUpdate SettingsKey = "auto_update"
)

type Settings struct {
Expand Down
Loading