@edifice.io/react 2.2.5-develop-b2school.20250507154133 → 2.2.5-develop-b2school.20250512143355

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.
@@ -11,4 +11,5 @@ export type CoreDate = IsoDate | MongoDate | NumberDate;
11
11
  export default function useDate(): {
12
12
  fromNow: (date: CoreDate | NumberDate) => string;
13
13
  formatDate: (date: CoreDate, format?: string) => string;
14
+ formatTimeAgo: (date: CoreDate | NumberDate) => string;
14
15
  };
@@ -9,13 +9,16 @@ import "dayjs/locale/fr.js";
9
9
  import "dayjs/locale/it.js";
10
10
  import "dayjs/locale/pt.js";
11
11
  import { useEdificeClient } from "../../providers/EdificeClientProvider/EdificeClientProvider.hook.js";
12
+ import { useTranslation } from "react-i18next";
12
13
  dayjs.extend(relativeTime);
13
14
  dayjs.extend(customParseFormat);
14
15
  dayjs.extend(localizedFormat);
15
16
  function useDate() {
16
17
  const {
17
18
  currentLanguage
18
- } = useEdificeClient(), parseDate = useCallback((date, lang) => {
19
+ } = useEdificeClient(), {
20
+ t
21
+ } = useTranslation(), parseDate = useCallback((date, lang) => {
19
22
  if (date.length < 11) return dayjs(date, ["YYYY-MM-DD"], lang);
20
23
  if (date.split("").findIndex((char) => "0" > char || char > "9") < 0)
21
24
  return dayjs(Number.parseInt(date)).locale(currentLanguage);
@@ -23,41 +26,44 @@ function useDate() {
23
26
  let day = dayjs(date).locale(currentLanguage);
24
27
  return day.isValid() || (day = dayjs(date, ["YYYY-MM-DD HH:mm:ss.SSS"]).locale(currentLanguage)), day;
25
28
  }
26
- }, [currentLanguage]), fromNow = useCallback((date) => {
29
+ }, [currentLanguage]), toComputedDate = useCallback((date) => {
27
30
  let computedDate = dayjs();
28
31
  try {
29
- return typeof date > "u" ? "" : (typeof date == "string" ? computedDate = parseDate(date) : typeof date == "number" ? computedDate = dayjs(date).locale(currentLanguage) : typeof date.$date == "number" ? computedDate = dayjs(new Date(date.$date)).locale(currentLanguage) : typeof date.$date == "string" && (computedDate = parseDate(date.$date)), computedDate.isValid() ? computedDate.fromNow() : "");
32
+ return typeof date > "u" ? void 0 : (typeof date == "string" ? computedDate = parseDate(date) : typeof date == "number" ? computedDate = dayjs(date).locale(currentLanguage) : typeof date.$date == "number" ? computedDate = dayjs(new Date(date.$date)).locale(currentLanguage) : typeof date.$date == "string" && (computedDate = parseDate(date.$date)), computedDate);
30
33
  } catch (error) {
31
- return console.error(error), "";
34
+ console.error(error);
32
35
  }
36
+ return computedDate;
37
+ }, [currentLanguage, parseDate]), formatTimeAgo = useCallback((date) => {
38
+ const computedDate = toComputedDate(date);
39
+ if (!(computedDate != null && computedDate.isValid())) return "";
40
+ const now = dayjs();
41
+ return computedDate.isSame(now, "date") ? now.diff(computedDate, "hours") <= 3 ? computedDate.fromNow() : computedDate.format(t("date.format.currentDay")) : computedDate.isSame(now.subtract(1, "day"), "date") ? t("date.format.yesterday") : now.diff(computedDate, "days") <= 7 ? computedDate.format(t("date.format.currentWeek")) : computedDate.isSame(now, "year") ? computedDate.format(t("date.format.currentYear")) : computedDate.format(t("date.format.previousYear"));
42
+ }, [currentLanguage, parseDate]), fromNow = useCallback((date) => {
43
+ const computedDate = toComputedDate(date);
44
+ return computedDate != null && computedDate.isValid() ? computedDate.fromNow() : "";
33
45
  }, [currentLanguage, parseDate]), formatDate = useCallback((date, format = "short") => {
34
- let computedDate = dayjs();
35
- try {
36
- if (typeof date > "u")
37
- return "";
38
- typeof date == "string" ? computedDate = parseDate(date) : typeof date == "number" ? computedDate = dayjs(date).locale(currentLanguage) : typeof date.$date == "number" ? computedDate = dayjs(new Date(date.$date)).locale(currentLanguage) : typeof date.$date == "string" && (computedDate = parseDate(date.$date));
39
- let dayjsFormat = "";
40
- switch (format) {
41
- case "short":
42
- dayjsFormat = "L";
43
- break;
44
- case "long":
45
- dayjsFormat = "LL";
46
- break;
47
- case "abbr":
48
- dayjsFormat = "ll";
49
- break;
50
- default:
51
- dayjsFormat = format;
52
- }
53
- return computedDate.isValid() ? computedDate.locale(currentLanguage).format(dayjsFormat) : "";
54
- } catch (error) {
55
- return console.error(error), "";
46
+ const computedDate = toComputedDate(date);
47
+ let dayjsFormat = "";
48
+ switch (format) {
49
+ case "short":
50
+ dayjsFormat = "L";
51
+ break;
52
+ case "long":
53
+ dayjsFormat = "LL";
54
+ break;
55
+ case "abbr":
56
+ dayjsFormat = "ll";
57
+ break;
58
+ default:
59
+ dayjsFormat = format;
56
60
  }
61
+ return computedDate != null && computedDate.isValid() ? computedDate.locale(currentLanguage).format(dayjsFormat) : "";
57
62
  }, [currentLanguage, parseDate]);
58
63
  return {
59
64
  fromNow,
60
- formatDate
65
+ formatDate,
66
+ formatTimeAgo
61
67
  };
62
68
  }
63
69
  export {
@@ -8,10 +8,19 @@ const useHttpErrorToast = (options) => {
8
8
  } = useTranslation();
9
9
  return useEffect(() => {
10
10
  const subscription = odeServices.notify().events().subscribe(LAYER_NAME.TRANSPORT, (event) => {
11
- var _a, _b;
12
- message.current = t(((_b = (_a = event == null ? void 0 : event.data) == null ? void 0 : _a.payload) == null ? void 0 : _b.error) || event.data.response.statusText), toast.error(/* @__PURE__ */ React.createElement("div", {
11
+ var _a;
12
+ if (!(event != null && event.data)) return;
13
+ const {
14
+ response
15
+ } = event.data, i18nKey = (
16
+ // The payload may include the i18n key of the error message to show,
17
+ ((_a = event.data.payload) == null ? void 0 : _a.error) || // otherwise, try showing the translation of some known HTTP error code.
18
+ ([400, 401, 403, 404, 408, 413, 500, 504].includes(response == null ? void 0 : response.status) ? `e${event.data.response.status}` : void 0) || // otherwise try showing the statusText (may be technical, in english),
19
+ (response == null ? void 0 : response.statusText)
20
+ );
21
+ typeof i18nKey == "string" && (message.current = t(i18nKey), toast.error(/* @__PURE__ */ React.createElement("div", {
13
22
  children: [message.current]
14
- }), options);
23
+ }), options));
15
24
  });
16
25
  return () => subscription.revoke();
17
26
  }, [t, toast]), message.current;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edifice.io/react",
3
- "version": "2.2.5-develop-b2school.20250507154133",
3
+ "version": "2.2.5-develop-b2school.20250512143355",
4
4
  "description": "Edifice React Library",
5
5
  "keywords": [
6
6
  "react",
@@ -118,9 +118,9 @@
118
118
  "react-slugify": "^3.0.3",
119
119
  "swiper": "^10.1.0",
120
120
  "ua-parser-js": "^1.0.36",
121
- "@edifice.io/bootstrap": "2.2.5-develop-b2school.20250507154133",
122
- "@edifice.io/tiptap-extensions": "2.2.5-develop-b2school.20250507154133",
123
- "@edifice.io/utilities": "2.2.5-develop-b2school.20250507154133"
121
+ "@edifice.io/bootstrap": "2.2.5-develop-b2school.20250512143355",
122
+ "@edifice.io/tiptap-extensions": "2.2.5-develop-b2school.20250512143355",
123
+ "@edifice.io/utilities": "2.2.5-develop-b2school.20250512143355"
124
124
  },
125
125
  "devDependencies": {
126
126
  "@babel/plugin-transform-react-pure-annotations": "^7.23.3",
@@ -151,8 +151,8 @@
151
151
  "vite": "^5.4.11",
152
152
  "vite-plugin-dts": "^4.1.0",
153
153
  "vite-tsconfig-paths": "^5.0.1",
154
- "@edifice.io/client": "2.2.5-develop-b2school.20250507154133",
155
- "@edifice.io/config": "2.2.5-develop-b2school.20250507154133"
154
+ "@edifice.io/client": "2.2.5-develop-b2school.20250512143355",
155
+ "@edifice.io/config": "2.2.5-develop-b2school.20250512143355"
156
156
  },
157
157
  "peerDependencies": {
158
158
  "@react-spring/web": "^9.7.5",