Files
twenty/packages/twenty-front/src/utils/recoil-effects.ts
Paul Rastoin 7fd89678b7 [CHORE] Avoid isDefined duplicated reference, move it to twenty-shared (#9967)
# Introduction
Avoid having multiple `isDefined` definition across our pacakges
Also avoid importing `isDefined` from `twenty-ui` which exposes a huge
barrel for a such little util function

## In a nutshell
Removed own `isDefined.ts` definition from `twenty-ui` `twenty-front`
and `twenty-server` to move it to `twenty-shared`.
Updated imports for each packages, and added explicit dependencies to
`twenty-shared` if not already in place

Related PR https://github.com/twentyhq/twenty/pull/9941
2025-02-01 12:10:10 +01:00

86 lines
2.3 KiB
TypeScript

import omit from 'lodash.omit';
import { AtomEffect } from 'recoil';
import { z } from 'zod';
import { isDefined } from 'twenty-shared';
import { cookieStorage } from '~/utils/cookie-storage';
export const localStorageEffect =
<T>(key?: string): AtomEffect<T> =>
({ setSelf, onSet, node }) => {
const savedValue = localStorage.getItem(key ?? node.key);
if (savedValue != null) {
setSelf(JSON.parse(savedValue));
}
onSet((newValue, _, isReset) => {
isReset
? localStorage.removeItem(key ?? node.key)
: localStorage.setItem(key ?? node.key, JSON.stringify(newValue));
});
};
const customCookieAttributeZodSchema = z.object({
cookieAttributes: z.object({
expires: z.union([z.number(), z.instanceof(Date)]).optional(),
path: z.string().optional(),
domain: z.string().optional(),
secure: z.boolean().optional(),
}),
});
export const isCustomCookiesAttributesValue = (
value: unknown,
): value is { cookieAttributes: Cookies.CookieAttributes } =>
customCookieAttributeZodSchema.safeParse(value).success;
export const cookieStorageEffect =
<T>(
key: string,
attributes?: Cookies.CookieAttributes,
hooks?: {
validateInitFn?: (payload: T) => boolean;
},
): AtomEffect<T | null> =>
({ setSelf, onSet }) => {
const savedValue = cookieStorage.getItem(key);
if (
isDefined(savedValue) &&
savedValue.length !== 0 &&
(!isDefined(hooks?.validateInitFn) ||
hooks.validateInitFn(JSON.parse(savedValue)))
) {
setSelf(JSON.parse(savedValue));
}
const defaultAttributes = {
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7),
...(attributes ?? {}),
};
onSet((newValue, _, isReset) => {
const cookieAttributes = {
...defaultAttributes,
...(isCustomCookiesAttributesValue(newValue)
? newValue.cookieAttributes
: {}),
};
if (
!newValue ||
(Object.keys(newValue).length === 1 &&
isCustomCookiesAttributesValue(newValue))
) {
cookieStorage.removeItem(key, cookieAttributes);
return;
}
isReset
? cookieStorage.removeItem(key, cookieAttributes)
: cookieStorage.setItem(
key,
JSON.stringify(omit(newValue, ['cookieAttributes'])),
cookieAttributes,
);
});
};