From 3704dfba10ae8ada3573c084848c7e0ebf8fb5c4 Mon Sep 17 00:00:00 2001
From: mrjvs <jellevs@gmail.com>
Date: Wed, 10 Jan 2024 22:04:21 +0100
Subject: [PATCH] Fix styling bugs, fix player not switching source after
 error, fix allowed state in extension, add ip locked sourced for extension

Co-authored-by: Jip Frijlink <JipFr@users.noreply.github.com>
---
 src/backend/extension/messaging.ts                     |  7 +++++--
 src/backend/extension/plasmo.ts                        |  5 +----
 src/backend/providers/providers.ts                     |  1 +
 .../player/atoms/settings/SourceSelectingView.tsx      |  2 +-
 src/stores/player/slices/source.ts                     | 10 +++++++++-
 src/utils/language.ts                                  |  2 +-
 6 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/backend/extension/messaging.ts b/src/backend/extension/messaging.ts
index 3d835a0e..4594fb23 100644
--- a/src/backend/extension/messaging.ts
+++ b/src/backend/extension/messaging.ts
@@ -37,7 +37,7 @@ export async function sendExtensionRequest<T>(
   ops: Omit<MessagesMetadata["makeRequest"]["req"], "requestDomain">,
 ): Promise<ExtensionMakeRequestResponse<T> | null> {
   return sendMessage("makeRequest", {
-    requestDomain: window.location.origin,
+    requestDomain: window.location.origin, // TODO unsafe
     ...ops,
   });
 }
@@ -54,13 +54,16 @@ export async function setDomainRule(
 export async function extensionInfo(): Promise<
   MessagesMetadata["hello"]["res"] | null
 > {
-  return sendMessage(
+  const message = await sendMessage(
     "hello",
     {
       requestDomain: window.location.origin,
     },
     300,
   );
+  if (!message?.success) return null;
+  if (!message.allowed) return null;
+  return message;
 }
 
 export function isExtensionActiveCached(): boolean {
diff --git a/src/backend/extension/plasmo.ts b/src/backend/extension/plasmo.ts
index 0a2b3dc5..e142ef6a 100644
--- a/src/backend/extension/plasmo.ts
+++ b/src/backend/extension/plasmo.ts
@@ -13,6 +13,7 @@ export type ExtensionBaseResponse<T = object> =
 
 export type ExtensionHelloResponse = ExtensionBaseResponse<{
   version: string;
+  allowed: boolean;
 }>;
 
 export interface ExtensionMakeRequest extends ExtensionBaseRequest {
@@ -38,10 +39,6 @@ export interface ExtensionPrepareStreamRequest extends ExtensionBaseRequest {
   responseHeaders?: Record<string, string>;
 }
 
-export interface ExtensionHelloReply {
-  version: string;
-}
-
 export interface MmMetadata {
   hello: {
     req: ExtensionBaseRequest;
diff --git a/src/backend/providers/providers.ts b/src/backend/providers/providers.ts
index 1a7b484a..ac4a7dfa 100644
--- a/src/backend/providers/providers.ts
+++ b/src/backend/providers/providers.ts
@@ -15,6 +15,7 @@ export function getProviders() {
     return makeProviders({
       fetcher: makeExtensionFetcher(),
       target: targets.BROWSER_EXTENSION,
+      consistentIpForRequests: true,
     });
   }
 
diff --git a/src/components/player/atoms/settings/SourceSelectingView.tsx b/src/components/player/atoms/settings/SourceSelectingView.tsx
index 03d0875d..f995308e 100644
--- a/src/components/player/atoms/settings/SourceSelectingView.tsx
+++ b/src/components/player/atoms/settings/SourceSelectingView.tsx
@@ -147,7 +147,7 @@ export function SourceSelectionView({
       <Menu.BackLink onClick={() => router.navigate("/")}>
         {t("player.menus.sources.title")}
       </Menu.BackLink>
-      <Menu.Section>
+      <Menu.Section className="pb-4">
         {sources.map((v) => (
           <SelectableLink
             key={v.id}
diff --git a/src/stores/player/slices/source.ts b/src/stores/player/slices/source.ts
index 56e84f74..441c55f6 100644
--- a/src/stores/player/slices/source.ts
+++ b/src/stores/player/slices/source.ts
@@ -116,6 +116,7 @@ export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
   },
   setSourceId(id) {
     set((s) => {
+      s.status = playerStatus.PLAYING;
       s.sourceId = id;
     });
   },
@@ -153,6 +154,8 @@ export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
       s.qualities = qualities as SourceQuality[];
       s.currentQuality = loadableStream.quality;
       s.captionList = captions;
+      s.interface.error = undefined;
+      s.status = playerStatus.PLAYING;
     });
     const store = get();
     store.redisplaySource(startAt);
@@ -166,7 +169,10 @@ export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
       automaticQuality: qualityPreferences.quality.automaticQuality,
       lastChosenQuality: quality,
     });
-
+    set((s) => {
+      s.interface.error = undefined;
+      s.status = playerStatus.PLAYING;
+    });
     store.display?.load({
       source: loadableStream.stream,
       startAt,
@@ -182,6 +188,8 @@ export const createSourceSlice: MakeSlice<SourceSlice> = (set, get) => ({
       if (!selectedQuality) return;
       set((s) => {
         s.currentQuality = quality;
+        s.status = playerStatus.PLAYING;
+        s.interface.error = undefined;
       });
       store.display?.load({
         source: selectedQuality,
diff --git a/src/utils/language.ts b/src/utils/language.ts
index 6fda7df8..41b8168b 100644
--- a/src/utils/language.ts
+++ b/src/utils/language.ts
@@ -86,7 +86,7 @@ function populateLanguageCode(language: string): string {
  * @returns pretty format for language, null if it no info can be found for language
  */
 export function getPrettyLanguageNameFromLocale(locale: string): string | null {
-  const tag = getTag(populateLanguageCode(locale), true);
+  const tag = getTag(locale, true);
   const lang = tag?.language?.Description?.[0] ?? null;
   if (!lang) return null;