index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {
  2. Platform
  3. } from "react-native";
  4. import Navigation from "./navigation";
  5. import defaultLocaleJSON from "./variants/locales/default.json";
  6. import defaultThemeJSON from "./variants/themes/default.json";
  7. import {
  8. type NCoreUIKitConfig,
  9. setupNCoreUIKit,
  10. Host
  11. } from "ncore-ui-kit";
  12. import {
  13. useFonts
  14. } from "expo-font";
  15. import "moment/locale/tr";
  16. import moment from "moment";
  17. if (Platform.OS === "web" && typeof window !== "undefined") {
  18. window.addEventListener("error", (e) => {
  19. if (
  20. e.message === "ResizeObserver loop limit exceeded" ||
  21. e.message === "ResizeObserver loop completed with undelivered notifications."
  22. ) {
  23. e.stopImmediatePropagation();
  24. }
  25. });
  26. }
  27. let NCoreConfig: NCoreUIKitConfig = {
  28. projectThemes: defaultThemeJSON as NCoreUIKit.Palette,
  29. initialSelectedGapPropagation: "spacious",
  30. projectLocales: defaultLocaleJSON,
  31. initialSelectedPalette: "nibgat",
  32. initialSelectedTheme: "light"
  33. };
  34. const themeStorage = Platform.OS === "web" ? window.localStorage.getItem("theme") : null;
  35. if(themeStorage) {
  36. const parsedThemeStorage = JSON.parse(themeStorage);
  37. NCoreConfig = {
  38. ...NCoreConfig,
  39. initialSelectedGapPropagation: parsedThemeStorage.activeGapPropagation,
  40. initialSelectedActiveSharpness: parsedThemeStorage.activeSharpness,
  41. initialSelectedTheme: parsedThemeStorage.activeTheme
  42. };
  43. }
  44. const localeStorage = Platform.OS === "web" ? window.localStorage.getItem("locale") : null;
  45. if(localeStorage) {
  46. const parsedLocaleStorage = JSON.parse(localeStorage);
  47. NCoreConfig = {
  48. ...NCoreConfig,
  49. initialSelectedLocale: parsedLocaleStorage.activeLocale
  50. };
  51. }
  52. if (Platform.OS === "web") {
  53. const urlParams = new URLSearchParams(window.location.search);
  54. const langParam = urlParams.get("lang");
  55. if (langParam) {
  56. const isValidLocale = defaultLocaleJSON.some(item => item.locale === langParam);
  57. if (isValidLocale) {
  58. NCoreConfig.initialSelectedLocale = langParam as keyof NCoreUIKit.LocaleKey;
  59. }
  60. }
  61. if (!NCoreConfig.initialSelectedLocale) {
  62. const browserLang = window.navigator.language || "";
  63. const exactMatch = defaultLocaleJSON.find(item => item.locale === browserLang);
  64. if (exactMatch) {
  65. NCoreConfig.initialSelectedLocale = exactMatch.locale as keyof NCoreUIKit.LocaleKey;
  66. } else {
  67. const primaryLang = (browserLang.split(/[-_]/)[0] || "").toLowerCase();
  68. const similarMatch = defaultLocaleJSON.find(item =>
  69. item.locale.toLowerCase() === primaryLang ||
  70. item.locale.toLowerCase().startsWith(`${primaryLang}-`)
  71. );
  72. if (similarMatch) {
  73. NCoreConfig.initialSelectedLocale = similarMatch.locale as keyof NCoreUIKit.LocaleKey;
  74. } else {
  75. NCoreConfig.initialSelectedLocale = (primaryLang === "tr" ? "tr-TR" : "en-US") as keyof NCoreUIKit.LocaleKey;
  76. }
  77. }
  78. }
  79. }
  80. const NCoreUIKitBase = setupNCoreUIKit(NCoreConfig);
  81. moment.locale("tr");
  82. const App = () => {
  83. return <Navigation/>;
  84. };
  85. const isExpo = typeof globalThis !== "undefined" && ("__expo" in globalThis || "Expo" in globalThis);
  86. const ContextAPI = () => {
  87. if(isExpo) {
  88. /* eslint-disable @typescript-eslint/no-require-imports */
  89. const [loaded] = useFonts({
  90. "Geist-ExtraLight": require("./assets/fonts/Geist-ExtraLight.ttf"),
  91. "Geist-ExtraBold": require("./assets/fonts/Geist-ExtraBold.ttf"),
  92. "Geist-SemiBold": require("./assets/fonts/Geist-SemiBold.ttf"),
  93. "Geist-Regular": require("./assets/fonts/Geist-Regular.ttf"),
  94. "Geist-Medium": require("./assets/fonts/Geist-Medium.ttf"),
  95. "Geist-Black": require("./assets/fonts/Geist-Black.ttf"),
  96. "Geist-Light": require("./assets/fonts/Geist-Light.ttf"),
  97. "Geist-Bold": require("./assets/fonts/Geist-Bold.ttf"),
  98. "Geist-Thin": require("./assets/fonts/Geist-Thin.ttf")
  99. });
  100. if (!loaded) return null;
  101. }
  102. return <NCoreUIKitBase.Provider>
  103. <Host
  104. name="main-header"
  105. >
  106. <App/>
  107. </Host>
  108. </NCoreUIKitBase.Provider>;
  109. };
  110. export default ContextAPI;