index.tsx 3.9 KB

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