UNPKG

create-cookie

Version:

Smarter Cookie Management, Seamless Sync

99 lines (98 loc) 5.08 kB
import { useCallback, useEffect, useState } from "react"; var useFunction = function (name, initialValue, options) { var _a = useState(function () { var _a; if (typeof document === "undefined") return initialValue; var cookieValue = (_a = document.cookie .split("; ") .find(function (row) { return row.startsWith("".concat(name, "=")); })) === null || _a === void 0 ? void 0 : _a.split("=")[1]; if (cookieValue !== undefined) { try { return JSON.parse(decodeURIComponent(cookieValue)); } catch (_b) { console.warn("Failed to parse cookie value for key \"".concat(name, "\". Falling back to raw value.")); return decodeURIComponent(cookieValue); } } return initialValue; }), value = _a[0], setStateValue = _a[1]; var get = useCallback(function () { return value; }, [value]); var set = useCallback(function (newValue, customOptions) { var _a, _b, _c; setStateValue(newValue); if (typeof document !== "undefined") { var serializedValue = typeof newValue === "string" ? newValue : JSON.stringify(newValue); var cookieParts = ["".concat(name, "=").concat(encodeURIComponent(serializedValue))]; var expiresOption = (_a = customOptions === null || customOptions === void 0 ? void 0 : customOptions.expires) !== null && _a !== void 0 ? _a : options === null || options === void 0 ? void 0 : options.expires; if (expiresOption instanceof Date) { cookieParts.push("expires=".concat(expiresOption.toUTCString())); } else if (typeof expiresOption === "number") { var date = new Date(); date.setTime(date.getTime() + expiresOption * 24 * 60 * 60 * 1000); cookieParts.push("expires=".concat(date.toUTCString())); } var path = (_c = (_b = customOptions === null || customOptions === void 0 ? void 0 : customOptions.path) !== null && _b !== void 0 ? _b : options === null || options === void 0 ? void 0 : options.path) !== null && _c !== void 0 ? _c : "/"; cookieParts.push("path=".concat(path)); document.cookie = cookieParts.join("; "); } }, [name, options]); var reset = useCallback(function () { var _a; setStateValue(initialValue); if (typeof document !== "undefined") { var path = (_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : "/"; document.cookie = "".concat(name, "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=").concat(path, ";"); } }, [name, initialValue, options]); var hasValue = useCallback(function () { return value !== ""; }, [value]); useEffect(function () { var _a, _b; if (typeof document === "undefined") return; var cookieValue = (_a = document.cookie .split("; ") .find(function (row) { return row.startsWith("".concat(name, "=")); })) === null || _a === void 0 ? void 0 : _a.split("=")[1]; if (cookieValue !== undefined) { try { var parsedValue = JSON.parse(decodeURIComponent(cookieValue)); if (JSON.stringify(parsedValue) !== JSON.stringify(value)) { setStateValue(parsedValue); } } catch (_c) { console.warn("Failed to parse cookie value for key \"".concat(name, "\". Falling back to raw value.")); var rawValue = decodeURIComponent(cookieValue); if (rawValue !== value) { setStateValue(rawValue); } } } else { var serializedValue = typeof initialValue === "string" ? initialValue : JSON.stringify(initialValue); var cookieParts = ["".concat(name, "=").concat(encodeURIComponent(serializedValue))]; var expiresOption = options === null || options === void 0 ? void 0 : options.expires; if (expiresOption instanceof Date) { cookieParts.push("expires=".concat(expiresOption.toUTCString())); } else if (typeof expiresOption === "number") { var date = new Date(); date.setTime(date.getTime() + expiresOption * 24 * 60 * 60 * 1000); cookieParts.push("expires=".concat(date.toUTCString())); } var path = (_b = options === null || options === void 0 ? void 0 : options.path) !== null && _b !== void 0 ? _b : "/"; cookieParts.push("path=".concat(path)); document.cookie = cookieParts.join("; "); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [name, options, initialValue]); return { get: get, set: set, reset: reset, hasValue: hasValue, }; }; export { useFunction as createCookie };