-
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] Config Name & Description UI
[editor] Config Name & Description UI # [editor] Config Name & Description UI Add the UI for changing config name / description, similar to workbook title UI: https://github.com/lastmile-ai/aiconfig/assets/5060851/f0f49638-d0c5-4d88-8edd-c0bbb0294f7c If config name is not set, keep the name / description in Edit state until the name is set
- Loading branch information
Ryan Holinshead
committed
Jan 2, 2024
1 parent
7cf4a63
commit d114595
Showing
6 changed files
with
234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DEBOUNCE_MS = 250; |