mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: wire up Default Tool View save in General settings
The <select> for Default Tool View was an uncontrolled dead control with no value binding, no onChange handler, and no save mechanism. This wires it up end-to-end: - Add defaultToolView to the Zustand settings store - Load the persisted value from the settings API on mount - Bind the <select> with value/onChange - Add Save Settings button mirroring SystemSection's pattern - Redirect home page to /fullscreen when defaultToolView is "fullscreen" Closes #75
This commit is contained in:
@@ -27,6 +27,7 @@ import { useCallback, useEffect, useMemo, useState } from "react";
|
|||||||
import { useAuth } from "@/hooks/use-auth";
|
import { useAuth } from "@/hooks/use-auth";
|
||||||
import { apiDelete, apiGet, apiPost, apiPut, clearToken, formatHeaders } from "@/lib/api";
|
import { apiDelete, apiGet, apiPost, apiPut, clearToken, formatHeaders } from "@/lib/api";
|
||||||
import { cn, copyToClipboard } from "@/lib/utils";
|
import { cn, copyToClipboard } from "@/lib/utils";
|
||||||
|
import { useSettingsStore } from "@/stores/settings-store";
|
||||||
import { GemLogo } from "../common/gem-logo";
|
import { GemLogo } from "../common/gem-logo";
|
||||||
import { AiFeaturesSection } from "./ai-features-section";
|
import { AiFeaturesSection } from "./ai-features-section";
|
||||||
|
|
||||||
@@ -179,19 +180,29 @@ interface TeamEntry {
|
|||||||
function GeneralSection() {
|
function GeneralSection() {
|
||||||
const [user, setUser] = useState<SessionUser | null>(null);
|
const [user, setUser] = useState<SessionUser | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [defaultToolView, setDefaultToolView] = useState("sidebar");
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [saveMsg, setSaveMsg] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
apiGet<{ user: SessionUser }>("/auth/session")
|
Promise.all([
|
||||||
.then((data) => setUser(data.user))
|
apiGet<{ user: SessionUser }>("/auth/session")
|
||||||
.catch(() => {
|
.then((data) => setUser(data.user))
|
||||||
// Fallback to localStorage if session endpoint fails
|
.catch(() => {
|
||||||
setUser({
|
setUser({
|
||||||
id: 0,
|
id: 0,
|
||||||
username: localStorage.getItem("ashim-username") || "",
|
username: localStorage.getItem("ashim-username") || "",
|
||||||
role: "unknown",
|
role: "unknown",
|
||||||
});
|
});
|
||||||
})
|
}),
|
||||||
.finally(() => setLoading(false));
|
apiGet<{ settings: Record<string, string> }>("/v1/settings")
|
||||||
|
.then((data) => {
|
||||||
|
if (data.settings.defaultToolView) {
|
||||||
|
setDefaultToolView(data.settings.defaultToolView);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {}),
|
||||||
|
]).finally(() => setLoading(false));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
@@ -200,6 +211,23 @@ function GeneralSection() {
|
|||||||
window.location.href = "/login";
|
window.location.href = "/login";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
setSaving(true);
|
||||||
|
setSaveMsg(null);
|
||||||
|
try {
|
||||||
|
await apiPut("/v1/settings", { defaultToolView });
|
||||||
|
setSaveMsg("Settings saved.");
|
||||||
|
useSettingsStore.setState({
|
||||||
|
defaultToolView: defaultToolView as "sidebar" | "fullscreen",
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
setSaveMsg("Failed to save settings.");
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
setTimeout(() => setSaveMsg(null), 3000);
|
||||||
|
}
|
||||||
|
}, [defaultToolView]);
|
||||||
|
|
||||||
const username = user?.username || "admin";
|
const username = user?.username || "admin";
|
||||||
const role = user?.role || "unknown";
|
const role = user?.role || "unknown";
|
||||||
|
|
||||||
@@ -237,7 +265,11 @@ function GeneralSection() {
|
|||||||
|
|
||||||
{/* Default view */}
|
{/* Default view */}
|
||||||
<SettingRow label="Default Tool View" description="How tools are displayed on the home page">
|
<SettingRow label="Default Tool View" description="How tools are displayed on the home page">
|
||||||
<select className="px-3 py-1.5 rounded-lg border border-border bg-background text-sm text-foreground">
|
<select
|
||||||
|
value={defaultToolView}
|
||||||
|
onChange={(e) => setDefaultToolView(e.target.value)}
|
||||||
|
className="px-3 py-1.5 rounded-lg border border-border bg-background text-sm text-foreground"
|
||||||
|
>
|
||||||
<option value="sidebar">Sidebar</option>
|
<option value="sidebar">Sidebar</option>
|
||||||
<option value="fullscreen">Fullscreen Grid</option>
|
<option value="fullscreen">Fullscreen Grid</option>
|
||||||
</select>
|
</select>
|
||||||
@@ -247,6 +279,30 @@ function GeneralSection() {
|
|||||||
<SettingRow label="App Version" description="Current version of ashim">
|
<SettingRow label="App Version" description="Current version of ashim">
|
||||||
<span className="text-sm font-mono text-muted-foreground">{APP_VERSION}</span>
|
<span className="text-sm font-mono text-muted-foreground">{APP_VERSION}</span>
|
||||||
</SettingRow>
|
</SettingRow>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-3 pt-2">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleSave}
|
||||||
|
disabled={saving}
|
||||||
|
className="flex items-center gap-2 px-4 py-2 rounded-lg bg-primary text-primary-foreground text-sm font-medium hover:bg-primary/90 transition-colors disabled:opacity-50"
|
||||||
|
>
|
||||||
|
{saving && <Loader2 className="h-3.5 w-3.5 animate-spin" />}
|
||||||
|
Save Settings
|
||||||
|
</button>
|
||||||
|
{saveMsg && (
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"text-sm",
|
||||||
|
saveMsg.includes("Failed")
|
||||||
|
? "text-destructive"
|
||||||
|
: "text-green-600 dark:text-green-400",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{saveMsg}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export function HomePage() {
|
|||||||
currentEntry,
|
currentEntry,
|
||||||
} = useFileStore();
|
} = useFileStore();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { fetch: fetchSettings } = useSettingsStore();
|
const { fetch: fetchSettings, defaultToolView, loaded: settingsLoaded } = useSettingsStore();
|
||||||
const { fetch: fetchFeatures, isToolInstalled } = useFeaturesStore();
|
const { fetch: fetchFeatures, isToolInstalled } = useFeaturesStore();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -32,6 +32,12 @@ export function HomePage() {
|
|||||||
fetchFeatures();
|
fetchFeatures();
|
||||||
}, [fetchSettings, fetchFeatures]);
|
}, [fetchSettings, fetchFeatures]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (settingsLoaded && defaultToolView === "fullscreen" && files.length === 0) {
|
||||||
|
navigate("/fullscreen", { replace: true });
|
||||||
|
}
|
||||||
|
}, [settingsLoaded, defaultToolView, files.length, navigate]);
|
||||||
|
|
||||||
const handleFiles = useCallback(
|
const handleFiles = useCallback(
|
||||||
(newFiles: File[]) => {
|
(newFiles: File[]) => {
|
||||||
reset();
|
reset();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { apiGet } from "@/lib/api";
|
|||||||
interface SettingsState {
|
interface SettingsState {
|
||||||
disabledTools: string[];
|
disabledTools: string[];
|
||||||
experimentalEnabled: boolean;
|
experimentalEnabled: boolean;
|
||||||
|
defaultToolView: "sidebar" | "fullscreen";
|
||||||
loaded: boolean;
|
loaded: boolean;
|
||||||
fetch: () => Promise<void>;
|
fetch: () => Promise<void>;
|
||||||
}
|
}
|
||||||
@@ -11,6 +12,7 @@ interface SettingsState {
|
|||||||
export const useSettingsStore = create<SettingsState>((set, get) => ({
|
export const useSettingsStore = create<SettingsState>((set, get) => ({
|
||||||
disabledTools: [],
|
disabledTools: [],
|
||||||
experimentalEnabled: false,
|
experimentalEnabled: false,
|
||||||
|
defaultToolView: "sidebar",
|
||||||
loaded: false,
|
loaded: false,
|
||||||
|
|
||||||
fetch: async () => {
|
fetch: async () => {
|
||||||
@@ -23,10 +25,10 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
|
|||||||
set({
|
set({
|
||||||
disabledTools: data.settings.disabledTools ? JSON.parse(data.settings.disabledTools) : [],
|
disabledTools: data.settings.disabledTools ? JSON.parse(data.settings.disabledTools) : [],
|
||||||
experimentalEnabled: data.settings.enableExperimentalTools === "true",
|
experimentalEnabled: data.settings.enableExperimentalTools === "true",
|
||||||
|
defaultToolView: data.settings.defaultToolView === "fullscreen" ? "fullscreen" : "sidebar",
|
||||||
loaded: true,
|
loaded: true,
|
||||||
});
|
});
|
||||||
} catch {
|
} catch {
|
||||||
// Settings fetch failed - default to no disabled tools
|
|
||||||
set({ loaded: true });
|
set({ loaded: true });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user