Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

3.4 KB
Raw Blame
import { useId, useRef, useState } from "react";
import { useCopy } from "../../ui/copy";
import { Glyph } from "../../ui/icons";
import { Btn } from "./primitives";
import { useAnchoredPopover } from "./use-anchored-popover";

// Permission belongs to an exact origin, never to an invitation path or fragment.
export function siteEntryOrigin(value: string): string | null {
  try {
    const address = value.trim();
    const url = new URL(address.includes("://") ? address : `https://${address}`);
    return ["http:", "https:"].includes(url.protocol) && !url.username && !url.password &&
      !/\s/.test(decodeURIComponent(url.hostname)) ? url.origin : null;
  } catch { return null; }
}

export function SiteAddressEntry({ initialAddress = "", onSelect }: {
  initialAddress?: string; onSelect: (origin: string) => void;
}) {
  const { vis, t } = useCopy();
  const id = useId();
  const anchor = useRef<HTMLSpanElement>(null);
  const panel = useRef<HTMLDivElement>(null);
  const input = useRef<HTMLInputElement>(null);
  const [open, setOpen] = useState(false);
  const [invalid, setInvalid] = useState(false);
  useAnchoredPopover(anchor, panel);
  function selectSite() {
    const origin = siteEntryOrigin(input.current?.value ?? "");
    if (!origin) {
      setInvalid(true);
      input.current?.focus();
      return;
    }
    panel.current?.hidePopover();
    onSelect(origin);
  }
  return <>
    <span ref={anchor} className="lr-client-site-add">
      <Btn icon="plus" title="client.connect.addSite" size="dense" quiet hint="hint-client-site"
        hintText="client.connect.addSiteHint" expanded={open} controls={id} popoverTarget={id} />
    </span>
    <div ref={panel} id={id} popover="auto" role="dialog" aria-label={t("client.connect.addSite")}
      className="lr-anchored-card lr-client-site-entry" onToggle={event => {
        if (event.target !== event.currentTarget) return;
        const opened = event.newState === "open";
        setOpen(opened);
        if (opened) {
          input.current?.focus();
          input.current?.select();
        }
      }}>
      <strong className={vis ? "visually-hidden" : undefined}>{t("client.connect.addSite")}</strong>
      <div className="lr-client-site-address">
        <label className="lr-input">
          <Glyph name="globe" size={18} />
          <input ref={input} defaultValue={initialAddress} type="text" inputMode="url" enterKeyHint="go" autoComplete="url"
            autoCapitalize="none" spellCheck={false} placeholder="piik.example"
            aria-label={t("client.connect.siteAddress")} aria-invalid={invalid}
            aria-describedby={invalid ? `${id}-error` : undefined} onInput={() => setInvalid(false)}
            onBlur={event => {
              const origin = siteEntryOrigin(event.currentTarget.value);
              if (origin) event.currentTarget.value = origin;
            }}
            onKeyDown={event => {
              if (event.key === "Enter" && !event.nativeEvent.isComposing) {
                event.preventDefault();
                selectSite();
              }
            }} />
        </label>
        <Btn icon="arrowRight" title="client.connect.continue" size="compact" tone="primary" onClick={selectSite} />
      </div>
      {invalid && <p id={`${id}-error`} role="alert">{t("client.connect.invalid")}</p>}
      <p className={vis ? "visually-hidden" : undefined}>{t("client.connect.addSiteHint")}</p>
    </div>
  </>;
}