Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 26 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 ? "View JSON" : "Toggle JSON Editor";
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,8 @@
import { Textarea } from "@mantine/core";
import { Spoiler, Textarea } from "@mantine/core";
import { PromptInput } from "aiconfig";
import { memo } from "react";
import { memo, useContext } from "react";
import AIConfigContext from "../../../contexts/AIConfigContext";
import { TextRenderer } from "../TextRenderer";

type Props = {
input: PromptInput;
Expand All @@ -11,10 +13,24 @@ export default memo(function PromptInputConfigRenderer({
input,
onChangeInput,
}: Props) {
return (
const {readOnly} = useContext(AIConfigContext);
return readOnly ? (
<div style={{ padding: "0.5em" }}>
<Spoiler
maxHeight={200}
showLabel="Show more"
hideLabel="Hide"
initialState={false}
transitionDuration={300}
>
<TextRenderer content={input as string} />
</Spoiler>
</div>
): (
<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,4 +1,4 @@
import { memo, useState } from "react";
import { memo, useContext, useState } from "react";
import type { Attachment, Attachment as InputAttachment } from "aiconfig";
import { PromptInputObjectAttachmentsSchema } from "../../../../utils/promptUtils";
import { Dropzone, FileRejection } from "@mantine/dropzone";
Expand All @@ -8,6 +8,7 @@ import {
} from "../../../../utils/dropzoneHelpers";
import { ActionIcon, Container, Text, Title, Tooltip } from "@mantine/core";
import { IconX } from "@tabler/icons-react";
import AIConfigContext from "../../../../contexts/AIConfigContext";

type Props = {
schema: PromptInputObjectAttachmentsSchema;
Expand Down Expand Up @@ -85,6 +86,7 @@ Props) {
const [fileList, setFileList] = useState<File[]>([]);
const [uploadState, setUploadState] = useState<UploadState>("idle");
const [uploadError, setUploadError] = useState<string | null>(null);
const {readOnly} = useContext(AIConfigContext);

const onDropzoneClick = async (files: File[]) => {
if (uploadState === "dropzone_error") {
Expand Down Expand Up @@ -163,6 +165,7 @@ Props) {
// TODO: Get these from schema,
// maxSize={MAX_IMAGE_FILE_SIZE}
accept={schema.items.mime_types}
disabled={readOnly}
>
{fileList.length > 0 ? (
`${fileList.length} File(s) Uploading...`
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)}
/>
) : (
) : (
<AttachmentUploader
schema={schema}
onUploadAttachments={(attachments: InputAttachment[]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import { PromptInputObjectDataSchema } from "../../../../utils/promptUtils";
import { Textarea } from "@mantine/core";
import { Spoiler, Textarea } from "@mantine/core";
import { JSONValue } from "aiconfig";
import { memo } from "react";
import { memo, useContext } from "react";
import AIConfigContext from "../../../../contexts/AIConfigContext";
import { TextRenderer } from "../../TextRenderer";

type Props = {
schema: PromptInputObjectDataSchema;
data?: JSONValue;
onChangeData: (value: JSONValue) => void;
};

export default memo(function PromptInputDataSchemaRenderer({
schema,
data,
onChangeData,
}: Props) {
export default memo(function PromptInputDataSchemaRenderer({ schema, data, onChangeData }: Props) {
const { readOnly } = useContext(AIConfigContext);

switch (schema.type) {
case "string":
return (
case "string": {
const valueData = data ? (data as string) : "";
return readOnly ? (
<div style={{ padding: "0.5em" }}>
<Spoiler maxHeight={200} showLabel="Show more" hideLabel="Hide" initialState={false} transitionDuration={300}>
<TextRenderer content={valueData} />
</Spoiler>
</div>
) : (
<Textarea
value={data ? (data as string) : ""}
value={valueData}
onChange={(e) => onChangeData(e.target.value)}
placeholder="Type a prompt"
disabled={readOnly}
placeholder={readOnly ? "" : "Type a prompt"}
/>
);
}
default:
return null; // TODO: Handle other input.data types
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
} from "../../../../utils/promptUtils";
import DataRenderer from "./PromptInputDataSchemaRenderer";
import AttachmentsRenderer from "./PromptInputAttachmentsSchemaRenderer";
import { Flex, Text, Textarea } from "@mantine/core";
import { Flex, Spoiler, 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";
import { TextRenderer } from "../../TextRenderer";

type Props = {
input: PromptInput;
Expand Down Expand Up @@ -62,6 +64,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 @@ -71,7 +75,19 @@ export default memo(function PromptInputSchemaRenderer(props: Props) {
</>
);
}
return (
return readOnly ? (
<div style={{ padding: "0.5em" }}>
<Spoiler
maxHeight={200}
showLabel="Show more"
hideLabel="Hide"
initialState={false}
transitionDuration={300}
>
<TextRenderer content={(props.input as string)} />
</Spoiler>
</div>
): (
<Textarea
value={props.input}
label="Prompt"
Expand Down

0 comments on commit 2dbcf0a

Please sign in to comment.