Files
NapCatQQ/.pnpm-store/v3/files/82/da74ffe8a91fe194027be632a149f603541c4eea1d586b742a34367e31144055afd8db75c168319582caa79e59baa47dfeda7f8f33a9d71deead1bfbd4e636
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

34 lines
1.3 KiB
Plaintext

import { __assign, __read, __rest } from "tslib";
import Cookies from 'js-cookie';
import { useState } from 'react';
import useMemoizedFn from '../useMemoizedFn';
import { isFunction, isString } from '../utils';
function useCookieState(cookieKey, options) {
if (options === void 0) { options = {}; }
var _a = __read(useState(function () {
var cookieValue = Cookies.get(cookieKey);
if (isString(cookieValue)) {
return cookieValue;
}
if (isFunction(options.defaultValue)) {
return options.defaultValue();
}
return options.defaultValue;
}), 2), state = _a[0], setState = _a[1];
var updateState = useMemoizedFn(function (newValue, newOptions) {
if (newOptions === void 0) { newOptions = {}; }
// eslint-disable-next-line @typescript-eslint/no-unused-vars
var _a = __assign(__assign({}, options), newOptions), defaultValue = _a.defaultValue, restOptions = __rest(_a, ["defaultValue"]);
var value = isFunction(newValue) ? newValue(state) : newValue;
setState(value);
if (value === undefined) {
Cookies.remove(cookieKey);
}
else {
Cookies.set(cookieKey, value, restOptions);
}
});
return [state, updateState];
}
export default useCookieState;