mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: implement modal component for password input and confirmation actions
This commit is contained in:
@@ -0,0 +1,76 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
export let show = false;
|
||||||
|
export let mode: 'decrypt' | 'encrypt' | 'confirm' = 'decrypt';
|
||||||
|
export let title = '';
|
||||||
|
export let error = '';
|
||||||
|
export let onConfirm: (value: string | boolean) => void;
|
||||||
|
export let onCancel: () => void;
|
||||||
|
|
||||||
|
let inputValue = '';
|
||||||
|
|
||||||
|
$: if (!show) {
|
||||||
|
inputValue = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSubmit() {
|
||||||
|
if (mode === 'confirm') {
|
||||||
|
onConfirm(true);
|
||||||
|
} else {
|
||||||
|
onConfirm(inputValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$: displayTitle =
|
||||||
|
title ||
|
||||||
|
(mode === 'decrypt'
|
||||||
|
? 'Enter password to decrypt'
|
||||||
|
: mode === 'encrypt'
|
||||||
|
? 'Enter password to encrypt'
|
||||||
|
: 'Confirm action');
|
||||||
|
|
||||||
|
$: confirmLabel = mode === 'decrypt' ? 'Decrypt' : mode === 'encrypt' ? 'Encrypt' : 'Confirm';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if show}
|
||||||
|
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm">
|
||||||
|
<form
|
||||||
|
on:submit|preventDefault={handleSubmit}
|
||||||
|
class="w-full max-w-md rounded bg-neutral-900 p-6"
|
||||||
|
>
|
||||||
|
<h2 class="mb-4 text-xl font-semibold text-white">
|
||||||
|
{displayTitle}
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
{#if mode !== 'confirm'}
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
bind:value={inputValue}
|
||||||
|
class="mb-3 w-full rounded border border-neutral-700 bg-neutral-800 p-2 text-white outline-none"
|
||||||
|
placeholder="Enter value..."
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if error}
|
||||||
|
<div class="mb-3 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="flex-1 cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white hover:bg-neutral-800"
|
||||||
|
>
|
||||||
|
{confirmLabel}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
on:click={onCancel}
|
||||||
|
class="cursor-pointer rounded border border-neutral-700 px-4 py-2 text-white hover:bg-neutral-950"
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
@@ -34,18 +34,18 @@
|
|||||||
import { getToken } from '$lib/utils/auth';
|
import { getToken } from '$lib/utils/auth';
|
||||||
import { user } from '$lib/stores/user';
|
import { user } from '$lib/stores/user';
|
||||||
import Dropdown from '$lib/components/Dropdown.svelte';
|
import Dropdown from '$lib/components/Dropdown.svelte';
|
||||||
|
import Modal from '$lib/components/Modal.svelte';
|
||||||
import type { Paste } from '$lib/types/paste';
|
import type { Paste } from '$lib/types/paste';
|
||||||
export let data: PageData;
|
export let data: PageData;
|
||||||
|
|
||||||
let showPasswordModal = false;
|
let showModal = false;
|
||||||
let passwordModalResolve: ((password: string) => void) | null = null;
|
let modalResolve: ((value: string) => void) | null = null;
|
||||||
let passwordModalMode: 'decrypt' | 'encrypt' = 'decrypt';
|
let modalMode: 'decrypt' | 'encrypt' | 'confirm' = 'decrypt';
|
||||||
let editing = false;
|
let editing = false;
|
||||||
let editContent: string | null = null;
|
let editContent: string | null = null;
|
||||||
let editMetadata: Paste | null = data.paste ? { ...data.paste } : null;
|
let editMetadata: Paste | null = data.paste ? { ...data.paste } : null;
|
||||||
let editError = '';
|
let editError = '';
|
||||||
let editLoading = false;
|
let editLoading = false;
|
||||||
let passwordInput = '';
|
|
||||||
let pasteContent = '';
|
let pasteContent = '';
|
||||||
let decryptedContent: string | null = null;
|
let decryptedContent: string | null = null;
|
||||||
let decryptError = '';
|
let decryptError = '';
|
||||||
@@ -162,7 +162,7 @@
|
|||||||
) {
|
) {
|
||||||
let rawContent = content;
|
let rawContent = content;
|
||||||
if (metadata?.visibility === 2 || data.paste.visibility === 2) {
|
if (metadata?.visibility === 2 || data.paste.visibility === 2) {
|
||||||
const password = await promptPassword('encrypt');
|
const password = await promptUser('encrypt');
|
||||||
if (!password) {
|
if (!password) {
|
||||||
editError = 'Password required for encryption.';
|
editError = 'Password required for encryption.';
|
||||||
editLoading = false;
|
editLoading = false;
|
||||||
@@ -230,11 +230,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function promptPassword(mode: 'decrypt' | 'encrypt' = 'decrypt'): Promise<string> {
|
async function promptUser(mode: 'decrypt' | 'encrypt' | 'confirm' = 'decrypt'): Promise<string> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
showPasswordModal = true;
|
showModal = true;
|
||||||
passwordModalResolve = resolve;
|
modalResolve = resolve;
|
||||||
passwordModalMode = mode;
|
modalMode = mode;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@
|
|||||||
const passwordFromUrl = atob(urlHash);
|
const passwordFromUrl = atob(urlHash);
|
||||||
decryptedContent = await decryptAES(data.content, passwordFromUrl);
|
decryptedContent = await decryptAES(data.content, passwordFromUrl);
|
||||||
if (decryptedContent === null) {
|
if (decryptedContent === null) {
|
||||||
const password = await promptPassword('decrypt');
|
const password = await promptUser('decrypt');
|
||||||
if (password) {
|
if (password) {
|
||||||
decryptedContent = await decryptAES(data.content, password);
|
decryptedContent = await decryptAES(data.content, password);
|
||||||
if (decryptedContent === null) {
|
if (decryptedContent === null) {
|
||||||
@@ -262,7 +262,7 @@
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const password = await promptPassword('decrypt');
|
const password = await promptUser('decrypt');
|
||||||
if (password) {
|
if (password) {
|
||||||
decryptedContent = await decryptAES(data.content, password);
|
decryptedContent = await decryptAES(data.content, password);
|
||||||
if (decryptedContent === null) {
|
if (decryptedContent === null) {
|
||||||
@@ -306,7 +306,7 @@
|
|||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
</h1>
|
</h1>
|
||||||
<div class="mt-1 flex items-center justify-center gap-2 text-sm text-neutral-400">
|
<div class="flex items-center justify-center gap-2 text-sm text-neutral-400">
|
||||||
<User class="inline-block h-4 w-4 text-neutral-400" />
|
<User class="inline-block h-4 w-4 text-neutral-400" />
|
||||||
{#if data.paste.author}
|
{#if data.paste.author}
|
||||||
<a
|
<a
|
||||||
@@ -540,6 +540,27 @@
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
class="flex cursor-pointer items-center rounded-md bg-neutral-700 px-2 py-0.5 text-sm hover:bg-red-900"
|
class="flex cursor-pointer items-center rounded-md bg-neutral-700 px-2 py-0.5 text-sm hover:bg-red-900"
|
||||||
|
on:click={() => {
|
||||||
|
promptUser('confirm').then(async (value) => {
|
||||||
|
if (value) {
|
||||||
|
const token = getToken();
|
||||||
|
if (!token) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const res = await fetch(`/api/paste/${data.paste?.id}/delete`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
window.location.href = resolve('/');
|
||||||
|
} else {
|
||||||
|
alert('Failed to delete paste.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Trash2 class="mr-1 h-5 w-5 text-red-400" />
|
<Trash2 class="mr-1 h-5 w-5 text-red-400" />
|
||||||
<span class="text-red-300">Delete</span>
|
<span class="text-red-300">Delete</span>
|
||||||
@@ -607,57 +628,34 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if showPasswordModal}
|
<Modal
|
||||||
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm">
|
bind:show={showModal}
|
||||||
<form
|
mode={modalMode}
|
||||||
on:submit|preventDefault={() => {
|
error={decryptError}
|
||||||
if (passwordModalResolve) {
|
onConfirm={(value) => {
|
||||||
passwordModalResolve(passwordInput);
|
if (modalResolve) {
|
||||||
passwordModalResolve = null;
|
modalResolve(String(value));
|
||||||
|
modalResolve = null;
|
||||||
}
|
}
|
||||||
showPasswordModal = false;
|
showModal = false;
|
||||||
decryptError = '';
|
decryptError = '';
|
||||||
}}
|
}}
|
||||||
class="w-full max-w-md rounded bg-neutral-900 p-6"
|
onCancel={() => {
|
||||||
>
|
showModal = false;
|
||||||
<h2 class="mb-4 text-xl font-semibold">
|
decryptError = '';
|
||||||
{passwordModalMode === 'decrypt'
|
if (modalResolve) {
|
||||||
? 'Enter password to decrypt'
|
modalResolve('');
|
||||||
: 'Enter password to encrypt'}
|
modalResolve = null;
|
||||||
</h2>
|
}
|
||||||
<input
|
}}
|
||||||
type="text"
|
title={
|
||||||
bind:value={passwordInput}
|
modalMode === 'decrypt'
|
||||||
class="mb-3 w-full rounded border border-neutral-700 bg-neutral-800 p-2"
|
? 'Decrypt Paste'
|
||||||
|
: modalMode === 'encrypt'
|
||||||
|
? 'Encrypt Paste'
|
||||||
|
: 'Are you sure you want to delete this paste?'
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
{#if decryptError}
|
|
||||||
<div class="mb-3 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200">
|
|
||||||
{decryptError}
|
|
||||||
</div>
|
|
||||||
{/if}
|
|
||||||
<div class="flex gap-2">
|
|
||||||
<button
|
|
||||||
type="submit"
|
|
||||||
class="flex-1 cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white hover:bg-neutral-800"
|
|
||||||
>{passwordModalMode === 'decrypt' ? 'Decrypt' : 'OK'}</button
|
|
||||||
>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
on:click={() => {
|
|
||||||
showPasswordModal = false;
|
|
||||||
decryptError = '';
|
|
||||||
if (passwordModalResolve) {
|
|
||||||
passwordModalResolve('');
|
|
||||||
passwordModalResolve = null;
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
class="cursor-pointer rounded border border-neutral-700 px-4 py-2 hover:bg-neutral-950"
|
|
||||||
>Cancel</button
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.hljs {
|
.hljs {
|
||||||
|
|||||||
Reference in New Issue
Block a user