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
50 changes: 50 additions & 0 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const state = vi.hoisted(() => ({
saveResetTimerDisplayModeMock: vi.fn(),
loadMenubarIconStyleMock: vi.fn(),
saveMenubarIconStyleMock: vi.fn(),
loadWeeklyWarningThresholdPercentMock: vi.fn(),
saveWeeklyWarningThresholdPercentMock: vi.fn(),
migrateLegacyTraySettingsMock: vi.fn(),
loadGlobalShortcutMock: vi.fn(),
saveGlobalShortcutMock: vi.fn(),
Expand Down Expand Up @@ -234,6 +236,8 @@ vi.mock("@/lib/settings", async () => {
saveResetTimerDisplayMode: state.saveResetTimerDisplayModeMock,
loadMenubarIconStyle: state.loadMenubarIconStyleMock,
saveMenubarIconStyle: state.saveMenubarIconStyleMock,
loadWeeklyWarningThresholdPercent: state.loadWeeklyWarningThresholdPercentMock,
saveWeeklyWarningThresholdPercent: state.saveWeeklyWarningThresholdPercentMock,
migrateLegacyTraySettings: state.migrateLegacyTraySettingsMock,
loadGlobalShortcut: state.loadGlobalShortcutMock,
saveGlobalShortcut: state.saveGlobalShortcutMock,
Expand Down Expand Up @@ -273,6 +277,8 @@ describe("App", () => {
state.saveResetTimerDisplayModeMock.mockReset()
state.loadMenubarIconStyleMock.mockReset()
state.saveMenubarIconStyleMock.mockReset()
state.loadWeeklyWarningThresholdPercentMock.mockReset()
state.saveWeeklyWarningThresholdPercentMock.mockReset()
state.migrateLegacyTraySettingsMock.mockReset()
state.loadGlobalShortcutMock.mockReset()
state.saveGlobalShortcutMock.mockReset()
Expand Down Expand Up @@ -311,6 +317,8 @@ describe("App", () => {
state.saveResetTimerDisplayModeMock.mockResolvedValue(undefined)
state.loadMenubarIconStyleMock.mockResolvedValue("provider")
state.saveMenubarIconStyleMock.mockResolvedValue(undefined)
state.loadWeeklyWarningThresholdPercentMock.mockResolvedValue(30)
state.saveWeeklyWarningThresholdPercentMock.mockResolvedValue(undefined)
state.migrateLegacyTraySettingsMock.mockResolvedValue(undefined)
state.loadGlobalShortcutMock.mockResolvedValue(null)
state.saveGlobalShortcutMock.mockResolvedValue(undefined)
Expand Down Expand Up @@ -606,6 +614,48 @@ describe("App", () => {
expect(state.traySetTitleMock).not.toHaveBeenCalled()
})

it("switches to weekly tray metric and alert styling when weekly remaining reaches the threshold", async () => {
state.invokeMock.mockImplementation(async (cmd: string) => {
if (cmd === "list_plugins") {
return [
{
id: "a",
name: "Alpha",
iconUrl: "icon-a",
primaryCandidates: ["Session"],
lines: [
{ type: "progress", label: "Session", scope: "overview" },
{ type: "progress", label: "Weekly", scope: "overview" },
],
},
]
}
return null
})
state.loadPluginSettingsMock.mockResolvedValueOnce({ order: ["a"], disabled: [] })

render(<App />)
await waitFor(() => expect(state.startBatchMock).toHaveBeenCalled())

state.probeHandlers?.onResult({
providerId: "a",
displayName: "Alpha",
iconUrl: "icon-a",
lines: [
{ type: "progress", label: "Session", used: 40, limit: 100, format: { kind: "percent" } },
{ type: "progress", label: "Weekly", used: 75, limit: 100, format: { kind: "percent" } },
],
})

await waitFor(() => {
const latestCall = state.renderTrayBarsIconMock.mock.calls.at(-1)?.[0]
expect(latestCall.tone).toBe("warning")
expect(latestCall.percentText).toBe("!25%")
})
await waitFor(() => expect(state.traySetIconAsTemplateMock).toHaveBeenLastCalledWith(false))
await waitFor(() => expect(state.traySetTitleMock).toHaveBeenLastCalledWith(""))
})

it("uses selected provider on detail view and keeps it on home/settings", async () => {
state.invokeMock.mockImplementation(async (cmd: string) => {
if (cmd === "list_plugins") {
Expand Down
9 changes: 9 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function App() {
setDisplayMode,
menubarIconStyle,
setMenubarIconStyle,
weeklyWarningThresholdPercent,
setWeeklyWarningThresholdPercent,
resetTimerDisplayMode,
setResetTimerDisplayMode,
setGlobalShortcut,
Expand All @@ -68,6 +70,8 @@ function App() {
setDisplayMode: state.setDisplayMode,
menubarIconStyle: state.menubarIconStyle,
setMenubarIconStyle: state.setMenubarIconStyle,
weeklyWarningThresholdPercent: state.weeklyWarningThresholdPercent,
setWeeklyWarningThresholdPercent: state.setWeeklyWarningThresholdPercent,
resetTimerDisplayMode: state.resetTimerDisplayMode,
setResetTimerDisplayMode: state.setResetTimerDisplayMode,
setGlobalShortcut: state.setGlobalShortcut,
Expand Down Expand Up @@ -101,6 +105,7 @@ function App() {
pluginStates,
displayMode,
menubarIconStyle,
weeklyWarningThresholdPercent,
activeView,
})

Expand All @@ -117,6 +122,7 @@ function App() {
setThemeMode,
setDisplayMode,
setMenubarIconStyle,
setWeeklyWarningThresholdPercent,
setResetTimerDisplayMode,
setGlobalShortcut,
setStartOnLogin,
Expand All @@ -133,12 +139,14 @@ function App() {
handleResetTimerDisplayModeChange,
handleResetTimerDisplayModeToggle,
handleMenubarIconStyleChange,
handleWeeklyWarningThresholdPercentChange,
} = useSettingsDisplayActions({
setThemeMode,
setDisplayMode,
resetTimerDisplayMode,
setResetTimerDisplayMode,
setMenubarIconStyle,
setWeeklyWarningThresholdPercent,
scheduleTrayIconUpdate,
})

Expand Down Expand Up @@ -247,6 +255,7 @@ function App() {
onResetTimerDisplayModeChange: handleResetTimerDisplayModeChange,
onResetTimerDisplayModeToggle: handleResetTimerDisplayModeToggle,
onMenubarIconStyleChange: handleMenubarIconStyleChange,
onWeeklyWarningThresholdPercentChange: handleWeeklyWarningThresholdPercentChange,
traySettingsPreview,
onGlobalShortcutChange: handleGlobalShortcutChange,
onStartOnLoginChange: handleStartOnLoginChange,
Expand Down
8 changes: 8 additions & 0 deletions src/components/app/app-content.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ function createProps(): AppContentProps {
onDisplayModeChange: vi.fn(),
onResetTimerDisplayModeChange: vi.fn(),
onResetTimerDisplayModeToggle: vi.fn(),
onMenubarIconStyleChange: vi.fn(),
onWeeklyWarningThresholdPercentChange: vi.fn(),
traySettingsPreview: {
bars: [],
providerBars: [],
providerPercentText: "--%",
providerAlertSeverity: "none" as const,
},
onGlobalShortcutChange: vi.fn(),
onStartOnLoginChange: vi.fn(),
}
Expand Down
7 changes: 7 additions & 0 deletions src/components/app/app-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
MenubarIconStyle,
ResetTimerDisplayMode,
ThemeMode,
WeeklyWarningThresholdPercent,
} from "@/lib/settings"

type AppContentDerivedProps = {
Expand All @@ -32,6 +33,7 @@ export type AppContentActionProps = {
onResetTimerDisplayModeChange: (mode: ResetTimerDisplayMode) => void
onResetTimerDisplayModeToggle: () => void
onMenubarIconStyleChange: (value: MenubarIconStyle) => void
onWeeklyWarningThresholdPercentChange: (value: WeeklyWarningThresholdPercent) => void
traySettingsPreview: TraySettingsPreview
onGlobalShortcutChange: (value: GlobalShortcut) => void
onStartOnLoginChange: (value: boolean) => void
Expand All @@ -52,6 +54,7 @@ export function AppContent({
onResetTimerDisplayModeChange,
onResetTimerDisplayModeToggle,
onMenubarIconStyleChange,
onWeeklyWarningThresholdPercentChange,
traySettingsPreview,
onGlobalShortcutChange,
onStartOnLoginChange,
Expand All @@ -67,6 +70,7 @@ export function AppContent({
resetTimerDisplayMode,
menubarIconStyle,
autoUpdateInterval,
weeklyWarningThresholdPercent,
globalShortcut,
themeMode,
startOnLogin,
Expand All @@ -76,6 +80,7 @@ export function AppContent({
resetTimerDisplayMode: state.resetTimerDisplayMode,
menubarIconStyle: state.menubarIconStyle,
autoUpdateInterval: state.autoUpdateInterval,
weeklyWarningThresholdPercent: state.weeklyWarningThresholdPercent,
globalShortcut: state.globalShortcut,
themeMode: state.themeMode,
startOnLogin: state.startOnLogin,
Expand Down Expand Up @@ -110,6 +115,8 @@ export function AppContent({
onResetTimerDisplayModeChange={onResetTimerDisplayModeChange}
menubarIconStyle={menubarIconStyle}
onMenubarIconStyleChange={onMenubarIconStyleChange}
weeklyWarningThresholdPercent={weeklyWarningThresholdPercent}
onWeeklyWarningThresholdPercentChange={onWeeklyWarningThresholdPercentChange}
traySettingsPreview={traySettingsPreview}
globalShortcut={globalShortcut}
onGlobalShortcutChange={onGlobalShortcutChange}
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/app/use-settings-bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
loadResetTimerDisplayModeMock,
loadStartOnLoginMock,
loadThemeModeMock,
loadWeeklyWarningThresholdPercentMock,
migrateLegacyTraySettingsMock,
normalizePluginSettingsMock,
savePluginSettingsMock,
Expand All @@ -36,6 +37,7 @@ const {
loadResetTimerDisplayModeMock: vi.fn(),
loadStartOnLoginMock: vi.fn(),
loadThemeModeMock: vi.fn(),
loadWeeklyWarningThresholdPercentMock: vi.fn(),
migrateLegacyTraySettingsMock: vi.fn(),
normalizePluginSettingsMock: vi.fn(),
savePluginSettingsMock: vi.fn(),
Expand All @@ -61,6 +63,7 @@ vi.mock("@/lib/settings", () => ({
DEFAULT_RESET_TIMER_DISPLAY_MODE: "relative",
DEFAULT_START_ON_LOGIN: false,
DEFAULT_THEME_MODE: "system",
DEFAULT_WEEKLY_WARNING_THRESHOLD_PERCENT: 30,
getEnabledPluginIds: getEnabledPluginIdsMock,
loadAutoUpdateInterval: loadAutoUpdateIntervalMock,
loadDisplayMode: loadDisplayModeMock,
Expand All @@ -70,6 +73,7 @@ vi.mock("@/lib/settings", () => ({
loadResetTimerDisplayMode: loadResetTimerDisplayModeMock,
loadStartOnLogin: loadStartOnLoginMock,
loadThemeMode: loadThemeModeMock,
loadWeeklyWarningThresholdPercent: loadWeeklyWarningThresholdPercentMock,
migrateLegacyTraySettings: migrateLegacyTraySettingsMock,
normalizePluginSettings: normalizePluginSettingsMock,
savePluginSettings: savePluginSettingsMock,
Expand All @@ -88,6 +92,7 @@ function createArgs() {
setGlobalShortcut: vi.fn(),
setStartOnLogin: vi.fn(),
setMenubarIconStyle: vi.fn(),
setWeeklyWarningThresholdPercent: vi.fn(),
setLoadingForPlugins: vi.fn(),
setErrorForPlugins: vi.fn(),
startBatch: vi.fn().mockResolvedValue(undefined),
Expand All @@ -111,6 +116,7 @@ describe("useSettingsBootstrap", () => {
loadResetTimerDisplayModeMock.mockReset()
loadStartOnLoginMock.mockReset()
loadThemeModeMock.mockReset()
loadWeeklyWarningThresholdPercentMock.mockReset()
migrateLegacyTraySettingsMock.mockReset()
normalizePluginSettingsMock.mockReset()
savePluginSettingsMock.mockReset()
Expand All @@ -136,6 +142,7 @@ describe("useSettingsBootstrap", () => {
loadResetTimerDisplayModeMock.mockResolvedValue("relative")
loadGlobalShortcutMock.mockResolvedValue("CommandOrControl+Shift+O")
loadMenubarIconStyleMock.mockResolvedValue("provider")
loadWeeklyWarningThresholdPercentMock.mockResolvedValue(30)
loadStartOnLoginMock.mockResolvedValue(true)
migrateLegacyTraySettingsMock.mockResolvedValue(undefined)
savePluginSettingsMock.mockResolvedValue(undefined)
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/app/use-settings-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DEFAULT_RESET_TIMER_DISPLAY_MODE,
DEFAULT_START_ON_LOGIN,
DEFAULT_THEME_MODE,
DEFAULT_WEEKLY_WARNING_THRESHOLD_PERCENT,
getEnabledPluginIds,
loadAutoUpdateInterval,
loadDisplayMode,
Expand All @@ -25,6 +26,7 @@ import {
loadResetTimerDisplayMode,
loadStartOnLogin,
loadThemeMode,
loadWeeklyWarningThresholdPercent,
normalizePluginSettings,
savePluginSettings,
type AutoUpdateIntervalMinutes,
Expand All @@ -34,6 +36,7 @@ import {
type PluginSettings,
type ResetTimerDisplayMode,
type ThemeMode,
type WeeklyWarningThresholdPercent,
} from "@/lib/settings"

type UseSettingsBootstrapArgs = {
Expand All @@ -46,6 +49,7 @@ type UseSettingsBootstrapArgs = {
setGlobalShortcut: (value: GlobalShortcut) => void
setStartOnLogin: (value: boolean) => void
setMenubarIconStyle: (value: MenubarIconStyle) => void
setWeeklyWarningThresholdPercent: (value: WeeklyWarningThresholdPercent) => void
setLoadingForPlugins: (ids: string[]) => void
setErrorForPlugins: (ids: string[], error: string) => void
startBatch: (pluginIds?: string[]) => Promise<string[] | undefined>
Expand All @@ -61,6 +65,7 @@ export function useSettingsBootstrap({
setGlobalShortcut,
setStartOnLogin,
setMenubarIconStyle,
setWeeklyWarningThresholdPercent,
setLoadingForPlugins,
setErrorForPlugins,
startBatch,
Expand Down Expand Up @@ -153,6 +158,13 @@ export function useSettingsBootstrap({
console.error("Failed to load menubar icon style:", error)
}

let storedWeeklyWarningThresholdPercent = DEFAULT_WEEKLY_WARNING_THRESHOLD_PERCENT
try {
storedWeeklyWarningThresholdPercent = await loadWeeklyWarningThresholdPercent()
} catch (error) {
console.error("Failed to load weekly warning threshold:", error)
}

if (isMounted) {
setPluginSettings(normalized)
setAutoUpdateInterval(storedInterval)
Expand All @@ -162,6 +174,7 @@ export function useSettingsBootstrap({
setGlobalShortcut(storedGlobalShortcut)
setStartOnLogin(storedStartOnLogin)
setMenubarIconStyle(storedMenubarIconStyle)
setWeeklyWarningThresholdPercent(storedWeeklyWarningThresholdPercent)

const enabledIds = getEnabledPluginIds(normalized)
setLoadingForPlugins(enabledIds)
Expand Down Expand Up @@ -192,6 +205,7 @@ export function useSettingsBootstrap({
setGlobalShortcut,
setLoadingForPlugins,
setMenubarIconStyle,
setWeeklyWarningThresholdPercent,
migrateLegacyTraySettings,
setPluginSettings,
setPluginsMeta,
Expand Down
Loading
Loading