Skip to content
Open
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
56 changes: 19 additions & 37 deletions apps/obsidian/src/components/AdminPanelSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,27 @@ export const AdminPanelSettings = () => {
const [syncModeEnabled, setSyncModeEnabled] = useState<boolean>(
plugin.settings.syncModeEnabled ?? false,
);
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);

const handleSyncModeToggle = useCallback((newValue: boolean) => {
setSyncModeEnabled(newValue);
setHasUnsavedChanges(true);
}, []);
const handleSyncModeToggle = useCallback(
async (newValue: boolean) => {
setSyncModeEnabled(newValue);
plugin.settings.syncModeEnabled = newValue;
await plugin.saveSettings();

const handleSave = async () => {
plugin.settings.syncModeEnabled = syncModeEnabled;
await plugin.saveSettings();
new Notice("Admin panel settings saved");
setHasUnsavedChanges(false);

if (syncModeEnabled) {
try {
await initializeSupabaseSync(plugin);
new Notice("Sync mode initialized successfully");
} catch (error) {
console.error("Failed to initialize sync mode:", error);
new Notice(
`Failed to initialize sync mode: ${error instanceof Error ? error.message : String(error)}`,
);
if (newValue) {
try {
await initializeSupabaseSync(plugin);
new Notice("Sync mode initialized successfully");
} catch (error) {
console.error("Failed to initialize sync mode:", error);
new Notice(
`Failed to initialize sync mode: ${error instanceof Error ? error.message : String(error)}`,
);
}
}
}
};
},
[plugin],
);

return (
<div className="general-settings">
Expand All @@ -46,26 +42,12 @@ export const AdminPanelSettings = () => {
<div className="setting-item-control">
<div
className={`checkbox-container ${syncModeEnabled ? "is-enabled" : ""}`}
onClick={() => handleSyncModeToggle(!syncModeEnabled)}
onClick={() => void handleSyncModeToggle(!syncModeEnabled)}
>
<input type="checkbox" checked={syncModeEnabled} />
</div>
</div>
</div>

<div className="setting-item">
<button
onClick={() => void handleSave()}
className={hasUnsavedChanges ? "mod-cta" : ""}
disabled={!hasUnsavedChanges}
>
Save Changes
</button>
</div>

{hasUnsavedChanges && (
<div className="text-muted mt-2">You have unsaved changes</div>
)}
</div>
);
};
85 changes: 33 additions & 52 deletions apps/obsidian/src/components/GeneralSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useCallback, useRef, useEffect } from "react";
import { usePlugin } from "./PluginContext";
import { Notice, setIcon } from "obsidian";
import { setIcon } from "obsidian";
import SuggestInput from "./SuggestInput";
import { SLACK_LOGO, WHITE_LOGO_SVG } from "~/icons";

Expand Down Expand Up @@ -157,57 +157,51 @@ const GeneralSettings = () => {
const [nodeTagHotkey, setNodeTagHotkey] = useState<string>(
plugin.settings.nodeTagHotkey,
);
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);

const handleToggleChange = (newValue: boolean) => {
setShowIdsInFrontmatter(newValue);
setHasUnsavedChanges(true);
plugin.settings.showIdsInFrontmatter = newValue;
void plugin.saveSettings();
};

const handleFolderPathChange = useCallback((newValue: string) => {
setNodesFolderPath(newValue);
setHasUnsavedChanges(true);
}, []);
const handleFolderPathChange = useCallback(
(newValue: string) => {
setNodesFolderPath(newValue);
plugin.settings.nodesFolderPath = newValue.trim();
void plugin.saveSettings();
},
[plugin],
);

const handleCanvasFolderPathChange = useCallback((newValue: string) => {
setCanvasFolderPath(newValue);
setHasUnsavedChanges(true);
}, []);
const handleCanvasFolderPathChange = useCallback(
(newValue: string) => {
setCanvasFolderPath(newValue);
plugin.settings.canvasFolderPath = newValue.trim();
void plugin.saveSettings();
},
[plugin],
);

const handleCanvasAttachmentsFolderPathChange = useCallback(
(newValue: string) => {
setCanvasAttachmentsFolderPath(newValue);
setHasUnsavedChanges(true);
plugin.settings.canvasAttachmentsFolderPath = newValue.trim();
void plugin.saveSettings();
},
[],
[plugin],
);

const handleNodeTagHotkeyChange = useCallback((newValue: string) => {
// Only allow single character
if (newValue.length <= 1) {
setNodeTagHotkey(newValue);
setHasUnsavedChanges(true);
}
}, []);

const handleSave = async () => {
const trimmedNodesFolderPath = nodesFolderPath.trim();
const trimmedCanvasFolderPath = canvasFolderPath.trim();
const trimmedCanvasAttachmentsFolderPath =
canvasAttachmentsFolderPath.trim();
plugin.settings.showIdsInFrontmatter = showIdsInFrontmatter;
plugin.settings.nodesFolderPath = trimmedNodesFolderPath;
plugin.settings.canvasFolderPath = trimmedCanvasFolderPath;
plugin.settings.canvasAttachmentsFolderPath =
trimmedCanvasAttachmentsFolderPath;
plugin.settings.nodeTagHotkey = nodeTagHotkey || "";
setNodesFolderPath(trimmedNodesFolderPath);
setCanvasFolderPath(trimmedCanvasFolderPath);
setCanvasAttachmentsFolderPath(trimmedCanvasAttachmentsFolderPath);
await plugin.saveSettings();
new Notice("General settings saved");
setHasUnsavedChanges(false);
};
const handleNodeTagHotkeyChange = useCallback(
(newValue: string) => {
// Only allow single character
if (newValue.length <= 1) {
setNodeTagHotkey(newValue);
plugin.settings.nodeTagHotkey = newValue;
void plugin.saveSettings();
}
},
[plugin],
);

return (
<div className="general-settings">
Expand Down Expand Up @@ -313,19 +307,6 @@ const GeneralSettings = () => {
</div>
</div>

<div className="setting-item">
<button
onClick={() => void handleSave()}
className={hasUnsavedChanges ? "mod-cta" : ""}
disabled={!hasUnsavedChanges}
>
Save Changes
</button>
</div>

{hasUnsavedChanges && (
<div className="text-muted mt-2">You have unsaved changes</div>
)}
<InfoSection />
</div>
);
Expand Down
Loading
Loading