Skip to content

Commit

Permalink
add opt out button
Browse files Browse the repository at this point in the history
  • Loading branch information
chuyouchia committed May 6, 2024
1 parent d6d41e2 commit a44af8c
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
2 changes: 2 additions & 0 deletions electron/main/Store/storeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ export interface StoreSchema {
chatHistories: {
[vaultDir: string]: ChatHistory[];
};
analytics?: boolean;
}

export enum StoreKeys {
hasUserOpenedAppBefore = "hasUserOpenedAppBefore",
Analytics = "analytics",
SchemaVersion = "schemaVersion",
DirectoryFromPreviousSession = "user.directoryFromPreviousSession",
LLMs = "LLMs",
Expand Down
10 changes: 10 additions & 0 deletions electron/main/Store/storeHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ export const registerStoreHandlers = (
return store.get(StoreKeys.LLMGenerationParameters);
});

ipcMain.handle("set-analytics-mode", (event, isAnalytics) => {
console.log("setting analytics mode", isAnalytics);
store.set(StoreKeys.Analytics, isAnalytics);
});

ipcMain.handle("get-analytics-mode", () => {
console.log("getting analytics params", store.get(StoreKeys.Analytics));
return store.get(StoreKeys.Analytics);
});

ipcMain.handle("has-user-opened-app-before", () => {
return store.get(StoreKeys.hasUserOpenedAppBefore);
});
Expand Down
8 changes: 8 additions & 0 deletions electron/main/Store/storeMigrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ export function setupDefaultStoreValues(store: Store<StoreSchema>) {
if (!store.get(StoreKeys.MaxRAGExamples)) {
store.set(StoreKeys.MaxRAGExamples, 15);
}
setupDefaultAnalyticsValue(store);

setupDefaultEmbeddingModels(store);

setupDefaultHardwareConfig(store);
}

const setupDefaultAnalyticsValue = (store: Store<StoreSchema>) => {
if (store.get(StoreKeys.Analytics) === undefined) {
store.set(StoreKeys.Analytics, true);
}
};

