-
-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added tagged passage and story count display to Passage and Story Tags Dialogs #1498
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,13 @@ export interface TagEditorProps { | |
allTags: string[]; | ||
color?: Color; | ||
name: string; | ||
count: number; | ||
onChangeColor: (color: Color) => void; | ||
onChangeName: (name: string) => void; | ||
} | ||
|
||
export const TagEditor: React.FC<TagEditorProps> = props => { | ||
const {allTags, color, name, onChangeColor, onChangeName} = props; | ||
const {allTags, color, name, count, onChangeColor, onChangeName} = props; | ||
const [newName, setNewName] = React.useState(name); | ||
const {t} = useTranslation(); | ||
|
||
|
@@ -52,6 +53,7 @@ export const TagEditor: React.FC<TagEditorProps> = props => { | |
> | ||
{t('common.color')} | ||
</TextSelect> | ||
<span className="tag-count">{`${count} ${count > 1 ? 'passages' : 'passage'}`}</span> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This text needs to be localized instead of hardcoding English. Take a look at StoryListRoute to see an example of how it handles plurals in the title. Putting just an English translation of the text in this PR is fine; I notify translators before a release so they can update the text. I'd suggest using the key Tests will need to be updated too--we don't need to test that the localization works correctly, just that the right keys are displayed. |
||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import { | |
setTagColor, | ||
storyWithId, | ||
renamePassageTag, | ||
storyPassageTags | ||
storyPassageTags, | ||
passageTagFrequencies | ||
} from '../store/stories'; | ||
import {useUndoableStoriesContext} from '../store/undoable-stories'; | ||
import {Color} from '../util/color'; | ||
|
@@ -24,7 +25,7 @@ export const PassageTagsDialog: React.FC<PassageTagsDialogProps> = props => { | |
|
||
const story = storyWithId(stories, storyId); | ||
const tags = storyPassageTags(story); | ||
|
||
const tagCountsInStory = passageTagFrequencies(story); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be memoized for performance reasons. |
||
function handleChangeColor(tagName: string, color: Color) { | ||
dispatch( | ||
setTagColor(story, tagName, color), | ||
|
@@ -54,6 +55,7 @@ export const PassageTagsDialog: React.FC<PassageTagsDialogProps> = props => { | |
color={story.tagColors[tag]} | ||
key={tag} | ||
name={tag} | ||
count={tagCountsInStory[tag]} | ||
onChangeColor={color => handleChangeColor(tag, color)} | ||
onChangeName={newName => handleChangeTagName(tag, newName)} | ||
/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import {useTranslation} from 'react-i18next'; | |
import {DialogCard} from '../components/container/dialog-card'; | ||
import {CardContent} from '../components/container/card'; | ||
import {DialogComponentProps} from './dialogs.types'; | ||
import {renameStoryTag, storyTags} from '../store/stories'; | ||
import {renameStoryTag, storyTags, storyTagFrequencies} from '../store/stories'; | ||
import {setPref, usePrefsContext} from '../store/prefs'; | ||
import {useUndoableStoriesContext} from '../store/undoable-stories'; | ||
import {Color} from '../util/color'; | ||
|
@@ -17,6 +17,7 @@ export const StoryTagsDialog: React.FC<StoryTagsDialogProps> = props => { | |
const {t} = useTranslation(); | ||
|
||
const tags = storyTags(stories); | ||
const tagCountsInStories = storyTagFrequencies(stories); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also should be memoized. |
||
|
||
function handleChangeColor(tagName: string, color: Color) { | ||
prefsDispatch( | ||
|
@@ -43,6 +44,7 @@ export const StoryTagsDialog: React.FC<StoryTagsDialogProps> = props => { | |
color={prefs.storyTagColors[tag]} | ||
key={tag} | ||
name={tag} | ||
count={tagCountsInStories[tag]} | ||
onChangeColor={color => handleChangeColor(tag, color)} | ||
onChangeName={newName => handleChangeTagName(tag, newName)} | ||
/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,22 @@ export function storyPassageTags(story: Story) { | |
).sort(); | ||
} | ||
|
||
export function passageTagFrequencies(story: Story) { | ||
const tagPassageCounts: { [key: string]: number } = {}; | ||
|
||
story.passages.forEach(passage => { | ||
if (passage.tags) { | ||
passage.tags.forEach(tag=>{ | ||
if (!tagPassageCounts[tag]) { | ||
tagPassageCounts[tag] = 1; | ||
} else { | ||
tagPassageCounts[tag] += 1; | ||
} | ||
})}; | ||
}); | ||
return tagPassageCounts; | ||
} | ||
|
||
Comment on lines
+162
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More idiomatically (and to parallel the name with export function storyPassageTagFrequencies(story: Story) {
const counts: Record<string, number> = {};
for (const passage of story.passages) {
for (const tag of passage.tags) {
counts[tag] = (counts[tag] ?? 0) + 1;
}
}
return counts;
} I'm not sure why you do a check on |
||
export function storyStats(story: Story) { | ||
const links = story.passages.reduce<string[]>( | ||
(links, passage) => [ | ||
|
@@ -196,6 +212,20 @@ export function storyTags(stories: Story[]) { | |
).sort(); | ||
} | ||
|
||
export function storyTagFrequencies(stories: Story[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same suggestion as with |
||
const tagFrequencies: {[key: string]: number} = {}; | ||
|
||
stories.forEach(story => { | ||
if (story.tags) { | ||
story.tags.forEach(tag => { | ||
tagFrequencies[tag] = (tagFrequencies[tag] || 0) + 1; | ||
}); | ||
} | ||
}); | ||
|
||
return tagFrequencies; | ||
} | ||
|
||
export function storyWithId(stories: Story[], storyId: string) { | ||
const result = stories.find(s => s.id === storyId); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a mock, let's just do