Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/767 accept language #1334

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.idea
1 change: 0 additions & 1 deletion desktop-app/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import path from 'path';
import { app, BrowserWindow, shell, screen, ipcMain } from 'electron';
import { setupTitlebar } from 'custom-electron-titlebar/main';
import { useState } from 'react';
import cli from './cli';
import { PROTOCOL } from '../common/constants';
import MenuBuilder from './menu';
Expand Down
13 changes: 13 additions & 0 deletions desktop-app/src/main/web-permissions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BrowserWindow, session } from 'electron';
import PermissionsManager, { PERMISSION_STATE } from './PermissionsManager';
import store from '../../store';

// eslint-disable-next-line import/prefer-default-export
export const WebPermissionHandlers = (mainWindow: BrowserWindow) => {
Expand All @@ -25,6 +26,18 @@ export const WebPermissionHandlers = (mainWindow: BrowserWindow) => {
return status === PERMISSION_STATE.GRANTED;
}
);

session.defaultSession.webRequest.onBeforeSendHeaders(
{
urls: ['<all_urls>'],
},
(details, callback) => {
details.requestHeaders['Accept-Language'] = store.get(
'userPreferences.webRequestHeaderAcceptLanguage'
);
callback({ requestHeaders: details.requestHeaders });
}
);
},
};
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, fireEvent, waitFor } from '@testing-library/react';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { FileUploader, FileUploaderProps } from './FileUploader';
import { useFileUpload } from './hooks';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as React from 'react';

import { render, fireEvent } from '@testing-library/react';

import { SettingsContent } from './SettingsContent';

const mockOnClose = jest.fn();

describe('SettingsContentHeader', () => {
const renderComponent = () =>
render(<SettingsContent onClose={mockOnClose} />);

it('Accept-Language is saved to store', () => {
const { getByTestId } = renderComponent();

const acceptLanguageInput = getByTestId('settings-accept_language-input');
const screenshotLocationInput = getByTestId(
'settings-screenshot_location-input'
);
const saveButton = getByTestId('settings-save-button');

fireEvent.change(acceptLanguageInput, { target: { value: 'cz-Cz' } });
fireEvent.change(screenshotLocationInput, {
target: { value: './path/location' },
});
fireEvent.click(saveButton);

expect(window.electron.store.set).toHaveBeenNthCalledWith(
1,
'userPreferences.screenshot.saveLocation',
'./path/location'
);
expect(window.electron.store.set).toHaveBeenNthCalledWith(
2,
'userPreferences.customTitlebar',
undefined
);
expect(window.electron.store.set).toHaveBeenNthCalledWith(
3,
'userPreferences.webRequestHeaderAcceptLanguage',
'cz-Cz'
);

expect(mockOnClose).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useId, useState } from 'react';

import Button from 'renderer/components/Button';
import Toggle from 'renderer/components/Toggle';
import { SettingsContentHeaders } from './SettingsContentHeaders';

interface Props {
onClose: () => void;
Expand All @@ -16,6 +17,13 @@ export const SettingsContent = ({ onClose }: Props) => {
window.electron.store.get('userPreferences.customTitlebar')
);

const [webRequestHeaderAcceptLanguage, setWebRequestHeaderAcceptLanguage] =
useState<string>(
window.electron.store.get(
'userPreferences.webRequestHeaderAcceptLanguage'
)
);

const onSave = () => {
if (screenshotSaveLocation === '' || screenshotSaveLocation == null) {
// eslint-disable-next-line no-alert
Expand All @@ -32,6 +40,11 @@ export const SettingsContent = ({ onClose }: Props) => {
enableCustomTitlebar
);

window.electron.store.set(
'userPreferences.webRequestHeaderAcceptLanguage',
webRequestHeaderAcceptLanguage
);

onClose();
};

Expand All @@ -43,6 +56,7 @@ export const SettingsContent = ({ onClose }: Props) => {
<label htmlFor={id} className="flex flex-col">
Location
<input
data-testid="settings-screenshot_location-input"
type="text"
id={id}
className="mt-2 rounded-md border border-gray-300 px-4 py-2 text-base focus-visible:outline-gray-400 dark:border-gray-500 dark:bg-slate-900"
Expand All @@ -56,7 +70,12 @@ export const SettingsContent = ({ onClose }: Props) => {
</div>
</div>

{(navigator as any).userAgentData.platform === 'Windows' && (
<SettingsContentHeaders
acceptLanguage={webRequestHeaderAcceptLanguage}
setAcceptLanguage={setWebRequestHeaderAcceptLanguage}
/>

{(navigator as any)?.userAgentData?.platform === 'Windows' && (
<>
<h2>Preferences</h2>
<div className="my-4 flex flex-col space-y-4 text-sm">
Expand All @@ -82,6 +101,7 @@ export const SettingsContent = ({ onClose }: Props) => {
)}

<Button
data-testid="settings-save-button"
className="mt-6 px-5 py-1"
onClick={onSave}
isPrimary
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FC, useId } from 'react';

interface ISettingsContentHeaders {
acceptLanguage: string;
setAcceptLanguage: (arg0: string) => void;
}

export const SettingsContentHeaders: FC<ISettingsContentHeaders> = ({
acceptLanguage = '',
setAcceptLanguage,
}) => {
const id = useId();

return (
<>
<h2>Request Headers</h2>
<div className="my-4 flex flex-col space-y-4 text-sm">
<div className="flex flex-col space-y-2">
<label htmlFor={id} className="flex flex-col">
Accept-Language
<input
data-testid="settings-accept_language-input"
type="text"
id={id}
placeholder="example: en-US"
className="mt-2 rounded-md border border-gray-300 px-4 py-2 text-base focus-visible:outline-gray-400 dark:border-gray-500 dark:bg-slate-900"
value={acceptLanguage}
onChange={(e) => setAcceptLanguage(e.target.value)}
/>
</label>
<p className="text-sm text-gray-500 dark:text-gray-400">
HTTP request Accept-Language parameter
</p>
</div>
</div>
</>
);
};
4 changes: 4 additions & 0 deletions desktop-app/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ const schema = {
userPreferences: {
type: 'object',
properties: {
webRequestHeaderAcceptLanguage: {
type: 'string',
default: 'us-US',
},
allowInsecureSSLConnections: {
type: 'boolean',
default: false,
Expand Down
Loading