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

Add permission request to settings #3649

Merged
merged 2 commits into from
Apr 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"message": "General",
"description": "'General' section"
},
"optionsPermissionsLabel": {
"message": "You have not granted Web Scrobbler the permission to access all sites, which means you have to click the extension icon to start scrobbling every time. Click here to grant the permission. Note that in safari the permission will not automatically be granted, but the next time you visit a supported site you will be able to click \"Always Allow on Every Website...\" to grant the permission.",
"description": "Label for permissions button"
},
"optionUseNotifications": {
"message": "Use now playing notifications",
"description": "Option label"
Expand Down
16 changes: 16 additions & 0 deletions src/ui/options/components/components.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,19 @@ summary {
margin-left: 0.5em;
padding: 0.25em 0.5em;
}

.permissionsPopup {
background-color: var(--sidebar-bg);
color: var(--text);
margin: 0;
text-align: center;

button {
background: none;
border: none;
cursor: pointer;
font-size: 1.25em;
margin: 0;
padding: 1em 15vw;
}
}
35 changes: 35 additions & 0 deletions src/ui/options/components/permissions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Setter, Show, createSignal } from 'solid-js';
import browser from 'webextension-polyfill';
import styles from './components.module.scss';
import { t } from '@/util/i18n';

const desiredPermissions = {
origins: ['http://*/', 'https://*/'],
};

export default function Permissions() {
const [perms, setPerms] = createSignal(true);
hasPermissions(setPerms);

return (
<Show when={!perms()}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? how does perms() know what perms are set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perms is the getter function of a signal

The signal is set to true by default, then the signal setter setPerms is uses to set it to false if perms are not granted, and set back to true if the user grants permission.

Signals allow the component to be reactive – solid only renders the component once, so if you just use a raw variable it will not change what is rendered when you change the variable value. Signals (and other similar solid utilities) tell the component to actually change what is rendered too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation thought it was something like that :)

<div class={styles.permissionsPopup}>
<button
type="button"
onClick={() => {
// This is inline and not async, as when it was not there were some issues with safari isTrusted
browser.permissions
.request(desiredPermissions)
.then(setPerms);
}}
>
{t('optionsPermissionsLabel')}
</button>
</div>
</Show>
);
}

async function hasPermissions(setPerms: Setter<boolean>) {
setPerms(await browser.permissions.contains(desiredPermissions));
}
2 changes: 2 additions & 0 deletions src/ui/options/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ContactComponent from './components/contact';
import OptionsComponent from './components/options/options';
import Accounts from './components/accounts';
import { EditsModal } from './components/options/edited-tracks';
import Permissions from './components/permissions';

/**
* All the different options pages, their sidebar labels, and icons.
Expand Down Expand Up @@ -93,6 +94,7 @@ function Options() {

return (
<>
<Permissions />
<div class={styles.settings}>
<Sidebar
items={settings}
Expand Down
7 changes: 6 additions & 1 deletion src/ui/options/settings.module.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
body {
background-color: var(--background);
color: var(--text);
font-family: Helvetica, Arial, sans-serif;
font-size: 14px;
margin: 0;
padding: 0;
width: 100%;
}

body,
button,
input {
font-family: Helvetica, Arial, sans-serif;
}

a {
color: var(--last-fm);
text-decoration: none;
Expand Down