Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

9.1 KB
Raw Blame
/**
 * The dialogs a channel needs: creating one (display name, then the id derived from it by
 * the field's own button, then the purpose) from the channel list's header, and the two
 * one-field edits its header menu opens — rename and purpose. Failures stay inside the
 * dialog: a rejected id lands under the id field, anything else in a strip above the footer,
 * so the fields never sit disabled behind a toast that has already gone.
 *
 * Plus the join prompt, which both entry points into joining raise — the channel view's
 * "you are not a member" notice and the sidebar row's own Join — so the two cannot ask
 * different questions.
 */
import { useEffect, useState } from "react";
import type { OrgChannelItem } from "@adelie/server/api";
import { Button, ConfirmModal, Input, Modal, Textarea } from "@adelie/ui";
import * as api from "../../api/endpoints";
import { ApiError } from "../../api/client";
import { S } from "../../lib/strings";
import { apiErrorText } from "../../lib/api-error";
import { channelIdProblem } from "./channel-list";
import type { ChannelIdProblem } from "./channel-list";
import { SemanticIdField } from "../semantic-id/semantic-id-field";
import { ErrorLine } from "./shared";

/** The error codes that are about the id the user typed; every other failure is the form's. */
const ID_ERROR_CODES = new Set(["channel_exists", "bad_request"]);

/** What the id field says about what was typed, in the reader's language. */
function idProblemText(problem: ChannelIdProblem): string {
  if (problem === "required") return S.common.requiredField;
  if (problem === "reserved") return S.company.channels.idReserved;
  if (problem === "taken") return S.company.channels.idTaken;
  return S.company.channels.idHint;
}

export function NewChannelDialog({
  open,
  projectId,
  orgId,
  taken,
  onClose,
  onCreated,
}: {
  open: boolean;
  projectId: string;
  orgId: string;
  /** Every channel id the listing holds, so a duplicate is refused before it collides. */
  taken: readonly string[];
  onClose: () => void;
  onCreated: (channel: OrgChannelItem) => void;
}) {
  const [channelId, setChannelId] = useState("");
  const [name, setName] = useState("");
  const [purpose, setPurpose] = useState("");
  const [idError, setIdError] = useState<string | undefined>(undefined);
  const [formError, setFormError] = useState<string | null>(null);
  const [busy, setBusy] = useState(false);

  // No draft is kept: the form starts empty every time it opens.
  useEffect(() => {
    if (!open) return;
    setChannelId("");
    setName("");
    setPurpose("");
    setIdError(undefined);
    setFormError(null);
  }, [open]);

  const submit = async () => {
    const id = channelId.trim();
    const problem = channelIdProblem(id, taken);
    if (problem !== null) {
      setIdError(idProblemText(problem));
      return;
    }
    setBusy(true);
    setFormError(null);
    try {
      const created = await api.createOrgChannel(projectId, orgId, {
        channelId: id,
        ...(name.trim() !== "" ? { name: name.trim() } : {}),
        ...(purpose.trim() !== "" ? { purpose: purpose.trim() } : {}),
      });
      onCreated(created);
    } catch (e) {
      const text = apiErrorText(e);
      if (e instanceof ApiError && ID_ERROR_CODES.has(e.code)) setIdError(text);
      else setFormError(text);
    } finally {
      setBusy(false);
    }
  };

  return (
    <Modal
      open={open}
      title={S.company.channels.createTitle}
      onClose={busy ? () => undefined : onClose}
      footer={
        <>
          <Button size="sm" onClick={onClose} disabled={busy}>
            {S.common.cancel}
          </Button>
          <Button size="sm" variant="primary" disabled={busy} onClick={() => void submit()}>
            {busy ? S.company.channels.creating : S.common.create}
          </Button>
        </>
      }
    >
      <div className="space-y-3">
        {/* The name comes first and the id is derived from it: an id is the harder half to
            invent, and naming the channel is where anyone starts anyway. */}
        <Input
          label={S.company.channels.nameField}
          size="sm"
          value={name}
          hint={S.company.channels.nameHint}
          autoFocus
          disabled={busy}
          onChange={(e) => setName(e.target.value)}
        />
        <SemanticIdField
          projectId={projectId}
          kind="channel"
          label={S.company.channels.idField}
          hint={S.company.channels.idHint}
          value={channelId}
          source={name}
          taken={taken}
          error={idError}
          disabled={busy}
          onChange={(id) => {
            setChannelId(id);
            setIdError(undefined);
          }}
          onEnter={() => void submit()}
        />
        <Textarea
          label={S.company.channels.purpose}
          size="sm"
          rows={2}
          value={purpose}
          hint={S.company.channels.purposeHint}
          disabled={busy}
          onChange={(e) => setPurpose(e.target.value)}
        />
        {formError !== null && <ErrorLine message={formError} onRetry={() => void submit()} />}
      </div>
    </Modal>
  );
}

