-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[editor] Support adding prompt at the top of the config (#682)
[editor] Support adding prompt at the top of the config # [editor] Support adding prompt at the top of the config Add the 'AddPromptButton' at index 0 to support adding a prompt at the top of the config: https://github.com/lastmile-ai/aiconfig/assets/5060851/76a28aa6-20ce-44e7-bbce-1714b5865a2c --- Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/lastmile-ai/aiconfig/pull/682). * #686 * __->__ #682 * #681 * #680
- Loading branch information
Showing
8 changed files
with
242 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
python/src/aiconfig/editor/client/src/components/ConfigNameDescription.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { | ||
createStyles, | ||
Stack, | ||
Text, | ||
Textarea, | ||
TextInput, | ||
Title, | ||
} from "@mantine/core"; | ||
import { useClickOutside } from "@mantine/hooks"; | ||
import { memo, useCallback, useRef, useState } from "react"; | ||
|
||
type Props = { | ||
name?: string; | ||
description?: string | null; | ||
setDescription: (description: string) => void; | ||
setName: (name: string) => void; | ||
}; | ||
|
||
const useStyles = createStyles((theme) => ({ | ||
// Match name input style to corresponding Title element styles | ||
// See https://github.com/mantinedev/mantine/blob/master/src/mantine-core/src/Title/Title.styles.ts | ||
nameInput: { | ||
...theme.fn.fontStyles(), | ||
fontFamily: theme.headings.fontFamily, | ||
fontWeight: theme.headings.fontWeight as number, | ||
fontSize: theme.headings.sizes.h1.fontSize, | ||
lineHeight: theme.headings.sizes.h1.lineHeight, | ||
width: "-webkit-fill-available", | ||
letterSpacing: "-1px", | ||
height: "44px", | ||
}, | ||
hoverContainer: { | ||
"&:hover": { | ||
backgroundColor: | ||
theme.colorScheme === "dark" | ||
? "rgba(255, 255, 255, 0.1)" | ||
: theme.colors.gray[1], | ||
}, | ||
borderRadius: theme.radius.sm, | ||
width: "-webkit-fill-available", | ||
}, | ||
})); | ||
|
||
export default memo(function ConfigNameDescription({ | ||
name, | ||
description, | ||
setDescription, | ||
setName, | ||
}: Props) { | ||
const { classes } = useStyles(); | ||
|
||
const [isEditing, setIsEditing] = useState(!name); | ||
const [initialFocus, setInitialFocus] = useState< | ||
"name" | "description" | null | ||
>("name"); | ||
|
||
const nameDisplayRef = useRef<HTMLHeadingElement | null>(null); | ||
const descriptionDisplayRef = useRef<HTMLDivElement | null>(null); | ||
|
||
const inputSectionRef = useClickOutside(() => { | ||
if (name) { | ||
setIsEditing(false); | ||
} | ||
}); | ||
|
||
const onHandleEnter = useCallback( | ||
(event: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => { | ||
if (event.key === "Enter") { | ||
event.stopPropagation(); | ||
setIsEditing(false); | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
const onClickEdit = useCallback( | ||
(event: React.MouseEvent<HTMLHeadingElement | HTMLDivElement>) => { | ||
setIsEditing(true); | ||
if (event.target === nameDisplayRef.current) { | ||
setInitialFocus("name"); | ||
} else if (event.target === descriptionDisplayRef.current) { | ||
setInitialFocus("description"); | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<Stack | ||
ref={isEditing ? inputSectionRef : undefined} | ||
spacing="xs" | ||
w="100%" | ||
ml="1em" | ||
mr="0.5em" | ||
> | ||
{isEditing ? ( | ||
<> | ||
<TextInput | ||
classNames={{ input: classes.nameInput }} | ||
placeholder={"Config name"} | ||
value={name} | ||
onKeyDown={onHandleEnter} | ||
autoFocus={initialFocus === "name"} | ||
onChange={(e) => setName(e.currentTarget.value)} | ||
/> | ||
<Textarea | ||
placeholder="Config description" | ||
value={description ?? undefined} | ||
onKeyDown={onHandleEnter} | ||
autoFocus={initialFocus === "description"} | ||
onChange={(e) => setDescription(e.currentTarget.value)} | ||
autosize | ||
minRows={2} | ||
/> | ||
</> | ||
) : ( | ||
<div> | ||
<Title | ||
ref={nameDisplayRef} | ||
onClick={onClickEdit} | ||
className={classes.hoverContainer} | ||
> | ||
{name} | ||
</Title> | ||
<Text | ||
ref={descriptionDisplayRef} | ||
onClick={onClickEdit} | ||
style={{ whiteSpace: "pre-wrap" }} | ||
className={classes.hoverContainer} | ||
> | ||
{description} | ||
</Text> | ||
</div> | ||
)} | ||
</Stack> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DEBOUNCE_MS = 250; |