Skip to content

Commit

Permalink
feat: encapsulated functional components
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Mar 15, 2023
1 parent a157351 commit 57520dd
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 90 deletions.
11 changes: 2 additions & 9 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,9 @@ onMounted(async () => {
正在与 <span class="mark">{{ currentRole?.name }}</span> 对话
</div>
<!-- TODO: 没有必要全部拆分,待功能完善将不必要的拆分直接挪到这边或者移步到一个组件即可 -->
<Increase />
<Function />
<ReAnswer />
<Delete />
<History />
<Settings />
<!-- <Settings /> -->
</div>
<Input />
Expand Down
4 changes: 0 additions & 4 deletions src/assets/css/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ body {
}
}

.arco-icon {
cursor: pointer;
}

.mark {
color: rgb(var(--arcoblue-6));
background-color: transparent;
Expand Down
14 changes: 0 additions & 14 deletions src/components/Delete/index.vue

This file was deleted.

73 changes: 73 additions & 0 deletions src/components/Function/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<script setup lang="ts">
import {
IconPlusCircle,
IconRefresh,
IconDelete,
IconHistory
} from '@arco-design/web-vue/es/icon'
import { useRecordStore } from '@/stores'
import { getArrayLength } from '@/utils'
const recordStore = useRecordStore()
const { createNewRecord, getAiMessage, deleteRecord } = recordStore
const { currentRecord, isThinking } = storeToRefs(recordStore)
const functions = computed(() => [
{
content: '新建对话',
icon: IconPlusCircle,
disabled: !!currentRecord,
handleClick: createNewRecord
},
{
content: '重新回答',
icon: IconRefresh,
disabled:
isThinking.value || !(getArrayLength(currentRecord.value?.data) > 2),
handleClick: () => getAiMessage()
},
{
content: '清空对话',
icon: IconDelete,
disabled: !!currentRecord,
handleClick: () => deleteRecord()
},
{
content: '历史记录',
icon: IconHistory,
disabled: false,
handleClick: () => {
// empty
}
}
])
</script>

<template>
<div class="function flex gap-2">
<a-tooltip
v-for="(item, index) in functions"
:content="item.content"
:key="index"
>
<a-button type="text" :disabled="item.disabled" @click="item.handleClick">
<template #icon>
<component
:is="item.icon"
class="text-5.5 text-[var(--color-text-2)]"
/>
</template>
</a-button>
</a-tooltip>
</div>
</template>

<style scoped lang="scss">
.function {
.arco-btn.arco-btn-disabled {
.arco-icon {
color: var(--color-text-4);
}
}
}
</style>
11 changes: 0 additions & 11 deletions src/components/History/index.vue

This file was deleted.

12 changes: 0 additions & 12 deletions src/components/Increase/index.vue

This file was deleted.

27 changes: 0 additions & 27 deletions src/components/ReAnswer/index.vue

This file was deleted.

6 changes: 5 additions & 1 deletion src/components/Settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { IconSettings } from '@arco-design/web-vue/es/icon'
<template>
<a-popover trigger="click" position="tr" content-class="p-4 leading-none">
<a-tooltip content="设置" position="tr">
<IconSettings />
<a-button type="text">
<template #icon>
<IconSettings />
</template>
</a-button>
</a-tooltip>

<template #content>
Expand Down
35 changes: 23 additions & 12 deletions src/stores/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ export const useRecordStore = defineStore('recordStore', () => {
const getAiMessage = async (value?: string) => {
isThinking.value = true

if (!currentRecord.value) {
// TODO: 新建的记录的 sql 参数包含角色,后期考虑如何加
if (!currentRecord.value && value) {
const { currentRole } = useRoleStore()

const newRecord: TablePayload = {
title: value,
data: []
}

const { currentRole } = useRoleStore()

newRecord.data?.push(
{
role: 'system',
Expand All @@ -37,8 +38,13 @@ export const useRecordStore = defineStore('recordStore', () => {
)

currentRecord.value = newRecord

addRecord()
} else if (!value) {
value = currentRecord.value.data?.at(-1)?.content
const newRecord = { ...currentRecord.value }
newRecord.data?.pop()

currentRecord.value = newRecord
}

const result = await getOpenAIResultApi()
Expand All @@ -47,7 +53,9 @@ export const useRecordStore = defineStore('recordStore', () => {

if (!result) return

currentRecord.value.data?.push(result?.message)
currentRecord.value?.data?.push(result?.message)

updateRecord()
}

const getRecord = async () => {
Expand All @@ -65,21 +73,24 @@ export const useRecordStore = defineStore('recordStore', () => {
}
}

const addRecord = async (payload: TablePayload) => {
await insertSQL('history', payload)
const addRecord = async () => {
await insertSQL('history', currentRecord.value!)

getRecord()
}

const updateRecord = async (payload: TablePayload) => {
await updateSQL('history', payload)
const updateRecord = async () => {
await updateSQL('history', currentRecord.value!)

getRecord()
}

// TODO:删除这块有 bug,在新对话时考虑禁用或者其它方法
const deleteRecord = async (id?: number) => {
await deleteSQL('history', id)
const deleteRecord = async (deleteAll = false) => {
if (deleteAll) {
await deleteSQL('history')
} else {
await deleteSQL('history', currentRecord.value?.id)
}

getRecord()
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ export const isString = (value: any) => typeof value === 'string'
* @param value 数据
*/
export const isObject = (value: any) => typeof value === 'object'

/**
* 获取数组长度
* @param value 数据
*/
export const getArrayLength = (value?: any[]) => value?.length ?? 0

0 comments on commit 57520dd

Please sign in to comment.