Fix DeepL on Web Readest and avoid cli parsing for Web

This commit is contained in:
chrox
2024-12-06 13:36:38 +01:00
parent 242fbf6743
commit b694203d26
3 changed files with 21 additions and 4 deletions
+8
View File
@@ -19,6 +19,14 @@ const nextConfig = {
// Configure assetPrefix or else the server won't properly resolve your assets.
assetPrefix: isProd ? '' : `http://${internalHost}:3000`,
reactStrictMode: true,
async rewrites() {
return [
{
source: '/api/deepl/:path*',
destination: 'https://api-free.deepl.com/v2/:path*',
},
];
},
};
export default nextConfig;
@@ -1,8 +1,8 @@
import React, { useEffect, useState } from 'react';
import Popup from '@/components/Popup';
import { Position } from '@/utils/sel';
import { fetch } from '@tauri-apps/plugin-http';
import { useSettingsStore } from '@/store/settingsStore';
import { isWebAppPlatform } from '@/services/environment';
const LANGUAGES = {
AUTO: 'Auto Detect',
@@ -65,8 +65,15 @@ const DeepLPopup: React.FC<DeepLPopupProps> = ({
console.error('DeepL API key not found. Set NEXT_PUBLIC_DEEPL_API_KEY in .env.local');
}
const { fetch, url } = isWebAppPlatform()
? { fetch: window.fetch, url: '/api/deepl/translate' }
: {
fetch: (await import('@tauri-apps/plugin-http')).fetch,
url: 'https://api-free.deepl.com/v2/translate',
};
try {
const response = await fetch('https://api-free.deepl.com/v2/translate', {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -82,7 +89,6 @@ const DeepLPopup: React.FC<DeepLPopupProps> = ({
if (!response.ok) {
throw new Error('Failed to fetch translation');
}
const data = await response.json();
const translatedText = data.translations[0]?.text;
const detectedSource = data.translations[0]?.detected_source_language;
+4 -1
View File
@@ -1,4 +1,4 @@
import { getMatches } from '@tauri-apps/plugin-cli';
import { isWebAppPlatform } from '@/services/environment';
declare global {
interface Window {
@@ -16,6 +16,7 @@ const parseWindowOpenWithFiles = () => {
};
const parseCLIOpenWithFiles = async () => {
const { getMatches } = await import('@tauri-apps/plugin-cli');
const matches = await getMatches();
const args = matches?.args;
const files: string[] = [];
@@ -32,6 +33,8 @@ const parseCLIOpenWithFiles = async () => {
};
export const parseOpenWithFiles = async () => {
if (isWebAppPlatform()) return [];
let files = parseWindowOpenWithFiles();
if (!files) {
files = await parseCLIOpenWithFiles();