Skip to content

Commit

Permalink
rspcで検索できるようになった
Browse files Browse the repository at this point in the history
  • Loading branch information
bundai223 committed Jul 26, 2023
1 parent 0a956ad commit 0465eee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 53 deletions.
3 changes: 2 additions & 1 deletion src-tauri/src/router/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub(crate) fn mount() -> RouterBuilder {
t(|_, search_word: String|
search(&search_word)
)
})
}
)
}

#[tauri::command]
Expand Down
48 changes: 18 additions & 30 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,39 @@ import Sidebar, { Item } from "./components/Sidebar";
import Content from "./components/Content";
import { registerAll, unregisterAll } from '@tauri-apps/api/globalShortcut';
import { useEffect, useState } from "react";
import { invoke } from "@tauri-apps/api";
import { tauriClient } from '@/client'

const getAppName = (): Promise<string> => {
// return await invoke('get_app_name')
return tauriClient.query(['app.getAppName']) // エディタ補完が効く
}

function App() {
const [ searchResults, setSearchResult ] = useState<Item[]>([]);

(async () => {
const a = await getAppName();
console.log('aaaaaaaa' + a)
})();

(async () => {
console.log('unregister')
await unregisterAll()
// (async () => {
// console.log('unregister')
// await unregisterAll()

console.log('register')
// 既に登録されていたら登録しない処理が必要
await registerAll(["CommandOrControl+Shift+C"], (shortcut) => {
alert(`Shortcut ${shortcut} triggered`);
});
})();
// console.log('register')
// // 既に登録されていたら登録しない処理が必要
// await registerAll(["CommandOrControl+Shift+C"], (shortcut) => {
// alert(`Shortcut ${shortcut} triggered`);
// });
// })();

useEffect(() => {
console.log('mounted')

// 初期データとしてdocsets一覧
invoke('docsets').then((docsets) => {
// console.log(docsets)
// // 初期データとしてdocsets一覧
// invoke('docsets').then((docsets) => {
// // console.log(docsets)

if(docsets instanceof Array<string>) {
setSearchResult(docsets.map((name: string) => { return { name: name, link: '' } }))
}
})
// if(docsets instanceof Array<string>) {
// setSearchResult(docsets.map((name: string) => { return { name: name, link: '' } }))
// }
// })
}, [])

const updateResult = (result: SearchResult[]) => {
const updateResult = (result: SearchResult) => {
console.log('this is test')
console.log(result)

setSearchResult(result.map((result) => { return { name: result.name, link: '' }} ))
setSearchResult(result.indices.map((result) => { return { name: result.name, link: '' }} ))
}

return (
Expand Down
32 changes: 10 additions & 22 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import Button from './atom/Button';
import './Header.css'
// import { invoke } from '@tauri-apps/api';
import { tauriClient } from '@/client'
import { SearchResult as RawSearchResult, SearchIndex as RawSearchIndex } from '@/types/rspc/bindings';

const search = (search_word: string): Promise<string> => {
const search = (search_word: string): Promise<RawSearchResult> => {
// return await invoke('get_app_name')
return tauriClient.query(['app.search', search_word]) // エディタ補完が効く
}


function toConfig() {
console.log('to config.')
}
Expand All @@ -18,35 +18,23 @@ type Props = {
searchHandler?: SearchHandler
}

export type SearchResult = {
id: number;
doctype: string;
html_path: string;
name: string;
}
export type SearchHandler = (searched: SearchResult[]) => void
export type SearchResult = RawSearchResult;
export type SearchIndex = RawSearchIndex;
export type SearchHandler = (searched: SearchResult) => void

function Header(props: Props) {
// console.log(props)
const [ search_query, setSearchQuery ] = useState<string>('')

function onChange(e: ChangeEvent<HTMLInputElement>) {
async function onChange(e: ChangeEvent<HTMLInputElement>) {
const _query = e.currentTarget.value;
setSearchQuery(_query);

search(_query).then(a => {
console.log(a)
});
const result = await search(_query);

// invoke('search', { word: _query }).then((docsets) => {
// console.log(docsets)
// if (docsets instanceof Array<string>) {
// const result: SearchResult[] = docsets.map((word: string) => { return { word: word } })
// if (props.searchHandler) {
// props.searchHandler(result)
// }
// }
// })
if (props.searchHandler) {
props.searchHandler(result);
}
}

function onSubmit(e: FormEvent) {
Expand Down

0 comments on commit 0465eee

Please sign in to comment.