Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup] Redesigning Notification Slider Per Figma Design #2358

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
59 changes: 59 additions & 0 deletions src/common/hooks/useReplaceTranslationVariables.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import reactStringReplace from 'react-string-replace';
import { Fragment, ReactNode, isValidElement } from 'react';

interface Replacements {
[key: string]: ReactNode;
}

export function useReplaceVariables() {
const isValidReplacement = (value: ReactNode): boolean => {
if (value === null || value === undefined) return true;
if (typeof value === 'string') return true;
if (typeof value === 'number') return true;
if (typeof value === 'boolean') return true;

if (isValidElement(value)) return true;

return false;
};

const sanitizeValue = (value: ReactNode): ReactNode => {
if (!isValidReplacement(value)) {
return '';
}

return value;
};

return (text: string, replacements: Replacements): ReactNode => {
try {
if (typeof text !== 'string') {
return '';
}

let result: string | ReactNode[] = text;

for (const [variable, value] of Object.entries(replacements)) {
const safeValue = sanitizeValue(value);

result = reactStringReplace(result, `:${variable}`, (_, i) => (
<Fragment key={`${variable}-${i}`}>{safeValue}</Fragment>
));
}

return Array.isArray(result) ? <Fragment>{result}</Fragment> : result;
} catch (error) {
return text;
}
};
}
Loading
Loading