Skip to content

Commit

Permalink
Add error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ksdev-pl committed Apr 4, 2024
1 parent 30073ad commit 62cb805
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
24 changes: 22 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ai-chat",
"version": "1.2.0",
"version": "1.3.0",
"private": true,
"type": "module",
"scripts": {
Expand All @@ -21,6 +21,7 @@
"markdown-it": "^14.1.0",
"openai": "^4.31.0",
"pinia": "^2.1.7",
"uuid": "^9.0.1",
"validator": "^13.11.0",
"vue": "^3.4.21",
"zod": "^3.22.4"
Expand All @@ -31,6 +32,7 @@
"@types/highlight.js": "^10.1.0",
"@types/markdown-it": "^13.0.7",
"@types/node": "^20.11.25",
"@types/uuid": "^9.0.8",
"@types/validator": "^13.11.9",
"@vitejs/plugin-vue": "^5.0.4",
"@vue/eslint-config-prettier": "^8.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/components/AppAbout.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script setup lang="ts">
import {FwbButton, FwbModal} from 'flowbite-vue';
import {useAppStore} from '@/stores/app.store';
import {FwbButton, FwbModal} from 'flowbite-vue';
import {useAppStore} from '@/stores/app.store';
const appStore = useAppStore();
const appStore = useAppStore();
</script>

<template>
Expand All @@ -14,7 +14,7 @@ const appStore = useAppStore();
<div class="flex">
<div><img src="/icon-512.png" alt="Logo"/></div>
<div class="ml-5"><pre class="text-xs">
<b>AI Chat 1.2</b>
<b>AI Chat 1.3</b>

Source code, additional information and contact:
<a href="https://github.com/ksdev-pl/ai-chat" class="hover:text-blue-600">https://github.com/ksdev-pl/ai-chat</a>
Expand Down
32 changes: 24 additions & 8 deletions src/components/AppContents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import MarkdownIt from 'markdown-it';
import hljs from 'highlight.js';
import {useSettingsStore} from '@/stores/settings.store';
import {FwbButton, FwbSpinner} from 'flowbite-vue';
import {FwbAlert, FwbButton, FwbSpinner} from 'flowbite-vue';
import {useAppStore} from '@/stores/app.store';
const input = ref('');
const numOfInputRows = ref(1);
Expand All @@ -17,6 +18,7 @@
const userScrolled = ref(false);
const pending = ref(false);
const appStore = useAppStore();
const chatStore = useChatStore();
const settingsStore = useSettingsStore();
Expand Down Expand Up @@ -50,13 +52,19 @@
async function onSend() {
pending.value = true;
userScrolled.value = false;
inputTextarea.value?.blur();
await chatStore.addMessage({role: Role.user, content: input.value});
autoScrollDown();
sendRequestForTitle(input.value);
input.value = '';
await sendRequestForResponse();
try {
userScrolled.value = false;
inputTextarea.value?.blur();
await chatStore.addMessage({role: Role.user, content: input.value});
autoScrollDown();
sendRequestForTitle(input.value);
input.value = '';
await sendRequestForResponse();
} catch (e) {
if (e instanceof Error) {
appStore.addError(e.message);
}
}
pending.value = false;
}
Expand Down Expand Up @@ -113,6 +121,14 @@

<template>
<div class="flex flex-1 flex-col overflow-auto">
<fwb-alert closable
type="danger"
class="mt-4 ml-4 mr-4"
v-for="error in appStore.errors"
:key="error.id"
@close="appStore.removeError(error.id)">
{{error.message}}
</fwb-alert>
<main class="flex-1 p-4 overflow-auto" ref="scrollingDiv" @scroll="checkIfUserScrolled()">
<template v-if="chatStore.currentChat">
<template v-for="(message, index) in chatStore.currentChat.messages" :key="index">
Expand Down
15 changes: 14 additions & 1 deletion src/stores/app.store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {defineStore} from 'pinia';
import {ref} from 'vue';
import {v4 as uuidv4} from 'uuid';

export const useAppStore = defineStore('app', () => {
const isSidebarVisible = ref(true);
const isAboutVisible = ref(false);
const errors = ref<{id: string, message: string}[]>([]);

function toggleSidebar() {
isSidebarVisible.value = !isSidebarVisible.value;
Expand All @@ -17,11 +19,22 @@ export const useAppStore = defineStore('app', () => {
isAboutVisible.value = false;
}

function addError(error: string) {
errors.value.push({id: uuidv4(), message: error});
}

function removeError(id: string) {
errors.value = errors.value.filter(error => error.id !== id);
}

return {
isSidebarVisible,
isAboutVisible,
errors,
toggleSidebar,
showAbout,
hideAbout
hideAbout,
addError,
removeError
};
});

0 comments on commit 62cb805

Please sign in to comment.