Skip to content

Commit 91adeb0

Browse files
authored
Tools (#1724)
2 parents 4eadf41 + 1704398 commit 91adeb0

File tree

23 files changed

+546
-0
lines changed

23 files changed

+546
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mutation CreateTool($input: CreateTool!) {
2+
createTool(input: $input) {
3+
tool {
4+
...ToolForm
5+
}
6+
}
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useMutation } from '@apollo/client';
2+
import { Except } from 'type-fest';
3+
import { ToolForm, ToolFormProps } from '../ToolForm/ToolForm';
4+
import { ToolFormFragment } from '../ToolForm/ToolForm.graphql';
5+
import { CreateToolDocument } from './CreateTool.graphql';
6+
7+
export type CreateToolProps = Except<
8+
ToolFormProps<ToolFormFragment>,
9+
'onSubmit'
10+
>;
11+
12+
export const CreateTool = (props: CreateToolProps) => {
13+
const [createTool] = useMutation(CreateToolDocument);
14+
15+
return (
16+
<ToolForm
17+
{...props}
18+
title="Create Tool"
19+
onSubmit={async (input) => {
20+
const { data } = await createTool({
21+
variables: { input },
22+
});
23+
return data!.createTool.tool;
24+
}}
25+
/>
26+
);
27+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mutation EditTool($input: UpdateTool!) {
2+
updateTool(input: $input) {
3+
tool {
4+
...ToolForm
5+
}
6+
}
7+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { useMutation } from '@apollo/client';
2+
import { useMemo } from 'react';
3+
import { Except, SetRequired } from 'type-fest';
4+
import { ToolForm, ToolFormProps } from '../ToolForm/ToolForm';
5+
import { ToolFormFragment } from '../ToolForm/ToolForm.graphql';
6+
import { EditToolDocument } from './EditTool.graphql';
7+
8+
export type EditToolProps = SetRequired<
9+
Except<ToolFormProps<ToolFormFragment>, 'onSubmit' | 'initialValues'>,
10+
'tool'
11+
>;
12+
13+
export const EditTool = (props: EditToolProps) => {
14+
const { tool } = props;
15+
16+
const [updateTool] = useMutation(EditToolDocument);
17+
18+
const initialValues = useMemo(
19+
() => ({
20+
name: tool.name.value!,
21+
aiBased: tool.aiBased.value!,
22+
}),
23+
[tool]
24+
);
25+
26+
return (
27+
<ToolForm
28+
{...props}
29+
title="Edit Tool"
30+
initialValues={initialValues}
31+
onSubmit={async (values) => {
32+
const { data } = await updateTool({
33+
variables: {
34+
input: {
35+
id: tool.id,
36+
...values,
37+
},
38+
},
39+
});
40+
return data!.updateTool.tool;
41+
}}
42+
/>
43+
);
44+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fragment ToolForm on Tool {
2+
id
3+
name {
4+
canRead
5+
canEdit
6+
value
7+
}
8+
aiBased {
9+
canRead
10+
canEdit
11+
value
12+
}
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { CreateTool } from '~/api/schema.graphql';
2+
import { DialogForm, DialogFormProps } from '../../Dialog/DialogForm';
3+
import { SecuredField, SubmitError, TextField } from '../../form';
4+
import { CheckboxField } from '../../form/CheckboxField';
5+
import { ToolFormFragment } from './ToolForm.graphql';
6+
7+
export type ToolFormProps<R> = DialogFormProps<Required<CreateTool>, R> & {
8+
tool?: ToolFormFragment;
9+
};
10+
11+
export const ToolForm = <R,>({ tool, ...rest }: ToolFormProps<R>) => (
12+
<DialogForm {...rest}>
13+
<SubmitError />
14+
<SecuredField obj={tool} name="name">
15+
{(props) => (
16+
<TextField
17+
{...props}
18+
label="Tool Name"
19+
placeholder="Enter tool name"
20+
required
21+
/>
22+
)}
23+
</SecuredField>
24+
<SecuredField obj={tool} name="aiBased">
25+
{(props) => (
26+
<CheckboxField
27+
{...props}
28+
label="AI Based"
29+
helperText="Check if this tool uses artificial intelligence"
30+
defaultValue={true}
31+
/>
32+
)}
33+
</SecuredField>
34+
</DialogForm>
35+
);

src/components/Tool/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './CreateTool/CreateTool';
2+
export * from './EditTool/EditTool';
3+
export * from './ToolForm/ToolForm';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Box, Chip, Tooltip, Typography } from '@mui/material';
2+
import { FormattedDate } from '../../Formatters';
3+
import { DisplayToolUsageFragment } from './displayToolUsage.graphql';
4+
5+
interface Props {
6+
usage: DisplayToolUsageFragment;
7+
}
8+
9+
export const DisplayToolUsage = ({ usage }: Props) => (
10+
<Tooltip
11+
key={usage.id}
12+
title={
13+
<Box>
14+
{usage.tool.aiBased.value && (
15+
<Typography variant="body2">AI-Based Tool</Typography>
16+
)}
17+
18+
{usage.startDate.value && (
19+
<Typography variant="body2">
20+
Started Using: <FormattedDate date={usage.startDate.value} />
21+
</Typography>
22+
)}
23+
</Box>
24+
}
25+
>
26+
<Chip
27+
label={usage.tool.name.value}
28+
color="default"
29+
sx={{
30+
cursor: 'default',
31+
color: 'text.primary',
32+
}}
33+
/>
34+
</Tooltip>
35+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fragment displayToolUsage on ToolUsage {
2+
id
3+
startDate {
4+
canRead
5+
canEdit
6+
value
7+
}
8+
tool {
9+
...ToolLookupItem
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
mutation CreateToolUsage($input: CreateToolUsage!) {
2+
createToolUsage(input: $input) {
3+
toolUsage {
4+
...ToolUsageForm
5+
# refresh containers usage list
6+
container {
7+
__typename
8+
id
9+
tools {
10+
items {
11+
id
12+
}
13+
}
14+
}
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)