m2m-components 0.1.1 → 0.1.2
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.
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createContext, useCallback, useContext } from "react";
|
|
2
|
+
import { useAuthFetch } from "matsuri-hooks";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { m2mNotifications_v1 } from "../endpoints/m2m-notifications.v1";
|
|
5
|
+
import { useServiceId } from "./ServiceIdProvider";
|
|
6
|
+
import React from "react";
|
|
7
|
+
const NotificationPermissionContext = createContext(undefined);
|
|
8
|
+
const NotificationsContext = createContext(undefined); // ServiceIdProviderの中で使うこと
|
|
9
|
+
|
|
10
|
+
export const NotificationsProvider = ({
|
|
11
|
+
token,
|
|
12
|
+
children
|
|
13
|
+
}) => {
|
|
14
|
+
const serviceId = useServiceId();
|
|
15
|
+
const {
|
|
16
|
+
data: subscriptions,
|
|
17
|
+
refetch: refetchSubscriptions
|
|
18
|
+
} = useAuthFetch(token, m2mNotifications_v1.findMySubscriptions(), {});
|
|
19
|
+
const checkPermission = useCallback(notificationType => {
|
|
20
|
+
const subscription = subscriptions?.find(item => {
|
|
21
|
+
return item.serviceId === serviceId && item.notificationType === notificationType && item.resourceSubscription.resourceSelectType === "any";
|
|
22
|
+
});
|
|
23
|
+
return subscription?.status;
|
|
24
|
+
}, [subscriptions]);
|
|
25
|
+
const {
|
|
26
|
+
data: notifications,
|
|
27
|
+
error,
|
|
28
|
+
refetch: refetchNotifications
|
|
29
|
+
} = useAuthFetch(token, m2mNotifications_v1.findNotificationsByServiceId({
|
|
30
|
+
id: serviceId
|
|
31
|
+
}), {
|
|
32
|
+
swrConfig: {
|
|
33
|
+
refreshInterval: 1000 * 60 * 5
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (error) {
|
|
38
|
+
// エラーが出てもユーザーは特に困らないので、ユーザーに伝える必要はないが、監視は必要。
|
|
39
|
+
console.error(error);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const permissionContextState = useMemo(() => {
|
|
43
|
+
return {
|
|
44
|
+
checkPermission,
|
|
45
|
+
refetch: refetchSubscriptions
|
|
46
|
+
};
|
|
47
|
+
}, [checkPermission, refetchSubscriptions]);
|
|
48
|
+
const notificationsContextState = useMemo(() => {
|
|
49
|
+
return {
|
|
50
|
+
notifications: notifications?.data ?? [],
|
|
51
|
+
isAllRead: notifications?.data.every(n => n.status === "read") ?? true,
|
|
52
|
+
refetch: refetchNotifications
|
|
53
|
+
};
|
|
54
|
+
}, [refetchNotifications, notifications?.data]);
|
|
55
|
+
return <NotificationPermissionContext.Provider value={permissionContextState}>
|
|
56
|
+
<NotificationsContext.Provider value={notificationsContextState}>
|
|
57
|
+
{children}
|
|
58
|
+
</NotificationsContext.Provider>
|
|
59
|
+
</NotificationPermissionContext.Provider>;
|
|
60
|
+
};
|
|
61
|
+
export const useNotifications = () => {
|
|
62
|
+
const ctx = useContext(NotificationsContext);
|
|
63
|
+
|
|
64
|
+
if (!ctx) {
|
|
65
|
+
throw new Error("NotificatoinProvider is not found");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return ctx;
|
|
69
|
+
};
|
|
70
|
+
export const useNotificationPermission = () => {
|
|
71
|
+
const ctx = useContext(NotificationPermissionContext);
|
|
72
|
+
|
|
73
|
+
if (!ctx) {
|
|
74
|
+
throw new Error("NotificatoinProvider is not found");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return ctx;
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=NotificationProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/notification/NotificationProvider.tsx"],"names":["createContext","useCallback","useContext","useAuthFetch","useMemo","m2mNotifications_v1","useServiceId","React","NotificationPermissionContext","undefined","NotificationsContext","NotificationsProvider","token","children","serviceId","data","subscriptions","refetch","refetchSubscriptions","findMySubscriptions","checkPermission","notificationType","subscription","find","item","resourceSubscription","resourceSelectType","status","notifications","error","refetchNotifications","findNotificationsByServiceId","id","swrConfig","refreshInterval","console","permissionContextState","notificationsContextState","isAllRead","every","n","useNotifications","ctx","Error","useNotificationPermission"],"mappings":"AAAA,SAASA,aAAT,EAAwBC,WAAxB,EAAqCC,UAArC,QAAuD,OAAvD;AACA,SAASC,YAAT,QAA6B,eAA7B;AACA,SAASC,OAAT,QAAwB,OAAxB;AACA,SAASC,mBAAT,QAAoC,mCAApC;AACA,SAASC,YAAT,QAA6B,qBAA7B;AAEA,OAAOC,KAAP,MAAkB,OAAlB;AAEA,MAAMC,6BAA6B,GAAGR,aAAa,CAQjDS,SARiD,CAAnD;AAUA,MAAMC,oBAAoB,GAAGV,aAAa,CAGxCS,SAHwC,CAA1C,C,CAKA;;AACA,OAAO,MAAME,qBAAkD,GAAG,CAAC;AACjEC,EAAAA,KADiE;AAEjEC,EAAAA;AAFiE,CAAD,KAG5D;AACJ,QAAMC,SAAS,GAAGR,YAAY,EAA9B;AAEA,QAAM;AAAES,IAAAA,IAAI,EAAEC,aAAR;AAAuBC,IAAAA,OAAO,EAAEC;AAAhC,MAAyDf,YAAY,CAEzES,KAFyE,EAElEP,mBAAmB,CAACc,mBAApB,EAFkE,EAEvB,EAFuB,CAA3E;AAGA,QAAMC,eAAe,GAAGnB,WAAW,CAChCoB,gBAAD,IAA8B;AAC5B,UAAMC,YAAY,GAAGN,aAAa,EAAEO,IAAf,CAAqBC,IAAD,IAAU;AACjD,aACEA,IAAI,CAACV,SAAL,KAAmBA,SAAnB,IACAU,IAAI,CAACH,gBAAL,KAA0BA,gBAD1B,IAEAG,IAAI,CAACC,oBAAL,CAA0BC,kBAA1B,KAAiD,KAHnD;AAKD,KANoB,CAArB;AAQA,WAAOJ,YAAY,EAAEK,MAArB;AACD,GAXgC,EAYjC,CAACX,aAAD,CAZiC,CAAnC;AAeA,QAAM;AACJD,IAAAA,IAAI,EAAEa,aADF;AAEJC,IAAAA,KAFI;AA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gC,GAAL,EAAU;AACR,UAAM,IAAIC,KAAJ,CAAU,mCAAV,CAAN;AACD;;AAED,SAAOD,GAAP;AACD,CAPM;AASP,OAAO,MAAME,yBAAyB,GAAG,MAAM;AAC7C,QAAMF,GAAG,GAAGxC,UAAU,CAACM,6BAAD,CAAtB;;AACA,MAAI,CAACkC,GAAL,EAAU;AACR,UAAM,IAAIC,KAAJ,CAAU,mCAAV,CAAN;AACD;;AAED,SAAOD,GAAP;AACD,CAPM","sourcesContent":["import { createContext, useCallback, useContext } from \"react\";\nimport { useAuthFetch } from \"matsuri-hooks\";\nimport { useMemo } from \"react\";\nimport { m2mNotifications_v1 } from \"../endpoints/m2m-notifications.v1\";\nimport { useServiceId } from \"./ServiceIdProvider\";\nimport { SubscriptionStatus, Subscription, Notification } from \"./domain\";\nimport React from \"react\";\n\nconst NotificationPermissionContext = createContext<\n | {\n checkPermission: (\n notificationType: string\n ) => SubscriptionStatus | undefined;\n refetch: () => void;\n }\n | undefined\n>(undefined);\n\nconst NotificationsContext = createContext<\n | { notifications: Notification[]; isAllRead: boolean; refetch: () => void }\n | undefined\n>(undefined);\n\n// ServiceIdProviderの中で使うこと\nexport const NotificationsProvider: React.FC<{ token: string }> = ({\n token,\n children,\n}) => {\n const serviceId = useServiceId();\n\n const { data: subscriptions, refetch: refetchSubscriptions } = useAuthFetch<\n Subscription[]\n >(token, m2mNotifications_v1.findMySubscriptions(), {});\n const checkPermission = useCallback(\n (notificationType: string) => {\n const subscription = subscriptions?.find((item) => {\n return (\n item.serviceId === serviceId &&\n item.notificationType === notificationType &&\n item.resourceSubscription.resourceSelectType === \"any\"\n );\n });\n\n return subscription?.status;\n },\n [subscriptions]\n );\n\n const {\n data: notifications,\n error,\n refetch: refetchNotifications,\n } = useAuthFetch<{ data: Notification[] }>(\n token,\n m2mNotifications_v1.findNotificationsByServiceId({ id: serviceId }),\n { swrConfig: { refreshInterval: 1000 * 60 * 5 } }\n );\n if (error) {\n // エラーが出てもユーザーは特に困らないので、ユーザーに伝える必要はないが、監視は必要。\n console.error(error);\n }\n\n const permissionContextState = useMemo(() => {\n return {\n checkPermission,\n refetch: refetchSubscriptions,\n };\n }, [checkPermission, refetchSubscriptions]);\n\n const notificationsContextState = useMemo(() => {\n return {\n notifications: notifications?.data ?? [],\n isAllRead: notifications?.data.every((n) => n.status === \"read\") ?? true,\n refetch: refetchNotifications,\n };\n }, [refetchNotifications, notifications?.data]);\n\n return (\n <NotificationPermissionContext.Provider value={permissionContextState}>\n <NotificationsContext.Provider value={notificationsContextState}>\n {children}\n </NotificationsContext.Provider>\n </NotificationPermissionContext.Provider>\n );\n};\n\nexport const useNotifications = () => {\n const ctx = useContext(NotificationsContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n\nexport const useNotificationPermission = () => {\n const ctx = useContext(NotificationPermissionContext);\n if (!ctx) {\n throw new Error(\"NotificatoinProvider is not found\");\n }\n\n return ctx;\n};\n"],"file":"NotificationProvider.js"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React, { createContext, useMemo } from "react";
|
|
2
|
+
const ServiceIdContext = createContext(undefined);
|
|
3
|
+
export const ServiceIdProvider = ({
|
|
4
|
+
serviceId,
|
|
5
|
+
children
|
|
6
|
+
}) => {
|
|
7
|
+
const state = useMemo(() => ({
|
|
8
|
+
serviceId
|
|
9
|
+
}), []);
|
|
10
|
+
return <ServiceIdContext.Provider value={state}>
|
|
11
|
+
{children}
|
|
12
|
+
</ServiceIdContext.Provider>;
|
|
13
|
+
};
|
|
14
|
+
export const useServiceId = () => {
|
|
15
|
+
const context = React.useContext(ServiceIdContext);
|
|
16
|
+
|
|
17
|
+
if (context === undefined) {
|
|
18
|
+
throw new Error("useServiceId must be used within a ServiceIdProvider");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return context.serviceId;
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=ServiceIdProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/notification/ServiceIdProvider.tsx"],"names":["React","createContext","useMemo","ServiceIdContext","undefined","ServiceIdProvider","serviceId","children","state","useServiceId","context","useContext","Error"],"mappings":"AAAA,OAAOA,KAAP,IAAgBC,aAAhB,EAA+BC,OAA/B,QAA8C,OAA9C;AAMA,MAAMC,gBAAgB,GAAGF,aAAa,CACpCG,SADoC,CAAtC;AAIA,OAAO,MAAMC,iBAAiB,GAAG,CAAC;AAChCC,EAAAA,SADgC;AAEhCC,EAAAA;AAFgC,CAAD,KAM3B;AACJ,QAAMC,KAAK,GAAGN,OAAO,CAAC,OAAO;AAAEI,IAAAA;AAAF,GAAP,CAAD,EAAwB,EAAxB,CAArB;AAEA,SACE,CAAC,iBAAiB,QAAlB,CAA2B,MAAM,CAACE,KAAD,CAAjC;AACJ,MAAM,CAACD,QAAD;AACN,IAAI,EAAE,iBAAiB,QAAnB,CAHF;AAKD,CAdM;AAgBP,OAAO,MAAME,YAAY,GAAG,MAAM;AAChC,QAAMC,OAAO,GAAGV,KAAK,CAACW,UAAN,CAAiBR,gBAAjB,CAAhB;;AACA,MAAIO,OAAO,KAAKN,SAAhB,EAA2B;AACzB,UAAM,IAAIQ,KAAJ,CAAU,sDAAV,CAAN;AACD;;AACD,SAAOF,OAAO,CAACJ,SAAf;AACD,CANM","sourcesContent":["import React, { createContext, useMemo } from \"react\";\n\ninterface ServiceIdContextState {\n serviceId: string;\n}\n\nconst ServiceIdContext = createContext<ServiceIdContextState | undefined>(\n undefined\n);\n\nexport const ServiceIdProvider = ({\n serviceId,\n children,\n}: {\n serviceId: string;\n children: React.ReactNode;\n}) => {\n const state = useMemo(() => ({ serviceId }), []);\n\n return (\n <ServiceIdContext.Provider value={state}>\n {children}\n </ServiceIdContext.Provider>\n );\n};\n\nexport const useServiceId = () => {\n const context = React.useContext(ServiceIdContext);\n if (context === undefined) {\n throw new Error(\"useServiceId must be used within a ServiceIdProvider\");\n }\n return context.serviceId;\n};\n"],"file":"ServiceIdProvider.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "m2m-components",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"module": "index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"build-app:hub:dev": "vite build website/hub/ --out-dir ../../build -m development",
|
|
51
51
|
"clean": "rm -rf {m2m-apps-menu,m2m-user-menu,LitMenuBaseElement,domains,index}.{d.ts.map,d.ts,js.map,js} storage/ notification/ endpoints/",
|
|
52
52
|
"build": "concurrently yarn:build:*",
|
|
53
|
-
"build:esm": "NODE_ENV=production BABEL_ENV=esm babel src --extensions .ts -d ./ --source-maps",
|
|
53
|
+
"build:esm": "NODE_ENV=production BABEL_ENV=esm babel src --extensions .ts,.tsx -d ./ --source-maps",
|
|
54
54
|
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly --declaration --declarationDir ./",
|
|
55
55
|
"lint": "eslint .",
|
|
56
56
|
"postlint": "prettier --check .",
|