Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d500d9412f | ||
|
|
d034ddf990 |
Vendored
+2
@@ -12,6 +12,8 @@ export { Analytics } from '../modules/analytics/client.js';
|
||||
*/
|
||||
export { ConsentProvider, CookieBanner, CookieButton, useConsent, useConsentContext, } from '../modules/consent/client.js';
|
||||
export type { CookieBannerClassNames } from '../modules/consent/client.js';
|
||||
export { useContactForm } from '../modules/forms/useContactForm.js';
|
||||
export type { ContactFormErrors, ContactFormValues, SubmitAction, } from '../modules/forms/useContactForm.js';
|
||||
export { buildLocalizedPath, getLocaleCodes, getLocalizedSlugs, switchLocalePath, } from '../modules/i18n/index.js';
|
||||
export type { I18nConfig, LocalizedSlugs } from '../modules/i18n/index.js';
|
||||
export { AlternateLinksProvider, AlternateLinksUpdater, LanguageSwitcher, useAlternateLinks, useSetAlternateLinks, } from '../modules/i18n/index.js';
|
||||
|
||||
Vendored
+1
@@ -10,6 +10,7 @@ export { Analytics } from '../modules/analytics/client.js';
|
||||
* components. Kept separate from the main entry so server bundles don't pull in
|
||||
* client-only code.
|
||||
*/ export { ConsentProvider, CookieBanner, CookieButton, useConsent, useConsentContext } from '../modules/consent/client.js';
|
||||
export { useContactForm } from '../modules/forms/useContactForm.js';
|
||||
// Pure i18n path helpers — no server/RSC deps, safe to import in client
|
||||
// components (e.g. a LanguageSwitcher that computes locale URLs on the client).
|
||||
export { buildLocalizedPath, getLocaleCodes, getLocalizedSlugs, switchLocalePath } from '../modules/i18n/index.js';
|
||||
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["'use client'\nexport { MaskedField } from '../globals/SiteIntegrations/components/MaskedField.js'\nexport { TestEmailButton } from '../globals/SiteIntegrations/components/TestEmailButton.js'\nexport {\n AccessibilityProvider,\n AccessibilityWidget,\n useAccessibility,\n} from '../modules/accessibility/client.js'\nexport type { A11yClassNames, A11yState, A11yTexts } from '../modules/accessibility/client.js'\nexport { Analytics } from '../modules/analytics/client.js'\n/**\n * Entry point: ipal-kit/client\n *\n * Client-side ('use client') exports — React hooks, providers, and UI\n * components. Kept separate from the main entry so server bundles don't pull in\n * client-only code.\n */\nexport {\n ConsentProvider,\n CookieBanner,\n CookieButton,\n useConsent,\n useConsentContext,\n} from '../modules/consent/client.js'\n\nexport type { CookieBannerClassNames } from '../modules/consent/client.js'\n// Pure i18n path helpers — no server/RSC deps, safe to import in client\n// components (e.g. a LanguageSwitcher that computes locale URLs on the client).\nexport {\n buildLocalizedPath,\n getLocaleCodes,\n getLocalizedSlugs,\n switchLocalePath,\n} from '../modules/i18n/index.js'\nexport type { I18nConfig, LocalizedSlugs } from '../modules/i18n/index.js'\nexport {\n AlternateLinksProvider,\n AlternateLinksUpdater,\n LanguageSwitcher,\n useAlternateLinks,\n useSetAlternateLinks,\n} from '../modules/i18n/index.js'\nexport type { AlternateLinks, LanguageSwitcherClassNames } from '../modules/i18n/index.js'\nexport { Turnstile } from '../modules/turnstile/client.js'\nexport { TurnstileProvider, useTurnstile } from '../modules/turnstile/client.js'\nexport type { TurnstileProps } from '../modules/turnstile/client.js'\n"],"names":["MaskedField","TestEmailButton","AccessibilityProvider","AccessibilityWidget","useAccessibility","Analytics","ConsentProvider","CookieBanner","CookieButton","useConsent","useConsentContext","buildLocalizedPath","getLocaleCodes","getLocalizedSlugs","switchLocalePath","AlternateLinksProvider","AlternateLinksUpdater","LanguageSwitcher","useAlternateLinks","useSetAlternateLinks","Turnstile","TurnstileProvider","useTurnstile"],"mappings":"AAAA;AACA,SAASA,WAAW,QAAQ,wDAAuD;AACnF,SAASC,eAAe,QAAQ,4DAA2D;AAC3F,SACEC,qBAAqB,EACrBC,mBAAmB,EACnBC,gBAAgB,QACX,qCAAoC;AAE3C,SAASC,SAAS,QAAQ,iCAAgC;AAC1D;;;;;;CAMC,GACD,SACEC,eAAe,EACfC,YAAY,EACZC,YAAY,EACZC,UAAU,EACVC,iBAAiB,QACZ,+BAA8B;AAGrC,wEAAwE;AACxE,gFAAgF;AAChF,SACEC,kBAAkB,EAClBC,cAAc,EACdC,iBAAiB,EACjBC,gBAAgB,QACX,2BAA0B;AAEjC,SACEC,sBAAsB,EACtBC,qBAAqB,EACrBC,gBAAgB,EAChBC,iBAAiB,EACjBC,oBAAoB,QACf,2BAA0B;AAEjC,SAASC,SAAS,QAAQ,iCAAgC;AAC1D,SAASC,iBAAiB,EAAEC,YAAY,QAAQ,iCAAgC"}
|
||||
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["'use client'\nexport { MaskedField } from '../globals/SiteIntegrations/components/MaskedField.js'\nexport { TestEmailButton } from '../globals/SiteIntegrations/components/TestEmailButton.js'\nexport {\n AccessibilityProvider,\n AccessibilityWidget,\n useAccessibility,\n} from '../modules/accessibility/client.js'\nexport type { A11yClassNames, A11yState, A11yTexts } from '../modules/accessibility/client.js'\nexport { Analytics } from '../modules/analytics/client.js'\n/**\n * Entry point: ipal-kit/client\n *\n * Client-side ('use client') exports — React hooks, providers, and UI\n * components. Kept separate from the main entry so server bundles don't pull in\n * client-only code.\n */\nexport {\n ConsentProvider,\n CookieBanner,\n CookieButton,\n useConsent,\n useConsentContext,\n} from '../modules/consent/client.js'\n\nexport type { CookieBannerClassNames } from '../modules/consent/client.js'\nexport { useContactForm } from '../modules/forms/useContactForm.js'\nexport type {\n ContactFormErrors,\n ContactFormValues,\n SubmitAction,\n} from '../modules/forms/useContactForm.js'\n// Pure i18n path helpers — no server/RSC deps, safe to import in client\n// components (e.g. a LanguageSwitcher that computes locale URLs on the client).\nexport {\n buildLocalizedPath,\n getLocaleCodes,\n getLocalizedSlugs,\n switchLocalePath,\n} from '../modules/i18n/index.js'\nexport type { I18nConfig, LocalizedSlugs } from '../modules/i18n/index.js'\nexport {\n AlternateLinksProvider,\n AlternateLinksUpdater,\n LanguageSwitcher,\n useAlternateLinks,\n useSetAlternateLinks,\n} from '../modules/i18n/index.js'\nexport type { AlternateLinks, LanguageSwitcherClassNames } from '../modules/i18n/index.js'\nexport { Turnstile } from '../modules/turnstile/client.js'\nexport { TurnstileProvider, useTurnstile } from '../modules/turnstile/client.js'\nexport type { TurnstileProps } from '../modules/turnstile/client.js'\n"],"names":["MaskedField","TestEmailButton","AccessibilityProvider","AccessibilityWidget","useAccessibility","Analytics","ConsentProvider","CookieBanner","CookieButton","useConsent","useConsentContext","useContactForm","buildLocalizedPath","getLocaleCodes","getLocalizedSlugs","switchLocalePath","AlternateLinksProvider","AlternateLinksUpdater","LanguageSwitcher","useAlternateLinks","useSetAlternateLinks","Turnstile","TurnstileProvider","useTurnstile"],"mappings":"AAAA;AACA,SAASA,WAAW,QAAQ,wDAAuD;AACnF,SAASC,eAAe,QAAQ,4DAA2D;AAC3F,SACEC,qBAAqB,EACrBC,mBAAmB,EACnBC,gBAAgB,QACX,qCAAoC;AAE3C,SAASC,SAAS,QAAQ,iCAAgC;AAC1D;;;;;;CAMC,GACD,SACEC,eAAe,EACfC,YAAY,EACZC,YAAY,EACZC,UAAU,EACVC,iBAAiB,QACZ,+BAA8B;AAGrC,SAASC,cAAc,QAAQ,qCAAoC;AAMnE,wEAAwE;AACxE,gFAAgF;AAChF,SACEC,kBAAkB,EAClBC,cAAc,EACdC,iBAAiB,EACjBC,gBAAgB,QACX,2BAA0B;AAEjC,SACEC,sBAAsB,EACtBC,qBAAqB,EACrBC,gBAAgB,EAChBC,iBAAiB,EACjBC,oBAAoB,QACf,2BAA0B;AAEjC,SAASC,SAAS,QAAQ,iCAAgC;AAC1D,SAASC,iBAAiB,EAAEC,YAAY,QAAQ,iCAAgC"}
|
||||
Vendored
+1
@@ -24,6 +24,7 @@ export { buildLocalizedPath, getDefaultLocale, getLocaleCodes, getLocaleDefiniti
|
||||
export type { LocaleMiddlewareResult } from './modules/i18n/index.js';
|
||||
export { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './modules/i18n/index.js';
|
||||
export { createProxy, DEFAULT_PROXY_MATCHER } from './modules/i18n/index.js';
|
||||
export { resolveLocalized } from './modules/i18n/index.js';
|
||||
export { normalizeFilename, normalizeFilenameHook } from './modules/media/index.js';
|
||||
export { getNotificationTexts, NOTIFICATION_FALLBACK, resolveFormMessage, } from './modules/notifications/index.js';
|
||||
export type { FormNotificationTexts, NotificationTexts } from './modules/notifications/index.js';
|
||||
|
||||
Vendored
+1
@@ -15,6 +15,7 @@ export { buildPreventDeleteSystemPage, buildRevalidateHook, buildValidateUniqueR
|
||||
export { buildLocalizedPath, getDefaultLocale, getLocaleCodes, getLocaleDefinition, getLocalizedSlugs, isValidLocale, LOCALE_COOKIE_NAME, matchAcceptLanguage, negotiateLocale, switchLocalePath } from './modules/i18n/index.js';
|
||||
export { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './modules/i18n/index.js';
|
||||
export { createProxy, DEFAULT_PROXY_MATCHER } from './modules/i18n/index.js';
|
||||
export { resolveLocalized } from './modules/i18n/index.js';
|
||||
export { normalizeFilename, normalizeFilenameHook } from './modules/media/index.js';
|
||||
export { getNotificationTexts, NOTIFICATION_FALLBACK, resolveFormMessage } from './modules/notifications/index.js';
|
||||
export { ALL_SYSTEM_PAGE_ROLES, getSystemPagePath } from './modules/pages/index.js';
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+2
@@ -4,5 +4,7 @@ export type { RateLimitArgs } from './rateLimit.js';
|
||||
export { submitForm } from './submitForm.js';
|
||||
export type { SubmitFormArgs, SubmitFormResult } from './submitForm.js';
|
||||
export type { FormsCollectionOverrides, FormsFieldsOverride, FormsOption } from './types.js';
|
||||
export { useContactForm } from './useContactForm.js';
|
||||
export type { ContactFormErrors, ContactFormValues, SubmitAction } from './useContactForm.js';
|
||||
export { validateSubmission } from './validateSubmission.js';
|
||||
export type { FormValidationResult } from './validateSubmission.js';
|
||||
|
||||
Vendored
+1
@@ -3,6 +3,7 @@
|
||||
export { buildFormsPlugin } from './formsPluginConfig.js';
|
||||
export { checkRateLimit } from './rateLimit.js';
|
||||
export { submitForm } from './submitForm.js';
|
||||
export { useContactForm } from './useContactForm.js';
|
||||
export { validateSubmission } from './validateSubmission.js';
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"sources":["../../../src/modules/forms/index.ts"],"sourcesContent":["// buildFormsPlugin is config-time (safe anywhere). submitForm is server-only\n// (Turnstile secret) — never import it from a client component.\nexport { buildFormsPlugin } from './formsPluginConfig.js'\nexport { checkRateLimit } from './rateLimit.js'\nexport type { RateLimitArgs } from './rateLimit.js'\nexport { submitForm } from './submitForm.js'\nexport type { SubmitFormArgs, SubmitFormResult } from './submitForm.js'\nexport type { FormsCollectionOverrides, FormsFieldsOverride, FormsOption } from './types.js'\nexport { validateSubmission } from './validateSubmission.js'\nexport type { FormValidationResult } from './validateSubmission.js'\n"],"names":["buildFormsPlugin","checkRateLimit","submitForm","validateSubmission"],"mappings":"AAAA,6EAA6E;AAC7E,gEAAgE;AAChE,SAASA,gBAAgB,QAAQ,yBAAwB;AACzD,SAASC,cAAc,QAAQ,iBAAgB;AAE/C,SAASC,UAAU,QAAQ,kBAAiB;AAG5C,SAASC,kBAAkB,QAAQ,0BAAyB"}
|
||||
{"version":3,"sources":["../../../src/modules/forms/index.ts"],"sourcesContent":["// buildFormsPlugin is config-time (safe anywhere). submitForm is server-only\n// (Turnstile secret) — never import it from a client component.\nexport { buildFormsPlugin } from './formsPluginConfig.js'\nexport { checkRateLimit } from './rateLimit.js'\nexport type { RateLimitArgs } from './rateLimit.js'\nexport { submitForm } from './submitForm.js'\nexport type { SubmitFormArgs, SubmitFormResult } from './submitForm.js'\nexport type { FormsCollectionOverrides, FormsFieldsOverride, FormsOption } from './types.js'\nexport { useContactForm } from './useContactForm.js'\nexport type { ContactFormErrors, ContactFormValues, SubmitAction } from './useContactForm.js'\nexport { validateSubmission } from './validateSubmission.js'\nexport type { FormValidationResult } from './validateSubmission.js'\n"],"names":["buildFormsPlugin","checkRateLimit","submitForm","useContactForm","validateSubmission"],"mappings":"AAAA,6EAA6E;AAC7E,gEAAgE;AAChE,SAASA,gBAAgB,QAAQ,yBAAwB;AACzD,SAASC,cAAc,QAAQ,iBAAgB;AAE/C,SAASC,UAAU,QAAQ,kBAAiB;AAG5C,SAASC,cAAc,QAAQ,sBAAqB;AAEpD,SAASC,kBAAkB,QAAQ,0BAAyB"}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
import type { SubmitFormResult } from './submitForm.js';
|
||||
/** Field values the form tracks. Projects add/remove keys freely. */
|
||||
export type ContactFormValues = {
|
||||
consent: boolean;
|
||||
email: string;
|
||||
message: string;
|
||||
name: string;
|
||||
phone: string;
|
||||
topic: string;
|
||||
};
|
||||
export type ContactFormErrors = Partial<Record<keyof ContactFormValues, boolean>>;
|
||||
/**
|
||||
* The project's server action that calls submitForm (server-only, holds the
|
||||
* Turnstile secret). The hook is a client hook, so it can't call submitForm
|
||||
* directly — the project injects its action.
|
||||
*/
|
||||
export type SubmitAction = (values: ContactFormValues, turnstileToken?: string) => Promise<{
|
||||
message?: string;
|
||||
success: boolean;
|
||||
} | SubmitFormResult>;
|
||||
type UseContactFormOptions = {
|
||||
/** Generic error message shown when the action throws. */
|
||||
errorFallback?: string;
|
||||
initialTopic?: string;
|
||||
/** Minimum message length. Default 10. */
|
||||
minMessageLength?: number;
|
||||
/** Whether consent is required (project passes true when consentText shown). */
|
||||
requireConsent?: boolean;
|
||||
/** Whether a topic must be chosen (project passes this when topics exist). */
|
||||
requireTopic?: boolean;
|
||||
/** Require a Turnstile token before submit (when the widget is shown). */
|
||||
requireTurnstile?: boolean;
|
||||
submitAction: SubmitAction;
|
||||
};
|
||||
/**
|
||||
* All contact-form logic (state, validation, Turnstile token, submit, success)
|
||||
* WITHOUT any markup — the project owns 100% of the look. Replaces the ~150
|
||||
* lines of identical logic copied into every project's ContactForm, while the
|
||||
* project keeps full control over field layout and styling.
|
||||
*
|
||||
* const f = useContactForm({ submitAction, requireTurnstile: Boolean(siteKey),
|
||||
* requireTopic: topics.length > 0, requireConsent: Boolean(consentText) })
|
||||
* // in JSX — your own fields/classes:
|
||||
* <input value={f.values.name} onChange={(e) => f.setField('name', e.target.value)}
|
||||
* className={f.errors.name ? 'border-red-500' : '…'} />
|
||||
* <Turnstile siteKey={siteKey} onToken={f.setTurnstileToken} />
|
||||
* <form onSubmit={f.handleSubmit}>…</form>
|
||||
* {f.isSuccess && <YourSuccessPanel onReset={f.reset} />}
|
||||
*/
|
||||
export declare function useContactForm(options: UseContactFormOptions): {
|
||||
errorMessage: string | null;
|
||||
errors: Partial<Record<keyof ContactFormValues, boolean>>;
|
||||
handleSubmit: (e: {
|
||||
preventDefault: () => void;
|
||||
}) => Promise<void>;
|
||||
isSubmitting: boolean;
|
||||
isSuccess: boolean;
|
||||
reset: () => void;
|
||||
setField: <K extends keyof ContactFormValues>(key: K, value: ContactFormValues[K]) => void;
|
||||
setTurnstileToken: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
||||
turnstileToken: string | null;
|
||||
values: ContactFormValues;
|
||||
};
|
||||
export {};
|
||||
Vendored
+126
@@ -0,0 +1,126 @@
|
||||
'use client';
|
||||
import { useCallback, useState } from 'react';
|
||||
const EMAIL_RE = /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/;
|
||||
const EMPTY = {
|
||||
name: '',
|
||||
consent: false,
|
||||
email: '',
|
||||
message: '',
|
||||
phone: '',
|
||||
topic: ''
|
||||
};
|
||||
/**
|
||||
* All contact-form logic (state, validation, Turnstile token, submit, success)
|
||||
* WITHOUT any markup — the project owns 100% of the look. Replaces the ~150
|
||||
* lines of identical logic copied into every project's ContactForm, while the
|
||||
* project keeps full control over field layout and styling.
|
||||
*
|
||||
* const f = useContactForm({ submitAction, requireTurnstile: Boolean(siteKey),
|
||||
* requireTopic: topics.length > 0, requireConsent: Boolean(consentText) })
|
||||
* // in JSX — your own fields/classes:
|
||||
* <input value={f.values.name} onChange={(e) => f.setField('name', e.target.value)}
|
||||
* className={f.errors.name ? 'border-red-500' : '…'} />
|
||||
* <Turnstile siteKey={siteKey} onToken={f.setTurnstileToken} />
|
||||
* <form onSubmit={f.handleSubmit}>…</form>
|
||||
* {f.isSuccess && <YourSuccessPanel onReset={f.reset} />}
|
||||
*/ export function useContactForm(options) {
|
||||
const { errorFallback = 'Something went wrong. Please try again.', initialTopic = '', minMessageLength = 10, requireConsent = false, requireTopic = false, requireTurnstile = false, submitAction } = options;
|
||||
const [values, setValues] = useState({
|
||||
...EMPTY,
|
||||
topic: initialTopic
|
||||
});
|
||||
const [errors, setErrors] = useState({});
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [isSuccess, setIsSuccess] = useState(false);
|
||||
const [errorMessage, setErrorMessage] = useState(null);
|
||||
const [turnstileToken, setTurnstileToken] = useState(null);
|
||||
const setField = useCallback((key, value)=>{
|
||||
setValues((prev)=>({
|
||||
...prev,
|
||||
[key]: value
|
||||
}));
|
||||
}, []);
|
||||
const validate = useCallback(()=>{
|
||||
const errs = {};
|
||||
if (!values.name.trim()) {
|
||||
errs.name = true;
|
||||
}
|
||||
if (!values.email.trim() || !EMAIL_RE.test(values.email)) {
|
||||
errs.email = true;
|
||||
}
|
||||
if (requireTopic && !values.topic) {
|
||||
errs.topic = true;
|
||||
}
|
||||
if (!values.message.trim() || values.message.length < minMessageLength) {
|
||||
errs.message = true;
|
||||
}
|
||||
if (requireConsent && !values.consent) {
|
||||
errs.consent = true;
|
||||
}
|
||||
setErrors(errs);
|
||||
return Object.keys(errs).length === 0;
|
||||
}, [
|
||||
values,
|
||||
requireTopic,
|
||||
requireConsent,
|
||||
minMessageLength
|
||||
]);
|
||||
const handleSubmit = useCallback(async (e)=>{
|
||||
e.preventDefault();
|
||||
setErrorMessage(null);
|
||||
if (!validate()) {
|
||||
return;
|
||||
}
|
||||
if (requireTurnstile && !turnstileToken) {
|
||||
setErrorMessage(errorFallback);
|
||||
return;
|
||||
}
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
const result = await submitAction(values, turnstileToken ?? undefined);
|
||||
if (result.success) {
|
||||
setIsSuccess(true);
|
||||
setTurnstileToken(null);
|
||||
} else {
|
||||
setErrorMessage('message' in result && result.message || errorFallback);
|
||||
}
|
||||
} catch (err) {
|
||||
setErrorMessage(err instanceof Error ? err.message : errorFallback);
|
||||
} finally{
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
}, [
|
||||
validate,
|
||||
requireTurnstile,
|
||||
turnstileToken,
|
||||
submitAction,
|
||||
values,
|
||||
errorFallback
|
||||
]);
|
||||
const reset = useCallback(()=>{
|
||||
setValues({
|
||||
...EMPTY,
|
||||
topic: initialTopic
|
||||
});
|
||||
setErrors({});
|
||||
setIsSuccess(false);
|
||||
setErrorMessage(null);
|
||||
setTurnstileToken(null);
|
||||
}, [
|
||||
initialTopic
|
||||
]);
|
||||
return {
|
||||
errorMessage,
|
||||
errors,
|
||||
handleSubmit,
|
||||
isSubmitting,
|
||||
isSuccess,
|
||||
reset,
|
||||
setField,
|
||||
setTurnstileToken,
|
||||
turnstileToken,
|
||||
values
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=useContactForm.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
Vendored
+1
@@ -12,5 +12,6 @@ export { buildLocalizationConfig } from './localizationConfig.js';
|
||||
export { buildLocalizedPath, switchLocalePath } from './localizedPath.js';
|
||||
export type { LocalizedSlugs } from './localizedPath.js';
|
||||
export { LOCALE_COOKIE_NAME, matchAcceptLanguage, negotiateLocale } from './negotiateLocale.js';
|
||||
export { resolveLocalized } from './resolveLocalized.js';
|
||||
export type { I18nConfig, LocaleDefinition } from './types.js';
|
||||
export { validateI18nConfig } from './validation.js';
|
||||
|
||||
Vendored
+1
@@ -8,6 +8,7 @@ export { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './localeMidd
|
||||
export { buildLocalizationConfig } from './localizationConfig.js';
|
||||
export { buildLocalizedPath, switchLocalePath } from './localizedPath.js';
|
||||
export { LOCALE_COOKIE_NAME, matchAcceptLanguage, negotiateLocale } from './negotiateLocale.js';
|
||||
export { resolveLocalized } from './resolveLocalized.js';
|
||||
export { validateI18nConfig } from './validation.js';
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"sources":["../../../src/modules/i18n/index.ts"],"sourcesContent":["export {\n AlternateLinksProvider,\n useAlternateLinks,\n useSetAlternateLinks,\n} from './AlternateLinksContext.js'\nexport type { AlternateLinks } from './AlternateLinksContext.js'\nexport { AlternateLinksUpdater } from './AlternateLinksUpdater.js'\nexport { createProxy, DEFAULT_PROXY_MATCHER } from './createProxy.js'\nexport { getLocalizedSlugs } from './getLocalizedSlugs.js'\nexport { getDefaultLocale, getLocaleCodes, getLocaleDefinition, isValidLocale } from './helpers.js'\nexport { LanguageSwitcher } from './LanguageSwitcher.js'\nexport type { LanguageSwitcherClassNames } from './LanguageSwitcher.js'\nexport { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './localeMiddleware.js'\nexport type { LocaleMiddlewareResult } from './localeMiddleware.js'\nexport { buildLocalizationConfig } from './localizationConfig.js'\nexport { buildLocalizedPath, switchLocalePath } from './localizedPath.js'\nexport type { LocalizedSlugs } from './localizedPath.js'\nexport { LOCALE_COOKIE_NAME, matchAcceptLanguage, negotiateLocale } from './negotiateLocale.js'\nexport type { I18nConfig, LocaleDefinition } from './types.js'\nexport { validateI18nConfig } from './validation.js'\n"],"names":["AlternateLinksProvider","useAlternateLinks","useSetAlternateLinks","AlternateLinksUpdater","createProxy","DEFAULT_PROXY_MATCHER","getLocalizedSlugs","getDefaultLocale","getLocaleCodes","getLocaleDefinition","isValidLocale","LanguageSwitcher","createLocaleMiddleware","DEFAULT_MIDDLEWARE_MATCHER","buildLocalizationConfig","buildLocalizedPath","switchLocalePath","LOCALE_COOKIE_NAME","matchAcceptLanguage","negotiateLocale","validateI18nConfig"],"mappings":"AAAA,SACEA,sBAAsB,EACtBC,iBAAiB,EACjBC,oBAAoB,QACf,6BAA4B;AAEnC,SAASC,qBAAqB,QAAQ,6BAA4B;AAClE,SAASC,WAAW,EAAEC,qBAAqB,QAAQ,mBAAkB;AACrE,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,gBAAgB,EAAEC,cAAc,EAAEC,mBAAmB,EAAEC,aAAa,QAAQ,eAAc;AACnG,SAASC,gBAAgB,QAAQ,wBAAuB;AAExD,SAASC,sBAAsB,EAAEC,0BAA0B,QAAQ,wBAAuB;AAE1F,SAASC,uBAAuB,QAAQ,0BAAyB;AACjE,SAASC,kBAAkB,EAAEC,gBAAgB,QAAQ,qBAAoB;AAEzE,SAASC,kBAAkB,EAAEC,mBAAmB,EAAEC,eAAe,QAAQ,uBAAsB;AAE/F,SAASC,kBAAkB,QAAQ,kBAAiB"}
|
||||
{"version":3,"sources":["../../../src/modules/i18n/index.ts"],"sourcesContent":["export {\n AlternateLinksProvider,\n useAlternateLinks,\n useSetAlternateLinks,\n} from './AlternateLinksContext.js'\nexport type { AlternateLinks } from './AlternateLinksContext.js'\nexport { AlternateLinksUpdater } from './AlternateLinksUpdater.js'\nexport { createProxy, DEFAULT_PROXY_MATCHER } from './createProxy.js'\nexport { getLocalizedSlugs } from './getLocalizedSlugs.js'\nexport { getDefaultLocale, getLocaleCodes, getLocaleDefinition, isValidLocale } from './helpers.js'\nexport { LanguageSwitcher } from './LanguageSwitcher.js'\nexport type { LanguageSwitcherClassNames } from './LanguageSwitcher.js'\nexport { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './localeMiddleware.js'\nexport type { LocaleMiddlewareResult } from './localeMiddleware.js'\nexport { buildLocalizationConfig } from './localizationConfig.js'\nexport { buildLocalizedPath, switchLocalePath } from './localizedPath.js'\nexport type { LocalizedSlugs } from './localizedPath.js'\nexport { LOCALE_COOKIE_NAME, matchAcceptLanguage, negotiateLocale } from './negotiateLocale.js'\nexport { resolveLocalized } from './resolveLocalized.js'\nexport type { I18nConfig, LocaleDefinition } from './types.js'\nexport { validateI18nConfig } from './validation.js'\n"],"names":["AlternateLinksProvider","useAlternateLinks","useSetAlternateLinks","AlternateLinksUpdater","createProxy","DEFAULT_PROXY_MATCHER","getLocalizedSlugs","getDefaultLocale","getLocaleCodes","getLocaleDefinition","isValidLocale","LanguageSwitcher","createLocaleMiddleware","DEFAULT_MIDDLEWARE_MATCHER","buildLocalizationConfig","buildLocalizedPath","switchLocalePath","LOCALE_COOKIE_NAME","matchAcceptLanguage","negotiateLocale","resolveLocalized","validateI18nConfig"],"mappings":"AAAA,SACEA,sBAAsB,EACtBC,iBAAiB,EACjBC,oBAAoB,QACf,6BAA4B;AAEnC,SAASC,qBAAqB,QAAQ,6BAA4B;AAClE,SAASC,WAAW,EAAEC,qBAAqB,QAAQ,mBAAkB;AACrE,SAASC,iBAAiB,QAAQ,yBAAwB;AAC1D,SAASC,gBAAgB,EAAEC,cAAc,EAAEC,mBAAmB,EAAEC,aAAa,QAAQ,eAAc;AACnG,SAASC,gBAAgB,QAAQ,wBAAuB;AAExD,SAASC,sBAAsB,EAAEC,0BAA0B,QAAQ,wBAAuB;AAE1F,SAASC,uBAAuB,QAAQ,0BAAyB;AACjE,SAASC,kBAAkB,EAAEC,gBAAgB,QAAQ,qBAAoB;AAEzE,SAASC,kBAAkB,EAAEC,mBAAmB,EAAEC,eAAe,QAAQ,uBAAsB;AAC/F,SAASC,gBAAgB,QAAQ,wBAAuB;AAExD,SAASC,kBAAkB,QAAQ,kBAAiB"}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Resolves a possibly-localized value to a string for the given locale.
|
||||
* Payload returns a localized field as a plain string (current locale) OR, with
|
||||
* locale:'all' / unflattened docs, as a { pl: '…', en: '…' } map. This handles
|
||||
* both, with a safe fallback to the first available value — replacing the
|
||||
* hand-rolled `getSafeString` with `any` copied into every project (JSON-LD,
|
||||
* rich text, form topics, …).
|
||||
*
|
||||
* import { resolveLocalized } from '@intecion/ipal-kit'
|
||||
* const name = resolveLocalized(item.label, locale) // '' if nothing
|
||||
* const city = resolveLocalized(company.city, locale, '—') // custom fallback
|
||||
*/
|
||||
export declare function resolveLocalized(value: unknown, locale: string, fallback?: string): string;
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Resolves a possibly-localized value to a string for the given locale.
|
||||
* Payload returns a localized field as a plain string (current locale) OR, with
|
||||
* locale:'all' / unflattened docs, as a { pl: '…', en: '…' } map. This handles
|
||||
* both, with a safe fallback to the first available value — replacing the
|
||||
* hand-rolled `getSafeString` with `any` copied into every project (JSON-LD,
|
||||
* rich text, form topics, …).
|
||||
*
|
||||
* import { resolveLocalized } from '@intecion/ipal-kit'
|
||||
* const name = resolveLocalized(item.label, locale) // '' if nothing
|
||||
* const city = resolveLocalized(company.city, locale, '—') // custom fallback
|
||||
*/ export function resolveLocalized(value, locale, fallback = '') {
|
||||
if (value == null) {
|
||||
return fallback;
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === 'object') {
|
||||
const map = value;
|
||||
const forLocale = map[locale];
|
||||
if (typeof forLocale === 'string') {
|
||||
return forLocale;
|
||||
}
|
||||
const first = Object.values(map).find((v)=>typeof v === 'string');
|
||||
return first ?? fallback;
|
||||
}
|
||||
return String(value);
|
||||
}
|
||||
|
||||
//# sourceMappingURL=resolveLocalized.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../src/modules/i18n/resolveLocalized.ts"],"sourcesContent":["/**\n * Resolves a possibly-localized value to a string for the given locale.\n * Payload returns a localized field as a plain string (current locale) OR, with\n * locale:'all' / unflattened docs, as a { pl: '…', en: '…' } map. This handles\n * both, with a safe fallback to the first available value — replacing the\n * hand-rolled `getSafeString` with `any` copied into every project (JSON-LD,\n * rich text, form topics, …).\n *\n * import { resolveLocalized } from '@intecion/ipal-kit'\n * const name = resolveLocalized(item.label, locale) // '' if nothing\n * const city = resolveLocalized(company.city, locale, '—') // custom fallback\n */\nexport function resolveLocalized(value: unknown, locale: string, fallback = ''): string {\n if (value == null) {return fallback}\n if (typeof value === 'string') {return value}\n if (typeof value === 'object') {\n const map = value as Record<string, unknown>\n const forLocale = map[locale]\n if (typeof forLocale === 'string') {return forLocale}\n const first = Object.values(map).find((v) => typeof v === 'string')\n return (first) ?? fallback\n }\n return String(value)\n}\n"],"names":["resolveLocalized","value","locale","fallback","map","forLocale","first","Object","values","find","v","String"],"mappings":"AAAA;;;;;;;;;;;CAWC,GACD,OAAO,SAASA,iBAAiBC,KAAc,EAAEC,MAAc,EAAEC,WAAW,EAAE;IAC5E,IAAIF,SAAS,MAAM;QAAC,OAAOE;IAAQ;IACnC,IAAI,OAAOF,UAAU,UAAU;QAAC,OAAOA;IAAK;IAC5C,IAAI,OAAOA,UAAU,UAAU;QAC7B,MAAMG,MAAMH;QACZ,MAAMI,YAAYD,GAAG,CAACF,OAAO;QAC7B,IAAI,OAAOG,cAAc,UAAU;YAAC,OAAOA;QAAS;QACpD,MAAMC,QAAQC,OAAOC,MAAM,CAACJ,KAAKK,IAAI,CAAC,CAACC,IAAM,OAAOA,MAAM;QAC1D,OAAO,AAACJ,SAAUH;IACpB;IACA,OAAOQ,OAAOV;AAChB"}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@intecion/ipal-kit",
|
||||
"version": "1.6.1",
|
||||
"version": "1.6.2",
|
||||
"description": "Intecion Payload Advanced Library — a Payload CMS 3 plugin: i18n, SEO, forms, consent, analytics, blog/archives.",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
|
||||
@@ -24,6 +24,12 @@ export {
|
||||
} from '../modules/consent/client.js'
|
||||
|
||||
export type { CookieBannerClassNames } from '../modules/consent/client.js'
|
||||
export { useContactForm } from '../modules/forms/useContactForm.js'
|
||||
export type {
|
||||
ContactFormErrors,
|
||||
ContactFormValues,
|
||||
SubmitAction,
|
||||
} from '../modules/forms/useContactForm.js'
|
||||
// Pure i18n path helpers — no server/RSC deps, safe to import in client
|
||||
// components (e.g. a LanguageSwitcher that computes locale URLs on the client).
|
||||
export {
|
||||
|
||||
+2
-1
@@ -91,6 +91,7 @@ export {
|
||||
export type { LocaleMiddlewareResult } from './modules/i18n/index.js'
|
||||
export { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './modules/i18n/index.js'
|
||||
export { createProxy, DEFAULT_PROXY_MATCHER } from './modules/i18n/index.js'
|
||||
export { resolveLocalized } from './modules/i18n/index.js'
|
||||
export { normalizeFilename, normalizeFilenameHook } from './modules/media/index.js'
|
||||
export {
|
||||
getNotificationTexts,
|
||||
@@ -109,8 +110,8 @@ export {
|
||||
SITE_SETTINGS_SLUG,
|
||||
} from './modules/payload/index.js'
|
||||
export { buildSecurityHeaders } from './modules/security/index.js'
|
||||
export { buildCsp } from './modules/security/index.js'
|
||||
|
||||
export { buildCsp } from './modules/security/index.js'
|
||||
export type { BuildCspArgs } from './modules/security/index.js'
|
||||
export type { BuildSecurityHeadersArgs, SecurityHeader } from './modules/security/index.js'
|
||||
export type { PageMetadata, SeoMeta, SeoOption } from './modules/seo/index.js'
|
||||
|
||||
@@ -6,5 +6,7 @@ export type { RateLimitArgs } from './rateLimit.js'
|
||||
export { submitForm } from './submitForm.js'
|
||||
export type { SubmitFormArgs, SubmitFormResult } from './submitForm.js'
|
||||
export type { FormsCollectionOverrides, FormsFieldsOverride, FormsOption } from './types.js'
|
||||
export { useContactForm } from './useContactForm.js'
|
||||
export type { ContactFormErrors, ContactFormValues, SubmitAction } from './useContactForm.js'
|
||||
export { validateSubmission } from './validateSubmission.js'
|
||||
export type { FormValidationResult } from './validateSubmission.js'
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
'use client'
|
||||
|
||||
import { useCallback, useState } from 'react'
|
||||
|
||||
import type { SubmitFormResult } from './submitForm.js'
|
||||
|
||||
/** Field values the form tracks. Projects add/remove keys freely. */
|
||||
export type ContactFormValues = {
|
||||
consent: boolean
|
||||
email: string
|
||||
message: string
|
||||
name: string
|
||||
phone: string
|
||||
topic: string
|
||||
}
|
||||
|
||||
export type ContactFormErrors = Partial<Record<keyof ContactFormValues, boolean>>
|
||||
|
||||
/**
|
||||
* The project's server action that calls submitForm (server-only, holds the
|
||||
* Turnstile secret). The hook is a client hook, so it can't call submitForm
|
||||
* directly — the project injects its action.
|
||||
*/
|
||||
export type SubmitAction = (
|
||||
values: ContactFormValues,
|
||||
turnstileToken?: string,
|
||||
) => Promise<{ message?: string; success: boolean } | SubmitFormResult>
|
||||
|
||||
type UseContactFormOptions = {
|
||||
/** Generic error message shown when the action throws. */
|
||||
errorFallback?: string
|
||||
initialTopic?: string
|
||||
/** Minimum message length. Default 10. */
|
||||
minMessageLength?: number
|
||||
/** Whether consent is required (project passes true when consentText shown). */
|
||||
requireConsent?: boolean
|
||||
/** Whether a topic must be chosen (project passes this when topics exist). */
|
||||
requireTopic?: boolean
|
||||
/** Require a Turnstile token before submit (when the widget is shown). */
|
||||
requireTurnstile?: boolean
|
||||
submitAction: SubmitAction
|
||||
}
|
||||
|
||||
const EMAIL_RE = /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/
|
||||
const EMPTY: ContactFormValues = {
|
||||
name: '',
|
||||
consent: false,
|
||||
email: '',
|
||||
message: '',
|
||||
phone: '',
|
||||
topic: '',
|
||||
}
|
||||
|
||||
/**
|
||||
* All contact-form logic (state, validation, Turnstile token, submit, success)
|
||||
* WITHOUT any markup — the project owns 100% of the look. Replaces the ~150
|
||||
* lines of identical logic copied into every project's ContactForm, while the
|
||||
* project keeps full control over field layout and styling.
|
||||
*
|
||||
* const f = useContactForm({ submitAction, requireTurnstile: Boolean(siteKey),
|
||||
* requireTopic: topics.length > 0, requireConsent: Boolean(consentText) })
|
||||
* // in JSX — your own fields/classes:
|
||||
* <input value={f.values.name} onChange={(e) => f.setField('name', e.target.value)}
|
||||
* className={f.errors.name ? 'border-red-500' : '…'} />
|
||||
* <Turnstile siteKey={siteKey} onToken={f.setTurnstileToken} />
|
||||
* <form onSubmit={f.handleSubmit}>…</form>
|
||||
* {f.isSuccess && <YourSuccessPanel onReset={f.reset} />}
|
||||
*/
|
||||
export function useContactForm(options: UseContactFormOptions) {
|
||||
const {
|
||||
errorFallback = 'Something went wrong. Please try again.',
|
||||
initialTopic = '',
|
||||
minMessageLength = 10,
|
||||
requireConsent = false,
|
||||
requireTopic = false,
|
||||
requireTurnstile = false,
|
||||
submitAction,
|
||||
} = options
|
||||
|
||||
const [values, setValues] = useState<ContactFormValues>({ ...EMPTY, topic: initialTopic })
|
||||
const [errors, setErrors] = useState<ContactFormErrors>({})
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const [isSuccess, setIsSuccess] = useState(false)
|
||||
const [errorMessage, setErrorMessage] = useState<null | string>(null)
|
||||
const [turnstileToken, setTurnstileToken] = useState<null | string>(null)
|
||||
|
||||
const setField = useCallback(
|
||||
<K extends keyof ContactFormValues>(key: K, value: ContactFormValues[K]) => {
|
||||
setValues((prev) => ({ ...prev, [key]: value }))
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const validate = useCallback((): boolean => {
|
||||
const errs: ContactFormErrors = {}
|
||||
if (!values.name.trim()) {errs.name = true}
|
||||
if (!values.email.trim() || !EMAIL_RE.test(values.email)) {errs.email = true}
|
||||
if (requireTopic && !values.topic) {errs.topic = true}
|
||||
if (!values.message.trim() || values.message.length < minMessageLength) {errs.message = true}
|
||||
if (requireConsent && !values.consent) {errs.consent = true}
|
||||
setErrors(errs)
|
||||
return Object.keys(errs).length === 0
|
||||
}, [values, requireTopic, requireConsent, minMessageLength])
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
async (e: { preventDefault: () => void }) => {
|
||||
e.preventDefault()
|
||||
setErrorMessage(null)
|
||||
if (!validate()) {return}
|
||||
if (requireTurnstile && !turnstileToken) {
|
||||
setErrorMessage(errorFallback)
|
||||
return
|
||||
}
|
||||
setIsSubmitting(true)
|
||||
try {
|
||||
const result = await submitAction(values, turnstileToken ?? undefined)
|
||||
if (result.success) {
|
||||
setIsSuccess(true)
|
||||
setTurnstileToken(null)
|
||||
} else {
|
||||
setErrorMessage(('message' in result && result.message) || errorFallback)
|
||||
}
|
||||
} catch (err) {
|
||||
setErrorMessage(err instanceof Error ? err.message : errorFallback)
|
||||
} finally {
|
||||
setIsSubmitting(false)
|
||||
}
|
||||
},
|
||||
[validate, requireTurnstile, turnstileToken, submitAction, values, errorFallback],
|
||||
)
|
||||
|
||||
const reset = useCallback(() => {
|
||||
setValues({ ...EMPTY, topic: initialTopic })
|
||||
setErrors({})
|
||||
setIsSuccess(false)
|
||||
setErrorMessage(null)
|
||||
setTurnstileToken(null)
|
||||
}, [initialTopic])
|
||||
|
||||
return {
|
||||
errorMessage,
|
||||
errors,
|
||||
handleSubmit,
|
||||
isSubmitting,
|
||||
isSuccess,
|
||||
reset,
|
||||
setField,
|
||||
setTurnstileToken,
|
||||
turnstileToken,
|
||||
values,
|
||||
}
|
||||
}
|
||||
@@ -16,5 +16,6 @@ export { buildLocalizationConfig } from './localizationConfig.js'
|
||||
export { buildLocalizedPath, switchLocalePath } from './localizedPath.js'
|
||||
export type { LocalizedSlugs } from './localizedPath.js'
|
||||
export { LOCALE_COOKIE_NAME, matchAcceptLanguage, negotiateLocale } from './negotiateLocale.js'
|
||||
export { resolveLocalized } from './resolveLocalized.js'
|
||||
export type { I18nConfig, LocaleDefinition } from './types.js'
|
||||
export { validateI18nConfig } from './validation.js'
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Resolves a possibly-localized value to a string for the given locale.
|
||||
* Payload returns a localized field as a plain string (current locale) OR, with
|
||||
* locale:'all' / unflattened docs, as a { pl: '…', en: '…' } map. This handles
|
||||
* both, with a safe fallback to the first available value — replacing the
|
||||
* hand-rolled `getSafeString` with `any` copied into every project (JSON-LD,
|
||||
* rich text, form topics, …).
|
||||
*
|
||||
* import { resolveLocalized } from '@intecion/ipal-kit'
|
||||
* const name = resolveLocalized(item.label, locale) // '' if nothing
|
||||
* const city = resolveLocalized(company.city, locale, '—') // custom fallback
|
||||
*/
|
||||
export function resolveLocalized(value: unknown, locale: string, fallback = ''): string {
|
||||
if (value == null) {return fallback}
|
||||
if (typeof value === 'string') {return value}
|
||||
if (typeof value === 'object') {
|
||||
const map = value as Record<string, unknown>
|
||||
const forLocale = map[locale]
|
||||
if (typeof forLocale === 'string') {return forLocale}
|
||||
const first = Object.values(map).find((v) => typeof v === 'string')
|
||||
return (first) ?? fallback
|
||||
}
|
||||
return String(value)
|
||||
}
|
||||
Reference in New Issue
Block a user