-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #690 from Stremio/feat/open-external-shell
feat: impl openExternal for shell compatibility
- Loading branch information
Showing
18 changed files
with
176 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { createContext, useContext } from 'react'; | ||
import { WHITELISTED_HOSTS } from 'stremio/common/CONSTANTS'; | ||
import useShell from './useShell'; | ||
import { name, isMobile } from './device'; | ||
|
||
interface PlatformContext { | ||
name: string; | ||
isMobile: boolean; | ||
openExternal: (url: string) => void; | ||
} | ||
|
||
const PlatformContext = createContext<PlatformContext | null>(null); | ||
|
||
type Props = { | ||
children: JSX.Element; | ||
}; | ||
|
||
const PlatformProvider = ({ children }: Props) => { | ||
const shell = useShell(); | ||
|
||
const openExternal = (url: string) => { | ||
try { | ||
const { hostname } = new URL(url); | ||
const isWhitelisted = WHITELISTED_HOSTS.some((host: string) => hostname.endsWith(host)); | ||
const finalUrl = !isWhitelisted ? `https://www.stremio.com/warning#${encodeURIComponent(url)}` : url; | ||
|
||
if (shell.active) { | ||
shell.send('open-external', finalUrl); | ||
} else { | ||
window.open(finalUrl, '_blank'); | ||
} | ||
} catch (e) { | ||
console.error('Failed to parse external url:', e); | ||
} | ||
}; | ||
|
||
return ( | ||
<PlatformContext.Provider value={{ openExternal, name, isMobile }}> | ||
{children} | ||
</PlatformContext.Provider> | ||
); | ||
}; | ||
|
||
const usePlatform = () => { | ||
return useContext(PlatformContext); | ||
}; | ||
|
||
export { | ||
PlatformProvider, | ||
usePlatform | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Bowser from 'bowser'; | ||
|
||
const APPLE_MOBILE_DEVICES = [ | ||
'iPad Simulator', | ||
'iPhone Simulator', | ||
'iPod Simulator', | ||
'iPad', | ||
'iPhone', | ||
'iPod', | ||
]; | ||
|
||
const { userAgent, platform, maxTouchPoints } = globalThis.navigator; | ||
|
||
// this detects ipad properly in safari | ||
// while bowser does not | ||
const isIOS = APPLE_MOBILE_DEVICES.includes(platform) || (userAgent.includes('Mac') && 'ontouchend' in document); | ||
|
||
// Edge case: iPad is included in this function | ||
// Keep in mind maxTouchPoints for Vision Pro might change in the future | ||
const isVisionOS = userAgent.includes('Macintosh') || maxTouchPoints === 5; | ||
|
||
const bowser = Bowser.getParser(userAgent); | ||
const os = bowser.getOSName().toLowerCase(); | ||
|
||
const name = isVisionOS ? 'visionos' : isIOS ? 'ios' : os || 'unknown'; | ||
const isMobile = ['ios', 'android'].includes(name); | ||
|
||
export { | ||
name, | ||
isMobile, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PlatformProvider, usePlatform } from './Platform'; | ||
export { | ||
PlatformProvider, | ||
usePlatform, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const createId = () => Math.floor(Math.random() * 9999) + 1; | ||
|
||
const useShell = () => { | ||
const transport = globalThis?.qt?.webChannelTransport; | ||
|
||
const send = (method: string, ...args: (string | number)[]) => { | ||
transport?.send(JSON.stringify({ | ||
id: createId(), | ||
type: 6, | ||
object: 'transport', | ||
method: 'onEvent', | ||
args: [method, ...args], | ||
})); | ||
}; | ||
|
||
return { | ||
active: !!transport, | ||
send, | ||
}; | ||
}; | ||
|
||
export default useShell; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.