Skip to content

Commit

Permalink
Polish last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
itzCozi committed Mar 27, 2024
1 parent b7be016 commit 075ec65
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 61 deletions.
23 changes: 3 additions & 20 deletions src/pages/onboarding/OnboardingExtension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
import { Trans, useTranslation } from "react-i18next";
import { useAsyncFn, useInterval } from "react-use";

import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
import { extensionInfo, sendPage } from "@/backend/extension/messaging";
import { sendPage } from "@/backend/extension/messaging";
import { Button } from "@/components/buttons/Button";
import { Icon, Icons } from "@/components/Icon";
import { Loading } from "@/components/layout/Loading";
Expand All @@ -22,24 +21,8 @@ import {
ExtensionDetectionResult,
detectExtensionInstall,
} from "@/utils/detectFeatures";

type ExtensionStatus =
| "unknown"
| "failed"
| "disallowed"
| "noperms"
| "outdated"
| "success";

async function getExtensionState(): Promise<ExtensionStatus> {
const info = await extensionInfo();
if (!info) return "unknown"; // cant talk to extension
if (!info.success) return "failed"; // extension failed to respond
if (!info.allowed) return "disallowed"; // extension is not enabled on this page
if (!info.hasPermission) return "noperms"; // extension has no perms to do it's tasks
if (!isAllowedExtensionVersion(info.version)) return "outdated"; // extension is too old
return "success"; // no problems
}
import { getExtensionState } from "@/utils/onboarding";
import type { ExtensionStatus } from "@/utils/onboarding";

