feat(autocomplete name): autofill name with wikidata suggestions

This commit is contained in:
ryan 2024-12-23 17:56:34 +03:00
parent 22e6ae38d4
commit e9b4c12a6d
8 changed files with 90 additions and 42 deletions

11
.idea/Compware.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Compware.iml" filepath="$PROJECT_DIR$/.idea/Compware.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -7,7 +7,12 @@
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component>
<component name="ChangeListManager">
<list default="true" id="eb7658c0-6daa-4def-a5ad-3d491e9b76c8" name="Changes" comment="" />
<list default="true" id="eb7658c0-6daa-4def-a5ad-3d491e9b76c8" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/app.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/editable_cell.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/editable_cell.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/items_list.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/items_list.rs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -28,26 +33,31 @@
}</component>
<component name="ProjectId" id="2pngw94pMiCvlaySWR2RUofHfwc" />
<component name="ProjectViewState">
<option name="autoscrollToSource" value="true" />
<option name="hideEmptyMiddlePackages" value="true" />
<option name="openDirectoriesWithSingleClick" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;,
&quot;RunOnceActivity.rust.reset.selective.auto.import&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;main&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;org.rust.cargo.project.model.PROJECT_DISCOVERY&quot;: &quot;true&quot;,
&quot;org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon&quot;: &quot;&quot;,
&quot;org.rust.first.attach.projects&quot;: &quot;true&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Cargo.Run compareware.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
"RunOnceActivity.rust.reset.selective.auto.import": "true",
"git-widget-placeholder": "editable-cells-wiki",
"last_opened_file_path": "/home/noah/Desktop/Internship_tasks/Compware",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"org.rust.cargo.project.model.PROJECT_DISCOVERY": "true",
"org.rust.cargo.project.model.impl.CargoExternalSystemProjectAware.subscribe.first.balloon": "",
"org.rust.first.attach.projects": "true",
"settings.editor.selected.configurable": "settings.sync",
"vue.rearranger.settings.migration": "true"
}
}</component>
}]]></component>
<component name="RunManager" selected="Cargo.Run compareware">
<configuration name="Run compareware" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="buildProfileId" value="dev" />
@ -97,6 +107,7 @@
<option name="presentableId" value="Default" />
<updated>1733407491778</updated>
<workItem from="1733407498746" duration="5306000" />
<workItem from="1734955316600" duration="1248000" />
</task>
<servers />
</component>

View File

@ -1,7 +1,7 @@
use leptos::*;
use leptos_meta::*;
use crate::components::items_list::ItemsList;
use crate::models::item::{Item, ReviewWithRating};
use crate::models::item::Item;
use crate::nostr::NostrClient;
use tokio::sync::mpsc;
use uuid::Uuid;

View File

