Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

9.7 KB
Raw Blame
import { fireAndForget } from '@core/util/fireAndForget';
import UsersThree from '@icon/system/regular/users-three.svg';
import { Button, Input, ToggleSwitch, Tooltip } from '@ui';
import { ResultAsync } from 'neverthrow';
import { createSignal, Show } from 'solid-js';
import { AudiencePanel } from '../components/share/audience-panel';
import { BrandingPanel } from '../components/share/branding-panel';
import {
  type FormMetadataPatch,
  type FormWriteFailure,
  useFormContext,
} from '../context/form-context';
import {
  instantFromLocalInput,
  localInputFromInstant,
} from '../core/date-answers';
import type { FormDetail } from '../core/form-model';
import { createAudienceChange } from '../primitives/create-audience-change';
import type { Preview } from '../primitives/create-preview';

/**
 * The Share tab (RFC 02 §2, §6): who can respond, the respond link, whether
 * the form takes responses and until when, whether responses and names are
 * shared, how the respond page is branded, and the people it is shared with
 * through the share dialog.
 */
export function ShareTabView(props: {
  detail: FormDetail;
  respondLink: string;
  trackWrites: Preview['trackWrites'];
  onOpenShare: () => void;
  /** The form went to the trash; the host closes it. */
  onTrashed: () => void;
}) {
  const context = useFormContext();
  const form = () => props.detail.form;
  const isOwner = () => props.detail.access === 'owner';
  const canEdit = () => props.detail.access !== 'view';
  const [pending, setPending] = createSignal(false);
  const summary = context.responses.createSummary(
    () => form().id,
    () => isOwner()
  );
  /** Whether the form is anonymous is fixed once anyone has responded. */
  const anonymityLocked = () => (summary.value()?.submitted ?? 0) > 0;
  const writes = new Set<ResultAsync<void, FormWriteFailure>>();
  props.trackWrites(() =>
    ResultAsync.combine([...writes]).map(() => undefined)
  );

  function updateMetadata(formId: string, patch: FormMetadataPatch) {
    const write = context.updateMetadata(formId, patch);
    writes.add(write);
    return write
      .andTee(() => writes.delete(write))
      .orTee(() => writes.delete(write));
  }

  async function moveToTrash() {
    const confirmed = await context.confirm({
      title: `Move “${form().name}” to the trash?`,
      body: 'It stops taking responses. Its database and every response row stay where they are.',
      confirmLabel: 'Move to trash',
      tone: 'danger',
    });
    if (!confirmed) return;
    const result = await context.trashForm(form().id);
    if (result.isErr()) {
      context.notify.failure(
        `The form wasn’t moved to the trash: ${result.error.message}`
      );
      return;
    }
    context.notify.success('Form moved to the trash.');
    props.onTrashed();
  }

  async function update(patch: FormMetadataPatch, what: string) {
    setPending(true);
    const result = await updateMetadata(form().id, patch);
    setPending(false);
    if (result.isErr())
      context.notify.failure(`${what} wasn’t saved: ${result.error.message}`);
  }

  const audience = createAudienceChange({
    detail: () => props.detail,
    updateMetadata,
    notify: context.notify,
  });

  return (
    <div class="h-full min-h-0 overflow-y-auto bg-canvas-base touch:pb-(--mobile-content-inset-bottom)">
      <div class="mx-auto flex w-full max-w-[680px] flex-col gap-4 px-4 py-6">
        <div class="rounded-xl border border-edge bg-surface p-4">
          <AudiencePanel
            audience={form().audience}
            canChange={isOwner()}
            respondLink={props.respondLink}
            onCopyFailure={() =>
              context.notify.failure('Could not copy form link.')
            }
            pending={pending() || audience.pending()}
            onChange={(next) =>
              fireAndForget(audience.change(next), 'form-share')
            }
          />
        </div>
        <div class="flex flex-col gap-4 rounded-xl border border-edge bg-surface p-4">
          <h3 class="text-sm font-semibold text-ink">Responses</h3>
          <ToggleSwitch
            size="md"
            label="Accepting responses"
            labelClass="text-sm text-ink"
            checked={form().status === 'open'}
            disabled={!isOwner() || pending()}
            onChange={(open) =>
              fireAndForget(
                update(
                  { status: open ? 'open' : 'closed' },
                  'Accepting responses'
                ),
                'form-share'
              )
            }
          />
          <label class="flex flex-col gap-1">
            <span class="text-xs font-medium text-ink-muted">
              Close automatically
            </span>
            <span class="flex items-center gap-2">
              <Input
                type="datetime-local"
                value={
                  form().closesAt
                    ? localInputFromInstant(form().closesAt ?? '')
                    : ''
                }
                disabled={!isOwner() || pending()}
                onChange={(event) => {
                  const closesAt = instantFromLocalInput(
                    event.currentTarget.value
                  );
                  if (closesAt)
                    fireAndForget(
                      update({ closesAt }, 'The closing date'),
                      'form-share'
                    );
                }}
              />
              <Show when={form().closesAt && isOwner()}>
                <Button
                  variant="ghost"
                  size="sm"
                  disabled={pending()}
                  onClick={() =>
                    fireAndForget(
                      update({ closesAt: null }, 'The closing date'),
                      'form-share'
                    )
                  }
                >
                  Clear
                </Button>
              </Show>
            </span>
          </label>
          <ToggleSwitch
            size="md"
            label="Share responses with team"
            labelClass="text-sm text-ink"
            checked={form().responsesVisible}
            disabled={!isOwner() || pending()}
            onChange={(responsesVisible) =>
              fireAndForget(
                update({ responsesVisible }, 'Sharing responses'),
                'form-share'
              )
            }
          />
          <Tooltip
            label={
              anonymityLocked()
                ? 'People have responded, so this can’t change.'
                : 'Nobody in Macro, you included, sees who gave which response.'
            }
            class="self-start"
          >
            <ToggleSwitch
              size="md"
              label="Anonymous"
              labelClass="text-sm text-ink"
              checked={form().anonymous}
              disabled={!isOwner() || pending() || anonymityLocked()}
              onChange={(anonymous) =>
                fireAndForget(
                  update({ anonymous }, 'Anonymous responses'),
                  'form-share'
                )
              }
            />
          </Tooltip>
          <label class="flex flex-col gap-1">
            <span class="text-xs font-medium text-ink-muted">
              Confirmation message
            </span>
            <textarea
              rows={2}
              value={form().confirmationMessage}
              placeholder="Your response is saved."
              maxlength={2000}
              disabled={!canEdit()}
              class="w-full resize-y rounded-md border border-edge-muted bg-input px-2.5 py-1.5 text-sm text-ink outline-none placeholder:text-ink-placeholder focus:border-edge-focus disabled:opacity-60"
              onBlur={(event) => {
                const confirmationMessage = event.currentTarget.value;
                if (confirmationMessage !== form().confirmationMessage)
                  fireAndForget(
                    update({ confirmationMessage }, 'The confirmation message'),
                    'form-share'
                  );
              }}
            />
          </label>
          <Show when={!isOwner()}>
            <p class="text-xs text-ink-muted">
              Only the owner can change who responds, close the form, show
              results, share responses or make them anonymous.
            </p>
          </Show>
        </div>
        <BrandingPanel
          branding={form().branding}
          canEdit={canEdit()}
          pending={pending()}
          onChange={update}
        />
        <div class="flex items-center gap-3 rounded-xl border border-edge bg-surface p-4">
          <UsersThree
            class="size-5 shrink-0 text-ink-muted"
            aria-hidden="true"
          />
          <div class="flex min-w-0 flex-1 flex-col">
            <span class="text-sm font-medium text-ink">
              People and channels
            </span>
            <span class="text-xs text-ink-muted">
              Posting the form in a channel lets its members respond.
            </span>
          </div>
          <Button variant="secondary" size="sm" onClick={props.onOpenShare}>
            Manage access
          </Button>
        </div>
        <Show when={isOwner()}>
          <div class="flex items-center gap-3 rounded-xl border border-edge bg-surface p-4">
            <div class="flex min-w-0 flex-1 flex-col">
              <span class="text-sm font-medium text-ink">Move to trash</span>
              <span class="text-xs text-ink-muted">
                The database and its rows are kept.
              </span>
            </div>
            <Button
              variant="danger"
              size="sm"
              onClick={() => fireAndForget(moveToTrash(), 'form-share')}
            >
              Move to trash
            </Button>
          </div>
        </Show>
      </div>
    </div>
  );
}