Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ActionIcon, Tooltip } from "@mantine/core";
import { IconBraces, IconBracesOff } from "@tabler/icons-react";
import AIConfigContext from "../contexts/AIConfigContext";
import { useContext } from "react";

type Props = {
isRawJSON: boolean;
Expand All @@ -10,8 +12,11 @@ export default function JSONEditorToggleButton({
isRawJSON,
setIsRawJSON,
}: Props) {
const { readOnly } = useContext(AIConfigContext);

const toggleJSONButtonLabel = !readOnly ? "Toggle JSON editor" : "View JSON";
return (
<Tooltip label="Toggle JSON editor" withArrow>
<Tooltip label={toggleJSONButtonLabel} withArrow>
<ActionIcon onClick={() => setIsRawJSON(!isRawJSON)}>
{isRawJSON ? <IconBracesOff size="1rem" /> : <IconBraces size="1rem" />}
</ActionIcon>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Prism } from "@mantine/prism";
import { JSONObject, JSONValue } from "aiconfig";
import { memo } from "react";
import { memo, useContext } from "react";
import JSONEditor from "./JSONEditor";
import AIConfigContext from "../contexts/AIConfigContext";

type Props = {
content: JSONValue;
Expand All @@ -14,7 +15,9 @@ export default memo(function JSONRenderer({
onChange,
schema,
}: Props) {
return !onChange ? (
const { readOnly } = useContext(AIConfigContext);
// Prism is a read only renderer.
return !onChange || readOnly ? (
<Prism language="json" styles={{ code: { textWrap: "pretty" } }}>
{JSON.stringify(content, null, 2)}
</Prism>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Textarea } from "@mantine/core";
import { PromptInput } from "aiconfig";
import { memo } from "react";
import { memo, useContext } from "react";
import AIConfigContext from "../../../contexts/AIConfigContext";

type Props = {
input: PromptInput;
Expand All @@ -11,10 +12,12 @@ export default memo(function PromptInputConfigRenderer({
input,
onChangeInput,
}: Props) {
const {readOnly} = useContext(AIConfigContext);
return (
<Textarea
value={input as string}
onChange={(e) => onChangeInput(e.target.value)}
disabled={readOnly}
/>
);
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { memo } from "react";
import { memo, useContext } from "react";
import type { Attachment as InputAttachment, JSONObject, AttachmentDataWithStringValue } from "aiconfig";
import { PromptInputObjectAttachmentsSchema } from "../../../../utils/promptUtils";
import { ActionIcon, Container, Flex, Tooltip } from "@mantine/core";
import { IconEdit, IconTrash } from "@tabler/icons-react";
import AttachmentMetadata from "./AttachmentMetadata";
import MimeTypeRenderer from "../../../MimeTypeRenderer";
import AIConfigContext from "../../../../contexts/AIConfigContext";

type Props = {
schema: PromptInputObjectAttachmentsSchema;
Expand All @@ -21,17 +22,18 @@ export default memo(function AttachmentContainer({
onRemoveAttachment,
onEditAttachment,
}: Props) {
const {readOnly} = useContext(AIConfigContext);
return (
<Container display="flex">
<Flex direction="column">
{onEditAttachment && (
{onEditAttachment && !readOnly && (
<ActionIcon onClick={onEditAttachment}>
<Tooltip label="Edit attachment">
<IconEdit size={16} />
</Tooltip>
</ActionIcon>
)}
{onRemoveAttachment && (
{onRemoveAttachment && !readOnly && (
<ActionIcon onClick={onRemoveAttachment}>
<Tooltip label="Remove attachment">
<IconTrash size={16} color="red" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PromptInputObjectAttachmentsSchema } from "../../../../utils/promptUtils";
import type { Attachment as InputAttachment, JSONObject } from "aiconfig";
import { memo, useState } from "react";
import { memo, useContext, useState } from "react";
import AttachmentContainer from "../attachments/AttachmentContainer";
import AttachmentUploader from "../attachments/AttachmentUploader";
import { Container } from "@mantine/core";
import AIConfigContext from "../../../../contexts/AIConfigContext";

type Props = {
schema: PromptInputObjectAttachmentsSchema;
Expand All @@ -25,6 +26,7 @@ function EditableAttachmentRenderer({
onRemoveAttachment?: () => void;
}) {
const [showUploader, setShowUploader] = useState(attachment?.data == null);
const {readOnly} = useContext(AIConfigContext);

return (
<Container m="xs">
Expand All @@ -38,7 +40,7 @@ function EditableAttachmentRenderer({
onRemoveAttachment={onRemoveAttachment}
onEditAttachment={() => setShowUploader(true)}
/>
) : (
) : !readOnly && (
<AttachmentUploader
schema={schema}
onUploadAttachments={(attachments: InputAttachment[]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PromptInputObjectDataSchema } from "../../../../utils/promptUtils";
import { Textarea } from "@mantine/core";
import { JSONValue } from "aiconfig";
import { memo } from "react";
import { memo, useContext } from "react";
import AIConfigContext from "../../../../contexts/AIConfigContext";

type Props = {
schema: PromptInputObjectDataSchema;
Expand All @@ -14,12 +15,15 @@ export default memo(function PromptInputDataSchemaRenderer({
data,
onChangeData,
}: Props) {
const {readOnly} = useContext(AIConfigContext);

switch (schema.type) {
case "string":
return (
<Textarea
value={data ? (data as string) : ""}
onChange={(e) => onChangeData(e.target.value)}
disabled={readOnly}
placeholder="Type a prompt"
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import DataRenderer from "./PromptInputDataSchemaRenderer";
import AttachmentsRenderer from "./PromptInputAttachmentsSchemaRenderer";
import { Flex, Text, Textarea } from "@mantine/core";
import { Attachment, JSONValue, PromptInput } from "aiconfig";
import { memo } from "react";
import { memo, useContext } from "react";
import JSONRenderer from "../../../JSONRenderer";
import AIConfigContext from "../../../../contexts/AIConfigContext";

type Props = {
input: PromptInput;
Expand Down Expand Up @@ -62,6 +63,8 @@ function SchemaRenderer({ input, schema, onChangeInput }: SchemaRendererProps) {
}

export default memo(function PromptInputSchemaRenderer(props: Props) {
const {readOnly} = useContext(AIConfigContext);

if (props.schema.type === "string") {
if (props.input && typeof props.input !== "string") {
return (
Expand All @@ -77,6 +80,7 @@ export default memo(function PromptInputSchemaRenderer(props: Props) {
label="Prompt"
onChange={(e) => props.onChangeInput(e.target.value)}
placeholder="Type a prompt"
disabled = {readOnly}
autosize
/>
);
Expand Down

0 comments on commit 561a694

Please sign in to comment.