Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { useContext } from 'react'

import AppHostTableCell from '@/features/AppUi/Components/AppHostTableCell.tsx'
import AppViewErrorBoundary from '@/features/AppUi/Components/AppViewErrorBoundary.tsx'
import { AppHostTableContext } from '@/features/AppUi/appHostTableContext.ts'
import usePrewarmAppHostAction from '@/features/AppUi/Hooks/usePrewarmAppHostAction.ts'
import type { AppHostTableColumn } from '@/features/AppUi/Types/app-host-table.types.ts'
import type { ProxyHostSummary } from '@/lib/Admin/ProxyHostManagement/Types/proxy-hosts.types.ts'

/** One host's cell in an app column; it opens the column's row action when the host offers it. */
export default function ProxyHostAppExtensionCell({
host,
column,
}: {
interface ProxyHostAppExtensionCellProps {
readonly host: ProxyHostSummary
readonly column: AppHostTableColumn
}) {
}

/** One host's cell in an app column; it opens the column's row action when the host offers it. */
export default function ProxyHostAppExtensionCell(props: ProxyHostAppExtensionCellProps) {
return (
<AppViewErrorBoundary compact>
<ProxyHostAppExtensionCellContent {...props} />
</AppViewErrorBoundary>
)
}

function ProxyHostAppExtensionCellContent({ host, column }: ProxyHostAppExtensionCellProps) {
const table = useContext(AppHostTableContext)
const prewarm = usePrewarmAppHostAction()
const onAction = table?.onAction
Expand Down
26 changes: 16 additions & 10 deletions web/src/features/AppUi/AppUiFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import AppFrameView from './Components/AppFrameView.tsx'
import PooledAppFrameView from './Components/PooledAppFrameView.tsx'
import type { AppFrameSource as OpenedSource } from './Types/app-frame-view.types.ts'
import AppContentSkeleton from './Components/AppContentSkeleton.tsx'
import AppViewErrorBoundary from './Components/AppViewErrorBoundary.tsx'

type FrameProps = {
app: AppUiSummary
Expand Down Expand Up @@ -49,9 +50,10 @@ export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill
if (!sessionId) return null

// Routing updates the open document through the bridge.
const retry = () => setAttempt((previous) => previous + 1)
return (
<AppDraftHandoffContext value={handoff}>
<AppUiMount
<AppViewErrorBoundary
key={JSON.stringify([
sessionId,
app.installationId,
Expand All @@ -60,15 +62,19 @@ export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill
hostId,
attempt,
])}
sessionId={sessionId}
app={app}
slot={slot}
hostId={hostId}
route={current}
fill={fill}
onNavigate={onNavigate ?? setLocalRoute}
onRetry={() => setAttempt((previous) => previous + 1)}
/>
onRetry={retry}
>
<AppUiMount
sessionId={sessionId}
app={app}
slot={slot}
hostId={hostId}
route={current}
fill={fill}
onNavigate={onNavigate ?? setLocalRoute}
onRetry={retry}
/>
</AppViewErrorBoundary>
</AppDraftHandoffContext>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts'
import { ConfirmDialog } from '@/shared/Modal/Components/ConfirmDialog.tsx'

export default function AppDialogCloseConfirmation({
open,
blocked,
onOpenChange,
onConfirm,
}: {
readonly open: boolean
readonly blocked: boolean
readonly onOpenChange: (open: boolean) => void
readonly onConfirm: () => void
}) {
const { t } = useTranslationStore()
return (
<ConfirmDialog
open={open}
title={t('appUi.remote.dialog.closeAnyway')}
description={t(
blocked
? 'appUi.remote.dialog.discardWarning'
: 'appUi.remote.dialog.unansweredClose',
)}
confirmLabel={t('appUi.remote.dialog.closeAnyway')}
destructive={blocked}
onOpenChange={onOpenChange}
onConfirm={onConfirm}
/>
)
}
88 changes: 58 additions & 30 deletions web/src/features/AppUi/Components/AppDialogSurfaceModal.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useMemo, useState, type ReactNode } from 'react'

import { buttonClassName } from '@/shared/Buttons/buttonStyles.ts'
import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts'
import { Modal } from '@/shared/Modal/index.tsx'
import { ModalActionButton } from '@/shared/Modal/Components/ModalActions.tsx'
import { AppDialogSurfaceContext } from '../appDialogSurfaceContext.ts'
import useAppDialogEscape from '../Hooks/useAppDialogEscape.ts'
import type { AppDialogChrome } from '../Types/app-dialog-surface.types.ts'
import AppDialogCloseConfirmation from './AppDialogCloseConfirmation.tsx'

/**
* A platform dialog an app frame is drawn in. An app drawn with the UI kit whose root is a dialog
Expand Down Expand Up @@ -32,39 +34,65 @@ export default function AppDialogSurfaceModal({
() => ({ setChrome, navigation, footer, close: () => onOpenChange(false) }),
[footer, navigation, onOpenChange],
)
// The user closed the dialog: the app's root dialog hears of it before the platform closes it.
const closeDisabled = chrome?.closeDisabled ?? false
const { state, handler } = useAppDialogEscape(open && closeDisabled, surface.close)
// The app's root dialog hears about an ordinary close before the platform closes it.
const close = () => {
if (closeDisabled) return
chrome?.onClose()
onOpenChange(false)
surface.close()
}

return (
<Modal
open={open}
onOpenChange={(next) => {
if (next) onOpenChange(true)
else close()
}}
closeDisabled={chrome?.closeDisabled ?? false}
title={chrome?.heading ?? title}
description={chrome?.description ?? description}
size="xl"
fixedHeight
height="tall"
navigation={chrome?.navigation ? <div ref={setNavigation} /> : undefined}
footer={
chrome?.footer ? (
<div ref={setFooter} className="contents" />
) : (
<button type="button" className={buttonClassName('secondary')} onClick={close}>
{t('appUi.hostTable.close')}
</button>
)
}
>
<div className={chrome ? 'h-full' : 'h-full p-4'}>
<AppDialogSurfaceContext value={surface}>{children}</AppDialogSurfaceContext>
</div>
</Modal>
<>
<Modal
open={open}
onOpenChange={(next) => {
if (next) onOpenChange(true)
else close()
}}
closeDisabled={closeDisabled}
onBlockedEscape={handler.openConfirmation}
title={chrome?.heading ?? title}
description={chrome?.description ?? description}
size="xl"
fixedHeight
height="tall"
navigation={chrome?.navigation ? <div ref={setNavigation} /> : undefined}
footer={
<>
{chrome?.footer ? (
<div ref={setFooter} className="contents" />
) : (
<ModalActionButton
variant="secondary"
disabled={closeDisabled}
onClick={close}
>
{t('appUi.hostTable.close')}
</ModalActionButton>
)}
{state.showEscape ? (
<ModalActionButton
variant="secondary"
onClick={handler.openConfirmation}
>
{t('appUi.remote.dialog.closeAnyway')}
</ModalActionButton>
) : null}
</>
}
>
<div className={chrome ? 'h-full' : 'h-full p-4'}>
<AppDialogSurfaceContext value={surface}>{children}</AppDialogSurfaceContext>
</div>
</Modal>
<AppDialogCloseConfirmation
open={open && state.confirming}
blocked={closeDisabled}
onOpenChange={handler.setConfirming}
onConfirm={handler.forceClose}
/>
</>
)
}
49 changes: 49 additions & 0 deletions web/src/features/AppUi/Components/AppViewErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Component, type ReactNode } from 'react'

import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts'
import { ModalActionButton } from '@/shared/Modal/Components/ModalActions.tsx'

interface AppViewErrorBoundaryProps {
readonly children: ReactNode
readonly onRetry?: () => void
readonly compact?: boolean
}

/** Unmounts the failed app view while leaving the host and other app views active. */
export default class AppViewErrorBoundary extends Component<AppViewErrorBoundaryProps> {
override state = { failed: false }

static getDerivedStateFromError() {
return { failed: true }
}

override render() {
if (!this.state.failed) return this.props.children
return (
<AppViewRecovery
compact={this.props.compact === true}
onRetry={this.props.onRetry ?? (() => this.setState({ failed: false }))}
/>
)
}
}

function AppViewRecovery({ compact, onRetry }: { compact: boolean; onRetry: () => void }) {
const { t } = useTranslationStore()
return (
<div
className={
compact
? 'grid justify-items-start gap-1.5'
: 'flex flex-wrap items-center gap-3 rounded-xl border border-border p-3'
}
>
<output className="text-sm text-muted">
{t(compact ? 'appUi.hostTable.unavailable' : 'appUi.unavailable')}
</output>
<ModalActionButton variant="secondary" onClick={onRetry}>
{t('common.retry')}
</ModalActionButton>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { uiKitCatalog } from '@rentnerproxy/app-contracts'
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'
import { createPortal } from 'react-dom'

import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts'
import { Modal } from '@/shared/Modal/index.tsx'
import { ModalActionButton } from '@/shared/Modal/Components/ModalActions.tsx'
import Tabs from '@/shared/Tabs/index.tsx'
import { AppDialogSurfaceContext } from '../../../appDialogSurfaceContext.ts'
import useAppDialogEscape from '../../../Hooks/useAppDialogEscape.ts'
import type { AppDialogSurface } from '../../../Types/app-dialog-surface.types.ts'
import AppDialogCloseConfirmation from '../../AppDialogCloseConfirmation.tsx'
import { useRemoteCloseBlocked } from '../Hooks/useRemoteForm.ts'
import useRemoteProperties from '../Hooks/useRemoteProperties.ts'
import useRemoteTabsLogic from '../Hooks/useRemoteTabsLogic.ts'
Expand Down Expand Up @@ -43,40 +47,83 @@ function RemoteDialogBody({
)
}

function RemoteModalDialog({ entry, children, slots, emit }: RemoteElementProps) {
const { heading, description, size, height, open } = useRemoteProperties(
uiKitCatalog['ui-dialog'].properties,
entry,
)
function RemoteModalDialog(props: RemoteElementProps) {
const properties = useRemoteProperties(uiKitCatalog['ui-dialog'].properties, props.entry)
if (properties.open === false) return null
// An app close followed by an open mounts a fresh dialog after a local dismissal.
return <OpenRemoteModalDialog {...props} properties={properties} />
}

function OpenRemoteModalDialog({
entry,
children,
slots,
emit,
properties: { heading, description, size, height },
}: RemoteElementProps & {
readonly properties: ReturnType<(typeof uiKitCatalog)['ui-dialog']['properties']['parse']>
}) {
const { t } = useTranslationStore()
const blocked = useRemoteCloseBlocked(entry.id)
const [body, setBody] = useState<HTMLDivElement | null>(null)
// Without a close listener the app keeps its dialog open until it removes it.
const [dismissed, setDismissed] = useState(false)
const [requested, setRequested] = useState(false)
const closable = entry.listeners.includes('close')
const { state, handler } = useAppDialogEscape(!dismissed && (blocked || requested), () =>
setDismissed(true),
)
const { openConfirmation } = handler
const requestClose = useCallback(() => {
if (!blocked) emit('close')
}, [blocked, emit])
if (blocked) return
if (!closable) {
setDismissed(true)
} else if (requested) {
openConfirmation()
} else {
setRequested(true)
emit('close')
}
}, [blocked, closable, emit, openConfirmation, requested])
const dialog = useMemo(
() => ({ scope: entry.id, requestClose, body }),
[body, entry.id, requestClose],
)
return (
<RemoteDialogContext value={dialog}>
<Modal
open={open ?? true}
open={!dismissed}
onOpenChange={(next) => {
if (!next) requestClose()
}}
closeDisabled={blocked || !closable}
closeDisabled={blocked}
onBlockedEscape={openConfirmation}
title={heading}
description={description ?? null}
size={size ?? 'md'}
height={height ?? 'default'}
fixedHeight={height === 'tall'}
navigation={slots.navigation}
footer={slots.footer}
footer={
slots.footer || state.showEscape ? (
<>
{slots.footer}
{state.showEscape ? (
<ModalActionButton variant="secondary" onClick={openConfirmation}>
{t('appUi.remote.dialog.closeAnyway')}
</ModalActionButton>
) : null}
</>
) : undefined
}
>
<RemoteDialogBody onBody={setBody}>{children}</RemoteDialogBody>
</Modal>
<AppDialogCloseConfirmation
open={!dismissed && state.confirming}
blocked={blocked}
onOpenChange={handler.setConfirming}
onConfirm={handler.forceClose}
/>
</RemoteDialogContext>
)
}
Expand Down
Loading