registry.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use client";
  2. import { defineRegistry } from "@json-render/react";
  3. import type { ComputedFunction } from "@json-render/core";
  4. import { shadcnComponents } from "@json-render/shadcn";
  5. import { catalog } from "./catalog";
  6. let confettiListener: (() => void) | null = null;
  7. export function onConfetti(cb: () => void) {
  8. confettiListener = cb;
  9. return () => {
  10. confettiListener = null;
  11. };
  12. }
  13. const cityData: Record<string, string[]> = {
  14. US: ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"],
  15. Canada: ["Toronto", "Vancouver", "Montreal", "Calgary", "Ottawa"],
  16. UK: ["London", "Manchester", "Birmingham", "Edinburgh", "Bristol"],
  17. Germany: ["Berlin", "Munich", "Hamburg", "Frankfurt", "Cologne"],
  18. Japan: ["Tokyo", "Osaka", "Kyoto", "Yokohama", "Sapporo"],
  19. };
  20. export const { registry } = defineRegistry(catalog, {
  21. components: {
  22. ...shadcnComponents,
  23. },
  24. actions: {
  25. confetti: async () => {
  26. confettiListener?.();
  27. },
  28. },
  29. });
  30. export const actionHandlers: Record<
  31. string,
  32. (params: Record<string, unknown>) => void
  33. > = {
  34. confetti: () => confettiListener?.(),
  35. };
  36. export const computedFunctions: Record<string, ComputedFunction> = {
  37. formatAddress: (args) => {
  38. const city = (args.city as string) ?? "";
  39. const country = (args.country as string) ?? "";
  40. if (!city && !country) return "No location selected";
  41. if (!city) return country;
  42. return `${city}, ${country}`;
  43. },
  44. citiesForCountry: (args) => {
  45. const country = (args.country as string) ?? "";
  46. return cityData[country] ?? [];
  47. },
  48. };