@aperture-browser/session-react 1.0.0-beta.2 → 1.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
6
6
  import { AppWindow, ArrowDownToLine, ArrowLeft, ArrowRight, ArrowUpFromLine, Bot, Camera, Check, CheckIcon, ChevronLeft, ChevronLeftIcon, ChevronRight, ChevronRightIcon, Circle, CircleAlert, CircleCheck, CircleCheckIcon, ClipboardList, ClipboardPaste, Copy, Download, ExternalLink, Eye, FileIcon, Film, FolderOpen, Gauge, Globe2, Hand, History, Hourglass, Image, Images, InfoIcon, Keyboard, Layers2, Link2, Loader2, Loader2Icon, Maximize2, Monitor, MoreHorizontal, MoreVertical, MousePointer2, OctagonXIcon, PanelRight, Paperclip, Pause, Pencil, Play, Plus, Radio, RefreshCw, RotateCcw, Scissors, Search, Smartphone, Square, SunMoon, TextSelect, Trash2, TriangleAlertIcon, Undo2, Unplug, Volume2, VolumeX, X, XIcon } from "lucide-react";
7
7
  import { useTheme } from "next-themes";
8
8
  import { Toaster, toast } from "sonner";
9
+ import { useHotkey } from "@tanstack/react-hotkeys";
9
10
  import { Drawer } from "@base-ui/react/drawer";
10
11
  import { Menu } from "@base-ui/react/menu";
11
12
  import { clsx } from "clsx";
