Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions editor/app/(api)/(public)/v1/west/t/invite/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { service_role } from "@/lib/supabase/server";
import { buildTenantSiteBaseUrl } from "@/lib/tenant-url";
import { headers } from "next/headers";
import { NextResponse, type NextRequest } from "next/server";
import { Platform } from "@/lib/platform";
Expand Down Expand Up @@ -49,21 +50,24 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: mint_err }, { status: 500 });
}

const { www_name, www_route_path } = invitation.campaign;
const baseUrl = new URL(
www_route_path ?? "",
IS_HOSTED
? `https://${www_name}.grida.site/`
: `http://${www_name}.localhost:3000/`
);
const { www_name: raw_www_name, www_route_path } = invitation.campaign;
assert(raw_www_name, "campaign.www_name is required");

const baseUrl = await buildTenantSiteBaseUrl({
www_name: raw_www_name,
www_route_path,
hosted: IS_HOSTED,
prefer_canonical: true,
});
const inviteUrl = `${baseUrl}/t/${invitation.code}`;

return NextResponse.json({
data: {
code: invitation.code,
sharable: {
referrer_name: invitation.referrer.referrer_name ?? "",
invitation_code: invitation.code,
url: `${baseUrl.toString()}/t/${invitation.code}`,
url: inviteUrl,
} satisfies Platform.WEST.Referral.SharableContext,
},
error: null,
Expand Down
19 changes: 11 additions & 8 deletions editor/app/(api)/(public)/v1/west/t/refresh/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { service_role } from "@/lib/supabase/server";
import { buildTenantSiteBaseUrl } from "@/lib/tenant-url";
import { headers } from "next/headers";
import { NextResponse, type NextRequest } from "next/server";
import { Platform } from "@/lib/platform";
Expand Down Expand Up @@ -56,22 +57,24 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: refresh_err }, { status: 400 });
}

const { www_name, www_route_path } = invitation.campaign;
const { www_name: raw_www_name, www_route_path } = invitation.campaign;
assert(raw_www_name, "campaign.www_name is required");

const baseUrl = new URL(
www_route_path ?? "",
IS_HOSTED
? `https://${www_name}.grida.site/`
: `http://${www_name}.localhost:3000/`
);
const baseUrl = await buildTenantSiteBaseUrl({
www_name: raw_www_name,
www_route_path,
hosted: IS_HOSTED,
prefer_canonical: true,
});
const inviteUrl = `${baseUrl}/t/${invitation.code}`;

