import { AnimatedSlidersIcon } from "@domicile-desktop/component-library/AnimatedSlidersIcon";
import { Button } from "@domicile-desktop/component-library/Button";
import { Popover } from "@domicile-desktop/component-library/Popover";
import type { SelectOption } from "@domicile-desktop/component-library/Select";
import { Select } from "@domicile-desktop/component-library/Select";
import { PermissionAsk } from "@domicile-desktop/manganese-page-requests/PermissionAsk";
import {
PERMISSION_ICONS,
PERMISSION_LABELS,
} from "@domicile-desktop/manganese-page-requests/permission-names";
import type { SitePermission } from "@domicile-desktop/manganese-page-requests/site-permissions";
import type { PermissionRequest } from "@domicile-desktop/manganese-page-requests/usePermissionRequest";
import type {
WebViewMediaDeviceKind,
WebViewPermission,
WebViewPermissionSetting,
} from "@domicile-desktop/sdk/webview-element";
import { useCallback, useEffect, useState } from "react";
import { css } from "../../styled-system/css";
import { hstack, vstack } from "../../styled-system/patterns";
import type { MediaDeviceChoice, MediaDevices } from "./media-devices";
type Props = {
/**
* The cameras and microphones, to pick the one a page that names none
* gets. Nothing from an engine without them.
*/
devices?: MediaDeviceChoice | undefined;
/** Stores `setting` for `permission` on the page's site. */
onSet: (
permission: WebViewPermission,
setting: WebViewPermissionSetting,
) => void;
/** The page's site's settings; empty for a page with no site. */
permissions: readonly SitePermission[];
/** The request the page is waiting on, if any. Opens the panel. */
request: PermissionRequest | undefined;
/**
* The page's address, for the panel's heading. Must come from the same
* report as `permissions` (see `useShownPage`).
*/
url: string;
};
/**
* The site permissions button in the address bar, with a panel that answers
* the page's permission requests and edits the site's settings.
*
* The engine draws no prompt, so a request opens the panel. Closing it
* without an answer dismisses the request, as closing Chrome's prompt does.
*
* Under the camera and microphone, the device a page gets when it names none.
* The choice is the profile's, so the panel lists the devices each time it
* opens.
*/
export const SitePermissions = ({
devices,
onSet,
permissions,
request,
url,
}: Props) => {
// Whether the user opened the panel. A request opens it regardless.
const [browsing, setBrowsing] = useState(false);
const open = browsing || request !== undefined;
const [listed, prefer] = useMediaDevices(devices, open);
const changeOpen = (open: boolean) => {
if (!open && request !== undefined) {
request.dismiss();
}
setBrowsing(open);
};
return (
{hostOf(url)}Site permissions
}
trigger={
}
wide
>
{request === undefined ? undefined : (
)}
{permissions.length === 0 ? (
This page has no site permissions.
) : (