Skip to content

Commit

Permalink
fixed dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
daslyfe committed Aug 11, 2024
1 parent d80c06c commit e9a0a06
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
21 changes: 6 additions & 15 deletions website/src/repl/components/panel/SettingsTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isUdels } from '../../util.mjs';
import { ButtonGroup } from './Forms.jsx';
import { AudioDeviceSelector } from './AudioDeviceSelector.jsx';
import { AudioEngineTargetSelector } from './AudioEngineTargetSelector.jsx';
import { confirmDialog } from '../../util.mjs';
import { isTauri } from '@src/tauri.mjs';

function Checkbox({ label, value, onChange, disabled = false }) {
Expand Down Expand Up @@ -82,18 +83,6 @@ const fontFamilyOptions = {

const RELOAD_MSG = 'Changing this setting requires the window to reload itself. OK?';

function confirmDialog(msg) {
// confirm dialog is a promise in Tauri and possibly other browsers... normalize it to be a promise everywhere
return new Promise(function (resolve, reject) {
let confirmed = confirm(msg);
if (confirmed instanceof Promise) {
confirmed.then((r) => (r ? resolve(true) : reject(false)));
} else {
return confirmed ? resolve(true) : reject(false);
}
});
}

export function SettingsTab({ started }) {
const {
theme,
Expand Down Expand Up @@ -244,9 +233,11 @@ export function SettingsTab({ started }) {
<button
className="bg-background p-2 max-w-[300px] rounded-md hover:opacity-50"
onClick={() => {
if (confirm('Sure?')) {
settingsMap.set(defaultSettings);
}
confirmDialog('Sure?').then((r) => {
if (r) {
settingsMap.set(defaultSettings);
}
})
}}
>
restore default settings
Expand Down
57 changes: 34 additions & 23 deletions website/src/repl/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ export function loadModules() {

return evalScope(settingPatterns, ...modules);
}
// confirm dialog is a promise in Tauri and possibly other browsers... normalize it to be a promise everywhere
export function confirmDialog(msg) {
const confirmed = confirm(msg);
if (confirmed instanceof Promise) {
return confirmed;
}
return new Promise((resolve) => {
resolve(confirmed)
})
}

let lastShared;
export async function shareCode(codeToShare) {
Expand All @@ -105,31 +115,32 @@ export async function shareCode(codeToShare) {
logger(`Link already generated!`, 'error');
return;
}
const isPublic = confirm(
'Do you want your pattern to be public? If no, press cancel and you will get just a private link.',
);
// generate uuid in the browser
const hash = nanoid(12);
const shareUrl = window.location.origin + window.location.pathname + '?' + hash;
const { error } = await supabase.from('code_v1').insert([{ code: codeToShare, hash, ['public']: isPublic }]);
if (!error) {
lastShared = codeToShare;
// copy shareUrl to clipboard
if (isTauri()) {
await writeText(shareUrl);

confirmDialog('Do you want your pattern to be public? If no, press cancel and you will get just a private link.').then(async (isPublic) => {
const hash = nanoid(12);
const shareUrl = window.location.origin + window.location.pathname + '?' + hash;
const { error } = await supabase.from('code_v1').insert([{ code: codeToShare, hash, ['public']: isPublic }]);
if (!error) {
lastShared = codeToShare;
// copy shareUrl to clipboard
if (isTauri()) {
await writeText(shareUrl);
} else {
await navigator.clipboard.writeText(shareUrl);
}
const message = `Link copied to clipboard: ${shareUrl}`;
alert(message);
// alert(message);
logger(message, 'highlight');
} else {
await navigator.clipboard.writeText(shareUrl);
console.log('error', error);
const message = `Error: ${error.message}`;
// alert(message);
logger(message);
}
const message = `Link copied to clipboard: ${shareUrl}`;
alert(message);
// alert(message);
logger(message, 'highlight');
} else {
console.log('error', error);
const message = `Error: ${error.message}`;
// alert(message);
logger(message);
}

})

}

export const ReplContext = createContext(null);
Expand Down
24 changes: 13 additions & 11 deletions website/src/user_pattern_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useStore } from '@nanostores/react';
import { logger } from '@strudel/core';
import { nanoid } from 'nanoid';
import { settingsMap } from './settings.mjs';
import { parseJSON, supabase } from './repl/util.mjs';
import { confirmDialog, parseJSON, supabase } from './repl/util.mjs';

export let $publicPatterns = atom([]);
export let $featuredPatterns = atom([]);
Expand Down Expand Up @@ -131,17 +131,19 @@ export const userPattern = {
return this.update(newPattern.id, { ...newPattern.data, code: data.code });
},
clearAll() {
if (!confirm(`This will delete all your patterns. Are you really sure?`)) {
return;
}
const viewingPatternData = getViewingPatternData();
setUserPatterns({});
confirmDialog(`This will delete all your patterns. Are you really sure?`).then((r) => {
if (r == false) {
return;
}
const viewingPatternData = getViewingPatternData();
setUserPatterns({});

if (viewingPatternData.collection !== this.collection) {
return { id: viewingPatternData.id, data: viewingPatternData };
}
setActivePattern(null);
return this.create();
if (viewingPatternData.collection !== this.collection) {
return { id: viewingPatternData.id, data: viewingPatternData };
}
setActivePattern(null);
return this.create();
});
},
delete(id) {
const userPatterns = this.getAll();
Expand Down

0 comments on commit e9a0a06

Please sign in to comment.