Skip to content

Commit

Permalink
#19: Search by words in title or description (#60)
Browse files Browse the repository at this point in the history
* Search in links
* Bump arklib version
---------
Co-authored-by: Kirill Taran <[email protected]>
  • Loading branch information
gwendalF authored Mar 20, 2024
1 parent 8cde8de commit e239e2e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version = "1.70"
tauri-build = { version = "1.0.3", features = [] }

[dependencies]
arklib = { git = "https://github.com/ARK-Builders/arklib", rev = "51cfa7d6" }
arklib = { git = "https://github.com/ARK-Builders/arklib", rev = "357e62f" }
clap = { version = "3.2.8", features = ["derive"] }
home = "0.5.3"
notify-debouncer-full = "0.3.1"
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/base/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::Path;

pub use arklib::link::{Link, Metadata, OpenGraph};
pub use arklib::link::{Link, OpenGraph};
use serde::{Deserialize, Serialize};
use walkdir::{DirEntry, WalkDir};

Expand Down
8 changes: 6 additions & 2 deletions src-tauri/src/command/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pub enum CommandError {
Url,
#[error("Arklib error")]
Arklib,
#[error("Alreay exist")]
LinkExist,
#[error("IO error")]
IO,
}
Expand All @@ -34,3 +32,9 @@ impl From<FromUtf8Error> for CommandError {
Self::IO
}
}

impl From<arklib::ArklibError> for CommandError {
fn from(_: arklib::ArklibError) -> Self {
Self::Arklib
}
}
48 changes: 17 additions & 31 deletions src-tauri/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use errors::{CommandError, Result};

use crate::{
base::{Link, OpenGraph, Score, Scores},
ARK_SHELF_WORKING_DIR, METADATA_PATH, PREVIEWS_PATH, SCORES_PATH,
ARK_SHELF_WORKING_DIR, PREVIEWS_PATH, PROPERTIES_PATH, SCORES_PATH,
};

use tauri::{Builder, Runtime};
Expand All @@ -20,24 +20,13 @@ use walkdir::{DirEntry, WalkDir};