return NextResponse.json({
data: {
code: invitation.code,
sharable: {
referrer_name: invitation.referrer.referrer_name ?? "",
invitation_code: invitation.code,
url: `${baseUrl.toString()}/t/${invitation.code}`,
url: inviteUrl,
} satisfies Platform.WEST.Referral.SharableContext,
},
error: null,
Expand Down
128 changes: 90 additions & 38 deletions editor/grida-forms-hosted/e/formview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ import { useValue } from "@/lib/spock";
import { Spinner } from "@/components/ui/spinner";
import { PhoneFieldDefaultCountryProvider } from "@/components/formfield/phone-field";
import type { FormAgentGeo } from "@/grida-forms/formstate/core/geo";
import resources from "@/i18n";
import { select_lang } from "@/i18n/utils";
import { supported_form_page_languages } from "@/k/supported_languages";

const html_form_id = "form";

Expand Down Expand Up @@ -94,25 +97,46 @@ export interface FormViewTranslation {
};
}

const default_form_view_translation_en: FormViewTranslation = {
next: "Next",
back: "Back",
submit: "Submit",
pay: "Pay",
email_challenge: {
verify: "Verify",
sending: "Sending",
verify_code: "Verify",
enter_verification_code: "Enter verification code",
code_sent: "A verification code has been sent to your inbox.",
didnt_receive_code: "Didn't receive a code?",
resend: "Resend",
retry: "Retry",
code_expired: "Verification code has expired.",
incorrect_code: "Incorrect verification code. Please try again.",
error_occurred: "An error occurred. Please try again later.",
},
};
/**
* Build a {@link FormViewTranslation} from shared i18n resources for the
* given language code. Unknown / unsupported codes fall back to `"en"`.
*/
function build_form_view_translation(lang: string): FormViewTranslation {
const lng = select_lang(lang, supported_form_page_languages, "en");
const t = resources[lng].translation;
const ec = t.email_challenge;

return {
next: t.next,
back: t.back,
submit: t.submit,
pay: t.pay,
email_challenge: {
verify: t.verify,
sending: t.sending,
verify_code: ec.verify_code,
enter_verification_code: ec.enter_verification_code,
code_sent: ec.code_sent,
didnt_receive_code: ec.didnt_receive_code,
resend: t.resend,
retry: t.retry,
code_expired: ec.code_expired,
incorrect_code: ec.incorrect_code,
error_occurred: ec.error_occurred,
},
};
}

/** Canonical English translation, derived from the shared i18n resources. */
const default_form_view_translation_en = build_form_view_translation("en");

const FormViewTranslationContext = React.createContext<FormViewTranslation>(
default_form_view_translation_en
);

function useFormViewTranslation() {
return React.useContext(FormViewTranslationContext);
}

type FormViewRootProps = {
form_id: string;
Expand All @@ -122,6 +146,21 @@ type FormViewRootProps = {
blocks: ClientRenderBlock[];
tree: FormBlockTree<ClientRenderBlock[]>;
defaultValues?: { [key: string]: string };
/**
* Optional language code (e.g. `"ko"`, `"en"`).
* Resolves a {@link FormViewTranslation} from the shared i18n resources and
* provides it via context so that `FormView.Body`, `FormView.Prev`,
* `FormView.Next`, and `FormView.Submit` are automatically localised.
*
* An explicit {@link translation} prop takes precedence over `lang`.
*/
lang?: string;
/**
* Explicit {@link FormViewTranslation} object.
* Takes precedence over {@link lang}. When both are omitted the context
* defaults to English.
*/
translation?: FormViewTranslation;
};

export function GridaFormsFormView(
Expand All @@ -143,7 +182,6 @@ export function GridaFormsFormView(
>
<GridaFormBody {...props} />
<GridaFormFooter
translation={props.translation}
is_powered_by_branding_enabled={
props.config.is_powered_by_branding_enabled
}
Expand All @@ -155,9 +193,24 @@ export function GridaFormsFormView(

export function FormViewRoot({
children,
lang,
translation: translationProp,
...props
}: React.PropsWithChildren<FormViewRootProps>) {
return <Providers {...props}>{children}</Providers>;
const translation = useMemo(
() =>
translationProp ??
(lang
? build_form_view_translation(lang)
: default_form_view_translation_en),
[translationProp, lang]
);

return (
<FormViewTranslationContext.Provider value={translation}>
<Providers {...props}>{children}</Providers>
</FormViewTranslationContext.Provider>
);
}

function Providers({
Expand Down Expand Up @@ -236,11 +289,13 @@ export function FormBody({
onSubmit,
onAfterSubmit,
className,
translation = default_form_view_translation_en,
translation: translationProp,
config,
stylesheet,
...formattributes
}: FormBodyProps & HtmlFormElementProps & IOnSubmit) {
const contextTranslation = useFormViewTranslation();
const translation = translationProp ?? contextTranslation;
const [state, dispatch] = useFormAgentState();
const { tree, session_id, current_section_id, submit_hidden, onNext } =
useFormAgent();
Expand Down Expand Up @@ -322,16 +377,14 @@ export function FormBody({

function GridaFormFooter({
is_powered_by_branding_enabled,
translation = default_form_view_translation_en,
}: {
is_powered_by_branding_enabled: boolean;
translation?: FormViewTranslation;
}) {
const { pay_hidden } = useFormAgent();

return (
<>
<Footer shouldHidePay={pay_hidden} translation={translation} />
<Footer shouldHidePay={pay_hidden} />
{/* on desktop, branding attribute is below footer */}
{is_powered_by_branding_enabled && (
<div className="hidden md:block">
Expand All @@ -342,13 +395,9 @@ function GridaFormFooter({
);
}

function Footer({
translation,
shouldHidePay,
}: {
shouldHidePay: boolean;
translation: FormViewTranslation;
}) {
function Footer({ shouldHidePay }: { shouldHidePay: boolean }) {
const translation = useFormViewTranslation();

return (
<footer
className={cn(
Expand All @@ -358,9 +407,9 @@ function Footer({
"md:static md:justify-start md:bg-transparent md:dark:bg-transparent"
)}
>
<FormPrev>{translation.back}</FormPrev>
<FormNext className="flex-1 md:w-auto">{translation.next}</FormNext>
<FormSubmit className="flex-1 md:w-auto">{translation.submit}</FormSubmit>
<FormPrev />
<FormNext className="flex-1 md:w-auto" />
<FormSubmit className="flex-1 md:w-auto" />
<TossPaymentsPayButton
data-pay-hidden={shouldHidePay}
className={cn(
Expand All @@ -382,6 +431,7 @@ function FormPrev({
className?: string;
}>) {
const { has_previous, onPrevious } = useFormAgent();
const translation = useFormViewTranslation();

return (
<Button
Expand All @@ -390,7 +440,7 @@ function FormPrev({
className={cn("data-[next-hidden='true']:hidden", className)}
onClick={onPrevious}
>
{children}
{children ?? translation.back}
</Button>
);
}
Expand All @@ -402,6 +452,7 @@ function FormNext({
className?: string;
}>) {
const { has_next } = useFormAgent();
const translation = useFormViewTranslation();

return (
<Button
Expand All @@ -411,7 +462,7 @@ function FormNext({
type="submit"
className={cn("data-[next-hidden='true']:hidden", className)}
>
{children}
{children ?? translation.next}
</Button>
);
}
Expand All @@ -423,6 +474,7 @@ function FormSubmit({
className?: string;
}>) {
const { submit_hidden, is_submitting } = useFormAgent();
const translation = useFormViewTranslation();

return (
<Button
Expand All @@ -442,7 +494,7 @@ function FormSubmit({
<Spinner className="me-2" />
</div>
)}
{children}
{children ?? translation.submit}
</Button>
);
}
Expand Down
25 changes: 12 additions & 13 deletions editor/i18n/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ export interface Translation {
submit: string;
pay: string;
home: string;
// Common UI labels (forms)
verify?: string;
resend?: string;
retry?: string;
sending?: string;
email_challenge?: {
verify_code?: string;
enter_verification_code?: string;
code_sent?: string;
didnt_receive_code?: string;
code_expired?: string;
incorrect_code?: string;
error_occurred?: string;
verify: string;
resend: string;
retry: string;
sending: string;
email_challenge: {
verify_code: string;
enter_verification_code: string;
code_sent: string;
didnt_receive_code: string;
code_expired: string;
incorrect_code: string;
error_occurred: string;
};
left_in_stock: string;
sold_out: string;
Expand Down
Loading