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

POC: Use modal dialog instead of notification when clipboard usage fails #7438

Closed
Closed
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
57 changes: 17 additions & 40 deletions src/utils/clipboardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import pDefer from "p-defer";
import notify, { hideNotification } from "@/utils/notify";
import { BusinessError } from "@/errors/businessErrors";
import legacyCopyText from "copy-text-to-clipboard";
import { getErrorMessage } from "@/errors/errorHelpers";
Expand Down Expand Up @@ -55,48 +53,27 @@ async function interactiveWriteToClipboard(
): Promise<void> {
try {
await clipboardFn();
return;
} catch (error) {
if (isDocumentFocusError(error)) {
const copyPromise = pDefer<void>();

const notificationId = notify.info({
message: `Click anywhere to copy ${type} to clipboard`,
autoDismissTimeMs: Number.POSITIVE_INFINITY,
dismissable: false,
});

const handler = async () => {
try {
hideNotification(notificationId);
await clipboardFn();
copyPromise.resolve();
} catch (error) {
if (isDocumentFocusError(error)) {
copyPromise.reject(
new BusinessError(
"Your Browser was unable to determine the user action that initiated the clipboard write.",
),
);
return;
}

copyPromise.reject(error);
}
};

document.body.addEventListener("click", handler);

try {
await copyPromise.promise;
} finally {
document.body.removeEventListener("click", handler);
}
if (!isDocumentFocusError(error)) {
throw error;
}
}

// Remember to return to avoid falling through to the original error
return;
// eslint-disable-next-line no-alert -- Easiest UI for this, we can revisit later
alert('Please click "OK" to allow PixieBrix to copy to your clipboard.');

try {
await clipboardFn();
} catch (error) {
if (!isDocumentFocusError(error)) {
throw error;
}

throw error;
throw new BusinessError(
"Your Browser was unable to determine the user action that initiated the clipboard write.",
{ cause: error },
);
}
}

Expand Down
Loading