#[tauri::command]
/// Create a `.link`
async fn create_link(url: String, metadata: arklib::link::Metadata) -> Result<String> {
async fn create_link(url: String, properties: arklib::link::Properties) -> Result<String> {
let url = Url::parse(url.as_str())?;
let id = arklib::id::ResourceId::compute_bytes(url.as_ref().as_bytes())
.map_err(|_| CommandError::Arklib)?;
let domain = url.domain().expect("Url has no domain");
let file_name = format!("{domain}-{id}.link");
let link_path = PathBuf::from(ARK_SHELF_WORKING_DIR.get().unwrap()).join(&file_name);

// Check that there is no resource with same id yet
if std::fs::metadata(&link_path).is_ok() {
Err(CommandError::LinkExist)
} else {
std::fs::write(link_path, url.as_str())?;

let meta_path = METADATA_PATH.get().unwrap().join(format!("{id}"));
std::fs::write(&meta_path, serde_json::to_string(&metadata).unwrap())?;
Ok(file_name)
}
let link = arklib::link::Link::new(url, properties.title, properties.desc);
link.save(ARK_SHELF_WORKING_DIR.get().unwrap(), true)
.await?;
let name = link.id()?.to_string();
Ok(name)
}

#[tauri::command]
Expand All @@ -50,7 +39,7 @@ async fn delete_link(name: String) -> Result<()> {
fs::remove_file(&path)?;

let id = id.to_string();
let metadata_path = METADATA_PATH.get().unwrap().join(&id);
let metadata_path = PROPERTIES_PATH.get().unwrap().join(&id);
let preview_path = PREVIEWS_PATH.get().unwrap().join(&id);
fs::remove_file(metadata_path).ok();
fs::remove_file(preview_path).ok();
Expand All @@ -72,7 +61,10 @@ async fn read_link_list() -> Vec<String> {

#[tauri::command]
async fn generate_link_preview(url: String) -> Result<OpenGraph> {
Link::get_preview(url).await.map_err(|_| CommandError::IO)
let filename = arklib::id::ResourceId::compute_bytes(url.as_ref())?.to_string();
let filepath = std::path::PathBuf::from(filename);
let link = arklib::link::Link::load(ARK_SHELF_WORKING_DIR.get().unwrap(), &filepath)?;
Ok(link.get_preview().await?)
}

/// Get the score list
Expand Down Expand Up @@ -164,16 +156,17 @@ pub struct LinkWrapper {
/// Read data from `.link` file
async fn read_link(name: String) -> Result<LinkWrapper> {
let file_path = PathBuf::from(ARK_SHELF_WORKING_DIR.get().unwrap()).join(&name);
let link = Link::load(ARK_SHELF_WORKING_DIR.get().unwrap(), &file_path)
let name = std::path::PathBuf::from(name);
let link = Link::load(ARK_SHELF_WORKING_DIR.get().unwrap(), &name)
.map_err(|_| CommandError::Arklib)?;
let meta = fs::metadata(&file_path)?;
let created_time = match meta.created() {
Ok(time) => Some(time),
Err(_) => None,
};
Ok(LinkWrapper {
title: link.meta.title,
desc: link.meta.desc,
title: link.prop.title,
desc: link.prop.desc,
url: link.url,
created_time,
})
Expand All @@ -183,14 +176,7 @@ fn get_fs_links() -> Vec<DirEntry> {
WalkDir::new(ARK_SHELF_WORKING_DIR.get().unwrap())
.max_depth(1)
.into_iter()
.filter(|file| {
file.as_ref()
.unwrap()
.file_name()
.to_str()
.unwrap()
.ends_with(".link")
})
.filter(|file| file.as_ref().unwrap().file_type().is_file())
.map(|e| e.unwrap())
.collect::<Vec<DirEntry>>()
}
Expand Down
29 changes: 15 additions & 14 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tauri::{AppHandle, Manager};

static ARK_SHELF_WORKING_DIR: OnceLock<PathBuf> = OnceLock::new();
static SCORES_PATH: OnceLock<PathBuf> = OnceLock::new();
static METADATA_PATH: OnceLock<PathBuf> = OnceLock::new();
static PROPERTIES_PATH: OnceLock<PathBuf> = OnceLock::new();
static PREVIEWS_PATH: OnceLock<PathBuf> = OnceLock::new();

#[derive(Parser, Default, Debug)]
Expand Down Expand Up @@ -62,25 +62,27 @@ struct PreviewLoaded {
fn init_statics_and_dir() {
let cli = Cli::parse();
let working_dir = PathBuf::from(&cli.path);
ARK_SHELF_WORKING_DIR.set(working_dir).unwrap();
ARK_SHELF_WORKING_DIR.set(working_dir.clone()).unwrap();
arklib::app_id::load(working_dir).unwrap();

let scores_path = PathBuf::from(&cli.path)
.join(arklib::STORAGES_FOLDER)
.join(arklib::ARK_FOLDER)
.join("scores");
SCORES_PATH.set(scores_path).unwrap();
let metadata_folder = PathBuf::from(&cli.path)
.join(arklib::STORAGES_FOLDER)
.join(arklib::METADATA_PATH);
METADATA_PATH.set(metadata_folder).unwrap();
let properties_folder = PathBuf::from(&cli.path)
.join(arklib::ARK_FOLDER)
.join(arklib::METADATA_STORAGE_FOLDER);
PROPERTIES_PATH.set(properties_folder).unwrap();
let preview_folder = PathBuf::from(&cli.path)
.join(arklib::STORAGES_FOLDER)
.join(arklib::PREVIEWS_PATH);
.join(arklib::ARK_FOLDER)
.join(arklib::PREVIEWS_STORAGE_FOLDER);
PREVIEWS_PATH.set(preview_folder).unwrap();
std::fs::create_dir_all(ARK_SHELF_WORKING_DIR.get().unwrap()).unwrap();
std::fs::create_dir_all(METADATA_PATH.get().unwrap()).unwrap();
std::fs::create_dir_all(PROPERTIES_PATH.get().unwrap()).unwrap();
std::fs::create_dir_all(PREVIEWS_PATH.get().unwrap()).unwrap();

let scores_path = SCORES_PATH.get().unwrap();
if let Err(_) = std::fs::metadata(SCORES_PATH.get().unwrap()) {
if std::fs::metadata(SCORES_PATH.get().unwrap()).is_err() {
File::create(scores_path).unwrap();
}
}
Expand All @@ -90,9 +92,8 @@ async fn get_preview(path: &PathBuf, manager: AppHandle) -> Result<()> {
let url = url::Url::parse(&file_content)?;
let id = arklib::id::ResourceId::compute_bytes(&url.as_str().as_bytes())
.map_err(|_| CommandError::Arklib)?;
let graph_preview = arklib::link::Link::get_preview(url.to_string())
.await
.map_err(|_| CommandError::Arklib)?;
let link = arklib::link::Link::load(ARK_SHELF_WORKING_DIR.get().unwrap(), path)?;
let graph_preview = link.get_preview().await?;
let image_data = graph_preview
.fetch_image()
.await
Expand Down
23 changes: 22 additions & 1 deletion src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
let initialFetch: Promise<LinkInfo[]>;
let search = '';
$: visibleLinks = $linksInfos.filter(l => {
if (search) {
return (
l.url.toLocaleLowerCase().includes(search.toLocaleLowerCase()) ||
l.title.toLocaleLowerCase().includes(search.toLocaleLowerCase()) ||
l.desc?.toLocaleLowerCase().includes(search.toLocaleLowerCase())
);
} else {
return true;
}
});
onMount(() => {
initialFetch = readCurrentLinks();
initialFetch.then(links => {
Expand All @@ -23,6 +37,13 @@
ARK Shelf
</h1>
<main class="absolute top-14 h-[calc(100vh-3.5rem)] w-screen">
<div class="bg-neutral-950 px-8 py-4">
<input
type="search"
class="w-56 rounded-md bg-neutral-950 py-3 outline-none ring-1 ring-neutral-500"
placeholder="Search"
bind:value={search} />
</div>
<div class="flex h-full overflow-hidden overflow-y-scroll bg-neutral-950 px-8 py-4">
<div class="flex grow flex-col items-center space-y-2 pr-4">
{#await initialFetch}
Expand All @@ -31,7 +52,7 @@
<p>Loading links...</p>
</div>
{:then}
{#each $linksInfos as link}
{#each visibleLinks as link}
<LinkCard {link} />
{/each}
{/await}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}, 200);
</script>

<div class="w-56">
<div class="sticky top-0 w-56">
<div class="flex w-full justify-between">
<button
class="rounded-md p-2"
Expand All @@ -57,7 +57,7 @@
</button>
</div>
<form
class="sticky top-0 flex flex-col space-y-2"
class="flex flex-col space-y-2"
on:submit|preventDefault={async e => {
const form = e.currentTarget;
const formData = new FormData(form);
Expand Down
2 changes: 1 addition & 1 deletion src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const createLink = async (
try {
const name = await invoke<string>('create_link', {
url: data.url,
metadata: {
properties: {
title: data.title,
description: data.desc,
},
Expand Down

0 comments on commit e239e2e

Please sign in to comment.