Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

16.2 KB
Raw Blame
import { html, render, nothing } from "lit";
import { live } from "lit/directives/live.js";
import { Building2, Globe, Lock, Search, X } from "lucide";
import { api } from "./core-bridge";
import { avatarTpl, closeFormMenus, menuSelect } from "./ui";
import { icon } from "./icons";
import { scopeChip } from "./contexts";
import { friendlyPrincipal } from "./deploy-view";
import { errMessage } from "../../chassis/src/errors";
import { peopleResults, type DirectoryMatch } from "./people-results";
import { appState } from "./shell-state";

interface Grant {
  scope: string;
  permission: "read" | "write";
}

type GeneralAccess = "restricted" | "org" | "public";

const isOrgGrant = (grant: Grant): boolean => grant.scope === "org" || grant.scope.startsWith("org:");

export async function openDeploymentPermissions(id: string, title: string, owner: string): Promise<void> {
  const opener = document.activeElement as HTMLElement | null;
  const dialog = document.createElement("dialog");
  dialog.className = "project-dialog deployment-permissions-dialog";
  dialog.setAttribute("aria-labelledby", "deployment-permissions-heading");
  document.body.append(dialog);
  let grantees: Grant[] = [];
  let publicAccess = false;
  let externalSharing = true;
  let matches: DirectoryMatch[] = [];
  let query = "";
  let access = "view";
  let selected: (DirectoryMatch & { email?: string }) | null = null;
  let searchSequence = 0;
  let timer: ReturnType<typeof setTimeout> | undefined;
  let searching = false;
  const names = new Map<string, string>();
  let busy = true;
  let loaded = false;
  let error = "";
  let searched = false;
  const endpoint = `/api/deployments/${encodeURIComponent(id)}/share`;
  const close = () => {
    clearTimeout(timer);
    searchSequence++;
    closeFormMenus();
    dialog.close();
    dialog.remove();
    if (opener?.isConnected) opener.focus();
  };
  dialog.addEventListener("cancel", (event) => {
    event.preventDefault();
    if (closeFormMenus()) return;
    close();
  });
  const change = async (scope: string, value: string, email?: string) => {
    if (busy) return;
    closeFormMenus();
    busy = true;
    error = "";
    draw();
    try {
      const response = await api<{ public: boolean; grantees: Grant[]; invitation?: { emailProblem?: string } }>(
        endpoint,
        {
          method: "POST",
          body: JSON.stringify({ ...(email ? { email } : { scope }), access: value }),
        },
      );
      publicAccess = response.public;
      grantees = response.grantees;
      if (response.invitation?.emailProblem)
        error = `Access granted, but no invitation email was sent: ${response.invitation.emailProblem}`;
      selected = null;
      query = "";
      searched = false;
      searchSequence++;
      matches = [];
    } catch (e) {
      error = errMessage(e, "Could not update permissions.");
    } finally {
      busy = false;
      if (dialog.isConnected) draw();
    }
  };
  const orgGrant = (): Grant | undefined => grantees.find(isOrgGrant);
  const generalAccess = (): GeneralAccess => {
    if (publicAccess) return "public";
    return orgGrant() ? "org" : "restricted";
  };
  const post = async (body: Record<string, unknown>) => {
    const response = await api<{ public: boolean; grantees: Grant[] }>(endpoint, {
      method: "POST",
      body: JSON.stringify(body),
    });
    publicAccess = response.public;
    grantees = response.grantees;
  };
  const changeGeneral = async (next: GeneralAccess) => {
    if (busy || next === generalAccess() || (next === "public" && !externalSharing)) return;
    closeFormMenus();
    busy = true;
    error = "";
    draw();
    try {
      if (next === "public") await post({ public: true });
      else {
        if (publicAccess) await post({ public: false });
        const grant = orgGrant();
        if (next === "org" && !grant) await post({ scope: "org", access: "view" });
        if (next === "restricted" && grant) await post({ scope: grant.scope, access: "none" });
      }
    } catch (e) {
      error = errMessage(e, "Could not update general access.");
    } finally {
      busy = false;
      if (dialog.isConnected) draw();
    }
  };
  const search = async () => {
    const sequence = ++searchSequence;
    const term = query.trim();
    if (!term) {
      matches = [];
      searched = false;
      searching = false;
      draw();
      return;
    }
    searching = true;
    error = "";
    draw();
    try {
      const response = await api<{ matches?: DirectoryMatch[] }>(
        `/api/directory/resolve?q=${encodeURIComponent(term)}`,
      );
      if (sequence !== searchSequence || !dialog.isConnected) return;
      matches = (response.matches ?? [])
        .filter(
          (person) =>
            `personal:${person.principalId}` !== owner &&
            !grantees.some((grant) => grant.scope === `personal:${person.principalId}`),
        )
        .slice(0, 8);
      searched = true;
    } catch (e) {
      if (sequence === searchSequence) error = errMessage(e, "Could not find people.");
    } finally {
      if (sequence === searchSequence && dialog.isConnected) {
        searching = false;
        draw();
      }
    }
  };
  const orgName = (): string => appState.me?.companyName?.trim() || "Your organization";
  const generalAccessNote = (): string => {
    const level = generalAccess();
    if (level === "public") return "Anyone on the internet with the link can open. No sign-in required";
    if (level === "org") return `Anyone signed in to ${orgName()} can open`;
    return "Only people with access can open";
  };
  const emailCandidate = (): string | null => {
    const email = query.trim().toLowerCase();
    if (!externalSharing) return null;
    if (email.length > 254 || !/^[^@\s,;<>"]+@[^@\s,;<>"]+\.[^@\s,;<>"]+$/.test(email)) return null;
    if (
      owner.toLowerCase() === `personal:${email}` ||
      grantees.some((g) => g.scope.toLowerCase() === `personal:${email}`)
    )
      return null;
    if (matches.some((m) => m.principalId.toLowerCase() === email)) return null;
    return email;
  };
  const permissionMenu = (value: string, label: string, update: (value: string) => void, removable = false) =>
    html`<fieldset class="permission-control" ?disabled=${busy}>
      ${menuSelect({
        value,
        ariaLabel: label,
        options: [
          { value: "view", label: "Can view" },
          { value: "manage", label: "Can manage" },
          ...(removable ? [{ value: "none", label: "Remove access" }] : []),
        ],
        onSelect: (next) => {
          if (next) update(next);
        },
      })}
    </fieldset>`;
  const draw = () =>
    render(
      html`
        <div class="project-dialog-head">
          <div>
            <h2 id="deployment-permissions-heading">App permissions</h2>
            <p>${title}</p>
          </div>
          <button class="chip-x" type="button" aria-label="Close" @click=${close}>${icon(X, 16)}</button>
        </div>
        ${
          loaded
            ? html` <div class="permission-search">
                ${
                  selected
                    ? html`<div class="permission-invite">
                        <div class="permission-person">
                          ${avatarTpl({
                            key: selected.principalId,
                            name: selected.displayName,
                            avatarUrl: selected.avatarUrl,
                          })}<span class="permission-person-label"
                            >${selected.displayName}<small>${selected.principalId}</small></span
                          >
                        </div>
                        <button
                          class="chip-x"
                          aria-label="Cancel selection"
                          ?disabled=${busy}
                          @click=${() => {
                            selected = null;
                            draw();
                          }}
                        >
                          ${icon(X, 14)}
                        </button>
                        <div class="permission-invite-actions">
                          ${
                            selected.email
                              ? html`<span class="permission-note">Can view</span>`
                              : permissionMenu(access, "New person's access", (value) => {
                                  access = value;
                                  draw();
                                })
                          }<button
                            class="btn primary"
                            ?disabled=${busy}
                            @click=${() => {
                              if (selected) {
                                names.set(`personal:${selected.principalId}`, selected.displayName);
                                void change(
                                  `personal:${selected.principalId}`,
                                  selected.email ? "view" : access,
                                  selected.email,
                                );
                              }
                            }}
                          >
                            Add
                          </button>
                        </div>
                      </div>`
                    : html`<form
                        @submit=${(event: SubmitEvent) => {
                          event.preventDefault();
                          clearTimeout(timer);
                          void search();
                        }}
                      >
                        <div class="project-member-search-row">
                          ${icon(Search, 16)}<input
                            id="app-people-query"
                            aria-label="Add people"
                            placeholder="Add people by name or email"
                            type="search"
                            autocomplete="off"
                            maxlength="254"
                            .value=${live(query)}
                            ?disabled=${busy}
                            @input=${(event: Event) => {
                              query = (event.currentTarget as HTMLInputElement).value;
                              searchSequence++;
                              matches = [];
                              searched = false;
                              clearTimeout(timer);
                              timer = setTimeout(() => void search(), 200);
                            }}
                          />
                        </div>
                        ${peopleResults(matches, busy, (person) => {
                          selected = person;
                          closeFormMenus();
                          draw();
                        })}
                        ${
                          !searching && searched && emailCandidate()
                            ? html`
                                <button
                                  class="project-member-result"
                                  type="button"
                                  ?disabled=${busy}
                                  @click=${() => {
                                    const email = emailCandidate();
                                    if (!email) return;
                                    selected = { principalId: email, displayName: email, type: "guest", email };
                                    access = "view";
                                    closeFormMenus();
                                    draw();
                                  }}
                                >
                                  Add ${emailCandidate()} with view access
                                </button>
                                <p class="permission-note">
                                  They receive an app link by email and sign in with this address. This does not add
                                  them to your organization.
                                </p>
                              `
                            : nothing
                        }
                        ${searching ? html`<p class="permission-note" role="status">Searching…</p>` : nothing}
                        ${!searching && searched && !matches.length && !emailCandidate() ? html`<p class="permission-note">No additional people found.</p>` : nothing}
                      </form>`
                }
              </div>`
            : nothing
        }
        <div class="permission-section-label">People with access</div>
        <div class="project-member-list">
          <div class="permission-row">
            ${avatarTpl({ key: owner, name: owner.replace("personal:", "") })}<span class="permission-person-label"
              >${owner.startsWith("personal:") ? friendlyPrincipal(owner.slice(9)) : scopeChip(owner)}</span
            ><span class="permission-owner">Owner</span>
          </div>
          ${grantees.filter((grant) => generalAccess() !== "org" || !isOrgGrant(grant)).map((grant) => html`<div class="permission-row">${avatarTpl({ key: grant.scope, name: isOrgGrant(grant) ? orgName() : grant.scope.replace("personal:", "") })}<span class="permission-person-label">${isOrgGrant(grant) ? orgName() : (names.get(grant.scope) ?? (grant.scope.startsWith("personal:") ? grant.scope.slice(9) : scopeChip(grant.scope)))}${names.has(grant.scope) ? html`<small>${grant.scope.replace("personal:", "")}</small>` : nothing}</span>${permissionMenu(grant.permission === "write" ? "manage" : "view", `Access for ${grant.scope}`, (value) => void change(grant.scope, value), true)}</div>`)}
        </div>
        <div class="permission-section-label permission-general-label">General access</div>
        <div class="project-member-list">
          <div class="permission-row">
            <span class="project-member-avatar" aria-hidden="true"
              >${icon({ public: Globe, org: Building2, restricted: Lock }[generalAccess()], 16)}</span
            >
            <span class="permission-person-label">
              <fieldset class="permission-control general-access-control" ?disabled=${busy}>
                ${menuSelect({
                  value: generalAccess(),
                  ariaLabel: "General access",
                  className: "general-access-menu",
                  options: [
                    { value: "restricted", label: "Restricted" },
                    { value: "org", label: orgName() },
                    {
                      value: "public",
                      label: "Anyone with the link",
                      ...(externalSharing
                        ? {}
                        : { disabledHint: "An org admin must turn on external app sharing first." }),
                    },
                  ],
                  onSelect: (next) => {
                    if (next === "restricted" || next === "org" || next === "public") void changeGeneral(next);
                  },
                })}
              </fieldset>
              <small>${generalAccessNote()}</small>
            </span>
            ${(() => {
              const grant = generalAccess() === "org" ? orgGrant() : undefined;
              return grant
                ? permissionMenu(
                    grant.permission === "write" ? "manage" : "view",
                    `Access for ${orgName()}`,
                    (value) => void change(grant.scope, value),
                  )
                : nothing;
            })()}
          </div>
        </div>
        ${
          publicAccess
            ? html`<p class="share-external-warning" role="status">⚠️ Public. Anyone can open this app.</p>`
            : nothing
        }
        ${busy ? html`<p role="status">Loading…</p>` : nothing}
        ${error ? html`<p class="composer-error" role="alert">${error}</p>` : nothing}
        <div class="project-dialog-actions actions"><button class="btn" @click=${close}>Done</button></div>
      `,
      dialog,
    );
  draw();
  dialog.showModal();
  try {
    const response = await api<{ public: boolean; externalSharing?: boolean; grantees: Grant[] }>(endpoint);
    publicAccess = response.public;
    externalSharing = response.externalSharing !== false;
    grantees = response.grantees;
    loaded = true;
  } catch (e) {
    error = errMessage(e, "Could not load permissions.");
  } finally {
    busy = false;
    if (dialog.isConnected) draw();
  }
}