📝 更新(ui/autoMate):更新应用UI组件的布局和样式

🐛 修复(index.ts):修复了窗口高度及属性设置的问题
  🐛 修复(App.tsx):修复了初始化数据类型和变量问题
  🐛 修复(Result/index.tsx):修复了键盘事件处理和样式问题
  🐛 修复(Search/index.tsx):修复了搜索输入及相关数据处理问题
  📝 添加(useCode.ts):添加自定义hook来获取代码数据并处理异常情况
This commit is contained in:
yuruo
2024-06-05 12:48:10 +08:00
parent cb260aad57
commit 219bdd008a
6 changed files with 78 additions and 13 deletions

View File

@@ -6,11 +6,11 @@ import icon from '../../resources/icon.png?asset'
function createWindow(): void { // Create the browser window.
const mainWindow = new BrowserWindow({
width: 600,
height: 600,
height: 500,
show: false,
transparent: true,
frame: false,
alwaysOnTop: true,
// transparent: true,
// frame: false,
// alwaysOnTop: true,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
webPreferences: {

View File

@@ -2,10 +2,10 @@ import { useState } from "react"
import Result from "./components/Result"
import Search from "./components/Search"
import { CodeContext } from "./context/CodeContext"
import { codes } from "@renderer/data"
import { DataType } from "@renderer/data"
function App(): JSX.Element {
const [data, setData] = useState(codes)
const [data, setData] = useState<DataType[]>([])
return (
<CodeContext.Provider value={{data, setData}}>
<Search />

View File

@@ -1,11 +1,38 @@
import { useState } from "react"
import { data as codes } from "@renderer/data"
export default function index() {
const [data, setData] = useState(codes)
import useCode from "@renderer/hooks/useCode"
import { useCallback, useEffect, useState } from "react"
import styles from './styles.module.scss'
export default function Result() {
const {data} = useCode()
const [currentIndex, setCurrentIndex] = useState(0)
const handleKeyEvent = useCallback((e: KeyboardEvent) => {
// 初始没有数据所以data.length为0 ,防止向上箭头,将数据变为-1
if (data.length === 0) return
switch(e.key){
case 'ArrowUp':
setCurrentIndex(currentIndex - 1 < 0 ? data.length - 1 : currentIndex - 1)
break
case 'ArrowDown':
setCurrentIndex(currentIndex + 1 >= data.length ? 0 : currentIndex + 1)
break
case 'Enter':
console.log(data[currentIndex].content)
break
}
// 将 data 和 currentIndex 变化时,重新定义该函数
}, [data, currentIndex])
useEffect(() => {
document.addEventListener('keydown', handleKeyEvent)
// 如果 data 发生变化,先前的事件监听器会被移除,然后再添加新的监听器,以确保使用最新的 data
// 如果不移除data变化会不停添加监听器
return () => {
document.removeEventListener('keydown', handleKeyEvent)
}
}, [handleKeyEvent])
return (
<main className='bg-slate-50 rounded-bl-lg rounded-br-lg -mt-[7px] pb-2'>
<main className = {styles.main}>
{currentIndex}
{data.map((item, index) => (
<div key={index} className='text-slate-700 truncate mb-2'>
<div key={item.id} className={currentIndex == index? styles.active : ''}>
<p>{item.content}</p>
</div>
))}

View File

@@ -0,0 +1,10 @@
.main{
@apply bg-slate-50 px-3 rounded-bl-lg rounded-br-lg -mt-[7px] pb-2;
div {
@apply text-slate-700 truncate px-2 py-1 rounded-lg;
&.active {
@apply bg-orange-400 text-white;
}
}
}

View File

@@ -1,8 +1,25 @@
import useCode from "@renderer/hooks/useCode"
import { ChangeEvent, useState } from "react"
import { codes } from "@renderer/data"
export default function Search(): JSX.Element {
const {setData} = useCode()
const [search, setSearch] = useState('')
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
setSearch(e.target.value)
setData(
codes.filter((code) =>
code.content.toLowerCase().includes(e.target.value.toLowerCase() || '@@@@@@')
)
)
}
return (
<div className="bg-slate-50 p-3 rounded-lg drag" >
<section className="bg-slate-200 p-3 rounded-lg">
<input className="w-full outline-none text-2xl text-slate-600 bg-slate-200" />
<input
value={search}
onChange={handleSearch}
className="w-full outline-none text-2xl text-slate-600 bg-slate-200" />
</section>
<section className="text-center text-slate-600 text-xs mt-2">autoMate</section>
</div>

View File

@@ -0,0 +1,11 @@
import { useContext } from 'react'
import { CodeContext } from '@renderer/context/CodeContext'
export default() =>{
const context = useContext(CodeContext)
if (!context?.data) {
throw new Error('CodeContext not found')
}
return {...context}
}