Skip to content

Commit

Permalink
#32
Browse files Browse the repository at this point in the history
  • Loading branch information
EbrithilNogare committed Apr 19, 2022
1 parent 52c0923 commit 300a45f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
36 changes: 21 additions & 15 deletions src/components/TranslationHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,28 @@ export function TranslationHistory({ getHistory, onSelect }) {
<ListSubheader component="div" id="nested-list-subheader" className={styles.headerTitle}>
History
</ListSubheader>
<Button
onClick={()=>{
if(!confirm("Are you sure to remove unstarred items from history?"))
return;
<Tooltip title="Wipe whole translation history">
<Button
onClick={()=>{
if(!confirm("Are you sure to remove unstarred items from history?"))
return;

let filteredHistory = getHistory().filter((item) => item.star);
let filteredHistory = getHistory().filter((item) => item.star);

localStorage.setItem("translationHistory", JSON.stringify(filteredHistory));
setHistory(getHistory());
}}
color="error">
<div>Remove everything</div>
<DeleteForeverIcon/>
</Button>
localStorage.setItem("translationHistory", JSON.stringify(filteredHistory));
setHistory(getHistory());
}}
color="error">
<div>Remove everything</div>
<DeleteForeverIcon/>
</Button>
</Tooltip>
</div>
}
>
{history.sort((a,b) => a.star ? -1 : b.star ? 1 : 0).map((value, index) => (
<div key={index} className={styles.historyItem}>
<Tooltip title={value.star ? "Mark as favorite" : "Unmark as favorite"}>
{ value.star ?
<Button onClick={()=>{changeStarInHistory(value, false); setHistory(getHistory());}}>
<StarIcon/>
Expand All @@ -102,16 +105,19 @@ export function TranslationHistory({ getHistory, onSelect }) {
<StarBorderIcon/>
</Button>
}
</Tooltip>
<ListItemButton onClick={() => selectItem(value)} className={styles.listItemText}>
<div>{value.fromLanguageId}</div>
<ArrowRightAltIcon/>
<div>{value.toLanguageId}</div>
<ArrowRightIcon/>
<div className={styles.valueText}>{value.text}</div>
</ListItemButton>
<Button onClick={()=>{removeItemFromHistory(value); setHistory(getHistory());}}>
<DeleteIcon/>
</Button>
<Tooltip title="Remove this from history">
<Button onClick={()=>{removeItemFromHistory(value); setHistory(getHistory());}}>
<DeleteIcon/>
</Button>
</Tooltip>
</div>
))}
</List>
Expand Down
1 change: 1 addition & 0 deletions src/components/TranslationHistory.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
font-weight: bold;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
2 changes: 1 addition & 1 deletion src/components/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import styles from "./form.module.scss"


const debouncedTranslate = debounce(translate, 500);
const debouncedSave = debounce(saveHistory, 5000);
const debouncedSave = debounce(saveHistory, 3000);

const languageUk = {
id: "uk",
Expand Down
7 changes: 5 additions & 2 deletions src/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ export function saveHistory(fromLanguageId, toLanguageId, text) {
}

let originalHistory = getHistory();
let foundDuplicate = originalHistory.findIndex((item) => item.text === text) !== -1;
let foundDuplicate = originalHistory.findIndex(
(item) => text.length >= item.text.length && text.substr(0,item.text.length) === item.text
);

if(foundDuplicate){
if(foundDuplicate !== -1){
originalHistory.sort((a,b) => a.text === text ? -1 : b.text === text ? 1 : 0);

This comment has been minimized.

Copy link
@martinpopel

martinpopel Apr 19, 2022

Member

Neměl by i zde být ten prefix? A proč vlastně voláme sort, když známe index, takže by stačilo vzít prvek na pozici foundIndex a přesunout na začátek? (Chápu, že při 100 prvcích je celkem jedno, jakou to má složitost a sort bude optimalizovaný, ale stejně.) Alternativně by foundDuplicate nemusel být ten index, ale text na tom indexu, čili ten prefix.

originalHistory[0].text = text;
} else {
originalHistory.unshift(historyItem);
}
Expand Down

1 comment on commit 300a45f

@martinpopel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ještě pro příště prosím do message commitu dávat něco víc než číslo issue, protože teď se nelze na GitHubu z issue #32 (reference) dostat kliknutím na daný commit (jen zpět na to issue) :-)

Please sign in to comment.