@keepkit/core 0.28.19 → 0.28.21

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/react.js CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  queryKeepItems,
10
10
  reorderKeepItems,
11
11
  revalidateKeepItems
12
- } from "./chunk-OLGYOC3D.js";
12
+ } from "./chunk-OHKVHU5I.js";
13
13
  import {
14
14
  createBrowserStorageAdapter,
15
15
  exportItems,
@@ -18,16 +18,18 @@ import {
18
18
  normalizeKeepTags,
19
19
  persistKeepItems,
20
20
  removeKeepItems
21
- } from "./chunk-HTTOCLOC.js";
21
+ } from "./chunk-IBODMDW3.js";
22
22
  import {
23
- parseKeepMeta
24
- } from "./chunk-5W4QSJHV.js";
23
+ parseKeepMeta,
24
+ stripUndefinedMeta
25
+ } from "./chunk-R5BTN27K.js";
25
26
 
26
27
  // src/react/components/KeepButton.tsx
27
28
  import {
28
29
  Children,
29
30
  cloneElement,
30
- isValidElement
31
+ isValidElement,
32
+ useState
31
33
  } from "react";
32
34
 
33
35
  // src/react/hooks/useKeepItem.ts
@@ -379,7 +381,8 @@ function KeepProviderContent({
379
381
  (action, id, update, onSuccess) => runMutation(action, id, (previous) => {
380
382
  const current = previous.find((item) => item.id === id);
381
383
  if (!current) return void 0;
382
- const next = update(current);
384
+ const updated = update(current);
385
+ const next = { ...updated, meta: stripUndefinedMeta(updated.meta) };
383
386
  return {
384
387
  next: previous.map((item) => item.id === id ? next : item),
385
388
  persist: () => storage.set(next),
@@ -393,7 +396,7 @@ function KeepProviderContent({
393
396
  async (item) => {
394
397
  let normalizedItem;
395
398
  try {
396
- const meta = migrationRef.current.schema ? await parseKeepMeta(migrationRef.current.schema, item.meta) : item.meta;
399
+ const meta = migrationRef.current.schema ? stripUndefinedMeta(await parseKeepMeta(migrationRef.current.schema, item.meta)) : stripUndefinedMeta(item.meta);
397
400
  normalizedItem = {
398
401
  ...item,
399
402
  meta,
@@ -887,7 +890,13 @@ function KeepProviderContent({
887
890
  let persistenceStarted = false;
888
891
  let summary;
889
892
  try {
890
- const activeRevalidator = revalidator ?? validateItem;
893
+ const batch = !revalidator ? autoRevalidationRef.current?.revalidateMany : void 0;
894
+ const batchResults = batch && previous.length ? await batch(previous) : void 0;
895
+ const activeRevalidator = batchResults ? (item) => {
896
+ const result = Object.keys(batchResults).includes(item.id) ? batchResults[item.id] : void 0;
897
+ if (!result) throw new Error(`Missing revalidation result for ${item.id}.`);
898
+ return result;
899
+ } : revalidator ?? autoRevalidationRef.current?.revalidator ?? validateItem ?? (batch ? () => "available" : void 0);
891
900
  if (!activeRevalidator)
892
901
  throw new Error("KeepProvider.revalidateItems requires a revalidator or validateItem.");
893
902
  summary = await revalidateKeepItems(previous, activeRevalidator, {
@@ -946,8 +955,8 @@ function KeepProviderContent({
946
955
  useEffect(() => {
947
956
  const settings = autoRevalidationRef.current;
948
957
  const activeRevalidator = settings?.revalidator ?? validateItem;
949
- if (!settings || !activeRevalidator) return;
950
- const run = () => void revalidateItems(activeRevalidator, { removeStatuses: settings.removeStatuses }).catch(() => void 0);
958
+ if (!settings || !activeRevalidator && !settings.revalidateMany) return;
959
+ const run = () => void revalidateItems(void 0, { removeStatuses: settings.removeStatuses }).catch(() => void 0);
951
960
  if (isHydrated && settings.onMount !== false && !didMountRevalidateRef.current) {
952
961
  didMountRevalidateRef.current = true;
953
962
  run();
@@ -1318,7 +1327,9 @@ function KeepButton({
1318
1327
  ref: providedRef,
1319
1328
  ...buttonProps
1320
1329
  }) {
1321
- const state = useKeepItem(item);
1330
+ const itemState = useKeepItem(item);
1331
+ const [failure, setFailure] = useState(null);
1332
+ const state = { ...itemState, error: failure?.id === item.id ? failure.error : null };
1322
1333
  const { isSaved, toggle } = state;
1323
1334
  const isDisabled = Boolean(disabled || state.isLoading || state.isMutating);
1324
1335
  async function handleClick(event) {
@@ -1331,9 +1342,11 @@ function KeepButton({
1331
1342
  );
1332
1343
  }
1333
1344
  if (event.defaultPrevented) return;
1345
+ setFailure(null);
1334
1346
  try {
1335
1347
  await toggle();
1336
1348
  } catch (error) {
1349
+ setFailure({ id: item.id, error });
1337
1350
  onToggleError?.(error);
1338
1351
  }
1339
1352
  }
@@ -1369,10 +1382,10 @@ function KeepButton({
1369
1382
  const commonProps = {
1370
1383
  ...buttonProps,
1371
1384
  "aria-pressed": isSaved,
1372
- "data-state": state.error ? "error" : isSaved ? "saved" : "unsaved",
1385
+ "data-state": failure?.id === item.id ? "error" : isSaved ? "saved" : "unsaved",
1373
1386
  "data-loading": state.isLoading || state.isMutating ? "true" : void 0,
1374
1387
  "data-disabled": isDisabled ? "true" : void 0,
1375
- "aria-label": ("aria-label" in buttonProps ? buttonProps["aria-label"] : void 0) ?? getAriaLabel?.(state) ?? (isSaved ? savedAriaLabel : unsavedAriaLabel) ?? getAccessibleLabel(isSaved, item, asChild),
1388
+ "aria-label": ("aria-label" in buttonProps ? buttonProps["aria-label"] : void 0) ?? getAriaLabel?.(state) ?? (isSaved ? savedAriaLabel : unsavedAriaLabel) ?? getAccessibleLabel(item, asChild),
1376
1389
  ...asChild ? {
1377
1390
  "aria-disabled": isDisabled,
1378
1391
  role: buttonProps.role ?? "button",
@@ -1403,11 +1416,11 @@ function composeRefs(...refs) {
1403
1416
  }
1404
1417
  };
1405
1418
  }
1406
- function getAccessibleLabel(isSaved, item, asChild) {
1407
- if (!asChild) return isSaved ? "Remove saved item" : "Save item";
1419
+ function getAccessibleLabel(item, asChild) {
1420
+ if (!asChild) return "Save item";
1408
1421
  const title = getMetaTitle(item.meta);
1409
1422
  const subject = title ? `${item.targetType ?? "item"}: ${title}` : item.targetType ?? "item";
1410
- return `${isSaved ? "Remove" : "Save"} ${subject}`;
1423
+ return `Save ${subject}`;
1411
1424
  }
1412
1425
  function getMetaTitle(meta) {
1413
1426
  if (typeof meta !== "object" || meta === null || !("title" in meta)) return void 0;
@@ -1638,11 +1651,11 @@ function sameCounts(left, right) {
1638
1651
  }
1639
1652
 
1640
1653
  // src/react/hooks/useKeepNavigator.ts
1641
- import { useCallback as useCallback5, useMemo as useMemo4, useState } from "react";
1654
+ import { useCallback as useCallback5, useMemo as useMemo4, useState as useState2 } from "react";
1642
1655
  function useKeepNavigator(options = {}) {
1643
1656
  const { store } = useKeepStore();
1644
1657
  const { currentId, initialIndex = 0, query } = options;
1645
- const [activeIndex, setActiveIndex] = useState(() => Math.max(0, Math.floor(initialIndex)));
1658
+ const [activeIndex, setActiveIndex] = useState2(() => Math.max(0, Math.floor(initialIndex)));
1646
1659
  const queryOptions = useMemo4(() => ({ ...query, pagination: void 0 }), [query]);
1647
1660
  const selector = useMemo4(() => {
1648
1661
  let previousSource;