/**
 * One text field in a dialog: the channel's name (one line) or its purpose (a short
 * paragraph). The caller owns the request, so the same shell serves both edits.
 */
export function ChannelTextDialog({
  open,
  title,
  label,
  hint,
  initial,
  multiline = false,
  required = false,
  onClose,
  onSubmit,
}: {
  open: boolean;
  title: string;
  label: string;
  hint?: string;
  initial: string;
  multiline?: boolean;
  required?: boolean;
  onClose: () => void;
  /** Writes the value; a rejection is shown inside the dialog and the field stays editable. */
  onSubmit: (value: string) => Promise<void>;
}) {
  const [value, setValue] = useState(initial);
  const [fieldError, setFieldError] = useState<string | undefined>(undefined);
  const [formError, setFormError] = useState<string | null>(null);
  const [busy, setBusy] = useState(false);

  // Every opening starts from what is stored now, not from the last edit's leftovers.
  useEffect(() => {
    if (!open) return;
    setValue(initial);
    setFieldError(undefined);
    setFormError(null);
  }, [open, initial]);

  const submit = async () => {
    const next = value.trim();
    if (required && next === "") {
      setFieldError(S.common.requiredField);
      return;
    }
    setBusy(true);
    setFormError(null);
    try {
      await onSubmit(next);
    } catch (e) {
      setFormError(apiErrorText(e));
    } finally {
      setBusy(false);
    }
  };

  return (
    <Modal
      open={open}
      title={title}
      onClose={busy ? () => undefined : onClose}
      footer={
        <>
          <Button size="sm" onClick={onClose} disabled={busy}>
            {S.common.cancel}
          </Button>
          <Button size="sm" variant="primary" disabled={busy} onClick={() => void submit()}>
            {busy ? S.common.saving : S.common.save}
          </Button>
        </>
      }
    >
      <div className="space-y-3">
        {multiline ? (
          <Textarea
            label={label}
            size="sm"
            rows={3}
            value={value}
            {...(hint !== undefined ? { hint } : {})}
            autoFocus
            disabled={busy}
            onChange={(e) => setValue(e.target.value)}
          />
        ) : (
          <Input
            label={label}
            size="sm"
            required={required}
            value={value}
            error={fieldError}
            {...(hint !== undefined ? { hint } : {})}
            autoFocus
            disabled={busy}
            onChange={(e) => {
              setValue(e.target.value);
              setFieldError(undefined);
            }}
            onKeyDown={(e) => {
              if (e.key === "Enter") {
                e.preventDefault();
                void submit();
              }
            }}
          />
        )}
        {formError !== null && <ErrorLine message={formError} onRetry={() => void submit()} />}
      </div>
    </Modal>
  );
}

/**
 * "Join this channel?". Joining is not a destructive act but it is a standing one: from then
 * on this channel's @-mentions reach the reader, which is what the body says rather than
 * "are you sure".
 *
 * A refused join keeps the card standing with the server's words inside it (`refusal`), next to
 * the button that retries it: the toast both entry points used to raise dismissed itself out of
 * the card the reader was still looking at.
 */
export function JoinChannelConfirm({
  open,
  busy,
  onClose,
  onConfirm,
  refusal,
}: {
  open: boolean;
  busy: boolean;
  onClose: () => void;
  onConfirm: () => void;
  /** The server's words for a refused join; absent (not empty) when nothing was refused. */
  refusal?: string;
}) {
  return (
    <ConfirmModal
      open={open}
      title={S.company.channels.joinTitle}
      tone="primary"
      confirmLabel={S.company.channels.join}
      cancelLabel={S.common.cancel}
      busy={busy}
      onClose={onClose}
      onConfirm={onConfirm}
      {...(refusal !== undefined ? { refusal } : {})}
    >
      <p className="text-sm text-gray-600 dark:text-gray-300">{S.company.channels.joinConfirm}</p>
    </ConfirmModal>
  );
}