import * as React from 'react'; import { shallow } from 'zustand/shallow'; import { Box, Button, Input, Modal, ModalClose, ModalDialog, Typography } from '@mui/joy'; import { Link } from './util/Link'; import { useSettingsStore } from '@/lib/store'; export const isValidOpenAIApiKey = (apiKey?: string) => !!apiKey && apiKey.startsWith('sk-') && apiKey.length > 40; /** * Component that allows the User to modify the application settings, * persisted on the client via localStorage. * * @param {boolean} open Whether the Settings modal is open * @param {() => void} onClose Call this to close the dialog from outside */ export function SettingsModal({ open, onClose }: { open: boolean, onClose: () => void; }) { const { apiKey, setApiKey } = useSettingsStore(state => ({ apiKey: state.apiKey, setApiKey: state.setApiKey, }), shallow); const handleApiKeyChange = (e: React.ChangeEvent) => setApiKey((e.target as HTMLInputElement).value); const handleApiKeyDown = (e: React.KeyboardEvent) => (e.key === 'Enter') && onClose(); const needsApiKey = !!process.env.REQUIRE_USER_API_KEYS; const isValidKey = isValidOpenAIApiKey(apiKey); return ( Settings Personal OpenAI API Key {needsApiKey ? '(required)' : '(not required)'} {!needsApiKey && ( This box lets you override the default API key )} ); }