function RefreshBar() {
const { t } = useTranslation();
Expand Down
98 changes: 58 additions & 40 deletions src/pages/parts/player/ScrapeErrorPart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { useEffect, useMemo, useState } from "react";
import { Trans, useTranslation } from "react-i18next";
import { useLocation } from "react-router-dom";

import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
import { extensionInfo, sendPage } from "@/backend/extension/messaging";
import { sendPage } from "@/backend/extension/messaging";
import { Button } from "@/components/buttons/Button";
import { Icons } from "@/components/Icon";
import { IconPill } from "@/components/layout/IconPill";
Expand All @@ -12,35 +11,18 @@ import { Paragraph } from "@/components/text/Paragraph";
import { Title } from "@/components/text/Title";
import { ScrapingItems, ScrapingSegment } from "@/hooks/useProviderScrape";
import { ErrorContainer, ErrorLayout } from "@/pages/layouts/ErrorLayout";
import { ExtensionStatus, getExtensionState } from "@/utils/onboarding";
import { getProviderApiUrls } from "@/utils/proxyUrls";

import { ErrorCardInModal } from "../errors/ErrorCard";

type ExtensionStatus =
| "unknown"
| "failed"
| "disallowed"
| "noperms"
| "outdated"
| "success";

export interface ScrapeErrorPartProps {
data: {
sources: Record<string, ScrapingSegment>;
sourceOrder: ScrapingItems[];
};
}

async function getExtensionState(): Promise<ExtensionStatus> {
const info = await extensionInfo();
if (!info) return "unknown"; // cant talk to extension
if (!info.success) return "failed"; // extension failed to respond
if (!info.allowed) return "disallowed"; // extension is not enabled on this page
if (!info.hasPermission) return "noperms"; // extension has no perms to do it's tasks
if (!isAllowedExtensionVersion(info.version)) return "outdated"; // extension is too old
return "success"; // no problems
}

export function ScrapeErrorPart(props: ScrapeErrorPartProps) {
const { t } = useTranslation();
const modal = useModal("error");
Expand All @@ -67,7 +49,7 @@ export function ScrapeErrorPart(props: ScrapeErrorPartProps) {
}, [props, location]);

useEffect(() => {
getExtensionState().then((state) => {
getExtensionState().then((state: ExtensionStatus) => {
setExtensionState(state);
if (state === "disallowed") {
setTitle(t("player.scraping.extensionFailure.disabledTitle"));
Expand All @@ -76,13 +58,13 @@ export function ScrapeErrorPart(props: ScrapeErrorPartProps) {
});
}, [t]);

return (
<ErrorLayout>
<ErrorContainer>
<IconPill icon={icon}>{t("player.scraping.notFound.badge")}</IconPill>
<Title>{title}</Title>
<Paragraph>
{extensionState === "disallowed" ? (
if (extensionState === "disallowed") {
return (
<ErrorLayout>
<ErrorContainer>
<IconPill icon={icon}>{t("player.scraping.notFound.badge")}</IconPill>
<Title>{title}</Title>
<Paragraph>
<Trans
i18nKey="player.scraping.extensionFailure.text"
components={{
Expand All @@ -91,16 +73,54 @@ export function ScrapeErrorPart(props: ScrapeErrorPartProps) {
),
}}
/>
) : (
<Trans
i18nKey="player.scraping.notFound.text"
components={{
bold: (
<span className="font-bold" style={{ color: "#cfcfcf" }} />
),
</Paragraph>
<div className="flex gap-3">
<Button
href="/"
theme="secondary"
padding="md:px-12 p-2.5"
className="mt-6"
>
{t("player.scraping.notFound.homeButton")}
</Button>
<Button
onClick={() => {
sendPage({
page: "PermissionGrant",
redirectUrl: window.location.href,
});
}}
/>
)}
theme="purple"
padding="md:px-12 p-2.5"
className="mt-6"
>
{t("player.scraping.extensionFailure.enableExtension")}
</Button>
</div>
</ErrorContainer>
{error ? (
<ErrorCardInModal
id={modal.id}
onClose={() => modal.hide()}
error={error}
/>
) : null}
</ErrorLayout>
);
}

return (
<ErrorLayout>
<ErrorContainer>
<IconPill icon={icon}>{t("player.scraping.notFound.badge")}</IconPill>
<Title>{title}</Title>
<Paragraph>
<Trans
i18nKey="player.scraping.notFound.text"
components={{
bold: <span className="font-bold" style={{ color: "#cfcfcf" }} />,
}}
/>
</Paragraph>
<div className="flex gap-3">
<Button
Expand All @@ -122,9 +142,7 @@ export function ScrapeErrorPart(props: ScrapeErrorPartProps) {
padding="md:px-12 p-2.5"
className="mt-6"
>
{extensionState === "unknown"
? t("player.scraping.notFound.detailsButton")
: t("player.scraping.extensionFailure.enableExtension")}
{t("player.scraping.notFound.detailsButton")}
</Button>
</div>
</ErrorContainer>
Expand Down
24 changes: 23 additions & 1 deletion src/utils/onboarding.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { isExtensionActive } from "@/backend/extension/messaging";
import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
import {
extensionInfo,
isExtensionActive,
} from "@/backend/extension/messaging";
import { conf } from "@/setup/config";
import { useAuthStore } from "@/stores/auth";
import { useOnboardingStore } from "@/stores/onboarding";

export type ExtensionStatus =
| "unknown"
| "failed"
| "disallowed"
| "noperms"
| "outdated"
| "success";

export async function getExtensionState(): Promise<ExtensionStatus> {
const info = await extensionInfo();
if (!info) return "unknown"; // cant talk to extension
if (!info.success) return "failed"; // extension failed to respond
if (!info.allowed) return "disallowed"; // extension is not enabled on this page
if (!info.hasPermission) return "noperms"; // extension has no perms to do it's tasks
if (!isAllowedExtensionVersion(info.version)) return "outdated"; // extension is too old
return "success"; // no problems
}

export async function needsOnboarding(): Promise<boolean> {
// if onboarding is dislabed, no onboarding needed
if (!conf().HAS_ONBOARDING) return false;
Expand Down

0 comments on commit 075ec65

Please sign in to comment.