-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gui): add error status to image card
- Loading branch information
Showing
11 changed files
with
124 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
import { doesExist, mustExist } from '@apextoaster/js-utils'; | ||
import { Grid, Typography } from '@mui/material'; | ||
import { useContext } from 'react'; | ||
import { useContext, ReactNode } from 'react'; | ||
import * as React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useStore } from 'zustand'; | ||
|
||
import { StateContext } from '../state.js'; | ||
import { ImageCard } from './ImageCard.js'; | ||
import { LoadingCard } from './LoadingCard.js'; | ||
import { ImageCard } from './card/ImageCard.js'; | ||
import { LoadingCard } from './card/LoadingCard.js'; | ||
import { ErrorCard } from './card/RetryCard.js'; | ||
|
||
export function ImageHistory() { | ||
const history = useStore(mustExist(useContext(StateContext)), (state) => state.history); | ||
const limit = useStore(mustExist(useContext(StateContext)), (state) => state.limit); | ||
const loading = useStore(mustExist(useContext(StateContext)), (state) => state.loading); | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const removeHistory = useStore(mustExist(useContext(StateContext)), (state) => state.removeHistory); | ||
|
||
const { t } = useTranslation(); | ||
|
||
const children = []; | ||
const children: Array<[string, ReactNode]> = []; | ||
|
||
if (loading.length > 0) { | ||
children.push(...loading.map((item) => <LoadingCard key={`loading-${item.image.outputs[0].key}`} index={0} loading={item.image} />)); | ||
if (history.length === 0) { | ||
children.push(['empty', <Typography>{t('history.empty')}</Typography>]); | ||
} | ||
|
||
if (history.length > 0) { | ||
children.push(...history.map((item) => <ImageCard key={`history-${item.outputs[0].key}`} value={item} onDelete={removeHistory} />)); | ||
} else { | ||
if (doesExist(loading) === false) { | ||
children.push(<Typography>{t('history.empty')}</Typography>); | ||
const limited = history.slice(0, limit); | ||
for (const item of limited) { | ||
const key = item.image.outputs[0].key; | ||
|
||
if (doesExist(item.ready) && item.ready.ready) { | ||
if (item.ready.cancelled || item.ready.failed) { | ||
children.push([key, <ErrorCard key={`history-${key}`} image={item.image} ready={item.ready} />]); | ||
continue; | ||
} | ||
|
||
children.push([key, <ImageCard key={`history-${key}`} image={item.image} onDelete={removeHistory} />]); | ||
continue; | ||
} | ||
} | ||
|
||
const limited = children.slice(0, limit); | ||
children.push([key, <LoadingCard key={`history-${key}`} index={0} image={item.image} />]); | ||
} | ||
|
||
return <Grid container spacing={2}>{limited.map((child, idx) => <Grid item key={idx} xs={6}>{child}</Grid>)}</Grid>; | ||
return <Grid container spacing={2}>{children.map(([key, child]) => <Grid item key={key} xs={6}>{child}</Grid>)}</Grid>; | ||
} |
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,59 @@ | ||
import { mustExist } from '@apextoaster/js-utils'; | ||
import { Box, Button, Card, CardContent, Typography } from '@mui/material'; | ||
import { Stack } from '@mui/system'; | ||
import * as React from 'react'; | ||
import { useContext } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useMutation } from 'react-query'; | ||
import { useStore } from 'zustand'; | ||
|
||
import { ImageResponse, ReadyResponse } from '../../client/api.js'; | ||
import { ClientContext, ConfigContext, StateContext } from '../../state.js'; | ||
|
||
export interface ErrorCardProps { | ||
image: ImageResponse; | ||
ready: ReadyResponse; | ||
} | ||
|
||
export function ErrorCard(props: ErrorCardProps) { | ||
const { image, ready } = props; | ||
|
||
const client = mustExist(React.useContext(ClientContext)); | ||
const { params } = mustExist(useContext(ConfigContext)); | ||
|
||
const state = mustExist(useContext(StateContext)); | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const removeHistory = useStore(state, (s) => s.removeHistory); | ||
const { t } = useTranslation(); | ||
|
||
// TODO: actually retry | ||
const retry = useMutation(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('retry', image); | ||
return Promise.resolve(true); | ||
}); | ||
|
||
return <Card sx={{ maxWidth: params.width.default }}> | ||
<CardContent sx={{ height: params.height.default }}> | ||
<Box sx={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
minHeight: params.height.default, | ||
}}> | ||
<Stack | ||
direction='column' | ||
spacing={2} | ||
sx={{ alignItems: 'center' }} | ||
> | ||
<Typography>{t('loading.progress', { | ||
current: ready.progress, | ||
total: image.params.steps, | ||
})}</Typography> | ||
<Button onClick={() => retry.mutate()}>{t('loading.retry')}</Button> | ||
<Button onClick={() => removeHistory(image)}>{t('loading.remove')}</Button> | ||
</Stack> | ||
</Box> | ||
</CardContent> | ||
</Card>; | ||
} |
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
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.