Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable AI buttons if AI API key is invalid #106

Merged
merged 3 commits into from
Dec 26, 2024
Merged
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
12 changes: 12 additions & 0 deletions src/renderer/context/AIContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ export const AIContextProvider = ({ children }) => {
[prompt]
);

const checkApiKeyValidity = async () => {
// TODO: Add regex for OpenAPI and Ollama API keys
const key = await window.electron.ipc.invoke('get-ai-key');

if (key !== null) {
return true;
}

return false;
}

const AIContextValue = {
ai,
baseUrl,
Expand All @@ -131,6 +142,7 @@ export const AIContextProvider = ({ children }) => {
setPrompt,
setKey: (secretKey) => window.electron.ipc.invoke('set-ai-key', secretKey),
getKey: () => window.electron.ipc.invoke('get-ai-key'),
validKey: checkApiKeyValidity,
deleteKey: () => window.electron.ipc.invoke('delete-ai-key'),
updateSettings: (newPrompt) =>
updateCurrentPile({ ...currentPile, AIPrompt: newPrompt }),
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Pile/Chat/Chat.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
&:active {
background: var(--bg-tertiary);
}

&.disabled {
pointer-events: none; // Prevent interaction
opacity: 0.5; // Make the whole container look disabled
}
}

button,
Expand Down
16 changes: 15 additions & 1 deletion src/renderer/pages/Pile/Chat/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ import Blobs from './Blobs';
import useChat from 'renderer/hooks/useChat';

export default function Chat() {
const { validKey } = useAIContext();
const { currentTheme, setTheme } = usePilesContext();
const { getAIResponse, addMessage, resetMessages } = useChat();
const [container, setContainer] = useState(null);
const [ready, setReady] = useState(false);
const [text, setText] = useState('');
const [querying, setQuerying] = useState(false);
const [history, setHistory] = useState([]);
const [aiApiKeyValid, setAiApiKeyValid] = useState(false);

// Check if the AI API key is valid
useEffect(() => {
const checkApiKeyValid = async () => {
const valid = await validKey();
setAiApiKeyValid(valid);
};
checkApiKeyValid();
}, [validKey]);

const onChangeText = (e) => {
setText(e.target.value);
Expand Down Expand Up @@ -92,7 +103,10 @@ export default function Chat() {
<>
<Dialog.Root>
<Dialog.Trigger asChild>
<div className={styles.iconHolder}>
<div
className={`${styles.iconHolder} ${!aiApiKeyValid ? styles.disabled : ''}`}
onClick={!aiApiKeyValid ? (e) => e.preventDefault() : undefined} // Prevent click if no AI api key is set
>
<ChatIcon className={styles.chatIcon} />
</div>
</Dialog.Trigger>
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/pages/Pile/Posts/Post/Post.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@
filter: contrast(0.85);
}

&:disabled {
color: var(--secondary);
cursor: not-allowed; // Prevent pointer cursor
pointer-events: none; // Disable any interactions (e.g., clicks)
opacity: 0.5; // Reduce opacity to visually indicate it's disabled
}

.icon {
display: flex;
justify-content: space-between;
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/pages/Pile/Posts/Post/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@ import {
import { useTimelineContext } from 'renderer/context/TimelineContext';
import Ball from './Ball';
import { useHighlightsContext } from 'renderer/context/HighlightsContext';
import { useAIContext } from 'renderer/context/AIContext';

const Post = memo(({ postPath, searchTerm = null, repliesCount = 0 }) => {
const { currentPile, getCurrentPilePath } = usePilesContext();
const { highlights } = useHighlightsContext();
const { validKey } = useAIContext();
// const { setClosestDate } = useTimelineContext();
const { post, cycleColor, refreshPost, setHighlight } = usePost(postPath);
const [hovering, setHover] = useState(false);
const [replying, setReplying] = useState(false);
const [isAIResplying, setIsAiReplying] = useState(false);
const [editable, setEditable] = useState(false);
const [aiApiKeyValid, setAiApiKeyValid] = useState(false);

// Check if the AI API key is valid
useEffect(() => {
const checkApiKeyValid = async () => {
const valid = await validKey();
setAiApiKeyValid(valid);
};
checkApiKeyValid();
}, [validKey]);

const closeReply = () => {
setReplying(false);
Expand Down Expand Up @@ -157,6 +169,7 @@ const Post = memo(({ postPath, searchTerm = null, repliesCount = 0 }) => {
<div className={styles.sep}>/</div>
<button
className={styles.openReply}
disabled={!aiApiKeyValid}
onClick={() => {
setIsAiReplying(true);
toggleReplying();
Expand Down
Loading