@ -9,7 +9,7 @@ pub fn EditableCell(
let handle_input = move |e: web_sys::Event| {
let new_value = event_target_value(&e);
set_input_value(new_value.clone());
set_input_value.set(new_value.clone());
on_input(new_value);
};

View File

@ -3,6 +3,9 @@ use crate::components::tag_editor::TagEditor;
use leptos::*;
use serde::Deserialize;
use uuid::Uuid;
use leptos::logging::log;
use crate::models::item::Item;
use std::sync::{Arc, Mutex};
#[derive(Deserialize, Clone, Debug)]
struct WikidataSuggestion {
@ -11,14 +14,6 @@ struct WikidataSuggestion {
description: Option<String>,
}
#[derive(Clone, Debug)]
struct Item {
id: String,
name: String,
description: String,
tags: Vec<(String, String)>,
wikidata_id: Option<String>,
}
#[component]
pub fn ItemsList(
@ -32,7 +27,7 @@ pub fn ItemsList(
let fetch_wikidata_suggestions = move |query: String| {
spawn_local(async move {
if query.is_empty() {
set_wikidata_suggestions(Vec::new());
set_wikidata_suggestions.set(Vec::new());
return;
}
@ -44,7 +39,7 @@ pub fn ItemsList(
match gloo_net::http::Request::get(&url).send().await {
Ok(response) => {
if let Ok(data) = response.json::<WikidataResponse>().await {
set_wikidata_suggestions(data.search);
set_wikidata_suggestions.set(data.search);
}
}
Err(_) => log!("Failed to fetch Wikidata suggestions"),
@ -87,13 +82,14 @@ pub fn ItemsList(
};
// Add a new item
let add_item = move || {
let add_item = move |_: web_sys::MouseEvent|{
set_items.update(|items| {
items.push(Item {
id: Uuid::new_v4().to_string(),
name: String::new(),
description: String::new(),
tags: vec![],
reviews:vec![],
wikidata_id: None,
});
});
@ -131,17 +127,24 @@ pub fn ItemsList(
/>
<ul>
{move || {
wikidata_suggestions.get().iter().map(|suggestion| {
let suggestions = wikidata_suggestions.get().to_vec();
suggestions.into_iter().map(|suggestion| {
// Clone all necessary fields upfront
let label_for_click = suggestion.label.clone();
let label_for_display = suggestion.label.clone();
let description = suggestion.description.clone().unwrap_or_default();
let id = suggestion.id.clone();
view! {
<li on:click=move |_| {
set_items.update(|items| {
if let Some(item) = items.get_mut(index) {
item.wikidata_id = Some(suggestion.id.clone());
item.name = suggestion.label.clone();
item.wikidata_id = Some(id.clone());
item.name = label_for_click.clone(); // Use the cloned version for updating
}
});
}>
{ format!("{} - {}", suggestion.label, suggestion.description.clone().unwrap_or_default()) }
{ format!("{} - {}", label_for_display, description) } // Use the cloned version for display
</li>
}
@ -161,7 +164,7 @@ pub fn ItemsList(
<TagEditor
tags=item.tags.clone()
on_add=move |key, value| add_tag(index, key, value)
on_remove=move |tag_index| remove_tag(index, tag_index)
on_remove=Arc::new (Mutex:: new (move |tag_index:usize| remove_tag(index, tag_index)))
/>
</td>
// Actions

View File

@ -1,19 +1,22 @@
use leptos::*;
use std::sync::{Arc, Mutex};
#[component]
pub fn TagEditor(
tags: Vec<(String, String)>,
on_add: impl Fn(String, String) + 'static,
on_remove: impl Fn(usize) + 'static,
on_remove: Arc<Mutex<dyn FnMut(usize) + Send + Sync>>,
) -> impl IntoView {
let (key, set_key) = create_signal(String::new());
let (value, set_value) = create_signal(String::new());
let add_tag = move || {
let add_tag = move |_| {
if !key.get().is_empty() && !value.get().is_empty() {
on_add(key.get(), value.get());
set_key(String::new());
set_value(String::new());
set_key.set(String::new());
set_value.set(String::new());
}
};
@ -21,10 +24,16 @@ pub fn TagEditor(
<div>
<ul>
{tags.iter().enumerate().map(|(index, (k, v))| {
let on_remove = on_remove.clone();
view! {
<li>
{format!("{}: {}", k, v)}
<button on:click=move |_| on_remove(index)>{ "Remove" }</button>
<button on:click=move |_| {
let mut on_remove = on_remove.lock().unwrap();
on_remove(index);
}>
{ "Remove" }
</button>
</li>
}
}).collect::<Vec<_>>()}
@ -32,12 +41,12 @@ pub fn TagEditor(
<input
placeholder="Key"
value={key.get()}
on:input=move |e| set_key(event_target_value(&e))
on:input=move |e| set_key.set(event_target_value(&e))
/>
<input
placeholder="Value"
value={value.get()}
on:input=move |e| set_value(event_target_value(&e))
on:input=move |e| set_value.set(event_target_value(&e))
/>
<button on:click=add_tag>{ "Add Tag" }</button>
</div>