diff --git a/.nvmrc b/.nvmrc deleted file mode 100644 index b009dfb9d9..0000000000 --- a/.nvmrc +++ /dev/null @@ -1 +0,0 @@ -lts/* diff --git a/src/documentViewer/ipc.ts b/src/documentViewer/ipc.ts new file mode 100644 index 0000000000..07b049b744 --- /dev/null +++ b/src/documentViewer/ipc.ts @@ -0,0 +1,29 @@ +import { handle } from '../ipc/main'; +import { SERVER_DOCUMENT_VIEWER_OPEN_URL } from '../servers/actions'; +import { dispatch, select } from '../store'; + +export const startDocumentViewerHandler = (): void => { + handle( + 'document-viewer/open-window', + async (event, url, _format, _options) => { + const validUrl = new URL(url); + const allowedProtocols = ['http:', 'https:']; + if (!allowedProtocols.includes(validUrl.protocol)) { + return; + } + const server = select(({ servers }) => + servers.find( + (s) => new URL(s.url).origin === new URL(event.getURL()).origin + ) + ); + if (!server) { + return; + } + + dispatch({ + type: SERVER_DOCUMENT_VIEWER_OPEN_URL, + payload: { server: server.url, documentUrl: url }, + }); + } + ); +}; diff --git a/src/injected.ts b/src/injected.ts index 613d210739..6cd8d93d78 100644 --- a/src/injected.ts +++ b/src/injected.ts @@ -202,6 +202,21 @@ const start = async () => { window.RocketChatDesktop.setGitCommitHash(gitCommitHash); }); + Tracker.autorun(() => { + const uid = Meteor.userId(); + if (!uid) return; + const themeAppearance: string = getUserPreference(uid, 'themeAppearence'); + if ( + ['dark', 'light', 'auto', 'high-contrast'].includes( + themeAppearance as any + ) + ) { + window.RocketChatDesktop.setUserThemeAppearance( + themeAppearance as 'auto' | 'dark' | 'light' | 'high-contrast' + ); + } + }); + Tracker.autorun(() => { const uid = Meteor.userId(); if (!uid) return; diff --git a/src/ipc/channels.ts b/src/ipc/channels.ts index 0c978177ef..13a4017be9 100644 --- a/src/ipc/channels.ts +++ b/src/ipc/channels.ts @@ -41,6 +41,11 @@ type ChannelToArgsMap = { 'outlook-calendar/has-credentials': () => Promise; 'outlook-calendar/clear-credentials': () => void; 'outlook-calendar/set-user-token': (token: string, userId: string) => void; + 'document-viewer/open-window': ( + url: string, + format: string, + options: any + ) => void; }; export type Channel = keyof ChannelToArgsMap; diff --git a/src/main.ts b/src/main.ts index 1c73801e53..8bb26c6d0b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,6 +8,7 @@ import { } from './app/main/data'; import { setUserDataDirectory } from './app/main/dev'; import { setupDeepLinks, processDeepLinksInArgs } from './deepLinks/main'; +import { startDocumentViewerHandler } from './documentViewer/ipc'; import { setupDownloads } from './downloads/main'; import { setupMainErrorHandling } from './errors'; import i18n from './i18n/main'; @@ -98,6 +99,7 @@ const start = async (): Promise => { handleJitsiDesktopCapturerGetSources(); handleDesktopCapturerGetSources(); startOutlookCalendarUrlHandler(); + startDocumentViewerHandler(); checkSupportedVersionServers(); await processDeepLinksInArgs(); diff --git a/src/preload.ts b/src/preload.ts index d562a3d7b7..46e88c78bc 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -24,6 +24,7 @@ declare global { } contextBridge.exposeInMainWorld('JitsiMeetElectron', JitsiMeetElectron); +contextBridge.exposeInMainWorld('RocketChatDesktop', RocketChatDesktop); const start = async (): Promise => { console.log('[Rocket.Chat Desktop] preload.ts start'); @@ -36,8 +37,6 @@ const start = async (): Promise => { window.removeEventListener('load', start); - contextBridge.exposeInMainWorld('RocketChatDesktop', RocketChatDesktop); - setServerUrl(serverUrl); await whenReady(); diff --git a/src/servers/actions.ts b/src/servers/actions.ts index bbf75e1658..feb497760f 100644 --- a/src/servers/actions.ts +++ b/src/servers/actions.ts @@ -4,6 +4,8 @@ export const SERVERS_LOADED = 'servers/loaded'; export const SERVER_URL_RESOLUTION_REQUESTED = 'server/url-resolution-requested'; export const SERVER_URL_RESOLVED = 'server/url-resolved'; +export const SERVER_DOCUMENT_VIEWER_OPEN_URL = + 'server/document-viewer/open-url'; export type ServersActionTypeToPayloadMap = { [SERVERS_LOADED]: { @@ -12,4 +14,8 @@ export type ServersActionTypeToPayloadMap = { }; [SERVER_URL_RESOLUTION_REQUESTED]: Server['url']; [SERVER_URL_RESOLVED]: ServerUrlResolutionResult; + [SERVER_DOCUMENT_VIEWER_OPEN_URL]: { + server: Server['url']; + documentUrl: string; + }; }; diff --git a/src/servers/common.ts b/src/servers/common.ts index bc563a8f8a..c7fd698e0b 100644 --- a/src/servers/common.ts +++ b/src/servers/common.ts @@ -25,6 +25,8 @@ export type Server = { supportedVersionsSource?: 'server' | 'cloud' | 'builtin'; supportedVersions?: SupportedVersions; expirationMessageLastTimeShown?: Date; + documentViewerOpenUrl?: string; + themeAppearance?: 'dark' | 'light' | 'auto' | 'high-contrast'; }; export const enum ServerUrlResolutionStatus { diff --git a/src/servers/preload/api.ts b/src/servers/preload/api.ts index cabf221dc8..03b0209a69 100644 --- a/src/servers/preload/api.ts +++ b/src/servers/preload/api.ts @@ -14,6 +14,7 @@ import { setUserPresenceDetection } from '../../userPresence/preload'; import type { Server } from '../common'; import { setBadge } from './badge'; import { writeTextToClipboard } from './clipboard'; +import { openDocumentViewer } from './documentViewer'; import { setFavicon } from './favicon'; import { setGitCommitHash } from './gitCommitHash'; import type { videoCallWindowOptions } from './internalVideoChatWindow'; @@ -26,6 +27,7 @@ import { setServerVersionToSidebar, setSidebarCustomTheme, } from './sidebar'; +import { setUserThemeAppearance } from './themeAppearance'; import { setTitle } from './title'; import { setUrlResolver } from './urls'; import { setUserLoggedIn } from './userLoggedIn'; @@ -52,6 +54,7 @@ export type RocketChatDesktopAPI = { idleThreshold: number | null; setUserOnline: (online: boolean) => void; }) => void; + setUserThemeAppearance: (themeAppearance: Server['themeAppearance']) => void; createNotification: ( options: NotificationOptions & { canReply?: boolean; @@ -72,6 +75,7 @@ export type RocketChatDesktopAPI = { hasOutlookCredentials: () => Promise; clearOutlookCredentials: () => void; setUserToken: (token: string, userId: string) => void; + openDocumentViewer: (url: string, format: string, options: any) => void; }; export const RocketChatDesktop: RocketChatDesktopAPI = { @@ -93,6 +97,7 @@ export const RocketChatDesktop: RocketChatDesktopAPI = { setTitle, setUserPresenceDetection, setUserLoggedIn, + setUserThemeAppearance, createNotification, destroyNotification, getInternalVideoChatWindowEnabled, @@ -105,4 +110,5 @@ export const RocketChatDesktop: RocketChatDesktopAPI = { clearOutlookCredentials, setUserToken, setSidebarCustomTheme, + openDocumentViewer, }; diff --git a/src/servers/preload/documentViewer.ts b/src/servers/preload/documentViewer.ts new file mode 100644 index 0000000000..1f43464bb6 --- /dev/null +++ b/src/servers/preload/documentViewer.ts @@ -0,0 +1,9 @@ +import { ipcRenderer } from 'electron'; + +export const openDocumentViewer = ( + url: string, + format: string, + options: any +): void => { + ipcRenderer.invoke('document-viewer/open-window', url, format, options); +}; diff --git a/src/servers/preload/themeAppearance.ts b/src/servers/preload/themeAppearance.ts new file mode 100644 index 0000000000..c9a6a864ce --- /dev/null +++ b/src/servers/preload/themeAppearance.ts @@ -0,0 +1,16 @@ +import { dispatch } from '../../store'; +import { WEBVIEW_USER_THEME_APPEARANCE_CHANGED } from '../../ui/actions'; +import type { Server } from '../common'; +import { getServerUrl } from './urls'; + +export const setUserThemeAppearance = ( + themeAppearance: Server['themeAppearance'] +): void => { + dispatch({ + type: WEBVIEW_USER_THEME_APPEARANCE_CHANGED, + payload: { + url: getServerUrl(), + themeAppearance, + }, + }); +}; diff --git a/src/servers/reducers.ts b/src/servers/reducers.ts index 97f0736ad3..33b9f69906 100644 --- a/src/servers/reducers.ts +++ b/src/servers/reducers.ts @@ -26,8 +26,9 @@ import { WEBVIEW_SERVER_VERSION_UPDATED, SUPPORTED_VERSION_DIALOG_DISMISS, WEBVIEW_SIDEBAR_CUSTOM_THEME_CHANGED, + WEBVIEW_USER_THEME_APPEARANCE_CHANGED, } from '../ui/actions'; -import { SERVERS_LOADED } from './actions'; +import { SERVERS_LOADED, SERVER_DOCUMENT_VIEWER_OPEN_URL } from './actions'; import type { Server } from './common'; const ensureUrlFormat = (serverUrl: string | null): string => { @@ -63,7 +64,9 @@ type ServersActionTypes = | ActionOf | ActionOf | ActionOf - | ActionOf; + | ActionOf + | ActionOf + | ActionOf; const upsert = (state: Server[], server: Server): Server[] => { const index = state.findIndex(({ url }) => url === server.url); @@ -136,6 +139,11 @@ export const servers: Reducer = ( return upsert(state, { url, uniqueID }); } + case WEBVIEW_USER_THEME_APPEARANCE_CHANGED: { + const { url, themeAppearance } = action.payload; + return upsert(state, { url, themeAppearance }); + } + case WEBVIEW_SERVER_IS_SUPPORTED_VERSION: { const { url, isSupportedVersion } = action.payload; return upsert(state, { url, isSupportedVersion }); @@ -206,7 +214,7 @@ export const servers: Reducer = ( case SERVERS_LOADED: { const { servers = state } = action.payload; - return servers.map((server) => ({ + return servers.map((server: Server) => ({ ...server, url: ensureUrlFormat(server.url), })); @@ -214,9 +222,10 @@ export const servers: Reducer = ( case APP_SETTINGS_LOADED: { const { servers = state } = action.payload; - return servers.map((server) => ({ + return servers.map((server: Server) => ({ ...server, url: ensureUrlFormat(server.url), + documentViewerOpenUrl: '', })); } @@ -235,6 +244,11 @@ export const servers: Reducer = ( return upsert(state, { url, outlookCredentials }); } + case SERVER_DOCUMENT_VIEWER_OPEN_URL: { + const { server, documentUrl } = action.payload; + return upsert(state, { url: server, documentViewerOpenUrl: documentUrl }); + } + default: return state; } diff --git a/src/ui/actions.ts b/src/ui/actions.ts index 49a58a9abf..cefcbcba5d 100644 --- a/src/ui/actions.ts +++ b/src/ui/actions.ts @@ -65,6 +65,8 @@ export const WEBVIEW_GIT_COMMIT_HASH_CHECK = 'webview/git-commit-hash-check'; export const WEBVIEW_TITLE_CHANGED = 'webview/title-changed'; export const WEBVIEW_UNREAD_CHANGED = 'webview/unread-changed'; export const WEBVIEW_USER_LOGGED_IN = 'webview/user-loggedin'; +export const WEBVIEW_USER_THEME_APPEARANCE_CHANGED = + 'webview/user-theme-appearance-changed'; export const WEBVIEW_ALLOWED_REDIRECTS_CHANGED = 'webview/allowed-redirects-changed'; export const SETTINGS_SET_REPORT_OPT_IN_CHANGED = @@ -159,6 +161,10 @@ export type UiActionTypeToPayloadMap = { url: Server['url']; userLoggedIn: Server['userLoggedIn']; }; + [WEBVIEW_USER_THEME_APPEARANCE_CHANGED]: { + url: Server['url']; + themeAppearance: Server['themeAppearance']; + }; [WEBVIEW_GIT_COMMIT_HASH_CHECK]: { url: Server['url']; gitCommitHash: Server['gitCommitHash']; diff --git a/src/ui/components/ServersView/DocumentViewer.tsx b/src/ui/components/ServersView/DocumentViewer.tsx new file mode 100644 index 0000000000..4a0eb33f19 --- /dev/null +++ b/src/ui/components/ServersView/DocumentViewer.tsx @@ -0,0 +1,65 @@ +import { Box, IconButton } from '@rocket.chat/fuselage'; +import { useDarkMode } from '@rocket.chat/fuselage-hooks'; + +const DocumentViewer = ({ + url, + partition, + closeDocumentViewer, + themeAppearance, +}: { + url: string; + partition: string; + themeAppearance: string | undefined; + closeDocumentViewer: () => void; +}) => { + const theme = useDarkMode( + themeAppearance === 'auto' ? undefined : themeAppearance === 'dark' + ) + ? 'dark' + : 'light'; + return ( + <> + + + +

PDF Viewer

+
+ + + + +
+ + ); +}; + +export default DocumentViewer; diff --git a/src/ui/components/ServersView/ServerPane.tsx b/src/ui/components/ServersView/ServerPane.tsx index 0b21da49ce..d2dbacfa36 100644 --- a/src/ui/components/ServersView/ServerPane.tsx +++ b/src/ui/components/ServersView/ServerPane.tsx @@ -1,17 +1,19 @@ import { ipcRenderer } from 'electron'; -import { useRef, useEffect } from 'react'; +import { useRef, useEffect, useState } from 'react'; import { useDispatch } from 'react-redux'; import type { Dispatch } from 'redux'; +import { SERVER_DOCUMENT_VIEWER_OPEN_URL } from '../../../servers/actions'; import type { RootAction } from '../../../store/actions'; import { LOADING_ERROR_VIEW_RELOAD_SERVER_CLICKED, WEBVIEW_ATTACHED, WEBVIEW_READY, } from '../../actions'; +import DocumentViewer from './DocumentViewer'; import ErrorView from './ErrorView'; import UnsupportedServer from './UnsupportedServer'; -import { StyledWebView, Wrapper } from './styles'; +import { DocumentViewerWrapper, StyledWebView, Wrapper } from './styles'; type ServerPaneProps = { lastPath: string | undefined; @@ -20,6 +22,8 @@ type ServerPaneProps = { isFailed: boolean; isSupported: boolean | undefined; title: string | undefined; + documentViewerOpenUrl: string | undefined; + themeAppearance: string | undefined; }; export const ServerPane = ({ @@ -28,9 +32,13 @@ export const ServerPane = ({ isSelected, isFailed, isSupported, + documentViewerOpenUrl, + themeAppearance, }: ServerPaneProps) => { const dispatch = useDispatch>(); + const [documentViewerActive, setDocumentViewerActive] = useState(false); + const webviewRef = useRef>(null); @@ -137,6 +145,19 @@ export const ServerPane = ({ } }, [lastPath, serverUrl]); + useEffect(() => { + const webview = webviewRef.current; + if (!webview) { + return; + } + + if (isSelected && documentViewerOpenUrl && documentViewerOpenUrl !== '') { + setDocumentViewerActive(true); + } else { + setDocumentViewerActive(false); + } + }, [documentViewerOpenUrl, isSelected]); + const handleReload = (): void => { dispatch({ type: LOADING_ERROR_VIEW_RELOAD_SERVER_CLICKED, @@ -153,8 +174,17 @@ export const ServerPane = ({ } else { webview?.blur(); } + // setDocumentViewerActive(true); }, [isSelected]); + const closeDocumentViewer = () => { + dispatch({ + type: SERVER_DOCUMENT_VIEWER_OPEN_URL, + payload: { server: serverUrl, documentUrl: '' }, + }); + setDocumentViewerActive(false); + }; + useEffect(() => { const handleOnline = () => { ipcRenderer.invoke('refresh-supported-versions', serverUrl); @@ -175,6 +205,14 @@ export const ServerPane = ({ partition={`persist:${serverUrl}`} {...({ allowpopups: 'allowpopups' } as any)} />{' '} + + + { isFailed={server.failed ?? false} isSupported={server.isSupportedVersion} title={server.title} + documentViewerOpenUrl={server.documentViewerOpenUrl} + themeAppearance={server.themeAppearance} /> ))} diff --git a/src/ui/components/ServersView/styles.tsx b/src/ui/components/ServersView/styles.tsx index 63f04274b4..3ba5593e8d 100644 --- a/src/ui/components/ServersView/styles.tsx +++ b/src/ui/components/ServersView/styles.tsx @@ -18,6 +18,16 @@ export const Wrapper = styled.section` `}; `; +type DocumentViewerWrapperProps = { + isVisible: boolean; +}; + +export const DocumentViewerWrapper = styled.section` + ${({ isVisible }) => css` + display: ${isVisible ? 'flex' : 'none'}; + `}; +`; + type StyledWebViewProps = { isFailed: boolean; }; diff --git a/src/ui/main/rootWindow.ts b/src/ui/main/rootWindow.ts index 0e4225f92c..11ad6a4edb 100644 --- a/src/ui/main/rootWindow.ts +++ b/src/ui/main/rootWindow.ts @@ -394,7 +394,7 @@ export const showRootWindow = async (): Promise => { } return new Promise((resolve) => { - browserWindow.addListener('ready-to-show', () => { + browserWindow.once('ready-to-show', () => { applyRootWindowState(browserWindow); const isTrayIconEnabled = select( @@ -422,7 +422,7 @@ export const exportLocalStorage = async (): Promise> => { tempWindow.loadFile(path.join(app.getAppPath(), 'app/index.html')); await new Promise((resolve) => { - tempWindow.addListener('ready-to-show', () => { + tempWindow.once('ready-to-show', () => { resolve(); }); }); diff --git a/src/ui/main/serverView/index.ts b/src/ui/main/serverView/index.ts index 1c9cbc7f34..868f38d846 100644 --- a/src/ui/main/serverView/index.ts +++ b/src/ui/main/serverView/index.ts @@ -325,7 +325,6 @@ export const attachGuestWebContentsEvents = async (): Promise => { guestWebContents.session.on( 'will-download', (event, item, _webContents) => { - console.log('will-download', item); const fileName = item.getFilename(); const extension = path.extname(fileName)?.slice(1).toLowerCase(); const savePath = dialog.showSaveDialogSync(rootWindow, {