const setupDefaultHardwareConfig = (store: Store<StoreSchema>) => {
const hardwareConfig = store.get(StoreKeys.Hardware);

Expand Down
8 changes: 8 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ declare global {
setHardwareConfig: (config: HardwareConfig) => void;
getLLMGenerationParams: () => Promise<LLMGenerationParameters>;
setLLMGenerationParams: (params: LLMGenerationParameters) => void;
getAnalyticsMode: () => Promise<boolean>;
setAnalyticsMode: (isAnalytics: boolean) => void;
getHasUserOpenedAppBefore: () => boolean;
setHasUserOpenedAppBefore: () => void;
getAllChatHistories: () => Promise<ChatHistory[]>;
Expand Down Expand Up @@ -269,6 +271,12 @@ contextBridge.exposeInMainWorld("electronStore", {
setLLMGenerationParams: (params: LLMGenerationParameters) => {
ipcRenderer.invoke("set-llm-generation-params", params);
},
getAnalyticsMode: () => {
return ipcRenderer.invoke("get-analytics-mode");
},
setAnalyticsMode: (isAnalytics: boolean) => {
ipcRenderer.invoke("set-analytics-mode", isAnalytics);
},
getHasUserOpenedAppBefore: () => {
return ipcRenderer.invoke("has-user-opened-app-before");
},
Expand Down
9 changes: 6 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import IndexingProgress from "./components/IndexingProgress";
import InitialSetupSinglePage from "./components/Settings/InitialSettingsSinglePage";
import posthog from "posthog-js";

posthog.init("phc_xi4hFToX1cZU657yzge1VW0XImaaRzuvnFUdbAKI8fu", {
api_host: "https://us.i.posthog.com",
});
if (await window.electronStore.getAnalyticsMode()) {
posthog.init("phc_xi4hFToX1cZU657yzge1VW0XImaaRzuvnFUdbAKI8fu", {
api_host: "https://us.i.posthog.com",
});
console.log("initializing posthog");
}

interface AppProps {}

Expand Down
63 changes: 63 additions & 0 deletions src/components/Settings/AnalyticsSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useState, useEffect } from "react";
import { Button } from "@material-tailwind/react";
import Switch from "@mui/material/Switch";

interface AnalyticsSettingsProps {}
const AnalyticsSettings: React.FC<AnalyticsSettingsProps> = () => {
const [isAnalyticsEnabled, setIsAnalyticsEnabled] = useState<boolean>(false);

const [userHasMadeUpdate, setUserHasMadeUpdate] = useState(false);

useEffect(() => {
const fetchParams = async () => {
const isAnalyticsEnabled = await window.electronStore.getAnalyticsMode();

if (isAnalyticsEnabled !== undefined) {
setIsAnalyticsEnabled(isAnalyticsEnabled);
}
};

fetchParams();
}, []);

const handleSave = () => {
// Execute the save function here
if (isAnalyticsEnabled !== undefined) {
window.electronStore.setAnalyticsMode(isAnalyticsEnabled);
setUserHasMadeUpdate(false);
}
};

return (
<div className="w-full bg-neutral-800 rounded pb-7 ">
<h2 className="text-2xl font-semibold mb-0 text-white">Analytics mode</h2>{" "}
<Switch
checked={isAnalyticsEnabled}
onChange={() => {
setUserHasMadeUpdate(true);
setIsAnalyticsEnabled(!isAnalyticsEnabled);
}}
// inputProps={{ "aria-label": "controlled" }}
/>
{userHasMadeUpdate && (
<div className="flex">
<Button
// variant="contained"
placeholder={""}
onClick={handleSave}
className="bg-orange-700 w-[150px] border-none h-8 hover:bg-orange-900 cursor-pointer text-center pt-0 pb-0 pr-2 pl-2 mb-0 mr-4 mt-2"
>
Save
</Button>
</div>
)}
{!isAnalyticsEnabled && (
<p className="text-yellow-500 text-xs">
Quit and restart the app for it to take effect
</p>
)}
</div>
);
};

export default AnalyticsSettings;
18 changes: 18 additions & 0 deletions src/components/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EmbeddingModelSettings from "./EmbeddingSettings";
import RagSettings from "./RagSettings";
import HardwareSettings from "./HardwareSettings";
import TextGenerationSettings from "./TextGenerationSettings";
import AnalyticsSettings from "./AnalyticsSettings";

interface ModalProps {
isOpen: boolean;
Expand All @@ -17,6 +18,7 @@ enum SettingsTab {
Hardware = "hardware",
TextGeneration = "textGeneration",
RAG = "RAG",
ANALYTICS = "analytics",
}

const SettingsModal: React.FC<ModalProps> = ({
Expand Down Expand Up @@ -97,6 +99,16 @@ const SettingsModal: React.FC<ModalProps> = ({
>
RAG{" "}
</div>
<div
className={`flex items-center rounded cursor-pointer p-2 border-b border-gray-200 hover:bg-neutral-600 text-sm ${
activeTab === SettingsTab.ANALYTICS
? "bg-neutral-700 text-white font-semibold"
: "text-gray-200"
}`}
onClick={() => setActiveTab(SettingsTab.ANALYTICS)}
>
Analytics{" "}
</div>
</div>

{/* Right Content Area */}
Expand Down Expand Up @@ -134,6 +146,12 @@ const SettingsModal: React.FC<ModalProps> = ({
</div>
)}

{activeTab === SettingsTab.ANALYTICS && (
<div className="w-full">
<AnalyticsSettings />
</div>
)}

{activeTab === SettingsTab.RAG && (
<div className="w-full">
<RagSettings>
Expand Down

0 comments on commit a44af8c

Please sign in to comment.