Skip to content

Commit

Permalink
Add warning when no API key is set (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichochar authored Jul 19, 2024
1 parent 97d59c0 commit e220f58
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
78 changes: 51 additions & 27 deletions packages/web/src/components/cells/code.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useHotkeys } from 'react-hotkeys-hook';
import CodeMirror, { keymap, Prec } from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
Expand Down Expand Up @@ -34,6 +35,7 @@ export default function CodeCell(props: {
channel: SessionChannel;
onUpdateCell: (cell: CodeCellType, attrs: CodeCellUpdateAttrsType) => Promise<string | null>;
onDeleteCell: (cell: CellType) => void;
hasOpenaiKey: boolean;
}) {
const { session, cell, channel, onUpdateCell, onDeleteCell } = props;
const [filenameError, _setFilenameError] = useState<string | null>(null);
Expand All @@ -42,11 +44,14 @@ export default function CodeCell(props: {
const [prompt, setPrompt] = useState('');
const [newSource, setNewSource] = useState('');

const navigate = useNavigate();

useHotkeys(
'mod+enter',
() => {
if (!prompt) return;
if (promptMode !== 'idle') return;
if (!props.hasOpenaiKey) return;
generate();
},
{ enableOnFormTags: ['textarea'] },
Expand Down Expand Up @@ -194,7 +199,12 @@ export default function CodeCell(props: {
<Sparkles size={16} />
</Button>
{promptMode === 'idle' && (
<Button variant="default" onClick={generate} tabIndex={1}>
<Button
variant="default"
onClick={generate}
tabIndex={1}
disabled={!props.hasOpenaiKey}
>
Generate
</Button>
)}
Expand All @@ -221,34 +231,48 @@ export default function CodeCell(props: {
</div>
</div>
{promptMode !== 'off' && (
<div className="flex items-start justify-between px-1">
<div className="flex items-start flex-grow">
<Sparkles size={16} className="m-2.5" />
<TextareaAutosize
className="flex w-full rounded-sm bg-transparent px-3 py-2 text-sm placeholder:text-muted-foreground focus-visible:outline-none resize-none"
autoFocus
placeholder="Ask the AI to edit this cell..."
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
/>
</div>
<div className="flex items-center gap-1.5">
<AiGenerateTipsDialog>
<Button size="icon" variant="icon">
<MessageCircleWarning size={16} />
<div className="flex flex-col gap-1.5">
<div className="flex items-start justify-between px-1">
<div className="flex items-start flex-grow">
<Sparkles size={16} className="m-2.5" />
<TextareaAutosize
className="flex w-full rounded-sm bg-transparent px-3 py-2 text-sm placeholder:text-muted-foreground focus-visible:outline-none resize-none"
autoFocus
placeholder="Ask the AI to edit this cell..."
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
/>
</div>
<div className="flex items-center gap-1.5">
<AiGenerateTipsDialog>
<Button size="icon" variant="icon">
<MessageCircleWarning size={16} />
</Button>
</AiGenerateTipsDialog>
<Button
size="icon"
variant="icon"
onClick={() => {
setPromptMode('off');
setPrompt('');
}}
>
<X size={16} />
</Button>
</AiGenerateTipsDialog>
<Button
size="icon"
variant="icon"
onClick={() => {
setPromptMode('off');
setPrompt('');
}}
>
<X size={16} />
</Button>
</div>
</div>

{!props.hasOpenaiKey && (
<div className="flex items-center justify-between bg-warning text-warning-foreground rounded-sm text-sm px-3 py-1 m-3">
<p>API key required</p>
<a
className="font-medium underline cursor-pointer"
onClick={() => navigate('/settings')}
>
Settings
</a>
</div>
)}
</div>
)}

Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
--inline-code-foreground: var(--foreground);
--error: var(--sb-red-30);
--error-foreground: var(--sb-red-80);
--warning: var(--sb-yellow-20);
--warning-foreground: var(--sb-yellow-80);
}

.dark {
Expand All @@ -78,6 +80,8 @@
--inline-code-foreground: var(--foreground);
--error: var(--sb-red-30);
--error-foreground: var(--sb-red-80);
--warning: var(--sb-yellow-20);
--warning-foreground: var(--sb-yellow-80);
}

/* shadcn variables light */
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/routes/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function Session(props: { session: SessionType; channel: SessionChannel; config:
channel={channel}
onUpdateCell={onUpdateCell}
onDeleteCell={onDeleteCell}
hasOpenaiKey={!!props.config.openaiKey}
/>
)}

Expand Down
4 changes: 4 additions & 0 deletions packages/web/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ module.exports = {
DEFAULT: 'hsl(var(--error))',
foreground: 'hsl(var(--error-foreground))',
},
warning: {
DEFAULT: 'hsl(var(--warning))',
foreground: 'hsl(var(--warning-foreground))',
},
sb: {
'core-0': 'hsl(var(--sb-core-0))',
'core-10': 'hsl(var(--sb-core-10))',
Expand Down

0 comments on commit e220f58

Please sign in to comment.