configProvider.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use client';
  2. import { createContext, useContext } from 'react';
  3. import Config from '@/types/config';
  4. // SSR fetchConfig 실패(API/SSR 일시 장애, DB Config 0행) 시 사용하는 안전 기본값.
  5. // 모든 하위 폼 객체를 널/기본 리프로 채워, 소비 컴포넌트의 config.account.X · config.company.X ·
  6. // config.attendance.X 같은 직접 접근이 크래시 없이 널을 반환하도록 보장한다.
  7. const DEFAULT_CONFIG: Config = {
  8. id: 0,
  9. basic: {
  10. siteName: null,
  11. siteURL: null,
  12. rootID: null,
  13. fromEmail: null,
  14. fromName: null,
  15. smtpServer: null,
  16. smtpPort: null,
  17. smtpEnableSSL: false,
  18. smtpUsername: null,
  19. smtpPassword: null,
  20. adminWhiteIPList: null,
  21. frontWhiteIPList: null,
  22. blockAlertTitle: null,
  23. blockAlertContent: null,
  24. isMaintenance: false,
  25. maintenanceContent: null
  26. },
  27. images: {
  28. faviconPath: null,
  29. logoSquarePath: null,
  30. logoHorizontalPath: null,
  31. ogDefaultPath: null,
  32. twitterImagePath: null,
  33. appleTouchIconPath: null,
  34. appIcon192Path: null,
  35. appIcon512Path: null
  36. },
  37. meta: {
  38. keywords: null,
  39. description: null,
  40. author: null,
  41. viewport: null,
  42. applicationName: null,
  43. generator: null,
  44. robots: null,
  45. adds: null
  46. },
  47. company: {
  48. name: null,
  49. regNo: null,
  50. address: null,
  51. zipCode: null,
  52. owner: null,
  53. tel: null,
  54. fax: null,
  55. retailSaleNo: null,
  56. addedSaleNo: null,
  57. hosting: null,
  58. adminName: null,
  59. adminEmail: null,
  60. siteUrl: null,
  61. bankCode: null,
  62. bankOwner: null,
  63. bankNumber: null
  64. },
  65. account: {
  66. isRegisterBlock: false,
  67. isRegisterEmailAuth: false,
  68. passwordMinLength: null,
  69. passwordUppercaseLength: null,
  70. passwordNumbersLength: null,
  71. passwordSpecialcharsLength: null,
  72. deniedEmailList: null,
  73. deniedNameList: null,
  74. changeEmailDay: null,
  75. changeNameDay: null,
  76. changeSummaryDay: null,
  77. changeIntroDay: null,
  78. changeThumbDay: null,
  79. changePasswordDay: null,
  80. isLoginEmailVerifiedOnly: false,
  81. maxLoginTryCount: null,
  82. maxLoginTryLimitSecond: null
  83. },
  84. emailTemplate: {
  85. registerEmailFormTitle: null,
  86. registerEmailFormContent: null,
  87. registrationEmailFormTitle: null,
  88. registrationEmailFormContent: null,
  89. resetPasswordEmailFormTitle: null,
  90. resetPasswordEmailFormContent: null,
  91. changedPasswordEmailFormTitle: null,
  92. changedPasswordEmailFormContent: null,
  93. withdrawEmailFormTitle: null,
  94. withdrawEmailFormContent: null,
  95. emailVerifyFormTitle: null,
  96. emailVerifyFormContent: null,
  97. changedEmailFormTitle: null,
  98. changedEmailFormContent: null
  99. },
  100. external: {
  101. youTubeApiKeyEnc: null,
  102. youTubeApiName: null,
  103. googleClientId: null,
  104. googleClientSecretEnc: null,
  105. googleAppId: null,
  106. naverLoginEnabled: true,
  107. kakaoLoginEnabled: true,
  108. googleLoginEnabled: true
  109. },
  110. toss: {
  111. tossPayMode: null,
  112. hasTestClientKey: false,
  113. hasTestSecretKey: false,
  114. hasLiveClientKey: false,
  115. hasLiveSecretKey: false
  116. },
  117. attendance: {
  118. isEnabled: false,
  119. baseExp: 0,
  120. basePoint: 0,
  121. useStreakBonus: false,
  122. streakBonusPerDay: 0,
  123. streakBonusPointPerDay: 0,
  124. streakBonusMaxDays: 0,
  125. useRankBonus: false,
  126. rankBonusConfig: null
  127. },
  128. navMenu: []
  129. };
  130. const ConfigContext = createContext<Config|null>(null);
  131. // Context Provider — initialConfig 가 null(SSR 장애)이어도 DEFAULT_CONFIG 로 대체해
  132. // 하위 트리가 절대 null config 를 만나지 않도록 한다.
  133. export function ConfigProvider({ children, initialConfig }: {
  134. children: React.ReactNode,
  135. initialConfig: Config|null
  136. }) {
  137. return (
  138. <ConfigContext.Provider value={initialConfig ?? DEFAULT_CONFIG}>
  139. {children}
  140. </ConfigContext.Provider>
  141. );
  142. }
  143. // Context 사용을 위한 커스텀 훅 — 값이 없어도(프로바이더 밖 사용 포함) 기본값을 반환해
  144. // 페이지가 예외로 다운되지 않도록 한다.
  145. export function useConfigContext(): Config {
  146. return useContext(ConfigContext) ?? DEFAULT_CONFIG;
  147. }