Skip to content

Commit

Permalink
feat: now can search translation directly
Browse files Browse the repository at this point in the history
  • Loading branch information
wtlin1228 committed Oct 5, 2024
1 parent b17ab4f commit 748c06f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions crates/api_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::{

struct AppState {
project_root: String,
translation_json: HashMap<String, String>,
i18n_to_symbol: HashMap<String, HashMap<String, HashSet<String>>>,
symbol_to_route: HashMap<String, HashMap<String, Vec<String>>>,
used_by_graph: UsedByGraph,
Expand All @@ -39,11 +40,16 @@ async fn search(
let search = path.into_inner();

// TODO: deal with search mode
let mut matched_i18n_keys: Vec<String> = Vec::new();
for (i18n_key, translation) in data.translation_json.iter() {
if translation == &search {
matched_i18n_keys.push(i18n_key.to_owned());
}
}

let mut dependency_tracker = DependencyTracker::new(&data.used_by_graph, true);
let matched_i18n_keys = vec![&search];
let mut trace_result = HashMap::new();
for i18n_key in matched_i18n_keys {
for i18n_key in matched_i18n_keys.iter() {
let mut route_to_paths = HashMap::new();
if let Some(i18n_key_usage) = data.i18n_to_symbol.get(i18n_key) {
for (module_path, symbols) in i18n_key_usage {
Expand Down Expand Up @@ -126,6 +132,7 @@ async fn main() -> std::io::Result<()> {
.wrap(Cors::default().allowed_origin("http://localhost:5173"))
.app_data(web::Data::new(AppState {
project_root: portable.project_root.clone(),
translation_json: portable.translation_json.clone(),
i18n_to_symbol: portable.i18n_to_symbol.clone(),
symbol_to_route: portable.symbol_to_route.clone(),
used_by_graph: portable.used_by_graph.clone(),
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ version = "0.1.0"
[dependencies]
anyhow = { workspace = true }
clap = { workspace = true }
serde_json = { workspace = true }

dt_core = { version = "0.1.0", path = "../dt_core" }
14 changes: 12 additions & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ use dt_core::{
route::SymbolToRoutes,
scheduler::ParserCandidateScheduler,
};

use std::{fs::File, io::prelude::*, path::PathBuf};
use std::{
fs::File,
io::{prelude::*, BufReader},
path::PathBuf,
};

#[derive(Parser)]
#[command(version, about = "Parse a project and serialize its output", long_about = None)]
Expand All @@ -19,6 +22,10 @@ struct Cli {
#[arg(short)]
input: String,

/// translation.json path
#[arg(short)]
translation_path: String,

/// Output path
#[arg(short)]
output: String,
Expand All @@ -27,6 +34,8 @@ struct Cli {
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let project_root = PathBuf::from(&cli.input).to_canonical_string()?;
let translation_json = File::open(&cli.translation_path)?;
let translation_json_reader = BufReader::new(translation_json);

let mut scheduler = ParserCandidateScheduler::new(&project_root);
let mut depend_on_graph = DependOnGraph::new(&project_root);
Expand All @@ -50,6 +59,7 @@ fn main() -> anyhow::Result<()> {

let portable = Portable::new(
project_root.to_owned(),
serde_json::from_reader(translation_json_reader)?,
i18n_to_symbol.table,
symbol_to_route.table,
UsedByGraph::from(&depend_on_graph),
Expand Down
5 changes: 4 additions & 1 deletion crates/dt_portable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::{HashMap, HashSet};
#[derive(Debug, Serialize, Deserialize)]
pub struct Portable {
pub project_root: String,
pub translation_json: HashMap<String, String>,

// {
// "i18n.bird" => {
Expand All @@ -22,7 +23,7 @@ pub struct Portable {
// "Foo" => ["/route/path/x", "/route/path/y"]
// "Bar" => ["/route/path/x"]
// }
// "module path 22" => {
// "module path 2" => {
// "Baz" => ["/route/path/z"]
// }
pub symbol_to_route: HashMap<String, HashMap<String, Vec<String>>>,
Expand All @@ -40,12 +41,14 @@ pub struct Portable {
impl Portable {
pub fn new(
project_root: String,
translation_json: HashMap<String, String>,
i18n_to_symbol: HashMap<String, HashMap<String, HashSet<String>>>,
symbol_to_route: HashMap<String, HashMap<String, Vec<String>>>,
used_by_graph: UsedByGraph,
) -> Self {
Self {
project_root,
translation_json,
i18n_to_symbol,
symbol_to_route,
used_by_graph,
Expand Down
6 changes: 1 addition & 5 deletions web/src/search/components/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ function TracePathToTreeView({
for (let [key, value] of groups) {
const uniuqueKey = uuidv4();
treeItems.push(
<TreeItem
key={uniuqueKey}
itemId={uniuqueKey}
label={`${key} (${value.length})`}
>
<TreeItem key={uniuqueKey} itemId={uniuqueKey} label={key}>
{depth + 1 < group2MaxLength.get(key)! ? (
<TracePathToTreeView tracePaths={value} depth={depth + 1} />
) : null}
Expand Down

0 comments on commit 748c06f

Please sign in to comment.