Commit 2b8b9411 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate TagsService to TS

parent 797e231e
import escapeStringRegexp from 'escape-string-regexp'; import escapeStringRegexp from 'escape-string-regexp';
/** import type { LocalStorageService } from './local-storage';
* @typedef Tag
* @prop {string} text - The label of the tag type Tag = {
* @prop {number} count - The number of times this tag has been used. /** The label of the tag */
* @prop {number} updated - The timestamp when this tag was last used. text: string;
*/ /** The number of times this tag has been used. */
count: number;
/** The timestamp when this tag was last used. */
updated: number;
};
const TAGS_LIST_KEY = 'hypothesis.user.tags.list'; const TAGS_LIST_KEY = 'hypothesis.user.tags.list';
const TAGS_MAP_KEY = 'hypothesis.user.tags.map'; const TAGS_MAP_KEY = 'hypothesis.user.tags.map';
...@@ -19,24 +23,23 @@ const TAGS_MAP_KEY = 'hypothesis.user.tags.map'; ...@@ -19,24 +23,23 @@ const TAGS_MAP_KEY = 'hypothesis.user.tags.map';
*/ */
// @inject // @inject
export class TagsService { export class TagsService {
private _storage: LocalStorageService;
/** /**
* @param {import('./local-storage').LocalStorageService} localStorage - * @param localStorage - Storage used to persist the tags
* Storage used to persist the tags
*/ */
constructor(localStorage) { constructor(localStorage: LocalStorageService) {
this._storage = localStorage; this._storage = localStorage;
} }
/** /**
* Return a list of tag suggestions matching `query`. * Return a list of tag suggestions matching `query`.
* *
* @param {string} query * @param limit - Optional limit of the results.
* @param {number|null} limit - Optional limit of the results. * @return List of matching tags
* @return {string[]} List of matching tags
*/ */
filter(query, limit = null) { filter(query: string, limit: number | null = null): string[] {
/** @type {string[]} */ const savedTags = this._storage.getObject<string[]>(TAGS_LIST_KEY) || [];
const savedTags = this._storage.getObject(TAGS_LIST_KEY) || [];
let resultCount = 0; let resultCount = 0;
// Match any tag where the query is a prefix of the tag or a word within the tag. // Match any tag where the query is a prefix of the tag or a word within the tag.
return savedTags.filter(tag => { return savedTags.filter(tag => {
...@@ -62,13 +65,11 @@ export class TagsService { ...@@ -62,13 +65,11 @@ export class TagsService {
/** /**
* Update the list of stored tag suggestions based on the tags that a user has * Update the list of stored tag suggestions based on the tags that a user has
* entered for a given annotation. * entered for a given annotation.
*
* @param {string[]} tags - List of tags.
*/ */
store(tags) { store(tags: string[]) {
// Update the stored (tag, frequency) map. // Update the stored (tag, frequency) map.
/** @type Record<string, { text: string; count: number; updated: number }> */ const savedTags: Record<string, Tag> =
const savedTags = this._storage.getObject(TAGS_MAP_KEY) || {}; this._storage.getObject(TAGS_MAP_KEY) || {};
tags.forEach(tag => { tags.forEach(tag => {
if (savedTags[tag]) { if (savedTags[tag]) {
savedTags[tag].count += 1; savedTags[tag].count += 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment