@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
161 lines (160 loc) • 6.67 kB
JavaScript
import { __, sprintf } from "@wordpress/i18n";
import { dateI18n, getDate, getSettings, humanTimeDiff } from "@wordpress/date";
import { store } from "@wordpress/core-data";
import { useSelect } from "@wordpress/data";
import { useMemo } from "@wordpress/element";
import { getGlobalStylesChanges } from "@wordpress/global-styles-engine";
import { DataViewsPicker } from "@wordpress/dataviews";
import { Badge } from "@wordpress/ui";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/global-styles-ui/src/screen-revisions/revisions-list.tsx
const DAY_IN_MILLISECONDS = 864e5;
const DEFAULT_LAYOUTS = { pickerActivity: true };
const getItemId = (item) => String(item.id);
function normalizeModified(modified) {
return modified instanceof Date ? modified.toISOString() : modified;
}
function getDisplayDate(modified) {
const modifiedString = normalizeModified(modified);
const modifiedDate = getDate(modifiedString ?? null);
const dateNowInMs = getDate(null).getTime();
return modifiedString && dateNowInMs - modifiedDate.getTime() > DAY_IN_MILLISECONDS ? dateI18n(getSettings().formats.datetimeAbbreviated, modifiedDate) : humanTimeDiff(modifiedString ?? modifiedDate, void 0);
}
function ChangesSummary({ revision, previousRevision }) {
const changes = getGlobalStylesChanges(revision, previousRevision, { maxResults: 7 });
if (!changes.length) return null;
return /* @__PURE__ */ jsx("ul", {
"data-testid": "global-styles-revision-changes",
className: "global-styles-ui-screen-revisions__changes",
children: changes.map((change) => /* @__PURE__ */ jsx("li", { children: change }, change))
});
}
/**
* Returns a label for the revision, also used as the option's accessible name.
* @param id
* @param authorDisplayName
* @param formattedModifiedDate
* @param areStylesEqual
*/
function getRevisionLabel(id, authorDisplayName, formattedModifiedDate, areStylesEqual) {
if ("parent" === id) return __("Reset the styles to the theme defaults");
if ("unsaved" === id) return sprintf(__("Unsaved changes by %s"), authorDisplayName);
return areStylesEqual ? sprintf(__("Changes saved by %1$s on %2$s. This revision matches current editor styles."), authorDisplayName, formattedModifiedDate) : sprintf(__("Changes saved by %1$s on %2$s"), authorDisplayName, formattedModifiedDate);
}
/**
* Renders the revisions timeline using the DataViews picker activity layout.
* @param root0
* @param root0.revisions
* @param root0.view
* @param root0.onChangeView
* @param root0.selection
* @param root0.onChangeSelection
* @param root0.isLoading
* @param root0.paginationInfo
* @param root0.canApplyRevision
* @param root0.actions
*/
function RevisionsList({ revisions, view, onChangeView, selection, onChangeSelection, isLoading, paginationInfo, canApplyRevision, actions }) {
const { currentThemeName, currentUser } = useSelect((select) => {
const { getCurrentTheme, getCurrentUser } = select(store);
const currentTheme = getCurrentTheme();
return {
currentThemeName: currentTheme?.name?.rendered || currentTheme?.stylesheet,
currentUser: getCurrentUser()
};
}, []);
const fields = useMemo(() => {
const getAuthor = (revision) => "unsaved" === revision.id ? currentUser : revision.author;
const getAuthorDisplayName = (revision) => getAuthor(revision)?.name || __("User");
const isSelected = (revision) => selection.includes(String(revision.id));
return [{
id: "date",
label: __("Date"),
getValue: ({ item }) => {
const formattedModifiedDate = "parent" === item.id || "unsaved" === item.id ? "" : dateI18n(getSettings().formats.datetimeAbbreviated, getDate(normalizeModified(item.modified) ?? null));
return getRevisionLabel(item.id, getAuthorDisplayName(item), formattedModifiedDate, !canApplyRevision && isSelected(item));
},
render: ({ item }) => {
if ("parent" === item.id) return /* @__PURE__ */ jsx("span", {
className: "global-styles-ui-screen-revisions__date",
children: __("Default styles")
});
if ("unsaved" === item.id) return /* @__PURE__ */ jsx("span", {
className: "global-styles-ui-screen-revisions__date",
children: __("(Unsaved)")
});
return /* @__PURE__ */ jsx("time", {
className: "global-styles-ui-screen-revisions__date",
dateTime: normalizeModified(item.modified),
children: getDisplayDate(item.modified)
});
},
enableSorting: false,
enableHiding: false
}, {
id: "details",
label: __("Details"),
render: ({ item }) => {
const authorDisplayName = getAuthorDisplayName(item);
const isReset = "parent" === item.id;
const meta = isReset ? /* @__PURE__ */ jsx("span", {
className: "global-styles-ui-screen-revisions__meta",
children: currentThemeName
}) : /* @__PURE__ */ jsxs("span", {
className: "global-styles-ui-screen-revisions__meta",
children: [/* @__PURE__ */ jsx("img", {
alt: authorDisplayName,
src: getAuthor(item)?.avatar_urls?.["48"]
}), authorDisplayName]
});
if (!isSelected(item)) return /* @__PURE__ */ jsx("span", {
className: "global-styles-ui-screen-revisions__description",
children: meta
});
const index = revisions.findIndex((revision) => String(revision.id) === String(item.id));
return /* @__PURE__ */ jsxs("span", {
className: "global-styles-ui-screen-revisions__description",
children: [
meta,
!isReset && /* @__PURE__ */ jsx(ChangesSummary, {
revision: revisions[index] ?? item,
previousRevision: index >= 0 ? revisions[index + 1] : void 0
}),
!canApplyRevision && /* @__PURE__ */ jsx(Badge, {
intent: "informational",
children: __("Active")
})
]
});
},
enableSorting: false,
enableHiding: false
}];
}, [
selection,
canApplyRevision,
currentUser,
currentThemeName,
revisions
]);
return /* @__PURE__ */ jsx("div", {
className: "global-styles-ui-screen-revisions",
children: /* @__PURE__ */ jsxs(DataViewsPicker, {
view,
onChangeView,
fields,
data: revisions,
isLoading,
paginationInfo,
defaultLayouts: DEFAULT_LAYOUTS,
getItemId,
selection,
onChangeSelection,
itemListLabel: __("Global styles revisions list"),
actions,
children: [/* @__PURE__ */ jsx(DataViewsPicker.Layout, {}), /* @__PURE__ */ jsxs(DataViewsPicker.Footer, { children: [/* @__PURE__ */ jsx(DataViewsPicker.Pagination, { children: /* @__PURE__ */ jsx(DataViewsPicker.PageSelect, {}) }), /* @__PURE__ */ jsx(DataViewsPicker.Actions, { className: "global-styles-ui-screen-revisions__actions" })] })]
})
});
}
export { RevisionsList as default };
//# sourceMappingURL=revisions-list.mjs.map