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
67 changes: 47 additions & 20 deletions web/src/features/AppUi/Components/AppHostActionDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,71 @@
import { useMemo, useState } 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 AppUiFrame from '../AppUiFrame.tsx'
import { AppDialogSurfaceContext } from '../appDialogSurfaceContext.ts'
import useAppHostActionDialogLogic from '../Hooks/useAppHostActionDialogLogic.ts'
import type { AppHostActionDialogProps } from '../Types/app-host-table.types.ts'
import type { AppDialogChrome } from '../Types/app-dialog-surface.types.ts'

/** A host row action of an app, opened in a platform dialog for that host. */
/**
* A host row action of an app, opened in a platform dialog for that host. An app drawn with the UI
* kit whose root is a dialog fills this dialog's title, navigation and footer itself.
*/
export default function AppHostActionDialog(props: AppHostActionDialogProps) {
const { t } = useTranslationStore()
const { state, handler } = useAppHostActionDialogLogic(props)
const { onOpenChange } = props
const [chrome, setChrome] = useState<AppDialogChrome | null>(null)
const [navigation, setNavigation] = useState<HTMLDivElement | null>(null)
const [footer, setFooter] = useState<HTMLDivElement | null>(null)
const surface = useMemo(
() => ({ 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 close = () => {
chrome?.onClose()
onOpenChange(false)
}

return (
<Modal
open={props.open}
onOpenChange={props.onOpenChange}
title={state.title ?? t('appUi.unavailable')}
description={props.hostDomains.join(', ')}
onOpenChange={(open) => {
if (open) onOpenChange(true)
else close()
}}
closeDisabled={chrome?.closeDisabled ?? false}
title={chrome?.heading ?? state.title ?? t('appUi.unavailable')}
description={chrome?.description ?? props.hostDomains.join(', ')}
size="xl"
fixedHeight
height="tall"
navigation={chrome?.navigation ? <div ref={setNavigation} /> : undefined}
footer={
<button
type="button"
className={buttonClassName('secondary')}
onClick={() => props.onOpenChange(false)}
>
{t('appUi.hostTable.close')}
</button>
chrome?.footer ? (
<div ref={setFooter} className="contents" />
) : (
<button type="button" className={buttonClassName('secondary')} onClick={close}>
{t('appUi.hostTable.close')}
</button>
)
}
>
{state.app && state.route ? (
<div className="h-full p-4">
<AppUiFrame
app={state.app}
slot="proxyHostTab"
hostId={props.hostId}
route={state.route}
onNavigate={handler.handleNavigate}
fill
/>
<div className={chrome ? 'h-full' : 'h-full p-4'}>
<AppDialogSurfaceContext value={surface}>
<AppUiFrame
app={state.app}
slot="proxyHostTab"
hostId={props.hostId}
route={state.route}
onNavigate={handler.handleNavigate}
fill
/>
</AppDialogSurfaceContext>
</div>
) : (
<output className="block p-4 text-sm text-muted">{t('appUi.unavailable')}</output>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
import { uiKitCatalog } from '@rentnerproxy/app-contracts'
import { LoaderCircle } from 'lucide-react'
import { useMemo } from 'react'
import { useContext, useMemo } from 'react'

import { ActionMenu } from '@/shared/ActionMenu/index.tsx'
import { buttonClassName } from '@/shared/Buttons/buttonStyles.ts'
import { ConfirmDialog } from '@/shared/Modal/Components/ConfirmDialog.tsx'
import useRemoteButtonLogic from '../Hooks/useRemoteButtonLogic.ts'
import { useRemoteFormState } from '../Hooks/useRemoteForm.ts'
import useRemoteProperties from '../Hooks/useRemoteProperties.ts'
import { useRemoteAppView } from '../Hooks/useRemoteUiEntry.ts'
import { RemoteFormContext } from '../remoteElementContexts.ts'
import type { RemoteElementProps } from '../Types/remote-app-view.types.ts'
import RemoteUiIcon from './RemoteUiIcon.tsx'

// Submit buttons and their forms arrive with the form elements; until then every button presses.
/**
* A button that sends press. A submit button saves the form it names, or the form it sits in, and
* is enabled only while that form may be saved.
*/
export function RemoteButton({ entry, emit }: RemoteElementProps) {
const { label, variant, icon, disabled, pending, pendingLabel, confirm } = useRemoteProperties(
uiKitCatalog['ui-button'].properties,
entry,
)
const { state, handler } = useRemoteButtonLogic(confirm !== undefined, () => emit('press'))
const { label, variant, icon, disabled, pending, pendingLabel, confirm, type, form } =
useRemoteProperties(uiKitCatalog['ui-button'].properties, entry)
const { forms } = useRemoteAppView()
const surroundingForm = useContext(RemoteFormContext)
const target = type === 'submit' ? (form ?? surroundingForm) : undefined
const targetState = useRemoteFormState(target)
const { state, handler } = useRemoteButtonLogic(confirm !== undefined, () => {
if (target !== undefined) forms.submit(target)
emit('press')
})
return (
<>
<button
type="button"
className={buttonClassName(variant ?? 'secondary')}
disabled={disabled === true || pending === true}
disabled={
disabled === true ||
pending === true ||
(target !== undefined && targetState?.canSubmit !== true)
}
aria-busy={pending === true ? true : undefined}
onClick={handler.handlePress}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { uiKitCatalog } from '@rentnerproxy/app-contracts'
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react'
import { createPortal } from 'react-dom'

import { Modal } from '@/shared/Modal/index.tsx'
import Tabs from '@/shared/Tabs/index.tsx'
import { AppDialogSurfaceContext } from '../../../appDialogSurfaceContext.ts'
import type { AppDialogSurface } from '../../../Types/app-dialog-surface.types.ts'
import { useRemoteCloseBlocked } from '../Hooks/useRemoteForm.ts'
import useRemoteProperties from '../Hooks/useRemoteProperties.ts'
import useRemoteTabsLogic from '../Hooks/useRemoteTabsLogic.ts'
import {
RemoteDialogContext,
RemoteEmbeddedDialogContext,
RemoteTabsContext,
} from '../remoteElementContexts.ts'
import type { RemoteElementProps } from '../Types/remote-app-view.types.ts'

/**
* An app dialog. As the root of an app opened in a platform dialog it fills that dialog's title,
* navigation and footer; anywhere else it opens a platform dialog the app controls with `open`.
*/
export function RemoteDialog(props: RemoteElementProps) {
const embedded = useContext(RemoteEmbeddedDialogContext) === props.entry.id
const surface = useContext(AppDialogSurfaceContext)
if (embedded && surface) return <RemoteEmbeddedDialog {...props} surface={surface} />
return <RemoteModalDialog {...props} />
}

// Tabs in the navigation slot draw their panels into the body, ahead of the other content.
function RemoteDialogBody({
children,
onBody,
}: {
readonly children: RemoteElementProps['children']
readonly onBody: (body: HTMLDivElement | null) => void
}) {
return (
<div className="grid min-w-0 gap-4">
<div ref={onBody} className="contents" />
{children}
</div>
)
}

function RemoteModalDialog({ entry, children, slots, emit }: RemoteElementProps) {
const { heading, description, size, height, open } = useRemoteProperties(
uiKitCatalog['ui-dialog'].properties,
entry,
)
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 closable = entry.listeners.includes('close')
const requestClose = useCallback(() => {
if (!blocked) emit('close')
}, [blocked, emit])
const dialog = useMemo(
() => ({ scope: entry.id, requestClose, body }),
[body, entry.id, requestClose],
)
return (
<RemoteDialogContext value={dialog}>
<Modal
open={open ?? true}
onOpenChange={(next) => {
if (!next) requestClose()
}}
closeDisabled={blocked || !closable}
title={heading}
description={description ?? null}
size={size ?? 'md'}
height={height ?? 'default'}
fixedHeight={height === 'tall'}
navigation={slots.navigation}
footer={slots.footer}
>
<RemoteDialogBody onBody={setBody}>{children}</RemoteDialogBody>
</Modal>
</RemoteDialogContext>
)
}

function RemoteEmbeddedDialog({
entry,
children,
slots,
emit,
surface,
}: RemoteElementProps & { readonly surface: AppDialogSurface }) {
const { heading, description, open } = useRemoteProperties(
uiKitCatalog['ui-dialog'].properties,
entry,
)
const blocked = useRemoteCloseBlocked(entry.id)
const [body, setBody] = useState<HTMLDivElement | null>(null)
const { setChrome, close } = surface
const navigation = slots.navigation !== undefined
const footer = slots.footer !== undefined
const onClose = useCallback(() => emit('close'), [emit])

useLayoutEffect(() => {
setChrome({ heading, description, navigation, footer, closeDisabled: blocked, onClose })
}, [blocked, description, footer, heading, navigation, onClose, setChrome])
useLayoutEffect(() => () => setChrome(null), [setChrome])
// `open: false` from the app closes the platform dialog it was drawn in.
useEffect(() => {
if (open === false) close()
}, [close, open])

const requestClose = useCallback(() => {
if (blocked) return
emit('close')
close()
}, [blocked, close, emit])
const dialog = useMemo(
() => ({ scope: entry.id, requestClose, body }),
[body, entry.id, requestClose],
)
return (
<RemoteDialogContext value={dialog}>
<RemoteDialogBody onBody={setBody}>{children}</RemoteDialogBody>
{surface.navigation && navigation
? createPortal(slots.navigation, surface.navigation)
: null}
{surface.footer && footer ? createPortal(slots.footer, surface.footer) : null}
</RemoteDialogContext>
)
}

export function RemoteTabs({ entry, children, emit }: RemoteElementProps) {
const { label } = useRemoteProperties(uiKitCatalog['ui-tabs'].properties, entry)
const { state, handler } = useRemoteTabsLogic(entry, emit)
const dialog = useContext(RemoteDialogContext)
const inNavigation = entry.slot === 'navigation'
const tabs = useMemo(
() => ({ idPrefix: state.idPrefix, selected: state.selected }),
[state.idPrefix, state.selected],
)
const panels = <RemoteTabsContext value={tabs}>{children}</RemoteTabsContext>
return (
<>
<Tabs
variant={inNavigation ? 'plain' : 'bordered'}
idPrefix={state.idPrefix}
label={label}
items={state.items}
value={state.selected}
onChange={handler.handleChange}
/>
{inNavigation ? (dialog?.body ? createPortal(panels, dialog.body) : null) : panels}
</>
)
}

export function RemoteTabPanel({ entry, children }: RemoteElementProps) {
const { value } = useRemoteProperties(uiKitCatalog['ui-tab-panel'].properties, entry)
const tabs = useContext(RemoteTabsContext)
if (!tabs) return null
return (
<section
role="tabpanel"
id={`${tabs.idPrefix}-panel-${value}`}
aria-labelledby={`${tabs.idPrefix}-tab-${value}`}
hidden={tabs.selected !== value}
tabIndex={0}
>
<div className="grid min-w-0 gap-4">{children}</div>
</section>
)
}
Loading