@@ -181,17 +182,18 @@ function WorkbenchProvider({ features: given, contributions, fit = true, appeara
181
182
  * gets none of it; a menu or dialog that is open keeps its own Escape.
182
183
  */
183
184
  function useEscapeEndsDrawing(drawing, setDrawing) {
184
- useEffect(() => {
185
- if (!drawing) return;
186
- const onKey = (event) => {
187
- if (event.key !== "Escape" || event.target instanceof Element && event.target.closest("[role=menu], [role=dialog], [role=listbox]") !== null) return;
188
- event.preventDefault();
189
- event.stopPropagation();
190
- setDrawing(false);
191
- };
192
- window.addEventListener("keydown", onKey, true);
193
- return () => window.removeEventListener("keydown", onKey, true);
194
- }, [drawing, setDrawing]);
185
+ useHotkey("Escape", (event) => {
186
+ if (event.target instanceof Element && event.target.closest("[role=menu], [role=dialog], [role=listbox]") !== null) return;
187
+ event.preventDefault();
188
+ event.stopPropagation();
189
+ setDrawing(false);
190
+ }, {
191
+ enabled: drawing,
192
+ capture: true,
193
+ preventDefault: false,
194
+ stopPropagation: false,
195
+ conflictBehavior: "allow"
196
+ });
195
197
  }
196
198
  function useWorkbench() {
197
199
  const value = useContext(WorkbenchContext);
@@ -885,12 +887,13 @@ function useAct() {
885
887
  function PageLinkItems() {
886
888
  const session = useSession();
887
889
  const link = useSessionSnapshot().menu?.link;
890
+ const { features } = useWorkbench();
888
891
  const act = useAct();
889
892
  if (link === void 0) return null;
890
- return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsxs(DropdownMenuItem, {
893
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [features.tabs ? /* @__PURE__ */ jsxs(DropdownMenuItem, {
891
894
  onClick: act(() => session.go({ url: link }, { newTab: true })),
892
895
  children: [/* @__PURE__ */ jsx(ExternalLink, {}), "Open link in new tab"]
893
- }), /* @__PURE__ */ jsxs(DropdownMenuItem, {
896
+ }) : null, /* @__PURE__ */ jsxs(DropdownMenuItem, {
894
897
  onClick: act(() => copyTextHere(session, link, "Link copied")),
895
898
  children: [/* @__PURE__ */ jsx(Link2, {}), "Copy link address"]
896
899
  })] });
@@ -903,10 +906,10 @@ function PagePictureItems() {
903
906
  const act = useAct();
904
907
  if (image === void 0) return null;
905
908
  return /* @__PURE__ */ jsxs(Fragment$1, { children: [
906
- /* @__PURE__ */ jsxs(DropdownMenuItem, {
909
+ features.tabs ? /* @__PURE__ */ jsxs(DropdownMenuItem, {
907
910
  onClick: act(() => session.go({ url: image }, { newTab: true })),
908
911
  children: [/* @__PURE__ */ jsx(ExternalLink, {}), "Open picture in new tab"]
909
- }),
912
+ }) : null,
910
913
  features.access.clipboard.read ? /* @__PURE__ */ jsxs(DropdownMenuItem, {
911
914
  onClick: act(() => session.copyPicture(image)),
912
915
  children: [/* @__PURE__ */ jsx(Image, {}), "Copy picture"]
@@ -1210,7 +1213,7 @@ function InputGroup({ className, ...props }) {
1210
1213
  return /* @__PURE__ */ jsx("div", {
1211
1214
  "data-slot": "input-group",
1212
1215
  role: "group",
1213
- className: cn("aperture:group/input-group aperture:relative aperture:flex aperture:h-8 aperture:w-full aperture:min-w-0 aperture:items-center aperture:rounded-lg aperture:border aperture:border-input aperture:transition-colors aperture:outline-none aperture:in-data-[slot=combobox-content]:focus-within:border-inherit aperture:in-data-[slot=combobox-content]:focus-within:ring-0 aperture:has-disabled:bg-input/50 aperture:has-disabled:opacity-50 aperture:has-[[data-slot=input-group-control]:focus-visible]:border-ring aperture:has-[[data-slot=input-group-control]:focus-visible]:ring-3 aperture:has-[[data-slot=input-group-control]:focus-visible]:ring-ring/50 aperture:has-[[data-slot][aria-invalid=true]]:border-destructive aperture:has-[[data-slot][aria-invalid=true]]:ring-3 aperture:has-[[data-slot][aria-invalid=true]]:ring-destructive/20 aperture:has-[>[data-align=block-end]]:h-auto aperture:has-[>[data-align=block-end]]:flex-col aperture:has-[>[data-align=block-start]]:h-auto aperture:has-[>[data-align=block-start]]:flex-col aperture:has-[>textarea]:h-auto aperture:dark:bg-input/30 aperture:dark:has-disabled:bg-input/80 aperture:dark:has-[[data-slot][aria-invalid=true]]:ring-destructive/40 aperture:has-[>[data-align=block-end]]:[&>input]:pt-3 aperture:has-[>[data-align=block-start]]:[&>input]:pb-3 aperture:has-[>[data-align=inline-end]]:[&>input]:pr-1.5 aperture:has-[>[data-align=inline-start]]:[&>input]:pl-1.5", className),
1216
+ className: cn("aperture:group/input-group aperture:relative aperture:flex aperture:h-8 aperture:w-full aperture:min-w-0 aperture:items-center aperture:rounded-lg aperture:border aperture:border-input aperture:transition-colors aperture:outline-none aperture:in-data-[slot=combobox-content]:focus-within:border-inherit aperture:in-data-[slot=combobox-content]:focus-within:ring-0 aperture:has-[[data-slot=input-group-control]:disabled]:bg-input/50 aperture:has-[[data-slot=input-group-control]:disabled]:opacity-50 aperture:has-[[data-slot=input-group-control]:focus-visible]:border-ring aperture:has-[[data-slot=input-group-control]:focus-visible]:ring-3 aperture:has-[[data-slot=input-group-control]:focus-visible]:ring-ring/50 aperture:has-[[data-slot][aria-invalid=true]]:border-destructive aperture:has-[[data-slot][aria-invalid=true]]:ring-3 aperture:has-[[data-slot][aria-invalid=true]]:ring-destructive/20 aperture:has-[>[data-align=block-end]]:h-auto aperture:has-[>[data-align=block-end]]:flex-col aperture:has-[>[data-align=block-start]]:h-auto aperture:has-[>[data-align=block-start]]:flex-col aperture:has-[>textarea]:h-auto aperture:dark:bg-input/30 aperture:dark:has-[[data-slot=input-group-control]:disabled]:bg-input/80 aperture:dark:has-[[data-slot][aria-invalid=true]]:ring-destructive/40 aperture:has-[>[data-align=block-end]]:[&>input]:pt-3 aperture:has-[>[data-align=block-start]]:[&>input]:pb-3 aperture:has-[>[data-align=inline-end]]:[&>input]:pr-1.5 aperture:has-[>[data-align=inline-start]]:[&>input]:pl-1.5", className),
1214
1217
  ...props
1215
1218
  });
1216
1219
  }
@@ -1465,7 +1468,9 @@ const ROLES$1 = {
1465
1468
  /** Who is in the session, as the old workbench showed its collaborators. */
1466
1469
  function SessionPresence() {
1467
1470
  const session = useSession();
1468
- const { presence } = useSessionSnapshot();
1471
+ const snapshot = useSessionSnapshot();
1472
+ const follows = useWorkbench().features.tabs;
1473
+ const { presence } = snapshot;
1469
1474
  if (presence === void 0 || presence.members.length === 0) return null;
1470
1475
  const holds = (member) => presence.held.some((held) => held.holder === member.id);
1471
1476
  const members = presence.members.toSorted((left, right) => {
@@ -1479,7 +1484,7 @@ function SessionPresence() {
1479
1484
  const button = (member) => /* @__PURE__ */ jsx(MemberButton, {
1480
1485
  member,
1481
1486
  presence,
1482
- onFollow: session.follow
1487
+ onFollow: follows ? session.follow : void 0
1483
1488
  }, member.id);
1484
1489
  return /* @__PURE__ */ jsxs("div", {
1485
1490
  "data-session-presence": "",
@@ -1527,10 +1532,10 @@ function MemberButton({ member, presence, onFollow }) {
1527
1532
  held.length === 0 ? void 0 : `holds tab ${held.map((one) => one.tab).join(", ")}`,
1528
1533
  follower ? "follows you" : void 0,
1529
1534
  followed ? "you follow them · click to stop" : void 0,
1530
- !you && !followed ? "click to follow them" : void 0
1535
+ !you && !followed && onFollow !== void 0 ? "click to follow them" : void 0
1531
1536
  ].filter((part) => part !== void 0).join(" · ");
1532
1537
  return /* @__PURE__ */ jsxs(Tooltip$1, { children: [/* @__PURE__ */ jsx(TooltipTrigger, {
1533
- render: you ? /* @__PURE__ */ jsx("span", {
1538
+ render: you || onFollow === void 0 ? /* @__PURE__ */ jsx("span", {
1534
1539
  className: "aperture:inline-flex aperture:size-7 aperture:shrink-0 aperture:items-center aperture:justify-center aperture:rounded-full",
1535
1540
  "aria-label": label
1536
1541
  }) : /* @__PURE__ */ jsx(Button$1, {
@@ -2009,16 +2014,15 @@ function TabPreviewPicture({ target }) {
2009
2014
  //#region src/components/browser-toolbar.tsx
2010
2015
  /** The workbench's first row: the tab strip, and who is in the session. */
2011
2016
  function WorkbenchTitlebar() {
2012
- const { features, contributions } = useWorkbench();
2017
+ const { features } = useWorkbench();
2013
2018
  const open = useSessionSnapshot().connection.state === "open";
2014
- const contributed = contributions.some((one) => one.slot.startsWith("titlebar."));
2015
- if (useShortWindow() || !features.tabs && !features.presence && !contributed) return null;
2019
+ if (useOneRow()) return null;
2016
2020
  return /* @__PURE__ */ jsxs("div", {
2017
2021
  "data-workbench-titlebar": "",
2018
2022
  className: "aperture:flex aperture:h-8 aperture:min-w-0 aperture:shrink-0 aperture:items-stretch aperture:border-b aperture:bg-muted/35 aperture:pointer-coarse:h-10",
2019
2023
  children: [
2020
2024
  /* @__PURE__ */ jsx(WorkbenchSlot, { name: "titlebar.start" }),
2021
- features.tabs ? /* @__PURE__ */ jsx(BrowserTabStrip, {}) : /* @__PURE__ */ jsx("div", { className: "aperture:min-w-0 aperture:flex-1" }),
2025
+ /* @__PURE__ */ jsx(BrowserTabStrip, {}),
2022
2026
  features.presence && open ? /* @__PURE__ */ jsx(SessionPresence, {}) : null,
2023
2027
  /* @__PURE__ */ jsx(WorkbenchSlot, { name: "titlebar.end" })
2024
2028
  ]
@@ -2039,6 +2043,15 @@ function useCrowdedToolbar() {
2039
2043
  /** Below this width the toolbar keeps the address bar its room by showing fewer buttons. */
2040
2044
  const COMPACT_BELOW = 520;
2041
2045
  /**
2046
+ * Whether the workbench has the one row of the toolbar, which then has what the title
2047
+ * bar has besides the tabs: in a window too short for two, and where there is no strip of
2048
+ * tabs, as for one who sees one tab alone.
2049
+ */
2050
+ function useOneRow() {
2051
+ const { features } = useWorkbench();
2052
+ return useShortWindow() || !features.tabs;
2053
+ }
2054
+ /**
2042
2055
  * The workbench's second row: navigation, the address, and the buttons at its end. Without
2043
2056
  * children it is the usual arrangement of its parts.
2044
2057
  */
@@ -2049,7 +2062,8 @@ function WorkbenchToolbar({ children }) {
2049
2062
  const open = useSessionSnapshot().connection.state === "open";
2050
2063
  const connecting = useSessionSnapshot().connection.state === "connecting";
2051
2064
  const short = useShortWindow();
2052
- if (!(children !== void 0 || features.navigation || features.addressBar || features.menus || short || contributions.some((one) => one.slot === "toolbar.start" || one.slot === "toolbar.end"))) return null;
2065
+ const oneRow = useOneRow();
2066
+ if (!(children !== void 0 || features.navigation || features.addressBar || features.menus || oneRow && features.presence || contributions.some((one) => oneRow ? one.slot.startsWith("toolbar.") || one.slot.startsWith("titlebar.") : one.slot.startsWith("toolbar.")))) return null;
2053
2067
  return /* @__PURE__ */ jsx(CompactContext.Provider, {
2054
2068
  value: compact,
2055
2069
  children: /* @__PURE__ */ jsx("div", {
@@ -2057,17 +2071,18 @@ function WorkbenchToolbar({ children }) {
2057
2071
  "data-workbench-toolbar": "",
2058
2072
  "data-compact": compact ? "" : void 0,
2059
2073
  "data-short": short ? "" : void 0,
2060
- className: "aperture:flex aperture:h-9 aperture:min-w-0 aperture:shrink-0 aperture:items-center aperture:gap-1 aperture:border-b aperture:bg-background aperture:px-1.5 aperture:pointer-coarse:h-11 aperture:data-short:pl-0",
2074
+ "data-flush": oneRow && contributions.some((one) => one.slot === "titlebar.start") ? "" : void 0,
2075
+ className: "aperture:flex aperture:h-9 aperture:min-w-0 aperture:shrink-0 aperture:items-center aperture:gap-1 aperture:border-b aperture:bg-background aperture:px-1.5 aperture:pointer-coarse:h-11 aperture:data-flush:pl-0",
2061
2076
  children: children ?? /* @__PURE__ */ jsxs(Fragment$1, { children: [
2062
- short ? /* @__PURE__ */ jsx(WorkbenchSlot, { name: "titlebar.start" }) : null,
2077
+ oneRow ? /* @__PURE__ */ jsx(WorkbenchSlot, { name: "titlebar.start" }) : null,
2063
2078
  short && features.tabs ? /* @__PURE__ */ jsx(TabsMenu, {}) : null,
2064
2079
  /* @__PURE__ */ jsx(WorkbenchSlot, { name: "toolbar.start" }),
2065
2080
  features.navigation ? /* @__PURE__ */ jsx(NavigationButtons, {}) : null,
2066
2081
  features.addressBar ? /* @__PURE__ */ jsx(AddressBar, {}) : /* @__PURE__ */ jsx("div", { className: "aperture:min-w-0 aperture:flex-1" }),
2067
2082
  connecting ? /* @__PURE__ */ jsx(Loader2, { className: "aperture:size-4 aperture:shrink-0 aperture:animate-spin aperture:text-muted-foreground" }) : null,
2068
2083
  /* @__PURE__ */ jsx(WorkbenchSlot, { name: "toolbar.end" }),
2069
- short && features.presence && open ? /* @__PURE__ */ jsx(SessionPresence, {}) : null,
2070
- short ? /* @__PURE__ */ jsx(WorkbenchSlot, { name: "titlebar.end" }) : null,
2084
+ oneRow && features.presence && open ? /* @__PURE__ */ jsx(SessionPresence, {}) : null,
2085
+ oneRow ? /* @__PURE__ */ jsx(WorkbenchSlot, { name: "titlebar.end" }) : null,
2071
2086
  features.menus ? /* @__PURE__ */ jsx(ToolbarActions, {}) : null
2072
2087
  ] })
2073
2088
  })
@@ -3729,7 +3744,17 @@ function SessionFiles({ uploads, request, downloads, offers, puts, sheet }) {
3729
3744
  }),
3730
3745
  /* @__PURE__ */ jsx("span", {
3731
3746
  className: "aperture:shrink-0 aperture:text-xs aperture:text-muted-foreground aperture:tabular-nums",
3732
- children: percent(file.loaded, file.total)
3747
+ children: file.rate === void 0 || file.rate <= 0 ? percent(file.loaded, file.total) : `${speed(file.rate)} · ${left((file.total - file.loaded) / file.rate)} · ${percent(file.loaded, file.total)}`
3748
+ }),
3749
+ /* @__PURE__ */ jsx(Button$1, {
3750
+ type: "button",
3751
+ size: "icon-xs",
3752
+ variant: "ghost",
3753
+ "aria-label": `Cancel the upload of ${file.name}`,
3754
+ "data-session-upload-cancel": "",
3755
+ className: "aperture:shrink-0",
3756
+ onClick: () => uploads.cancel(file.name),
3757
+ children: /* @__PURE__ */ jsx(X, {})
3733
3758
  })
3734
3759
  ]
3735
3760
  }), /* @__PURE__ */ jsx("div", {
@@ -3846,6 +3871,16 @@ function giveLabel(request, chosen) {
3846
3871
  if (chosen > 1) return `Give ${chosen} files`;
3847
3872
  return request.multiple && chosen === 0 ? "Give the files" : "Give the file";
3848
3873
  }
3874
+ function speed(bytesPerSecond) {
3875
+ const [unit, size] = bytesPerSecond >= 1024 ** 3 ? ["GB", 1024 ** 3] : bytesPerSecond >= 1024 ** 2 ? ["MB", 1024 ** 2] : ["KB", 1024];
3876
+ return `${Number((bytesPerSecond / size).toFixed(1))} ${unit}/s`;
3877
+ }
3878
+ function left(seconds) {
3879
+ const whole = Math.max(1, Math.ceil(seconds));
3880
+ if (whole < 60) return `${whole} s left`;
3881
+ const minutes = Math.ceil(whole / 60);
3882
+ return minutes < 60 ? `${minutes} min left` : `${Math.floor(minutes / 60)} h ${minutes % 60} min left`;
3883
+ }
3849
3884
  function percent(loaded, total) {
3850
3885
  return total <= 0 ? "0%" : `${Math.min(100, Math.round(100 * loaded / total))}%`;
3851
3886
  }
@@ -4606,7 +4641,8 @@ function trace(context, points) {
4606
4641
  * what is contributed to `viewport`.
4607
4642
  */
4608
4643
  function BrowserViewport({ className }) {
4609
- const showLease = useWorkbench().features.presence;
4644
+ const { features } = useWorkbench();
4645
+ const showLease = features.presence;
4610
4646
  const session = useSession();
4611
4647
  const snapshot = useSessionSnapshot();
4612
4648
  const reconnect = useReconnect();
@@ -4647,9 +4683,9 @@ function BrowserViewport({ className }) {
4647
4683
  variant: "icon",
4648
4684
  children: /* @__PURE__ */ jsx(AppWindow, {})
4649
4685
  }),
4650
- /* @__PURE__ */ jsx(EmptyTitle, { children: "No tabs" }),
4651
- /* @__PURE__ */ jsx(EmptyDescription, { children: "The session's browser has no tab open." })
4652
- ] }), snapshot.features.tabs.write ? /* @__PURE__ */ jsx(EmptyContent, { children: /* @__PURE__ */ jsxs(Button$1, {
4686
+ /* @__PURE__ */ jsx(EmptyTitle, { children: features.tabs ? "No tabs" : "This tab was closed" }),
4687
+ /* @__PURE__ */ jsx(EmptyDescription, { children: features.tabs ? "The session's browser has no tab open." : "There is nothing more to see here." })
4688
+ ] }), features.tabs && snapshot.features.tabs.write ? /* @__PURE__ */ jsx(EmptyContent, { children: /* @__PURE__ */ jsxs(Button$1, {
4653
4689
  type: "button",
4654
4690
  size: "sm",
4655
4691
  onClick: () => session.newTab(),
@@ -6142,6 +6178,7 @@ function PeopleButton({ rail, onOpen }) {
6142
6178
  function PeopleSheet({ side, open, onOpenChange }) {
6143
6179
  const session = useSession();
6144
6180
  const { presence } = useSessionSnapshot();
6181
+ const follows = useWorkbench().features.tabs;
6145
6182
  const last = useRef(presence);
6146
6183
  if (presence !== void 0) last.current = presence;
6147
6184
  const told = presence ?? last.current;
@@ -6189,7 +6226,7 @@ function PeopleSheet({ side, open, onOpenChange }) {
6189
6226
  children: said.join(" · ")
6190
6227
  })]
6191
6228
  }),
6192
- member.id === told?.you ? null : /* @__PURE__ */ jsx(Button$1, {
6229
+ member.id === told?.you || !follows ? null : /* @__PURE__ */ jsx(Button$1, {
6193
6230
  type: "button",
6194
6231
  variant: followed ? "secondary" : "outline",
6195
6232
  "aria-pressed": followed,
@@ -6305,12 +6342,13 @@ function useDarkTheme(theme) {
6305
6342
  * The workbench of one session: connects to it, shows it, and ends the connection when
6306
6343
  * unmounted. Needs `@aperture-browser/session-react/styles.css`.
6307
6344
  */
6308
- function ApertureSession({ url, token, name, pixels, features, theme = "system", onThemeChange, className, onSession, ...workbench }) {
6345
+ function ApertureSession({ url, token, name, pixels, tab, features, theme = "system", onThemeChange, className, onSession, ...workbench }) {
6309
6346
  const { session, reconnect } = useConnectedSession({
6310
6347
  url,
6311
6348
  token,
6312
6349
  name,
6313
- pixels
6350
+ pixels,
6351
+ tab
6314
6352
  });
6315
6353
  useEffect(() => {
6316
6354
  if (session === null || onSession === void 0) return;