@taruvi/navkit 0.0.48-beta.4 → 0.0.48-beta.5
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.
package/package.json
CHANGED
package/src/App.tsx
CHANGED
|
@@ -13,6 +13,23 @@ import taruviLogo from "./assets/logo.svg";
|
|
|
13
13
|
import taruviLogoWhite from "./assets/taruvi-logo-white.png";
|
|
14
14
|
import type { Client } from "@taruvi/sdk"
|
|
15
15
|
|
|
16
|
+
/** Top-level storage access for the chat origin (Chromium). Call synchronously from the chat button click
|
|
17
|
+
* so transient activation is preserved; pairs with the iframe autologin bridge so no second in-frame login
|
|
18
|
+
* click is needed when the browser supports this API. */
|
|
19
|
+
function requestMattermostStorageAccessFromUserGesture(chatUrl: string) {
|
|
20
|
+
try {
|
|
21
|
+
const origin = new URL(chatUrl).origin
|
|
22
|
+
const doc = document as Document & {
|
|
23
|
+
requestStorageAccessFor?: (requestedOrigin: string) => Promise<void>
|
|
24
|
+
}
|
|
25
|
+
if (typeof doc.requestStorageAccessFor === "function") {
|
|
26
|
+
void doc.requestStorageAccessFor(origin).catch(() => {})
|
|
27
|
+
}
|
|
28
|
+
} catch {
|
|
29
|
+
// invalid chatUrl or API unavailable
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
16
33
|
const ChatIcon = ({ color }: { color?: string }) => (
|
|
17
34
|
<SvgIcon viewBox="-2 -2 24 24" sx={{ fontSize: 23, display: 'block', position: 'relative', top: 2 }}>
|
|
18
35
|
<path d="M18 0H2C0.9 0 0 0.9 0 2V20L4 16H18C19.1 16 20 15.1 20 14V2C20 0.9 19.1 0 18 0Z" fill={color || "#9DE5FD"} />
|
|
@@ -61,7 +78,13 @@ const NavkitContent = () => {
|
|
|
61
78
|
<Box component={"div"} sx={styles.rightSection}>
|
|
62
79
|
<Shortcuts onMenuToggle={() => setShowShortcutsMenu(!showShortcutsMenu)} iconColor={iconColor} />
|
|
63
80
|
{isUserAuthenticated && appsList.some(a => a.id === 'chat') && chatUrl && (
|
|
64
|
-
<Box
|
|
81
|
+
<Box
|
|
82
|
+
onClick={() => {
|
|
83
|
+
requestMattermostStorageAccessFromUserGesture(chatUrl)
|
|
84
|
+
setShowChat(true)
|
|
85
|
+
}}
|
|
86
|
+
sx={{ ...styles.appLauncherContainer, cursor: 'pointer' }}
|
|
87
|
+
>
|
|
65
88
|
<ChatIcon color={iconColor} />
|
|
66
89
|
</Box>
|
|
67
90
|
)}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useLayoutEffect, useRef, useState} from 'react'
|
|
2
2
|
import {Box, Badge} from '@mui/material'
|
|
3
3
|
|
|
4
4
|
interface MattermostChatProps {
|
|
@@ -13,20 +13,30 @@ interface MattermostChatProps {
|
|
|
13
13
|
|
|
14
14
|
const MattermostChat = ({ mattermostUrl, loginId, sessionToken, onNotification, onUrlClick, width = '100%', height = '100%' }: MattermostChatProps) => {
|
|
15
15
|
const iframeRef = useRef<HTMLIFrameElement>(null)
|
|
16
|
+
const credentialsRef = useRef({ loginId, sessionToken })
|
|
17
|
+
credentialsRef.current = { loginId, sessionToken }
|
|
16
18
|
const [unreadCount, setUnreadCount] = useState(0)
|
|
17
19
|
const [isWindowFocused, setIsWindowFocused] = useState(true)
|
|
18
20
|
const mattermostOrigin = new URL(mattermostUrl).origin
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
const postLoginToBridge = () => {
|
|
23
|
+
const { loginId: id, sessionToken: token } = credentialsRef.current
|
|
24
|
+
if (!id || !token) return
|
|
25
|
+
iframeRef.current?.contentWindow?.postMessage(
|
|
26
|
+
{ type: 'mm-login', loginId: id, token },
|
|
27
|
+
mattermostOrigin
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// useLayoutEffect so the listener is attached in the same commit as the iframe
|
|
32
|
+
// before the bridge can fire mm-bridge-ready (useEffect runs too late and misses it).
|
|
33
|
+
useLayoutEffect(() => {
|
|
21
34
|
const handleMessage = (event: MessageEvent) => {
|
|
22
35
|
if (event.origin !== mattermostOrigin) return
|
|
23
36
|
|
|
24
37
|
// Bridge ready — send login credentials
|
|
25
38
|
if (event.data?.type === 'mm-bridge-ready') {
|
|
26
|
-
|
|
27
|
-
{ type: 'mm-login', loginId, token: sessionToken },
|
|
28
|
-
mattermostOrigin
|
|
29
|
-
)
|
|
39
|
+
postLoginToBridge()
|
|
30
40
|
return
|
|
31
41
|
}
|
|
32
42
|
|
|
@@ -62,7 +72,7 @@ const MattermostChat = ({ mattermostUrl, loginId, sessionToken, onNotification,
|
|
|
62
72
|
window.removeEventListener('focus', handleFocus)
|
|
63
73
|
window.removeEventListener('blur', handleBlur)
|
|
64
74
|
}
|
|
65
|
-
}, [mattermostOrigin,
|
|
75
|
+
}, [mattermostOrigin, isWindowFocused, onNotification, onUrlClick])
|
|
66
76
|
|
|
67
77
|
// Always load the autologin bridge first — it handles storage access + login,
|
|
68
78
|
// then redirects the iframe to Mattermost
|
|
@@ -96,6 +106,7 @@ const MattermostChat = ({ mattermostUrl, loginId, sessionToken, onNotification,
|
|
|
96
106
|
/>
|
|
97
107
|
)}
|
|
98
108
|
<iframe
|
|
109
|
+
key={`${loginId}:${sessionToken ? '1' : '0'}`}
|
|
99
110
|
ref={iframeRef}
|
|
100
111
|
src={bridgeUrl}
|
|
101
112
|
title="Mattermost Chat"
|