payload-admin-bar
Version:
An admin bar for React apps using Payload CMS
182 lines • 13.1 kB
JavaScript
'use client';
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PayloadAdminBar = void 0;
var react_1 = __importStar(require("react"));
var dummyUser = {
email: 'dev@email.com',
id: '12345'
};
var PayloadAdminBar = function (props) {
var _a = props.cmsURL, cmsURL = _a === void 0 ? 'http://localhost:8000' : _a, _b = props.apiPath, apiPath = _b === void 0 ? '/api' : _b, _c = props.adminPath, adminPath = _c === void 0 ? '/admin' : _c, _d = props.authCollection, authCollection = _d === void 0 ? 'users' : _d, collection = props.collection, collectionLabels = props.collectionLabels, id = props.id, logo = props.logo, className = props.className, logoProps = props.logoProps, editProps = props.editProps, createProps = props.createProps, userProps = props.userProps, logoutProps = props.logoutProps, divProps = props.divProps, style = props.style, unstyled = props.unstyled, onAuthChange = props.onAuthChange, classNames = props.classNames, devMode = props.devMode, preview = props.preview, onPreviewExit = props.onPreviewExit, previewProps = props.previewProps;
var _e = (0, react_1.useState)(), user = _e[0], setUser = _e[1];
(0, react_1.useEffect)(function () {
var fetchMe = function () { return __awaiter(void 0, void 0, void 0, function () {
var meRequest, meResponse, user_1, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
return [4 /*yield*/, fetch("" + cmsURL + apiPath + "/" + authCollection + "/me", {
method: 'get',
credentials: 'include',
})];
case 1:
meRequest = _a.sent();
return [4 /*yield*/, meRequest.json()];
case 2:
meResponse = _a.sent();
user_1 = meResponse.user;
if (user_1) {
setUser(user_1);
}
else {
if (devMode !== true) {
setUser(null);
}
else {
setUser(dummyUser);
}
}
return [3 /*break*/, 4];
case 3:
err_1 = _a.sent();
console.warn(err_1);
if (devMode === true) {
setUser(dummyUser);
}
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
fetchMe();
}, [
cmsURL,
adminPath,
apiPath
]);
(0, react_1.useEffect)(function () {
if (typeof onAuthChange === 'function') {
onAuthChange(user);
}
}, [
user,
onAuthChange
]);
if (user) {
var email = user.email, userID = user.id;
return (react_1.default.createElement("div", { className: className, style: __assign(__assign({}, unstyled !== true ? {
fontSize: 'small',
position: 'fixed',
display: 'flex',
minWidth: '0',
alignItems: 'center',
top: 0,
left: 0,
width: '100%',
padding: '0.5rem',
zIndex: 99999,
boxSizing: 'border-box',
backgroundColor: '#222',
color: '#fff',
fontFamily: '-apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, sans-serif'
} : {}), style) },
react_1.default.createElement("a", __assign({ href: "" + cmsURL + adminPath, className: classNames === null || classNames === void 0 ? void 0 : classNames.logo }, logoProps, { style: __assign({}, unstyled !== true ? __assign({ marginRight: '10px', flexShrink: 0, display: 'flex', height: '20px', textDecoration: 'none', color: 'inherit', alignItems: 'center' }, (logoProps === null || logoProps === void 0 ? void 0 : logoProps.style) ? __assign({}, logoProps.style) : {}) : {}) }), logo || 'Payload CMS'),
react_1.default.createElement("a", __assign({ href: "" + cmsURL + adminPath + "/collections/" + authCollection + "/" + userID, target: "_blank", rel: "noopener noreferrer", className: classNames === null || classNames === void 0 ? void 0 : classNames.user }, userProps, { style: __assign({}, unstyled !== true ? __assign({ marginRight: '10px', display: 'block', minWidth: '50px', overflow: 'hidden', textOverflow: 'ellipsis', textDecoration: 'none', whiteSpace: 'nowrap', color: 'inherit' }, (userProps === null || userProps === void 0 ? void 0 : userProps.style) ? __assign({}, userProps.style) : {}) : {}) }),
react_1.default.createElement("span", { style: __assign({}, unstyled !== true ? {
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
} : {}) }, email || 'Profile')),
react_1.default.createElement("div", __assign({ className: classNames === null || classNames === void 0 ? void 0 : classNames.controls }, divProps, { style: __assign({}, unstyled !== true ? __assign({ display: 'flex', marginRight: '10px', flexShrink: 1, flexGrow: 1, alignItems: 'center', justifyContent: 'flex-end' }, (divProps === null || divProps === void 0 ? void 0 : divProps.style) ? __assign({}, divProps.style) : {}) : {}) }),
collection && id && (react_1.default.createElement("a", __assign({ href: "" + cmsURL + adminPath + "/collections/" + collection + "/" + id, target: "_blank", rel: "noopener noreferrer", className: classNames === null || classNames === void 0 ? void 0 : classNames.edit }, editProps, { style: __assign({ display: 'block' }, unstyled !== true ? __assign({ marginRight: '10px', textDecoration: 'none', color: 'inherit', textOverflow: 'ellipsis', overflow: 'hidden', whiteSpace: 'nowrap', flexShrink: 1 }, (editProps === null || editProps === void 0 ? void 0 : editProps.style) ? __assign({}, editProps.style) : {}) : {}) }),
react_1.default.createElement("span", { style: __assign({}, unstyled !== true ? {
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
} : {}) }, "Edit " + ((collectionLabels === null || collectionLabels === void 0 ? void 0 : collectionLabels.singular) || 'page')))),
collection && (react_1.default.createElement("a", __assign({ href: "" + cmsURL + adminPath + "/collections/" + collection + "/create", target: "_blank", rel: "noopener noreferrer", className: classNames === null || classNames === void 0 ? void 0 : classNames.create }, createProps, { style: __assign({}, unstyled !== true ? __assign({ flexShrink: 1, display: 'block', textDecoration: 'none', color: 'inherit', textOverflow: 'ellipsis', overflow: 'hidden', whiteSpace: 'nowrap' }, (createProps === null || createProps === void 0 ? void 0 : createProps.style) ? __assign({}, createProps.style) : {}) : {}) }),
react_1.default.createElement("span", { style: __assign({}, unstyled !== true ? {
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
} : {}) }, "New " + ((collectionLabels === null || collectionLabels === void 0 ? void 0 : collectionLabels.singular) || 'page')))),
preview && (react_1.default.createElement("button", __assign({ className: classNames === null || classNames === void 0 ? void 0 : classNames.preview, onClick: onPreviewExit }, previewProps, { style: __assign({}, unstyled !== true ? __assign({ marginLeft: '10px', background: 'none', border: 'none', padding: 0, cursor: 'pointer', color: 'inherit', fontFamily: 'inherit', fontSize: 'inherit' }, (previewProps === null || previewProps === void 0 ? void 0 : previewProps.style) ? __assign({}, previewProps.style) : {}) : {}) }), "Exit preview mode"))),
react_1.default.createElement("a", __assign({ href: "" + cmsURL + adminPath + "/logout", target: "_blank", rel: "noopener noreferrer", className: classNames === null || classNames === void 0 ? void 0 : classNames.logout }, logoutProps, { style: __assign({}, unstyled !== true ? __assign({ textDecoration: 'none', color: 'inherit', textOverflow: 'ellipsis', whiteSpace: 'nowrap', overflow: 'hidden', display: 'block', flexShrink: 1 }, (logoutProps === null || logoutProps === void 0 ? void 0 : logoutProps.style) ? __assign({}, logoutProps.style) : {}) : {}) }),
react_1.default.createElement("span", { style: __assign({}, unstyled !== true ? {
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
overflow: 'hidden',
} : {}) }, "Logout"))));
}
return null;
};
exports.PayloadAdminBar = PayloadAdminBar;
//# sourceMappingURL=index.js.map