aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Corentin THOMASSET <corentin.thomasset74@gmail.com> 2023-11-01 11:11:51 +0100
committerGravatar GitHub <noreply@github.com> 2023-11-01 10:11:51 +0000
commit02b0d0d1a13cb8bcbf7f98339278fd874a0cbc19 (patch)
tree7ecbf28e38d9382811d63d39ea76649ed72dc5c1 /src
parent4d5a67d96d68e8c7ce790cf95f34c8c15736845e (diff)
downloadit-tools-02b0d0d1a13cb8bcbf7f98339278fd874a0cbc19.tar.gz
it-tools-02b0d0d1a13cb8bcbf7f98339278fd874a0cbc19.tar.zst
it-tools-02b0d0d1a13cb8bcbf7f98339278fd874a0cbc19.zip
fix(encryption): alert on decryption error (#711)
* update(c-alert): Add variant 'error' * fix(encryption): Alert decryption error (#652) * feat(c-alert): added title * refactor(composable): mutualized computedCatch --------- Co-authored-by: code2933 <code2933@outlook.com>
Diffstat (limited to 'src')
-rw-r--r--src/composable/computed/catchedComputed.ts22
-rw-r--r--src/tools/encryption/encryption.vue12
-rw-r--r--src/ui/c-alert/c-alert.demo.vue10
-rw-r--r--src/ui/c-alert/c-alert.theme.ts13
-rw-r--r--src/ui/c-alert/c-alert.vue7
5 files changed, 58 insertions, 6 deletions
diff --git a/src/composable/computed/catchedComputed.ts b/src/composable/computed/catchedComputed.ts
new file mode 100644
index 0000000..fd00a12
--- /dev/null
+++ b/src/composable/computed/catchedComputed.ts
@@ -0,0 +1,22 @@
+import { type Ref, ref, watchEffect } from 'vue';
+
+export { computedCatch };
+
+function computedCatch<T, D>(getter: () => T, { defaultValue }: { defaultValue: D; defaultErrorMessage?: string }): [Ref<T | D>, Ref<string | undefined>];
+function computedCatch<T, D>(getter: () => T, { defaultValue, defaultErrorMessage = 'Unknown error' }: { defaultValue?: D; defaultErrorMessage?: string } = {}) {
+ const error = ref<string | undefined>();
+ const value = ref<T | D | undefined>();
+
+ watchEffect(() => {
+ try {
+ error.value = undefined;
+ value.value = getter();
+ }
+ catch (err) {
+ error.value = err instanceof Error ? err.message : err?.toString() ?? defaultErrorMessage;
+ value.value = defaultValue;
+ }
+ });
+
+ return [value, error] as const;
+}
diff --git a/src/tools/encryption/encryption.vue b/src/tools/encryption/encryption.vue
index 4a348f8..2ad47b5 100644
--- a/src/tools/encryption/encryption.vue
+++ b/src/tools/encryption/encryption.vue
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { AES, RC4, Rabbit, TripleDES, enc } from 'crypto-js';
+import { computedCatch } from '@/composable/computed/catchedComputed';
const algos = { AES, TripleDES, Rabbit, RC4 };
@@ -11,9 +12,10 @@ const cypherOutput = computed(() => algos[cypherAlgo.value].encrypt(cypherInput.
const decryptInput = ref('U2FsdGVkX1/EC3+6P5dbbkZ3e1kQ5o2yzuU0NHTjmrKnLBEwreV489Kr0DIB+uBs');
const decryptAlgo = ref<keyof typeof algos>('AES');
const decryptSecret = ref('my secret key');
-const decryptOutput = computed(() =>
- algos[decryptAlgo.value].decrypt(decryptInput.value, decryptSecret.value).toString(enc.Utf8),
-);
+const [decryptOutput, decryptError] = computedCatch(() => algos[decryptAlgo.value].decrypt(decryptInput.value, decryptSecret.value).toString(enc.Utf8), {
+ defaultValue: '',
+ defaultErrorMessage: 'Unable to decrypt your text',
+});
</script>
<template>
@@ -63,7 +65,11 @@ const decryptOutput = computed(() =>
/>
</div>
</div>
+ <c-alert v-if="decryptError" type="error" mt-12 title="Error while decrypting">
+ {{ decryptError }}
+ </c-alert>
<c-input-text
+ v-else
label="Your decrypted text:"
:value="decryptOutput"
placeholder="Your string hash"
diff --git a/src/ui/c-alert/c-alert.demo.vue b/src/ui/c-alert/c-alert.demo.vue
index 546c785..69f3ea4 100644
--- a/src/ui/c-alert/c-alert.demo.vue
+++ b/src/ui/c-alert/c-alert.demo.vue
@@ -1,11 +1,19 @@
<script lang="ts" setup>
-const variants = ['warning'] as const;
+const variants = ['warning', 'error'] as const;
</script>
<template>
+ <h2>Basic</h2>
<c-alert v-for="variant in variants" :key="variant" :type="variant" mb-4>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni reprehenderit itaque enim? Suscipit magni optio velit
quia, eveniet repellat pariatur quaerat laudantium dignissimos natus, beatae deleniti adipisci, atque necessitatibus
odio!
</c-alert>
+
+ <h2>With title</h2>
+ <c-alert v-for="variant in variants" :key="variant" :type="variant" title="This is the title" mb-4>
+ Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni reprehenderit itaque enim? Suscipit magni optio velit
+ quia, eveniet repellat pariatur quaerat laudantium dignissimos natus, beatae deleniti adipisci, atque necessitatibus
+ odio!
+ </c-alert>
</template>
diff --git a/src/ui/c-alert/c-alert.theme.ts b/src/ui/c-alert/c-alert.theme.ts
index 36d5d34..2176a10 100644
--- a/src/ui/c-alert/c-alert.theme.ts
+++ b/src/ui/c-alert/c-alert.theme.ts
@@ -3,6 +3,7 @@ import { defineThemes } from '../theme/theme.models';
import { appThemes } from '../theme/themes';
import WarningIcon from '~icons/mdi/alert-circle-outline';
+import ErrorIcon from '~icons/mdi/close-circle-outline';
export const { useTheme } = defineThemes({
dark: {
@@ -12,6 +13,12 @@ export const { useTheme } = defineThemes({
textColor: appThemes.dark.warning.color,
icon: WarningIcon,
},
+ error: {
+ backgroundColor: appThemes.dark.error.colorFaded,
+ borderColor: appThemes.dark.error.color,
+ textColor: appThemes.dark.error.color,
+ icon: ErrorIcon,
+ },
},
light: {
warning: {
@@ -20,5 +27,11 @@ export const { useTheme } = defineThemes({
textColor: darken(appThemes.light.warning.color, 40),
icon: WarningIcon,
},
+ error: {
+ backgroundColor: appThemes.light.error.colorFaded,
+ borderColor: appThemes.light.error.color,
+ textColor: darken(appThemes.light.error.color, 40),
+ icon: ErrorIcon,
+ },
},
});
diff --git a/src/ui/c-alert/c-alert.vue b/src/ui/c-alert/c-alert.vue
index 607acb9..8a3b55e 100644
--- a/src/ui/c-alert/c-alert.vue
+++ b/src/ui/c-alert/c-alert.vue
@@ -1,8 +1,8 @@
<script lang="ts" setup>
import { useTheme } from './c-alert.theme';
-const props = withDefaults(defineProps<{ type?: 'warning' }>(), { type: 'warning' });
-const { type } = toRefs(props);
+const props = withDefaults(defineProps<{ type?: 'warning'; title?: string }>(), { type: 'warning', title: undefined });
+const { type, title } = toRefs(props);
const theme = useTheme();
const variantTheme = computed(() => theme.value[type.value]);
@@ -17,6 +17,9 @@ const variantTheme = computed(() => theme.value[type.value]);
</div>
<div class="c-alert--content">
+ <div v-if="title" class="c-alert--title" text-15px fw-600>
+ {{ title }}
+ </div>
<slot />
</div>
</div>