@sunggang/ui-lib 0.4.45 → 0.4.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -456,20 +456,17 @@ var LiffContext = /*#__PURE__*/ React.createContext(undefined);
456
456
  var LiffProvider = function(param) {
457
457
  var children = param.children, liffId = param.liffId, _param_localStorageKey = param.localStorageKey, localStorageKey = _param_localStorageKey === void 0 ? "one-gobo-global-login" : _param_localStorageKey, checkTokenUrl = param.checkTokenUrl, loginUrl = param.loginUrl, accountLoginUrl = param.accountLoginUrl, memberInfoUrl = param.memberInfoUrl, _param_openInApp = param.openInApp, openInApp = _param_openInApp === void 0 ? false : _param_openInApp, _param_ignoreCheckAddFriend = param.ignoreCheckAddFriend, ignoreCheckAddFriend = _param_ignoreCheckAddFriend === void 0 ? false : _param_ignoreCheckAddFriend, _param_ignoreRoute = param.ignoreRoute, ignoreRoute = _param_ignoreRoute === void 0 ? false : _param_ignoreRoute, _param_loginByUser = param.loginByUser, loginByUser = _param_loginByUser === void 0 ? false : _param_loginByUser, _param_liffLogin = param.liffLogin, liffLogin = _param_liffLogin === void 0 ? false : _param_liffLogin, customerRedirectUrl = param.customerRedirectUrl, inviteCode = param.inviteCode, handleLoginError = param.handleLoginError;
458
458
  var _useState = _sliced_to_array(React.useState(null), 2), liffObject = _useState[0], setLiffObject = _useState[1];
459
- var _useState1 = _sliced_to_array(React.useState(false), 2), liffInitialized = _useState1[0], setLiffInitialized = _useState1[1];
460
- var _useState2 = _sliced_to_array(React.useState(""), 2), accessToken = _useState2[0], setAccessToken = _useState2[1];
461
- var _useState3 = _sliced_to_array(React.useState(null), 2), liffError = _useState3[0], setLiffError = _useState3[1];
462
- var _useState4 = _sliced_to_array(React.useState(null), 2), loginData = _useState4[0], setLoginData = _useState4[1];
463
- var _useState5 = _sliced_to_array(React.useState(null), 2), userInfo = _useState5[0], setUserInfo = _useState5[1];
464
- var _useState6 = _sliced_to_array(React.useState(null), 2), liffProfile = _useState6[0], setLiffProfile = _useState6[1];
465
- var _useState7 = _sliced_to_array(React.useState(null), 2), isFriendship = _useState7[0], setFriendship = _useState7[1];
466
- var _useState8 = _sliced_to_array(React.useState(null), 2), loginType = _useState8[0], setLoginType = _useState8[1];
459
+ var _useState1 = _sliced_to_array(React.useState(""), 2), accessToken = _useState1[0], setAccessToken = _useState1[1];
460
+ var _useState2 = _sliced_to_array(React.useState(null), 2), liffError = _useState2[0], setLiffError = _useState2[1];
461
+ var _useState3 = _sliced_to_array(React.useState(null), 2), loginData = _useState3[0], setLoginData = _useState3[1];
462
+ var _useState4 = _sliced_to_array(React.useState(null), 2), userInfo = _useState4[0], setUserInfo = _useState4[1];
463
+ var _useState5 = _sliced_to_array(React.useState(null), 2), liffProfile = _useState5[0], setLiffProfile = _useState5[1];
464
+ var _useState6 = _sliced_to_array(React.useState(null), 2), isFriendship = _useState6[0], setFriendship = _useState6[1];
465
+ var _useState7 = _sliced_to_array(React.useState(null), 2), loginType = _useState7[0], setLoginType = _useState7[1];
467
466
  // userInfo 的 localStorage key
468
467
  var userInfoStorageKey = "".concat(localStorageKey, "-userInfo");
469
- // liff profile 的 localStorage key
470
- var liffProfileStorageKey = "".concat(localStorageKey, "-liffProfile");
471
- // liff initialized flag 的 localStorage key
472
- var liffInitializedStorageKey = "".concat(localStorageKey, "-liffInitialized");
468
+ // liff userInfo 的 localStorage key
469
+ var liffUserInfoStorageKey = "".concat(localStorageKey, "-liffUserInfo");
473
470
  // 從 localStorage 載入 userInfo
474
471
  var loadUserInfoFromStorage = function() {
475
472
  try {
@@ -484,50 +481,36 @@ var LiffProvider = function(param) {
484
481
  }
485
482
  return null;
486
483
  };
487
- // localStorage 載入 LIFF profile
488
- var loadLiffProfileFromStorage = function() {
484
+ // 儲存 userInfo localStorage
485
+ var saveUserInfoToStorage = function(info) {
486
+ try {
487
+ window.localStorage.setItem(userInfoStorageKey, JSON.stringify(info));
488
+ } catch (err) {
489
+ console.error("儲存 userInfo 失敗:", err);
490
+ }
491
+ };
492
+ // 從 localStorage 載入 LIFF userInfo
493
+ var loadLiffUserInfoFromStorage = function() {
489
494
  try {
490
- var stored = window.localStorage.getItem(liffProfileStorageKey);
495
+ var stored = window.localStorage.getItem(liffUserInfoStorageKey);
491
496
  if (stored) {
492
497
  var parsed = JSON.parse(stored);
493
498
  setLiffProfile(parsed);
494
499
  return parsed;
495
500
  }
496
501
  } catch (err) {
497
- console.error("載入 liffProfile 失敗:", err);
502
+ console.error("載入 liffUserInfo 失敗:", err);
498
503
  }
499
504
  return null;
500
505
  };
501
- // 儲存 userInfo 到 localStorage
502
- var saveUserInfoToStorage = React.useCallback(function(info) {
503
- try {
504
- window.localStorage.setItem(userInfoStorageKey, JSON.stringify(info));
505
- } catch (err) {
506
- console.error("儲存 userInfo 失敗:", err);
507
- }
508
- }, [
509
- userInfoStorageKey
510
- ]);
511
- // 儲存 LIFF profile 到 localStorage
512
- var saveLiffProfileToStorage = React.useCallback(function(info) {
506
+ // 儲存 LIFF userInfo 到 localStorage
507
+ var saveLiffUserInfoToStorage = function(info) {
513
508
  try {
514
- window.localStorage.setItem(liffProfileStorageKey, JSON.stringify(info));
509
+ window.localStorage.setItem(liffUserInfoStorageKey, JSON.stringify(info));
515
510
  } catch (err) {
516
- console.error("儲存 liffProfile 失敗:", err);
511
+ console.error("儲存 liffUserInfo 失敗:", err);
517
512
  }
518
- }, [
519
- liffProfileStorageKey
520
- ]);
521
- // 儲存 liffInitialized flag 到 localStorage
522
- var saveLiffInitializedToStorage = React.useCallback(function() {
523
- try {
524
- window.localStorage.setItem(liffInitializedStorageKey, "1");
525
- } catch (err) {
526
- console.error("儲存 liffInitialized 失敗:", err);
527
- }
528
- }, [
529
- liffInitializedStorageKey
530
- ]);
513
+ };
531
514
  var fetchMemberInfo = function() {
532
515
  var _ref = _async_to_generator(function(token) {
533
516
  var response, err;
@@ -636,13 +619,6 @@ var LiffProvider = function(param) {
636
619
  // token 無效時清除 userInfo
637
620
  setUserInfo(null);
638
621
  window.localStorage.removeItem(userInfoStorageKey);
639
- // token 無效也清除 liff 相關快取與已初始化標記
640
- try {
641
- window.localStorage.removeItem(liffProfileStorageKey);
642
- window.localStorage.removeItem(liffInitializedStorageKey);
643
- } catch (e) {
644
- // ignore
645
- }
646
622
  return [
647
623
  3,
648
624
  7
@@ -752,7 +728,7 @@ var LiffProvider = function(param) {
752
728
  // 初始化 LIFF
753
729
  var initializeLiff = function() {
754
730
  var _ref = _async_to_generator(function(isValid) {
755
- var _sdk_getAccessToken, _sdk_isLoggedIn, imported, sdk, lineToken, redirectUri, tempFriendship, friendship, friendFlag, error, err;
731
+ var customFetch, liff, originalFetch, profile, lineToken, redirectUri, tempFriendship, friendship, friendFlag, error, err;
756
732
  return _ts_generator(this, function(_state) {
757
733
  switch(_state.label){
758
734
  case 0:
@@ -762,27 +738,48 @@ var LiffProvider = function(param) {
762
738
  ,
763
739
  11
764
740
  ]);
765
- // 確保 LIFF 已初始化(只執行一次)
741
+ customFetch = // 自訂 fetch 函數,避免每次請求都改變 URL
742
+ // eslint-disable-next-line no-inner-declarations
743
+ function customFetch(url, options) {
744
+ if (url.toString().startsWith("https://liffsdk.line-scdn.net/xlt/") && url.toString().endsWith(".json")) {
745
+ url = "".concat(url, "?ts=").concat(Math.random());
746
+ }
747
+ return originalFetch(url, options);
748
+ };
766
749
  return [
767
750
  4,
768
- ensureLiffInitialized()
751
+ Promise.resolve().then(function () { return require('./index.cjs3.js'); })
769
752
  ];
770
753
  case 1:
771
- _state.sent();
754
+ liff = _state.sent().liff;
755
+ originalFetch = window.fetch;
756
+ window.fetch = customFetch;
757
+ if (openInApp && !(liff === null || liff === void 0 ? void 0 : liff.isInClient()) && (liff === null || liff === void 0 ? void 0 : liff.getOS()) !== "web") window.location.href = "line://app/".concat(liffId);
772
758
  return [
773
759
  4,
774
- Promise.resolve().then(function () { return require('./index.cjs3.js'); })
760
+ liff.init({
761
+ liffId: liffId,
762
+ withLoginOnExternalBrowser: false
763
+ })
775
764
  ];
776
765
  case 2:
777
- imported = _state.sent();
778
- sdk = liffObject || imported.liff;
779
- setLiffObject(sdk);
780
- lineToken = sdk === null || sdk === void 0 ? void 0 : (_sdk_getAccessToken = sdk.getAccessToken) === null || _sdk_getAccessToken === void 0 ? void 0 : _sdk_getAccessToken.call(sdk);
781
- // 未登入先進行登入(呼叫 login 後立即 return,避免後續執行)
782
- if (!(sdk === null || sdk === void 0 ? void 0 : (_sdk_isLoggedIn = sdk.isLoggedIn) === null || _sdk_isLoggedIn === void 0 ? void 0 : _sdk_isLoggedIn.call(sdk))) {
766
+ _state.sent();
767
+ console.log("LIFF init succeeded.");
768
+ setLiffObject(liff);
769
+ return [
770
+ 4,
771
+ liff.getProfile()
772
+ ];
773
+ case 3:
774
+ profile = _state.sent();
775
+ setLiffProfile(profile);
776
+ saveLiffUserInfoToStorage(profile);
777
+ lineToken = liff === null || liff === void 0 ? void 0 : liff.getAccessToken();
778
+ // 未登入先進行登入
779
+ if (!liff.isLoggedIn()) {
783
780
  redirectUri = customerRedirectUrl || "".concat(window.location.href);
784
781
  if (liffLogin) {
785
- sdk.login({
782
+ liff.login({
786
783
  redirectUri: redirectUri
787
784
  });
788
785
  return [
@@ -792,42 +789,33 @@ var LiffProvider = function(param) {
792
789
  if (loginByUser || ignoreRoute) return [
793
790
  2
794
791
  ];
795
- sdk.login({
792
+ liff.login({
796
793
  redirectUri: redirectUri
797
794
  });
798
- return [
799
- 2
800
- ];
801
795
  }
802
796
  tempFriendship = null;
803
797
  if (!lineToken) return [
804
798
  3,
805
799
  9
806
800
  ];
807
- _state.label = 3;
808
- case 3:
801
+ _state.label = 4;
802
+ case 4:
809
803
  _state.trys.push([
810
- 3,
804
+ 4,
811
805
  6,
812
806
  ,
813
807
  7
814
808
  ]);
815
- if (!(sdk.isLoggedIn && sdk.isLoggedIn())) return [
816
- 3,
817
- 5
818
- ];
819
809
  return [
820
810
  4,
821
- sdk.getFriendship()
811
+ liff.getFriendship()
822
812
  ];
823
- case 4:
813
+ case 5:
824
814
  friendship = _state.sent();
825
815
  friendFlag = friendship === null || friendship === void 0 ? void 0 : friendship.friendFlag;
826
816
  setFriendship(friendFlag);
827
817
  tempFriendship = friendFlag;
828
818
  console.log("isFriendship", friendFlag);
829
- _state.label = 5;
830
- case 5:
831
819
  return [
832
820
  3,
833
821
  7
@@ -875,125 +863,6 @@ var LiffProvider = function(param) {
875
863
  return _ref.apply(this, arguments);
876
864
  };
877
865
  }();
878
- // 確保 LIFF init 只執行一次且在需要時才執行
879
- var ensureLiffInitialized = React.useCallback(/*#__PURE__*/ _async_to_generator(function() {
880
- var storedFlag, customFetch, liff, originalFetch, profile, e1;
881
- return _ts_generator(this, function(_state) {
882
- switch(_state.label){
883
- case 0:
884
- console.log("ensureLiffInitialized called", liffInitialized);
885
- // 如果 state 或 localStorage 上已有 initialized 的標記,就視為已初始化
886
- if (liffInitialized) return [
887
- 2
888
- ];
889
- try {
890
- storedFlag = window.localStorage.getItem(liffInitializedStorageKey);
891
- if (storedFlag === "1") {
892
- setLiffInitialized(true);
893
- console.log("liffInitialized loaded from storage");
894
- return [
895
- 2
896
- ];
897
- }
898
- } catch (e) {
899
- // ignore
900
- }
901
- if (!liffId) {
902
- console.warn("liffId 未提供,跳過 liff.init()。");
903
- return [
904
- 2
905
- ];
906
- }
907
- _state.label = 1;
908
- case 1:
909
- _state.trys.push([
910
- 1,
911
- 8,
912
- ,
913
- 9
914
- ]);
915
- customFetch = // 自訂 fetch 函數,避免每次請求都改變 URL
916
- // eslint-disable-next-line no-inner-declarations
917
- function customFetch(url, options) {
918
- if (url.toString().startsWith("https://liffsdk.line-scdn.net/xlt/") && url.toString().endsWith(".json")) {
919
- url = "".concat(url, "?ts=").concat(Math.random());
920
- }
921
- return originalFetch(url, options);
922
- };
923
- return [
924
- 4,
925
- Promise.resolve().then(function () { return require('./index.cjs3.js'); })
926
- ];
927
- case 2:
928
- liff = _state.sent().liff;
929
- originalFetch = window.fetch;
930
- window.fetch = customFetch;
931
- if (openInApp && !(liff === null || liff === void 0 ? void 0 : liff.isInClient()) && (liff === null || liff === void 0 ? void 0 : liff.getOS()) !== "web") window.location.href = "line://app/".concat(liffId);
932
- return [
933
- 4,
934
- liff.init({
935
- liffId: liffId,
936
- withLoginOnExternalBrowser: false
937
- })
938
- ];
939
- case 3:
940
- _state.sent();
941
- _state.label = 4;
942
- case 4:
943
- _state.trys.push([
944
- 4,
945
- 6,
946
- ,
947
- 7
948
- ]);
949
- return [
950
- 4,
951
- liff.getProfile()
952
- ];
953
- case 5:
954
- profile = _state.sent();
955
- setLiffProfile(profile);
956
- saveLiffProfileToStorage(profile);
957
- return [
958
- 3,
959
- 7
960
- ];
961
- case 6:
962
- _state.sent();
963
- return [
964
- 3,
965
- 7
966
- ];
967
- case 7:
968
- setLiffObject(liff);
969
- setLiffInitialized(true);
970
- saveLiffInitializedToStorage();
971
- console.log("LIFF initialized.");
972
- return [
973
- 3,
974
- 9
975
- ];
976
- case 8:
977
- e1 = _state.sent();
978
- console.error("ensureLiffInitialized failed:", e1);
979
- return [
980
- 3,
981
- 9
982
- ];
983
- case 9:
984
- return [
985
- 2
986
- ];
987
- }
988
- });
989
- }), [
990
- liffInitialized,
991
- liffId,
992
- openInApp,
993
- saveLiffInitializedToStorage,
994
- saveLiffProfileToStorage,
995
- liffInitializedStorageKey
996
- ]);
997
866
  var accountLogin = function() {
998
867
  var _ref = _async_to_generator(function(username, password) {
999
868
  var response, token, tokenType, finalToken, err;
@@ -1160,10 +1029,10 @@ var LiffProvider = function(param) {
1160
1029
  return trimmed.toLowerCase().startsWith("bearer ") ? trimmed : "Bearer ".concat(trimmed);
1161
1030
  };
1162
1031
  var searchParams = typeof window !== "undefined" ? new URLSearchParams(window.location.search) : null;
1163
- // 初始載入時從 localStorage 載入 userInfo 與 LIFF profile
1032
+ // 初始載入時從 localStorage 載入 userInfo
1164
1033
  React.useEffect(function() {
1165
1034
  loadUserInfoFromStorage();
1166
- loadLiffProfileFromStorage();
1035
+ loadLiffUserInfoFromStorage();
1167
1036
  }, []);
1168
1037
  // 客戶端初始化 LIFF
1169
1038
  React.useEffect(function() {
@@ -1241,21 +1110,34 @@ var LiffProvider = function(param) {
1241
1110
  }();
1242
1111
  checkAndInitialize();
1243
1112
  }, []);
1244
- var contextValue = {
1245
- liffObject: liffObject,
1246
- loginData: loginData,
1247
- userInfo: userInfo,
1248
- accessToken: accessToken,
1249
- liffError: liffError,
1250
- initializeLiff: function() {
1251
- return initializeLiff(false);
1252
- },
1253
- liffProfile: liffProfile,
1254
- isFriendship: isFriendship,
1255
- ensureLiffTokenValid: ensureLiffTokenValid,
1256
- accountLogin: accountLogin,
1257
- loginType: loginType
1258
- };
1113
+ var contextValue = React.useMemo(function() {
1114
+ return {
1115
+ liffObject: liffObject,
1116
+ loginData: loginData,
1117
+ userInfo: userInfo,
1118
+ accessToken: accessToken,
1119
+ liffError: liffError,
1120
+ initializeLiff: function() {
1121
+ return initializeLiff(false);
1122
+ },
1123
+ isFriendship: isFriendship,
1124
+ ensureLiffTokenValid: ensureLiffTokenValid,
1125
+ accountLogin: accountLogin,
1126
+ loginType: loginType,
1127
+ liffProfile: liffProfile
1128
+ };
1129
+ }, [
1130
+ liffObject,
1131
+ loginData,
1132
+ userInfo,
1133
+ accessToken,
1134
+ liffError,
1135
+ isFriendship,
1136
+ ensureLiffTokenValid,
1137
+ accountLogin,
1138
+ loginType,
1139
+ liffProfile
1140
+ ]);
1259
1141
  return /*#__PURE__*/ jsxRuntime.jsx(LiffContext.Provider, {
1260
1142
  value: contextValue,
1261
1143
  children: children
@@ -1,5 +1,5 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
- import { createContext, useState, useCallback, useEffect, useContext } from 'react';
2
+ import { createContext, useState, useCallback, useEffect, useMemo, useContext } from 'react';
3
3
 
4
4
  function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
5
5
  try {
@@ -452,20 +452,17 @@ var LiffContext = /*#__PURE__*/ createContext(undefined);
452
452
  var LiffProvider = function(param) {
453
453
  var children = param.children, liffId = param.liffId, _param_localStorageKey = param.localStorageKey, localStorageKey = _param_localStorageKey === void 0 ? "one-gobo-global-login" : _param_localStorageKey, checkTokenUrl = param.checkTokenUrl, loginUrl = param.loginUrl, accountLoginUrl = param.accountLoginUrl, memberInfoUrl = param.memberInfoUrl, _param_openInApp = param.openInApp, openInApp = _param_openInApp === void 0 ? false : _param_openInApp, _param_ignoreCheckAddFriend = param.ignoreCheckAddFriend, ignoreCheckAddFriend = _param_ignoreCheckAddFriend === void 0 ? false : _param_ignoreCheckAddFriend, _param_ignoreRoute = param.ignoreRoute, ignoreRoute = _param_ignoreRoute === void 0 ? false : _param_ignoreRoute, _param_loginByUser = param.loginByUser, loginByUser = _param_loginByUser === void 0 ? false : _param_loginByUser, _param_liffLogin = param.liffLogin, liffLogin = _param_liffLogin === void 0 ? false : _param_liffLogin, customerRedirectUrl = param.customerRedirectUrl, inviteCode = param.inviteCode, handleLoginError = param.handleLoginError;
454
454
  var _useState = _sliced_to_array(useState(null), 2), liffObject = _useState[0], setLiffObject = _useState[1];
455
- var _useState1 = _sliced_to_array(useState(false), 2), liffInitialized = _useState1[0], setLiffInitialized = _useState1[1];
456
- var _useState2 = _sliced_to_array(useState(""), 2), accessToken = _useState2[0], setAccessToken = _useState2[1];
457
- var _useState3 = _sliced_to_array(useState(null), 2), liffError = _useState3[0], setLiffError = _useState3[1];
458
- var _useState4 = _sliced_to_array(useState(null), 2), loginData = _useState4[0], setLoginData = _useState4[1];
459
- var _useState5 = _sliced_to_array(useState(null), 2), userInfo = _useState5[0], setUserInfo = _useState5[1];
460
- var _useState6 = _sliced_to_array(useState(null), 2), liffProfile = _useState6[0], setLiffProfile = _useState6[1];
461
- var _useState7 = _sliced_to_array(useState(null), 2), isFriendship = _useState7[0], setFriendship = _useState7[1];
462
- var _useState8 = _sliced_to_array(useState(null), 2), loginType = _useState8[0], setLoginType = _useState8[1];
455
+ var _useState1 = _sliced_to_array(useState(""), 2), accessToken = _useState1[0], setAccessToken = _useState1[1];
456
+ var _useState2 = _sliced_to_array(useState(null), 2), liffError = _useState2[0], setLiffError = _useState2[1];
457
+ var _useState3 = _sliced_to_array(useState(null), 2), loginData = _useState3[0], setLoginData = _useState3[1];
458
+ var _useState4 = _sliced_to_array(useState(null), 2), userInfo = _useState4[0], setUserInfo = _useState4[1];
459
+ var _useState5 = _sliced_to_array(useState(null), 2), liffProfile = _useState5[0], setLiffProfile = _useState5[1];
460
+ var _useState6 = _sliced_to_array(useState(null), 2), isFriendship = _useState6[0], setFriendship = _useState6[1];
461
+ var _useState7 = _sliced_to_array(useState(null), 2), loginType = _useState7[0], setLoginType = _useState7[1];
463
462
  // userInfo 的 localStorage key
464
463
  var userInfoStorageKey = "".concat(localStorageKey, "-userInfo");
465
- // liff profile 的 localStorage key
466
- var liffProfileStorageKey = "".concat(localStorageKey, "-liffProfile");
467
- // liff initialized flag 的 localStorage key
468
- var liffInitializedStorageKey = "".concat(localStorageKey, "-liffInitialized");
464
+ // liff userInfo 的 localStorage key
465
+ var liffUserInfoStorageKey = "".concat(localStorageKey, "-liffUserInfo");
469
466
  // 從 localStorage 載入 userInfo
470
467
  var loadUserInfoFromStorage = function() {
471
468
  try {
@@ -480,50 +477,36 @@ var LiffProvider = function(param) {
480
477
  }
481
478
  return null;
482
479
  };
483
- // localStorage 載入 LIFF profile
484
- var loadLiffProfileFromStorage = function() {
480
+ // 儲存 userInfo localStorage
481
+ var saveUserInfoToStorage = function(info) {
482
+ try {
483
+ window.localStorage.setItem(userInfoStorageKey, JSON.stringify(info));
484
+ } catch (err) {
485
+ console.error("儲存 userInfo 失敗:", err);
486
+ }
487
+ };
488
+ // 從 localStorage 載入 LIFF userInfo
489
+ var loadLiffUserInfoFromStorage = function() {
485
490
  try {
486
- var stored = window.localStorage.getItem(liffProfileStorageKey);
491
+ var stored = window.localStorage.getItem(liffUserInfoStorageKey);
487
492
  if (stored) {
488
493
  var parsed = JSON.parse(stored);
489
494
  setLiffProfile(parsed);
490
495
  return parsed;
491
496
  }
492
497
  } catch (err) {
493
- console.error("載入 liffProfile 失敗:", err);
498
+ console.error("載入 liffUserInfo 失敗:", err);
494
499
  }
495
500
  return null;
496
501
  };
497
- // 儲存 userInfo 到 localStorage
498
- var saveUserInfoToStorage = useCallback(function(info) {
499
- try {
500
- window.localStorage.setItem(userInfoStorageKey, JSON.stringify(info));
501
- } catch (err) {
502
- console.error("儲存 userInfo 失敗:", err);
503
- }
504
- }, [
505
- userInfoStorageKey
506
- ]);
507
- // 儲存 LIFF profile 到 localStorage
508
- var saveLiffProfileToStorage = useCallback(function(info) {
502
+ // 儲存 LIFF userInfo 到 localStorage
503
+ var saveLiffUserInfoToStorage = function(info) {
509
504
  try {
510
- window.localStorage.setItem(liffProfileStorageKey, JSON.stringify(info));
505
+ window.localStorage.setItem(liffUserInfoStorageKey, JSON.stringify(info));
511
506
  } catch (err) {
512
- console.error("儲存 liffProfile 失敗:", err);
507
+ console.error("儲存 liffUserInfo 失敗:", err);
513
508
  }
514
- }, [
515
- liffProfileStorageKey
516
- ]);
517
- // 儲存 liffInitialized flag 到 localStorage
518
- var saveLiffInitializedToStorage = useCallback(function() {
519
- try {
520
- window.localStorage.setItem(liffInitializedStorageKey, "1");
521
- } catch (err) {
522
- console.error("儲存 liffInitialized 失敗:", err);
523
- }
524
- }, [
525
- liffInitializedStorageKey
526
- ]);
509
+ };
527
510
  var fetchMemberInfo = function() {
528
511
  var _ref = _async_to_generator(function(token) {
529
512
  var response, err;
@@ -632,13 +615,6 @@ var LiffProvider = function(param) {
632
615
  // token 無效時清除 userInfo
633
616
  setUserInfo(null);
634
617
  window.localStorage.removeItem(userInfoStorageKey);
635
- // token 無效也清除 liff 相關快取與已初始化標記
636
- try {
637
- window.localStorage.removeItem(liffProfileStorageKey);
638
- window.localStorage.removeItem(liffInitializedStorageKey);
639
- } catch (e) {
640
- // ignore
641
- }
642
618
  return [
643
619
  3,
644
620
  7
@@ -748,7 +724,7 @@ var LiffProvider = function(param) {
748
724
  // 初始化 LIFF
749
725
  var initializeLiff = function() {
750
726
  var _ref = _async_to_generator(function(isValid) {
751
- var _sdk_getAccessToken, _sdk_isLoggedIn, imported, sdk, lineToken, redirectUri, tempFriendship, friendship, friendFlag, error, err;
727
+ var customFetch, liff, originalFetch, profile, lineToken, redirectUri, tempFriendship, friendship, friendFlag, error, err;
752
728
  return _ts_generator(this, function(_state) {
753
729
  switch(_state.label){
754
730
  case 0:
@@ -758,27 +734,48 @@ var LiffProvider = function(param) {
758
734
  ,
759
735
  11
760
736
  ]);
761
- // 確保 LIFF 已初始化(只執行一次)
737
+ customFetch = // 自訂 fetch 函數,避免每次請求都改變 URL
738
+ // eslint-disable-next-line no-inner-declarations
739
+ function customFetch(url, options) {
740
+ if (url.toString().startsWith("https://liffsdk.line-scdn.net/xlt/") && url.toString().endsWith(".json")) {
741
+ url = "".concat(url, "?ts=").concat(Math.random());
742
+ }
743
+ return originalFetch(url, options);
744
+ };
762
745
  return [
763
746
  4,
764
- ensureLiffInitialized()
747
+ import('./index.esm3.js')
765
748
  ];
766
749
  case 1:
767
- _state.sent();
750
+ liff = _state.sent().liff;
751
+ originalFetch = window.fetch;
752
+ window.fetch = customFetch;
753
+ if (openInApp && !(liff === null || liff === void 0 ? void 0 : liff.isInClient()) && (liff === null || liff === void 0 ? void 0 : liff.getOS()) !== "web") window.location.href = "line://app/".concat(liffId);
768
754
  return [
769
755
  4,
770
- import('./index.esm3.js')
756
+ liff.init({
757
+ liffId: liffId,
758
+ withLoginOnExternalBrowser: false
759
+ })
771
760
  ];
772
761
  case 2:
773
- imported = _state.sent();
774
- sdk = liffObject || imported.liff;
775
- setLiffObject(sdk);
776
- lineToken = sdk === null || sdk === void 0 ? void 0 : (_sdk_getAccessToken = sdk.getAccessToken) === null || _sdk_getAccessToken === void 0 ? void 0 : _sdk_getAccessToken.call(sdk);
777
- // 未登入先進行登入(呼叫 login 後立即 return,避免後續執行)
778
- if (!(sdk === null || sdk === void 0 ? void 0 : (_sdk_isLoggedIn = sdk.isLoggedIn) === null || _sdk_isLoggedIn === void 0 ? void 0 : _sdk_isLoggedIn.call(sdk))) {
762
+ _state.sent();
763
+ console.log("LIFF init succeeded.");
764
+ setLiffObject(liff);
765
+ return [
766
+ 4,
767
+ liff.getProfile()
768
+ ];
769
+ case 3:
770
+ profile = _state.sent();
771
+ setLiffProfile(profile);
772
+ saveLiffUserInfoToStorage(profile);
773
+ lineToken = liff === null || liff === void 0 ? void 0 : liff.getAccessToken();
774
+ // 未登入先進行登入
775
+ if (!liff.isLoggedIn()) {
779
776
  redirectUri = customerRedirectUrl || "".concat(window.location.href);
780
777
  if (liffLogin) {
781
- sdk.login({
778
+ liff.login({
782
779
  redirectUri: redirectUri
783
780
  });
784
781
  return [
@@ -788,42 +785,33 @@ var LiffProvider = function(param) {
788
785
  if (loginByUser || ignoreRoute) return [
789
786
  2
790
787
  ];
791
- sdk.login({
788
+ liff.login({
792
789
  redirectUri: redirectUri
793
790
  });
794
- return [
795
- 2
796
- ];
797
791
  }
798
792
  tempFriendship = null;
799
793
  if (!lineToken) return [
800
794
  3,
801
795
  9
802
796
  ];
803
- _state.label = 3;
804
- case 3:
797
+ _state.label = 4;
798
+ case 4:
805
799
  _state.trys.push([
806
- 3,
800
+ 4,
807
801
  6,
808
802
  ,
809
803
  7
810
804
  ]);
811
- if (!(sdk.isLoggedIn && sdk.isLoggedIn())) return [
812
- 3,
813
- 5
814
- ];
815
805
  return [
816
806
  4,
817
- sdk.getFriendship()
807
+ liff.getFriendship()
818
808
  ];
819
- case 4:
809
+ case 5:
820
810
  friendship = _state.sent();
821
811
  friendFlag = friendship === null || friendship === void 0 ? void 0 : friendship.friendFlag;
822
812
  setFriendship(friendFlag);
823
813
  tempFriendship = friendFlag;
824
814
  console.log("isFriendship", friendFlag);
825
- _state.label = 5;
826
- case 5:
827
815
  return [
828
816
  3,
829
817
  7
@@ -871,125 +859,6 @@ var LiffProvider = function(param) {
871
859
  return _ref.apply(this, arguments);
872
860
  };
873
861
  }();
874
- // 確保 LIFF init 只執行一次且在需要時才執行
875
- var ensureLiffInitialized = useCallback(/*#__PURE__*/ _async_to_generator(function() {
876
- var storedFlag, customFetch, liff, originalFetch, profile, e1;
877
- return _ts_generator(this, function(_state) {
878
- switch(_state.label){
879
- case 0:
880
- console.log("ensureLiffInitialized called", liffInitialized);
881
- // 如果 state 或 localStorage 上已有 initialized 的標記,就視為已初始化
882
- if (liffInitialized) return [
883
- 2
884
- ];
885
- try {
886
- storedFlag = window.localStorage.getItem(liffInitializedStorageKey);
887
- if (storedFlag === "1") {
888
- setLiffInitialized(true);
889
- console.log("liffInitialized loaded from storage");
890
- return [
891
- 2
892
- ];
893
- }
894
- } catch (e) {
895
- // ignore
896
- }
897
- if (!liffId) {
898
- console.warn("liffId 未提供,跳過 liff.init()。");
899
- return [
900
- 2
901
- ];
902
- }
903
- _state.label = 1;
904
- case 1:
905
- _state.trys.push([
906
- 1,
907
- 8,
908
- ,
909
- 9
910
- ]);
911
- customFetch = // 自訂 fetch 函數,避免每次請求都改變 URL
912
- // eslint-disable-next-line no-inner-declarations
913
- function customFetch(url, options) {
914
- if (url.toString().startsWith("https://liffsdk.line-scdn.net/xlt/") && url.toString().endsWith(".json")) {
915
- url = "".concat(url, "?ts=").concat(Math.random());
916
- }
917
- return originalFetch(url, options);
918
- };
919
- return [
920
- 4,
921
- import('./index.esm3.js')
922
- ];
923
- case 2:
924
- liff = _state.sent().liff;
925
- originalFetch = window.fetch;
926
- window.fetch = customFetch;
927
- if (openInApp && !(liff === null || liff === void 0 ? void 0 : liff.isInClient()) && (liff === null || liff === void 0 ? void 0 : liff.getOS()) !== "web") window.location.href = "line://app/".concat(liffId);
928
- return [
929
- 4,
930
- liff.init({
931
- liffId: liffId,
932
- withLoginOnExternalBrowser: false
933
- })
934
- ];
935
- case 3:
936
- _state.sent();
937
- _state.label = 4;
938
- case 4:
939
- _state.trys.push([
940
- 4,
941
- 6,
942
- ,
943
- 7
944
- ]);
945
- return [
946
- 4,
947
- liff.getProfile()
948
- ];
949
- case 5:
950
- profile = _state.sent();
951
- setLiffProfile(profile);
952
- saveLiffProfileToStorage(profile);
953
- return [
954
- 3,
955
- 7
956
- ];
957
- case 6:
958
- _state.sent();
959
- return [
960
- 3,
961
- 7
962
- ];
963
- case 7:
964
- setLiffObject(liff);
965
- setLiffInitialized(true);
966
- saveLiffInitializedToStorage();
967
- console.log("LIFF initialized.");
968
- return [
969
- 3,
970
- 9
971
- ];
972
- case 8:
973
- e1 = _state.sent();
974
- console.error("ensureLiffInitialized failed:", e1);
975
- return [
976
- 3,
977
- 9
978
- ];
979
- case 9:
980
- return [
981
- 2
982
- ];
983
- }
984
- });
985
- }), [
986
- liffInitialized,
987
- liffId,
988
- openInApp,
989
- saveLiffInitializedToStorage,
990
- saveLiffProfileToStorage,
991
- liffInitializedStorageKey
992
- ]);
993
862
  var accountLogin = function() {
994
863
  var _ref = _async_to_generator(function(username, password) {
995
864
  var response, token, tokenType, finalToken, err;
@@ -1156,10 +1025,10 @@ var LiffProvider = function(param) {
1156
1025
  return trimmed.toLowerCase().startsWith("bearer ") ? trimmed : "Bearer ".concat(trimmed);
1157
1026
  };
1158
1027
  var searchParams = typeof window !== "undefined" ? new URLSearchParams(window.location.search) : null;
1159
- // 初始載入時從 localStorage 載入 userInfo 與 LIFF profile
1028
+ // 初始載入時從 localStorage 載入 userInfo
1160
1029
  useEffect(function() {
1161
1030
  loadUserInfoFromStorage();
1162
- loadLiffProfileFromStorage();
1031
+ loadLiffUserInfoFromStorage();
1163
1032
  }, []);
1164
1033
  // 客戶端初始化 LIFF
1165
1034
  useEffect(function() {
@@ -1237,21 +1106,34 @@ var LiffProvider = function(param) {
1237
1106
  }();
1238
1107
  checkAndInitialize();
1239
1108
  }, []);
1240
- var contextValue = {
1241
- liffObject: liffObject,
1242
- loginData: loginData,
1243
- userInfo: userInfo,
1244
- accessToken: accessToken,
1245
- liffError: liffError,
1246
- initializeLiff: function() {
1247
- return initializeLiff(false);
1248
- },
1249
- liffProfile: liffProfile,
1250
- isFriendship: isFriendship,
1251
- ensureLiffTokenValid: ensureLiffTokenValid,
1252
- accountLogin: accountLogin,
1253
- loginType: loginType
1254
- };
1109
+ var contextValue = useMemo(function() {
1110
+ return {
1111
+ liffObject: liffObject,
1112
+ loginData: loginData,
1113
+ userInfo: userInfo,
1114
+ accessToken: accessToken,
1115
+ liffError: liffError,
1116
+ initializeLiff: function() {
1117
+ return initializeLiff(false);
1118
+ },
1119
+ isFriendship: isFriendship,
1120
+ ensureLiffTokenValid: ensureLiffTokenValid,
1121
+ accountLogin: accountLogin,
1122
+ loginType: loginType,
1123
+ liffProfile: liffProfile
1124
+ };
1125
+ }, [
1126
+ liffObject,
1127
+ loginData,
1128
+ userInfo,
1129
+ accessToken,
1130
+ liffError,
1131
+ isFriendship,
1132
+ ensureLiffTokenValid,
1133
+ accountLogin,
1134
+ loginType,
1135
+ liffProfile
1136
+ ]);
1255
1137
  return /*#__PURE__*/ jsx(LiffContext.Provider, {
1256
1138
  value: contextValue,
1257
1139
  children: children
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sunggang/ui-lib",
3
- "version": "0.4.45",
3
+ "version": "0.4.47",
4
4
  "sideEffects": [
5
5
  "*.css",
6
6
  "./src/style.css",
@@ -3,7 +3,6 @@ type LiffContextType = {
3
3
  liffObject: any;
4
4
  loginData: any;
5
5
  userInfo?: any;
6
- liffProfile?: any;
7
6
  accessToken: string;
8
7
  liffError: string | null;
9
8
  initializeLiff: () => Promise<void>;