Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

feat: auto pair mode for cast

httpjamesm80eda678d995
diff --git a/apps/photos/public/locales/en/translation.json b/apps/photos/public/locales/en/translation.json
index 21ef9aa19b..993439210b 100644
--- a/apps/photos/public/locales/en/translation.json
+++ b/apps/photos/public/locales/en/translation.json
@@ -626,5 +626,9 @@
"CAST_ALBUM_TO_TV": "Play album on TV",
"ENTER_CAST_PIN_CODE": "Enter the code you see on the TV below to pair this device.",
"PAIR_DEVICE_TO_TV": "Pair devices",
- "TV_NOT_FOUND": "TV not found. Did you enter the PIN correctly?"
+ "TV_NOT_FOUND": "TV not found. Did you enter the PIN correctly?",
+ "AUTO_CAST_PAIR": "Auto Pair",
+ "AUTO_CAST_PAIR_REQUIRES_CONNECTION_TO_GOOGLE": "Auto Pair requires connecting to Google servers. They will not receive sensitive data, such as your photos.",
+ "PAIR_WITH_PIN": "Pair with PIN",
+ "CHOOSE_DEVICE_FROM_BROWSER": "Choose a cast-compatible device from the browser popup."
}
diff --git a/apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx b/apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx
index 7e6e3e5d7a..58cb55b8df 100644
--- a/apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx
+++ b/apps/photos/src/components/Collections/CollectionOptions/AlbumCastDialog.tsx
@@ -1,4 +1,4 @@
-import { Typography } from '@mui/material';
+import { Button, Typography } from '@mui/material';
import DialogBoxV2 from '@ente/shared/components/DialogBoxV2';
import SingleInputForm, {
SingleInputFormProps,
@@ -7,9 +7,11 @@ import { t } from 'i18next';
import { getKexValue, setKexValue } from '@ente/shared/network/kexService';
import { SESSION_KEYS, getKey } from '@ente/shared/storage/sessionStorage';
import { boxSeal, toB64 } from '@ente/shared/crypto/internal/libsodium';
-import { useCastSender } from '@ente/shared/hooks/useCastSender';
-import { useEffect } from 'react';
-import { logError } from '@ente/shared/sentry';
+import { loadSender } from '@ente/shared/hooks/useCastSender';
+import { useEffect, useState } from 'react';
+import EnteButton from '@ente/shared/components/EnteButton';
+import EnteSpinner from '@ente/shared/components/EnteSpinner';
+import { VerticallyCentered } from '@ente/shared/components/Container';
interface Props {
show: boolean;
@@ -22,7 +24,9 @@ enum AlbumCastError {
}
export default function AlbumCastDialog(props: Props) {
- const { cast } = useCastSender();
+ const [view, setView] = useState<
+ 'choose' | 'auto' | 'pin' | 'auto-cast-error'
+ >('choose');
const onSubmit: SingleInputFormProps['callback'] = async (
value,
@@ -73,17 +77,54 @@ export default function AlbumCastDialog(props: Props) {
// hey TV, we acknowlege you!
await setKexValue(encryptedPayloadForTvKexKey, encryptedPayload);
};
+
useEffect(() => {
- if (!cast || !props.show) return;
- cast.framework.CastContext.getInstance()
- .requestSession()
- // .then(function (session) {
- // // Session started successfully
- // })
- .catch((e) => {
- logError(e, 'Failed to start cast session');
+ if (view === 'auto') {
+ loadSender().then(async (sender) => {
+ const { cast } = sender;
+
+ const instance = await cast.framework.CastContext.getInstance();
+ try {
+ await instance.requestSession();
+ } catch (e) {
+ console.log('Error requesting session:', e);
+ return;
+ }
+ const session = instance.getCurrentSession();
+ console.log(session);
+ session.addMessageListener(
+ 'urn:x-cast:pair-request',
+ (namespace, message) => {
+ const data = message;
+ const obj = JSON.parse(data);
+ const code = obj.code;
+
+ if (code) {
+ doCast(code)
+ .then(() => {
+ setView('choose');
+ props.onHide();
+ })
+ .catch((e) => {
+ setView('auto-cast-error');
+ console.error(e);
+ });
+ }
+ }
+ );
+
+ console.log('sending message');
+ session
+ .sendMessage('urn:x-cast:pair-request', {})
+ .then(() => {
+ console.log('Message sent successfully');
+ })
+ .catch((error) => {
+ console.log('Error sending message:', error);
+ });
});
- }, [cast, props.show]);
+ }
+ }, [view]);
return (
<DialogBoxV2
@@ -93,14 +134,69 @@ export default function AlbumCastDialog(props: Props) {
attributes={{
title: t('CAST_ALBUM_TO_TV'),
}}>
- <Typography>{t('ENTER_CAST_PIN_CODE')}</Typography>
- <SingleInputForm
- callback={onSubmit}
- fieldType="text"
- placeholder={'123456'}
- buttonText={t('PAIR_DEVICE_TO_TV')}
- submitButtonProps={{ sx: { mt: 1, mb: 2 } }}
- />
+ {view === 'choose' && (
+ <>
+ <Button
+ onClick={() => {
+ setView('auto');
+ }}>
+ {t('AUTO_CAST_PAIR')}
+ </Button>
+ <Typography color={'text.muted'}>
+ {t('AUTO_CAST_PAIR_REQUIRES_CONNECTION_TO_GOOGLE')}
+ </Typography>
+ <Button
+ onClick={() => {
+ setView('pin');
+ }}>
+ {t('PAIR_WITH_PIN')}
+ </Button>
+ </>
+ )}
+ {view === 'auto' && (
+ <VerticallyCentered gap="1rem">
+ <EnteSpinner />
+ <Typography>{t('CHOOSE_DEVICE_FROM_BROWSER')}</Typography>
+ <EnteButton
+ variant="text"
+ onClick={() => {
+ setView('choose');
+ }}>
+ {t('GO_BACK')}
+ </EnteButton>
+ </VerticallyCentered>
+ )}
+ {view === 'auto-cast-error' && (
+ <VerticallyCentered gap="1rem">
+ <Typography>{t('TV_NOT_FOUND')}</Typography>
+ <EnteButton
+ variant="text"
+ onClick={() => {
+ setView('choose');
+ }}>
+ {t('GO_BACK')}
+ </EnteButton>
+ </VerticallyCentered>
+ )}
+ {view === 'pin' && (
+ <>
+ <Typography>{t('ENTER_CAST_PIN_CODE')}</Typography>
+ <SingleInputForm
+ callback={onSubmit}
+ fieldType="text"
+ placeholder={'123456'}
+ buttonText={t('PAIR_DEVICE_TO_TV')}
+ submitButtonProps={{ sx: { mt: 1, mb: 2 } }}
+ />
+ <EnteButton
+ variant="text"
+ onClick={() => {
+ setView('choose');
+ }}>
+ {t('GO_BACK')}
+ </EnteButton>
+ </>
+ )}
</DialogBoxV2>
);
}
diff --git a/packages/shared/hooks/useCastSender.tsx b/packages/shared/hooks/useCastSender.tsx
index 207d4b1052..7b9da6b426 100644
--- a/packages/shared/hooks/useCastSender.tsx
+++ b/packages/shared/hooks/useCastSender.tsx
@@ -14,7 +14,7 @@ type Sender = {
cast: typeof cast;
};
-const load = (() => {
+export const loadSender = (() => {
let promise: Promise<Sender> | null = null;
return () => {
@@ -54,7 +54,7 @@ export const useCastSender = () => {
);
useEffect(() => {
- load().then((sender) => {
+ loadSender().then((sender) => {
setSender(sender);
});
}, []);
diff --git a/packages/shared/utils/cast.ts b/packages/shared/utils/cast.ts
new file mode 100644
index 0000000000..1a4dcaa4e5
--- /dev/null
+++ b/packages/shared/utils/cast.ts
@@ -0,0 +1,13 @@
+declare global {
+ interface Window {
+ chrome: any;
+ }
+}
+
+export const isChromecastSupported = () => {
+ return !!(
+ window.chrome &&
+ window.chrome.cast &&
+ window.chrome.cast.isAvailable
+ );
+};