From 0e69a11ad7ac87691c29e373c1105bad812e6453 Mon Sep 17 00:00:00 2001 From: Evan Yang <251205668@qq.com> Date: Fri, 17 Mar 2023 17:35:26 +0800 Subject: [PATCH] feat: add eslint verification configuration, add lint CI workflows (#284) * feat: add eslint configuration * chore: update pnpm version --- .eslintignore | 7 + .eslintrc.js | 31 + .github/workflows/lint.yml | 31 + .npmrc | 1 + .vscode/extensions.json | 2 +- .vscode/settings.json | 13 + package.json | 12 +- plugins/vercelDisableBlocks.ts | 4 +- pnpm-lock.yaml | 2271 +++++++++++++++++++++++-- shims.d.ts | 4 +- src/components/BackTop.astro | 51 +- src/components/ErrorMessageItem.tsx | 2 +- src/components/Generator.tsx | 63 +- src/components/Logo.astro | 2 +- src/components/MessageItem.tsx | 19 +- src/components/SystemRoleSettings.tsx | 2 +- src/components/Themetoggle.astro | 34 +- src/components/icons/Clear.tsx | 4 +- src/components/icons/Env.tsx | 4 +- src/components/icons/Refresh.tsx | 4 +- src/env.d.ts | 15 + src/layouts/Layout.astro | 120 +- src/pages/api/auth.ts | 2 +- src/pages/api/generate.ts | 30 +- src/pages/index.astro | 4 +- src/pages/password.astro | 5 +- src/utils/auth.ts | 7 +- src/utils/openAI.ts | 10 +- unocss.config.ts | 11 +- 29 files changed, 2392 insertions(+), 373 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc.js create mode 100644 .github/workflows/lint.yml create mode 100644 .vscode/settings.json diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 000000000..59a29c9bc --- /dev/null +++ b/.eslintignore @@ -0,0 +1,7 @@ +dist +public +node_modules +.netlify +.vercel +.github +.changeset diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 000000000..242f49bf2 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,31 @@ +module.exports = { + extends: ['@evan-yang', 'plugin:astro/recommended'], + rules: { + 'no-console': ['error', { allow: ['error'] }], + 'react/display-name': 'off', + 'react-hooks/rules-of-hooks': 'off', + '@typescript-eslint/no-use-before-define': 'off', + }, + overrides: [ + { + files: ['*.astro'], + parser: 'astro-eslint-parser', + parserOptions: { + parser: '@typescript-eslint/parser', + extraFileExtensions: ['.astro'], + }, + rules: { + 'no-mixed-spaces-and-tabs': ['error', 'smart-tabs'], + }, + }, + { + // Define the configuration for ` diff --git a/src/components/ErrorMessageItem.tsx b/src/components/ErrorMessageItem.tsx index b0c8a759f..fcdf72f98 100644 --- a/src/components/ErrorMessageItem.tsx +++ b/src/components/ErrorMessageItem.tsx @@ -1,5 +1,5 @@ -import type { ErrorMessage } from '@/types' import IconRefresh from './icons/Refresh' +import type { ErrorMessage } from '@/types' interface Props { data: ErrorMessage diff --git a/src/components/Generator.tsx b/src/components/Generator.tsx index ab8d73414..3a64990b4 100644 --- a/src/components/Generator.tsx +++ b/src/components/Generator.tsx @@ -1,11 +1,11 @@ -import type { ChatMessage, ErrorMessage } from '@/types' -import { createSignal, Index, Show, onMount, onCleanup } from 'solid-js' +import { Index, Show, createSignal, onCleanup, onMount } from 'solid-js' +import { useThrottleFn } from 'solidjs-use' +import { generateSignature } from '@/utils/auth' import IconClear from './icons/Clear' import MessageItem from './MessageItem' import SystemRoleSettings from './SystemRoleSettings' import ErrorMessageItem from './ErrorMessageItem' -import { generateSignature } from '@/utils/auth' -import { useThrottleFn } from 'solidjs-use' +import type { ChatMessage, ErrorMessage } from '@/types' export default () => { let inputRef: HTMLTextAreaElement @@ -17,19 +17,17 @@ export default () => { const [loading, setLoading] = createSignal(false) const [controller, setController] = createSignal(null) - onMount(() => { try { - if (localStorage.getItem('messageList')) { + if (localStorage.getItem('messageList')) setMessageList(JSON.parse(localStorage.getItem('messageList'))) - } - if (localStorage.getItem('systemRoleSettings')) { + + if (localStorage.getItem('systemRoleSettings')) setCurrentSystemRoleSettings(localStorage.getItem('systemRoleSettings')) - } } catch (err) { console.error(err) } - + window.addEventListener('beforeunload', handleBeforeUnload) onCleanup(() => { window.removeEventListener('beforeunload', handleBeforeUnload) @@ -41,12 +39,13 @@ export default () => { localStorage.setItem('systemRoleSettings', currentSystemRoleSettings()) } - const handleButtonClick = async () => { + const handleButtonClick = async() => { const inputValue = inputRef.value - if (!inputValue) { + if (!inputValue) return - } - // @ts-ignore + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error if (window?.umami) umami.trackEvent('chat_generate') inputRef.value = '' setMessageList([ @@ -63,7 +62,7 @@ export default () => { window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' }) }, 300, false, true) - const requestWithLatestMessage = async () => { + const requestWithLatestMessage = async() => { setLoading(true) setCurrentAssistantMessage('') setCurrentError(null) @@ -99,9 +98,9 @@ export default () => { throw new Error('Request failed') } const data = response.body - if (!data) { + if (!data) throw new Error('No data') - } + const reader = data.getReader() const decoder = new TextDecoder('utf-8') let done = false @@ -109,13 +108,13 @@ export default () => { while (!done) { const { value, done: readerDone } = await reader.read() if (value) { - let char = decoder.decode(value) - if (char === '\n' && currentAssistantMessage().endsWith('\n')) { + const char = decoder.decode(value) + if (char === '\n' && currentAssistantMessage().endsWith('\n')) continue - } - if (char) { + + if (char) setCurrentAssistantMessage(currentAssistantMessage() + char) - } + smoothToBottom() } done = readerDone @@ -147,7 +146,7 @@ export default () => { const clear = () => { inputRef.value = '' - inputRef.style.height = 'auto'; + inputRef.style.height = 'auto' setMessageList([]) setCurrentAssistantMessage('') setCurrentSystemRoleSettings('') @@ -163,21 +162,19 @@ export default () => { const retryLastFetch = () => { if (messageList().length > 0) { const lastMessage = messageList()[messageList().length - 1] - console.log(lastMessage) - if (lastMessage.role === 'assistant') { + if (lastMessage.role === 'assistant') setMessageList(messageList().slice(0, -1)) - } + requestWithLatestMessage() } } const handleKeydown = (e: KeyboardEvent) => { - if (e.isComposing || e.shiftKey) { + if (e.isComposing || e.shiftKey) return - } - if (e.key === 'Enter') { + + if (e.key === 'Enter') handleButtonClick() - } } return ( @@ -224,11 +221,11 @@ export default () => { autocomplete="off" autofocus onInput={() => { - inputRef.style.height = 'auto'; - inputRef.style.height = inputRef.scrollHeight + 'px'; + inputRef.style.height = 'auto' + inputRef.style.height = `${inputRef.scrollHeight}px` }} rows="1" - class='gen-textarea' + class="gen-textarea" />