-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #694 from exacaster/session_statements_ui
Add Session statements UI
- Loading branch information
Showing
13 changed files
with
1,063 additions
and
22 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
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,35 @@ | ||
import {Application} from '../client/types'; | ||
import {useStatements} from '../hooks/session'; | ||
import {Spinner, useColorMode, VStack} from '@chakra-ui/react'; | ||
import {a11yLight, a11yDark} from 'react-code-blocks'; | ||
import Statement from './statement/Statement'; | ||
import React from 'react'; | ||
import StatementForm from './statement/StatementForm'; | ||
|
||
interface StatementsProps { | ||
session: Application; | ||
} | ||
|
||
const Statements: React.FC<StatementsProps> = ({session}) => { | ||
const {data: page, isLoading} = useStatements(session.id, 5, 0); | ||
const {colorMode} = useColorMode(); | ||
const theme = colorMode === 'light' ? a11yLight : a11yDark; | ||
|
||
if (isLoading) { | ||
return <Spinner />; | ||
} | ||
if (!page?.statements.length) { | ||
return <div>Session has no statements</div>; | ||
} | ||
|
||
return ( | ||
<VStack align="stretch" spacing={2}> | ||
{page.statements.toReversed().map((statement) => ( | ||
<Statement key={statement.id} sessionId={session.id} statement={statement} theme={theme} /> | ||
))} | ||
<StatementForm session={session} /> | ||
</VStack> | ||
); | ||
}; | ||
|
||
export default Statements; |
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,49 @@ | ||
import React, {useMemo} from 'react'; | ||
import {SessionStatement} from '../../client/types'; | ||
import {useSessionStatementCancel} from '../../hooks/session'; | ||
import {CheckIcon, CloseIcon, WarningTwoIcon} from '@chakra-ui/icons'; | ||
import {Box, Card, CardBody, Flex, IconButton, Spinner, VStack} from '@chakra-ui/react'; | ||
import {CodeBlock} from 'react-code-blocks'; | ||
import StatementOutput from './StatementOutput'; | ||
|
||
const Statement: React.FC<{sessionId: string; statement: SessionStatement; theme: any}> = ({sessionId, statement, theme}) => { | ||
const {mutate: cancel, isLoading: isCanceling} = useSessionStatementCancel(sessionId, statement.id); | ||
|
||
const statusIcon = useMemo(() => { | ||
switch (statement.state) { | ||
case 'available': | ||
return <CheckIcon color="green.500" />; | ||
case 'canceled': | ||
return <CloseIcon />; | ||
case 'error': | ||
return <WarningTwoIcon color="red.500" />; | ||
case 'waiting': | ||
return <Spinner />; | ||
} | ||
}, [statement.state]); | ||
|
||
return ( | ||
<Card> | ||
<CardBody> | ||
<VStack align="stretch" spacing={1}> | ||
<Flex gap={2}> | ||
<Box flex={1}> | ||
<CodeBlock language="python" text={statement.code} theme={theme} /> | ||
<StatementOutput theme={theme} output={statement.output} /> | ||
</Box> | ||
<Box> | ||
<VStack> | ||
{statusIcon} | ||
{statement.state !== 'canceled' ? ( | ||
<IconButton onClick={() => cancel()} isLoading={isCanceling} aria-label="Cancel" icon={<CloseIcon />} /> | ||
) : null} | ||
</VStack> | ||
</Box> | ||
</Flex> | ||
</VStack> | ||
</CardBody> | ||
</Card> | ||
); | ||
}; | ||
|
||
export default Statement; |
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,56 @@ | ||
import {Button, Card, CardBody, FormControl, FormLabel, HStack, Spacer, Textarea, VStack} from '@chakra-ui/react'; | ||
import React from 'react'; | ||
import {useSessionStatementSubmit} from '../../hooks/session'; | ||
import {Application} from '../../client/types'; | ||
|
||
interface StatementFormProps { | ||
session: Application; | ||
} | ||
|
||
const deadStates = ['SHUTTING_DOWN', 'ERROR', 'DEAD', 'KILLED']; | ||
|
||
const StatementForm: React.FC<StatementFormProps> = ({session}) => { | ||
const {mutateAsync: submit, isLoading: isSubmitting} = useSessionStatementSubmit(session.id); | ||
const handleSubmit = (event: React.FormEvent) => { | ||
// @ts-ignore | ||
const code = event.target.elements.code.value; | ||
// @ts-ignore | ||
submit({code}).then(() => (event.target.elements.code.value = '')); | ||
event.preventDefault(); | ||
}; | ||
|
||
const isSessionDead = deadStates.includes(session.state); | ||
|
||
if (isSessionDead) { | ||
return ( | ||
<Card align="center"> | ||
<CardBody>Session cannot accept new statements.</CardBody> | ||
</Card> | ||
); | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<Card> | ||
<CardBody> | ||
<VStack align="stretch" spacing={2}> | ||
<FormControl> | ||
<FormLabel>New Statement</FormLabel> | ||
<Textarea name="code" /> | ||
</FormControl> | ||
<FormControl> | ||
<HStack> | ||
<Spacer /> | ||
<Button type="submit" isLoading={isSubmitting}> | ||
Submit | ||
</Button> | ||
</HStack> | ||
</FormControl> | ||
</VStack> | ||
</CardBody> | ||
</Card> | ||
</form> | ||
); | ||
}; | ||
|
||
export default StatementForm; |
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,22 @@ | ||
import React from 'react'; | ||
import {SessionStatement} from '../../client/types'; | ||
import {CodeBlock} from 'react-code-blocks'; | ||
|
||
const StatementOutput: React.FC<{output?: SessionStatement['output']; theme: any}> = ({output, theme}) => { | ||
if (output?.traceback) { | ||
return <CodeBlock showLineNumbers={false} theme={theme} text={output.traceback} />; | ||
} | ||
|
||
if (!output?.data) { | ||
return null; | ||
} | ||
|
||
const text = String(Object.values(output.data)[0]); | ||
if (!text) { | ||
return null; | ||
} | ||
|
||
return <CodeBlock showLineNumbers={false} theme={theme} text={text} />; | ||
}; | ||
|
||
export default StatementOutput; |
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
Oops, something went wrong.