@cboxdk/cbox-ui 0.10.1 → 0.10.2

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
@@ -2011,6 +2011,7 @@ function CommandPalette({ onClose }) {
2011
2011
  const [input, setInput] = useState("");
2012
2012
  const [query2, setQuery] = useState("");
2013
2013
  const [groups, setGroups] = useState({});
2014
+ const [groupsFor, setGroupsFor] = useState("");
2014
2015
  const [expanded, setExpanded] = useState(null);
2015
2016
  const [activeKey, setActiveKey] = useState(null);
2016
2017
  const field = useRef(null);
@@ -2037,14 +2038,15 @@ function CommandPalette({ onClose }) {
2037
2038
  const q2 = query2.trim();
2038
2039
  const controller = new AbortController();
2039
2040
  const run = (key, load) => {
2040
- setGroups((current) => ({ ...current, [key]: { status: "loading", items: current[key]?.items ?? [] } }));
2041
+ setGroups((current2) => ({ ...current2, [key]: { status: "loading", items: current2[key]?.items ?? [] } }));
2041
2042
  load().then((items) => {
2042
- if (!controller.signal.aborted) setGroups((current) => ({ ...current, [key]: { status: "ready", items } }));
2043
+ if (!controller.signal.aborted) setGroups((current2) => ({ ...current2, [key]: { status: "ready", items } }));
2043
2044
  }).catch(() => {
2044
- if (!controller.signal.aborted) setGroups((current) => ({ ...current, [key]: { status: "error", items: [] } }));
2045
+ if (!controller.signal.aborted) setGroups((current2) => ({ ...current2, [key]: { status: "error", items: [] } }));
2045
2046
  });
2046
2047
  };
2047
2048
  const signal = controller.signal;
2049
+ setGroupsFor(q2);
2048
2050
  if (q2 === "") {
2049
2051
  setGroups({});
2050
2052
  return () => controller.abort();
@@ -2181,8 +2183,14 @@ function CommandPalette({ onClose }) {
2181
2183
  return { ...section, start, count: items.length + (more ? 1 : 0) };
2182
2184
  });
2183
2185
  const keyOf = (option) => option.kind === "item" ? `${option.group}:${option.item.id}` : `all:${option.group}`;
2184
- const found = activeKey === null ? -1 : options.findIndex((option) => keyOf(option) === activeKey);
2185
- const active = found >= 0 ? found : 0;
2186
+ const debounced = q === query2.trim() && groupsFor === q;
2187
+ const current = (group) => group === "pages" || group === "actions" || debounced && sections.find((section) => section.key === group)?.state.status === "ready";
2188
+ const busy = !debounced || sections.some((section) => section.state.status === "loading");
2189
+ const firstResult = options.find((option) => option.group !== "ask" && current(option.group));
2190
+ const autoKey = firstResult !== void 0 ? keyOf(firstResult) : !busy && options[0] !== void 0 ? keyOf(options[0]) : null;
2191
+ const indexOf = (key) => key === null ? -1 : options.findIndex((option) => keyOf(option) === key);
2192
+ const found = indexOf(activeKey);
2193
+ const active = found >= 0 ? found : indexOf(autoKey);
2186
2194
  const setActive = (next) => {
2187
2195
  const index = typeof next === "function" ? next(active) : next;
2188
2196
  const option = options[index];
@@ -2191,10 +2199,9 @@ function CommandPalette({ onClose }) {
2191
2199
  useEffect(() => {
2192
2200
  setActiveKey(null);
2193
2201
  }, [q]);
2194
- const firstKey = options[0] === void 0 ? null : keyOf(options[0]);
2195
2202
  useEffect(() => {
2196
- if (activeKey === null && firstKey !== null) setActiveKey(firstKey);
2197
- }, [activeKey, firstKey]);
2203
+ if (activeKey === null && autoKey !== null) setActiveKey(autoKey);
2204
+ }, [activeKey, autoKey]);
2198
2205
  useEffect(() => {
2199
2206
  revealInList(`${id}-${active}`);
2200
2207
  }, [active, id]);