Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { ListSkeleton } from '@app/components/view-shell/ListSkeleton';
import { usePending } from '@app/observability/flow';
import {
startUiOperation,
type UiOperation,
} from '@app/observability/ui-operation';
import { fireAndForget } from '@core/util/fireAndForget';
import ArrowLeft from '@icon/system/regular/arrow-left.svg';
import ArrowRight from '@icon/system/regular/arrow-right.svg';
import CalendarCheck from '@icon/system/regular/calendar-check.svg';
import PaperPlaneTilt from '@icon/system/regular/paper-plane-tilt.svg';
import Spinner from '@icon/system/regular/spinner.svg';
import { Button, cn } from '@ui';
import {
createMemo,
createUniqueId,
For,
type JSX,
Match,
onCleanup,
Show,
Switch,
} from 'solid-js';
import { fieldId, QuestionField } from '../components/respond/question-field';
import {
ClosedNotice,
Confirmation,
ProgressStrip,
RespondTitleCard,
SectionHeading,
StopScreen,
} from '../components/respond/respond-screens';
import { useFormContext } from '../context/form-context';
import { brandingStyle } from '../core/branding';
import { questionNumbers } from '../core/form-layout';
import type { FormCellValue, FormDetail } from '../core/form-model';
import { formAvailability, shortDate } from '../core/form-status';
import { resolvedWidget } from '../core/question-types';
import { createRespond } from '../primitives/create-respond';
import { BookingStepView } from './booking-step-view';
import '../components/respond/branding.css';
/**
* The respond page (RFC 02 §4), shared by the app split, the public route
* and the inline card (`compact`). One section per screen; nothing is sent
* until Submit; a gate can stop on Next and the server has the last word.
*/
export function RespondView(props: {
detail: FormDetail;
compact: boolean;
/** Author preview: answers and selected files stay in this tab. */
preview?: boolean;
/** Who is answering, as the page's own header says it. */
header?: JSX.Element;
/** Where the page scrolls, to bring each new section to the top. */
scrollContainer?: () => HTMLElement | undefined;
/** Read the form again; resolves whether the read succeeded. */
refetch: () => Promise<boolean>;
/** Open the Responses table, offered when the form shares its responses. */
onOpenResponses?: () => void;
}) {
const context = useFormContext();
// One per mount: a form can show as a page and a card at once.
const scope = createUniqueId();
const signedIn = () => !!context.viewer.userId();
const mine = context.responses.createMine(() =>
props.preview ? undefined : props.detail.form.id
);
const respond = createRespond({
detail: () => props.detail,
preview: props.preview,
mine: () => (signedIn() ? mine.response() : null),
mineFailed: () => signedIn() && !!mine.failure(),
reloadMine: mine.refetch,
refetch: async () => {
await props.refetch();
},
signedIn,
submit: (answers) =>
context.responses.submit(props.detail.form.id, answers),
editMine: (answers) =>
context.responses.editMine(props.detail.form.id, answers),
now: () => new Date(),
});
usePending('form.response', () => respond.view().kind === 'loading');
let root: HTMLDivElement | undefined;
let submission: UiOperation | undefined;
let disposed = false;
onCleanup(() => {
disposed = true;
submission?.cancel();
submission = undefined;
});
const form = () => props.detail.form;
// An inline card stays in Macro's look; the page wears the owner's.
const branding = () => (props.compact ? undefined : form().branding);
const brandingImage = (
fileId: string | null | undefined,
size: 'small' | 'medium'
) => (fileId ? context.branding.imageUrl(fileId, size) : undefined);
const isPublic = () => form().audience === 'public';
const numbers = createMemo(() => questionNumbers(props.detail.layout));
const open = () =>
formAvailability(form(), props.detail.tableGone, new Date()).kind ===
'open';
const editNote = () => {
if (props.preview || !signedIn() || !open()) return undefined;
const closesAt = form().closesAt;
return closesAt
? `You can edit your response until the form closes on ${shortDate(new Date(closesAt), new Date())}.`
: 'You can edit your response while the form is open.';
};
function revealTop() {
queueMicrotask(() => {
if (disposed) return;
const container = props.scrollContainer?.();
if (container) container.scrollTo({ top: 0, behavior: 'smooth' });
else root?.scrollIntoView({ block: 'start', behavior: 'smooth' });
});
}
/** Bring the first refused question into view and focus it. */
function revealProblem() {
queueMicrotask(() => {
if (disposed) return;
const first = Object.keys(respond.problems())[0];
if (!first) return;
const field = document.getElementById(fieldId(scope, first));
field?.scrollIntoView({ block: 'center', behavior: 'smooth' });
field?.focus({ preventScroll: true });
});
}
async function submitAndReveal(event: Event) {
submission?.cancel();
const operation = startUiOperation('form.submit', {
event,
onFinish: () => {
if (submission === operation) submission = undefined;
},
attributes: {
'form.id': props.detail.form.id,
'form.preview': !!props.preview,
},
});
submission = operation;
try {
await operation.run(() => respond.submit());
if (disposed) return;
revealProblem();
if (respond.view().kind !== 'answering') revealTop();
if (respond.failure()) operation.finish('error');
else operation.afterPaint();
} catch (error) {
operation.finish('error');
throw error;
}
}
/** From the update notice: read the form again, then back to the answers. */
async function retry() {
let read: boolean;
try {
read = await props.refetch();
} catch {
read = false;
}
if (!read) {
context.notify.failure(
'The form couldn’t be read again. Try again in a moment.'
);
return;
}
respond.checkAnswers();
}
const previewFiles = new Set<string>();
onCleanup(() => {
for (const url of previewFiles) URL.revokeObjectURL(url);
});
const upload = async (file: File) => {
if (props.preview) {
const url = URL.createObjectURL(file);
previewFiles.add(url);
return url;
}
const finished = respond.beginUpload();
const uploaded = await context.uploadFile(file);
finished();
if (uploaded.isOk()) return uploaded.value;
context.notify.failure(
`The file couldn’t be uploaded: ${uploaded.error.message}`
);
return undefined;
};
const receipt = (answers: Record<string, FormCellValue>) =>
props.detail.layout.sections.flatMap((section) =>
section.questions.flatMap((question) => {
const column = respond.columns().get(question.columnId);
return column
? [
{
question,
column,
widget: resolvedWidget(column.kind, question.widget),
value: answers[question.id],
},
]
: [];
})
);
return (
<div class={props.compact ? undefined : 'px-6'}>
<div
ref={root}
class={cn(
'mx-auto flex w-full flex-col',
props.compact ? 'gap-3' : 'max-w-3xl gap-6 pt-12 pb-24',
branding()?.accentColor && 'form-branded'
)}
style={brandingStyle(branding()?.accentColor ?? null)}
data-form-respond={form().id}
>
<Show when={props.header}>{props.header}</Show>
<Show when={brandingImage(branding()?.coverFileId, 'medium')}>
{(cover) => (
<img
src={cover()}
alt=""
class="aspect-[4/1] w-full rounded-xl object-cover"
/>
)}
</Show>
<Show when={!props.compact}>
<RespondTitleCard
name={form().name}
description={form().description}
logoUrl={brandingImage(branding()?.logoFileId, 'small')}
compact={false}
/>
</Show>
<Switch>
<Match
when={(() => {
const view = respond.view();
return view.kind === 'preview-complete' ? view : undefined;
})()}
>
{(completed) => (
<Confirmation
message={form().confirmationMessage || 'Preview complete.'}
fresh
submittedAt={undefined}
receipt={receipt(completed().answers)}
canEdit={false}
compact={props.compact}
renderEntityLabel={context.ui.renderEntityLabel}
onEdit={respond.restartPreview}
notice={
<p class="text-sm text-ink-muted">No response was saved.</p>
}
footer={
<Button
variant="secondary"
class="self-start"
onClick={() => {
respond.restartPreview();
revealTop();
}}
>
Try again
</Button>
}
/>
)}
</Match>
<Match
when={(() => {
const view = respond.view();
return view.kind === 'booking' ? view : undefined;
})()}
>
{(step) => (
<>
<BookingStepView
booking={step().booking}
preview={step().preview}
/>
<Show when={step().preview}>
<Button
variant="secondary"
class="self-start"
onClick={() => {
respond.restartPreview();
revealTop();
}}
>
Try again
</Button>
</Show>
</>
)}
</Match>
<Match when={respond.view().kind === 'loading'}>
<div
class="flex flex-col gap-3"
aria-busy="true"
aria-label="Loading"
>
<ListSkeleton.Bar class="h-24 w-full rounded-xl" />
<ListSkeleton.Bar class="h-24 w-full rounded-xl" />
</div>
</Match>
<Match
when={(() => {
const view = respond.view();
return view.kind === 'closed' ? view : undefined;
})()}
>
{(closed) => <ClosedNotice reason={closed().reason} />}
</Match>
<Match
when={(() => {
const view = respond.view();
return view.kind === 'response-unavailable' ? view : undefined;
})()}
>
{(unavailable) => (
<div
role="alert"
class="flex flex-col gap-3 rounded-xl border border-edge bg-surface p-5"
>
<p class="font-medium text-ink">You’ve already responded</p>
<p class="text-sm text-ink-muted">
Your saved response couldn’t be loaded.
</p>
<Button
variant="secondary"
class="self-start"
disabled={unavailable().retrying}
aria-busy={unavailable().retrying}
onClick={() =>
fireAndForget(respond.retryResponse(), 'form-respond')
}
>
Try again
</Button>
</div>
)}
</Match>
<Match when={respond.view().kind === 'updating'}>
<div
role="status"
class="flex flex-col gap-1 rounded-xl border border-edge bg-surface p-5"
>
<p class="text-sm font-medium text-ink">
This form is being updated
</p>
<p class="text-sm text-ink-muted">
Nothing you entered was submitted. Try again in a moment.
</p>
<Button
variant="secondary"
size="sm"
class="mt-2 self-start"
onClick={() => fireAndForget(retry(), 'form-respond')}
>
Try again
</Button>
</div>
</Match>
<Match
when={(() => {
const view = respond.view();
return view.kind === 'stopped' ? view : undefined;
})()}
>
{(stopped) => (
<StopScreen
message={stopped().message}
editing={respond.editing()}
onCheckAnswers={() => {
respond.checkAnswers();
revealTop();
}}
onMessageOwner={
signedIn() && context.viewer.userId() !== form().ownerId
? () => {
context
.messageOwner(form().ownerId)
.mapErr((failure) =>
context.notify.failure(
`The conversation couldn’t be opened: ${failure.message}`
)
);
}
: undefined
}
/>
)}
</Match>
<Match
when={(() => {
const view = respond.view();
return view.kind === 'confirmation' ? view : undefined;
})()}
>
{(confirmation) => (
<Confirmation
message={form().confirmationMessage}
fresh={confirmation().fresh}
submittedAt={confirmation().submittedAt}
receipt={
// An anonymous response's answers are known only to the
// visit that gave them.
form().anonymous && !confirmation().fresh
? []
: receipt(confirmation().answers)
}
canEdit={confirmation().canEdit}
compact={props.compact}
renderEntityLabel={context.ui.renderEntityLabel}
onEdit={() => {
respond.editResponse();
revealTop();
}}
notice={
<>
<Show
when={
!confirmation().fresh && !open() && !form().anonymous
}
>
<p class="text-xs text-ink">
This form is closed, so your response can’t be changed.
</p>
</Show>
<Show when={confirmation().alreadyResponded}>
<p class="text-xs text-ink">
{form().anonymous
? 'You had already responded, so the answers you just gave weren’t sent.'
: 'You had already responded, so the answers you just gave weren’t sent. This is your saved response.'}
</p>
</Show>
<Show when={!confirmation().fresh && form().anonymous}>
<p class="text-xs text-ink">
You responded. This form is anonymous, so your answers
aren’t kept with your name and can’t be shown or
changed.
</p>
</Show>
</>
}
footer={
<>
<Show when={confirmation().booking}>
<Button
variant="primary"
class="self-start"
onClick={() => {
respond.openBooking();
revealTop();
}}
>
<CalendarCheck class="size-3.5" />
Book a time
</Button>
</Show>
<Show when={props.onOpenResponses}>
{(open) => (
<Button
variant="secondary"
class="self-start"
onClick={() => open()()}
>
See responses
</Button>
)}
</Show>
<p class="px-1 text-xs text-ink-muted">
Stored as a row in a database the form owner controls.{' '}
{props.detail.form.anonymous
? 'This form is anonymous: nobody in Macro, its owner included, can see which response is yours, and it can’t be changed.'
: props.detail.form.responsesVisible
? 'Everyone who can open this form sees your answers and your name.'
: 'Your answers are visible to the form’s editors.'}
</p>
</>
}
/>
)}
</Match>
<Match
when={(() => {
const view = respond.view();
return view.kind === 'answering' ? view : undefined;
})()}
>
{(answering) => (
<form
class="flex flex-col gap-3"
noValidate
aria-label={answering().section?.title || form().name}
onSubmit={(event) => {
event.preventDefault();
if (answering().isLast) {
fireAndForget(submitAndReveal(event), 'form-respond');
return;
}
if (respond.next()) revealTop();
else revealProblem();
}}
>
<div
class={
props.compact
? 'flex flex-col gap-3'
: 'overflow-hidden rounded-xl border border-edge bg-surface shadow-xs divide-y divide-edge-divider'
}
>
<Show
when={
answering().total > 1 ||
answering().section?.title ||
answering().section?.description
}
>
<div
class={
props.compact
? 'flex flex-col gap-3'
: 'flex flex-col gap-3 pt-3 pr-2 pb-2 pl-4'
}
>
<Show when={answering().total > 1}>
<ProgressStrip
position={answering().position}
total={answering().total}
/>
</Show>
<SectionHeading
title={answering().section?.title ?? ''}
description={answering().section?.description ?? ''}
/>
</div>
</Show>
<For each={answering().section?.questions}>
{(question) => {
const column = () =>
respond.columns().get(question.columnId);
return (
<Show when={column()}>
{(columnFacts) => {
const widget = () =>
resolvedWidget(
columnFacts().kind,
question.widget
);
// Files and Macro pickers need an account; an
// anonymous visitor is told so, not shown a dead end.
const needsSignIn = () => {
if (signedIn()) return undefined;
if (widget() === 'file' && isPublic())
return 'asks for a file';
const kind = columnFacts().kind.type;
return kind === 'entity' || kind === 'relation'
? 'picks from Macro'
: undefined;
};
return (
<Show
when={!needsSignIn()}
fallback={
<div
class={
props.compact
? 'rounded-xl border border-edge bg-surface px-4 py-3 text-sm text-ink-muted'
: 'py-3 pr-2 pl-4 text-sm text-ink-muted'
}
>
<p>
“{columnFacts().name}” {needsSignIn()},
which needs you to sign in.
</p>
<Show
when={respond.problems()[question.id]}
>
{(problem) => (
<p
id={`${fieldId(scope, question.id)}-error`}
class="mt-1 text-xs text-failure-ink"
>
{problem()}
</p>
)}
</Show>
</div>
}
>
<QuestionField
scope={scope}
question={question}
column={columnFacts()}
widget={widget()}
number={numbers().get(question.id) ?? 0}
value={respond.answers()[question.id]}
problem={respond.problems()[question.id]}
disabled={answering().submitting}
compact={props.compact}
upload={upload}
onChange={(value) =>
respond.setAnswer(question.id, value)
}
renderEntityPicker={(picker) =>
context.ui.renderEntityPicker({
...picker,
label: columnFacts().name,
value: respond.answers()[question.id],
onChange: (value) =>
respond.setAnswer(question.id, value),
})
}
renderRelationPicker={(picker) =>
context.ui.renderRelationPicker({
...picker,
label: columnFacts().name,
value: respond.answers()[question.id],
onChange: (value) =>
respond.setAnswer(question.id, value),
})
}
/>
</Show>
);
}}
</Show>
);
}}
</For>
</div>
<Show
when={
answering().section?.questions.some(
(question) => question.required
) || editNote()
}
>
<div
role="note"
aria-label="Form guidance"
class="flex flex-wrap items-center gap-x-3 gap-y-1 px-1 pt-1 text-xs text-ink-muted"
>
<Show
when={answering().section?.questions.some(
(question) => question.required
)}
>
<p class="inline-flex items-center gap-1.5">
<span class="text-failure-ink" aria-hidden="true">
*
</span>
<span>Required fields</span>
</p>
</Show>
<Show when={editNote()}>{(note) => <p>{note()}</p>}</Show>
</div>
</Show>
<Show when={respond.failure()}>
{(message) => (
<p role="alert" class="px-1 text-sm text-failure-ink">
{message()}
</p>
)}
</Show>
<div
class={cn(
'flex items-center justify-between gap-2',
!props.compact &&
'max-sm:fixed max-sm:inset-x-0 max-sm:bottom-0 touch:max-sm:bottom-[var(--mobile-content-inset-bottom,0px)] max-sm:z-10 max-sm:border-t max-sm:border-edge max-sm:bg-surface max-sm:px-4 max-sm:py-3 max-sm:pb-[max(0.75rem,env(safe-area-inset-bottom))]'
)}
>
<Show when={!answering().isFirst} fallback={<span />}>
<Button
type="button"
variant="ghost"
size="md"
disabled={answering().submitting}
onClick={() => {
respond.back();
revealTop();
}}
>
<ArrowLeft class="size-3.5" />
Back
</Button>
</Show>
<Button
type="submit"
variant="primary"
size="md"
disabled={answering().submitting || answering().uploading}
aria-busy={answering().submitting}
>
<Show
when={!answering().uploading}
fallback={<>Waiting for upload…</>}
>
<Show
when={answering().isLast}
fallback={
<>
Next
<ArrowRight class="size-3.5" />
</>
}
>
<Show
when={answering().submitting}
fallback={<PaperPlaneTilt class="size-3.5" />}
>
<Spinner class="size-3.5 animate-spin" />
</Show>
{respond.editing()
? 'Update response'
: answering().continuesToBooking
? 'Continue to booking'
: 'Submit'}
</Show>
</Show>
</Button>
</div>
</form>
)}
</Match>
</Switch>
</div>
</div>
);
}