Skip to content

Commit

Permalink
refactor: use separate encode chip components
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILLIPS71 committed May 12, 2024
1 parent 136e2bd commit 6506c2b
Show file tree
Hide file tree
Showing 15 changed files with 305 additions and 295 deletions.
12 changes: 6 additions & 6 deletions app/src/app/(dashboard)/encode/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ const EncodePage: React.FC<EncodePageProps> = ({ params }) => {
<div className="max-w-6xl mx-auto ">
<div className="flex flex-col lg:flex-row gap-3">
<div className="flex flex-col grow gap-3">
<Card>
<Card.Body>
<FileSystemBreadcrumb $key={query.encode.file} />
</Card.Body>
</Card>

{query.encode.failure_reason && (
<Alert color="danger">
<IconAlertCircleFilled size={16} />
Expand All @@ -71,6 +65,12 @@ const EncodePage: React.FC<EncodePageProps> = ({ params }) => {
</Alert>
)}

<Card>
<Card.Body>
<FileSystemBreadcrumb $key={query.encode.file} />
</Card.Body>
</Card>

<Card>
<Card.Header>
<Typography.Text>Size</Typography.Text>
Expand Down
47 changes: 47 additions & 0 deletions app/src/components/interfaces/encode/chips/EncodeDuration.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { EncodeDurationFragment$key } from '@/__generated__/EncodeDurationFragment.graphql'

import { Chip } from '@giantnodes/react'
import dayjs from 'dayjs'
import React from 'react'
import { graphql, useFragment } from 'react-relay'

const FRAGMENT = graphql`
fragment EncodeDurationFragment on Encode {
status
failed_at
cancelled_at
completed_at
created_at
}
`

type EncodeDurationChipProps = {
$key: EncodeDurationFragment$key
}

const EncodeDuration: React.FC<EncodeDurationChipProps> = ({ $key }) => {
const data = useFragment(FRAGMENT, $key)

const date = React.useMemo<Date | undefined>(() => {
switch (data.status) {
case 'COMPLETED':
return data.completed_at
case 'CANCELLED':
return data.cancelled_at

case 'FAILED':
return data.failed_at

default:
return undefined
}
}, [data.cancelled_at, data.completed_at, data.failed_at, data.status])

return (
<Chip color="info" title={dayjs(date).format('L LT')}>
{dayjs.duration(dayjs(date).diff(data.created_at)).format('H[h] m[m] s[s]')}
</Chip>
)
}

export default EncodeDuration
51 changes: 51 additions & 0 deletions app/src/components/interfaces/encode/chips/EncodePercent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { EncodePercentFragment$key } from '@/__generated__/EncodePercentFragment.graphql'

import { Chip } from '@giantnodes/react'
import React from 'react'
import { graphql, useFragment, useSubscription } from 'react-relay'

import { percent } from '@/utilities/numbers'

const FRAGMENT = graphql`
fragment EncodePercentFragment on Encode {
id
percent
}
`

const SUBSCRIPTION = graphql`
subscription EncodePercentSubscription($where: EncodeFilterInput) {
encode_progressed(where: $where) {
...EncodePercentFragment
}
}
`

type EncodePercentProps = {
$key: EncodePercentFragment$key
}

const EncodePercent: React.FC<EncodePercentProps> = ({ $key }) => {
const data = useFragment(FRAGMENT, $key)

useSubscription({
subscription: SUBSCRIPTION,
variables: {
variables: {
where: {
id: {
eq: data.id,
},
},
},
},
})

if (data.percent == null) {
return undefined
}

return <Chip color="brand">{percent(data.percent)}</Chip>
}

export default EncodePercent
62 changes: 62 additions & 0 deletions app/src/components/interfaces/encode/chips/EncodeSpeed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { EncodeSpeedFragment$key } from '@/__generated__/EncodeSpeedFragment.graphql'

import { Chip } from '@giantnodes/react'
import { filesize } from 'filesize'
import React from 'react'
import { graphql, useFragment, useSubscription } from 'react-relay'

const FRAGMENT = graphql`
fragment EncodeSpeedFragment on Encode {
id
speed {
frames
bitrate
scale
}
}
`

const SUBSCRIPTION = graphql`
subscription EncodeSpeedSubscription($where: EncodeFilterInput) {
encode_speed_change(where: $where) {
...EncodeSpeedFragment
}
}
`

type EncodeSpeedProps = {
$key: EncodeSpeedFragment$key
}

const EncodeSpeed: React.FC<EncodeSpeedProps> = ({ $key }) => {
const data = useFragment(FRAGMENT, $key)

useSubscription({
subscription: SUBSCRIPTION,
variables: {
variables: {
where: {
id: {
eq: data.id,
},
},
},
},
})

if (data.speed == null) {
return undefined
}

return (
<>
<Chip color="info">{data.speed.frames} fps</Chip>

<Chip color="info">{filesize(data.speed.bitrate * 0.125, { bits: true }).toLowerCase()}/s</Chip>

<Chip color="info">{data.speed.scale.toFixed(2)}x</Chip>
</>
)
}

export default EncodeSpeed
76 changes: 76 additions & 0 deletions app/src/components/interfaces/encode/chips/EncodeStatus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { EncodeStatusFragment$key } from '@/__generated__/EncodeStatusFragment.graphql'
import type { ChipProps } from '@giantnodes/react'

import { Chip } from '@giantnodes/react'
import dayjs from 'dayjs'
import React from 'react'
import { graphql, useFragment } from 'react-relay'

const FRAGMENT = graphql`
fragment EncodeStatusFragment on Encode {
status
failed_at
cancelled_at
completed_at
}
`

type EncodeStatusChipProps = {
$key: EncodeStatusFragment$key
}

const EncodeStatus: React.FC<EncodeStatusChipProps> = ({ $key }) => {
const data = useFragment(FRAGMENT, $key)

const color = React.useMemo<ChipProps['color']>(() => {
switch (data.status) {
case 'SUBMITTED':
return 'info'

case 'QUEUED':
return 'info'

case 'ENCODING':
return 'success'

case 'DEGRADED':
return 'warning'

case 'COMPLETED':
return 'success'

case 'CANCELLED':
return 'neutral'

case 'FAILED':
return 'danger'

default:
return 'neutral'
}
}, [data.status])

const title = React.useMemo<string | undefined>(() => {
switch (data.status) {
case 'COMPLETED':
return dayjs(data.completed_at).format('L LT')

case 'CANCELLED':
return dayjs(data.cancelled_at).format('L LT')

case 'FAILED':
return dayjs(data.failed_at).format('L LT')

default:
return undefined
}
}, [data.cancelled_at, data.completed_at, data.failed_at, data.status])

return (
<Chip color={color} title={title}>
{data.status.toLowerCase()}
</Chip>
)
}

export default EncodeStatus
19 changes: 1 addition & 18 deletions app/src/components/interfaces/encode/dialog/EncodeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { DialogProps } from '@giantnodes/react'
import { Button, Card, Chip, Dialog, Typography } from '@giantnodes/react'
import { IconX } from '@tabler/icons-react'
import React from 'react'
import { graphql, useFragment, useSubscription } from 'react-relay'
import { graphql, useFragment } from 'react-relay'

import EncodeDialogSidebar from '@/components/interfaces/encode/dialog/EncodeDialogSidebar'
import EncodeAnalyticsPanel from '@/components/interfaces/encode/dialog/panels/EncodeAnalyticsPanel'
Expand All @@ -14,7 +14,6 @@ import {
EncodeDialogPanel,
useEncodeDialog,
} from '@/components/interfaces/encode/dialog/use-encode-dialog.hook'
import EncodeStatusBadge from '@/components/ui/encode-badges/EncodeStatusBadge'

const FRAGMENT = graphql`
fragment EncodeDialogFragment on Encode {
Expand All @@ -26,20 +25,11 @@ const FRAGMENT = graphql`
name
}
}
...EncodeStatusBadgeFragment
...EncodeScriptPanelFragment
...EncodeAnalyticsPanelFragment
}
`

const OUTPUTTED_SUBSCRIPTION = graphql`
subscription EncodeDialogOutputtedSubscription {
encode_outputted {
output
}
}
`

type EncodeDialogProps = React.PropsWithChildren & {
$key: EncodeDialogFragment$key
} & DialogProps
Expand All @@ -48,11 +38,6 @@ const EncodeDialog: React.FC<EncodeDialogProps> = ({ $key, children, ...rest })
const data = useFragment(FRAGMENT, $key)
const context = useEncodeDialog({ panel: EncodeDialogPanel.SCRIPT })

useSubscription({
subscription: OUTPUTTED_SUBSCRIPTION,
variables: {},
})

const content = React.useCallback(() => {
switch (context.panel) {
case EncodeDialogPanel.SCRIPT:
Expand Down Expand Up @@ -82,8 +67,6 @@ const EncodeDialog: React.FC<EncodeDialogProps> = ({ $key, children, ...rest })
<div className="flex flex-row items-center flex-wrap gap-3">
<Typography.Paragraph>{data.file.path_info.name}</Typography.Paragraph>

<EncodeStatusBadge $key={data} />

<Chip color="info">{data.recipe.name}</Chip>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { EncodeScriptPanelFragment$key } from '@/__generated__/EncodeScriptPanelFragment.graphql'

import { Card, Typography } from '@giantnodes/react'
import { Alert, Card, Typography } from '@giantnodes/react'
import { IconAlertCircleFilled } from '@tabler/icons-react'
import { graphql, useFragment } from 'react-relay'

import { EncodeCommandWidget, EncodeOperationWidget, EncodeOutputWidget } from '@/components/interfaces/encode'

const FRAGMENT = graphql`
fragment EncodeScriptPanelFragment on Encode {
failure_reason
...EncodeOperationWidgetFragment
...EncodeCommandWidgetFragment
...EncodeOutputWidgetFragment
Expand All @@ -22,6 +24,18 @@ const EncodeScriptPanel: React.FC<EncodeScriptPanelProps> = ({ $key }) => {

return (
<>
{data.failure_reason && (
<Alert color="danger">
<IconAlertCircleFilled size={16} />
<Alert.Body>
<Alert.Heading>The encode operation encountered an error</Alert.Heading>
<Alert.List>
<Alert.Item>{data.failure_reason}</Alert.Item>
</Alert.List>
</Alert.Body>
</Alert>
)}

<Card className="flex-none">
<EncodeOperationWidget $key={data} />
</Card>
Expand Down
5 changes: 5 additions & 0 deletions app/src/components/interfaces/encode/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export { default as EncodeDuration } from '@/components/interfaces/encode/chips/EncodeDuration'
export { default as EncodePercent } from '@/components/interfaces/encode/chips/EncodePercent'
export { default as EncodeSpeed } from '@/components/interfaces/encode/chips/EncodeSpeed'
export { default as EncodeStatus } from '@/components/interfaces/encode/chips/EncodeStatus'

export { default as EncodeDialog } from '@/components/interfaces/encode/dialog/EncodeDialog'

export { default as EncodeCommandWidget } from '@/components/interfaces/encode/widgets/EncodeCommandWidget'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { EncodeCommandWidgetFragment$key } from '@/__generated__/EncodeComm

import { graphql, useFragment } from 'react-relay'

import CodeBlock from '@/components/ui/code-block/CodeBlock'
import { CodeBlock } from '@/components/ui'

const FRAGMENT = graphql`
fragment EncodeCommandWidgetFragment on Encode {
Expand Down
Loading

0 comments on commit 6506c2b

Please sign in to comment.