Skip to content

Commit

Permalink
filter invalid in: tokens
Browse files Browse the repository at this point in the history
- if in: token references a journal that does not exist, drop it from the UI to clarify its not being included in the search
- clean up some comments / typos
  • Loading branch information
cloverich committed Jan 15, 2024
1 parent 4cbc599 commit 0c1358c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/preload/client/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class DocumentsClient {
if (q?.titles?.length) {
for (const title of q.titles) {
// note: andWhereILike throws a SQL syntax error in SQLite.
// It seems case insensitie without it?
// It seems case insensitive without it?
query = query.andWhereLike('title', `%${title}%`);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/views/documents/SearchStore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { suite, test } from "mocha";


suite("SearchStore", function () {

// TODO: Test _tokens, computed getter, setter works
// TODO: Test add / remove token work through it
// TODO: Test it filters out invalid in: journal tokens
test("setTokens")
});
23 changes: 20 additions & 3 deletions src/views/documents/SearchStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IClient } from "../../hooks/useClient";
import { observable, IObservableArray, reaction, computed } from "mobx";
import { observable, IObservableArray, reaction, computed, action } from "mobx";
import { JournalsStore } from "../../hooks/stores/journals";
import { SearchToken } from "./search/tokens";
import { TagSearchStore } from "./TagSearchStore";
Expand All @@ -19,19 +19,36 @@ export class SearchV2Store {
private tagSeachStore: TagSearchStore;
private setTokensUrl: any; // todo: This is react-router-dom's setUrl; type it

@observable tokens: IObservableArray<SearchToken> = observable([]);
@observable private _tokens: IObservableArray<SearchToken> = observable([]);

constructor(private client: IClient, journals: JournalsStore, setTokensUrl: any) {
this.journals = journals;
this.tagSeachStore = new TagSearchStore(this);
this.setTokensUrl = setTokensUrl;

// Re-run the search query anytime the tokens change.
reaction(() => this.tokens.slice(), this.search, {
reaction(() => this._tokens.slice(), this.search, {
fireImmediately: false,
});
}

@action
setTokens = (tokens: SearchToken[]) => {
// Filter out invalid in: journal tokens
// Additional validation can go here as well
tokens = tokens.filter((t) => {
if (t.type !== "in") return true;
const journal = this.journals.idForName(t.value as string);
return !!journal;
});

this.tokens.replace(tokens);
}

@computed get tokens(): IObservableArray<SearchToken> {
return this._tokens;
}

// todo: this might be better as a @computed get
private tokensToQuery = () => {
const journals = this.tokens
Expand Down
8 changes: 4 additions & 4 deletions src/views/documents/TagSearchStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { TitleTokenParser } from "./search/parsers/title";
import { TextTokenParser } from "./search/parsers/text";
import { BeforeTokenParser } from './search/parsers/before';

// NOTE: This won't allow searching where value has colon in it
// Feel free to improe this
// TODO: This won't allow searching where value has colon in it
const tokenRegex = /^(.*):(.*)/;

interface TokenParser<T = any> {
Expand All @@ -36,6 +35,7 @@ const parsers: Record<SearchToken["type"], TokenParser<any>> = {
*/
export interface ITokensStore {
tokens: IObservableArray<SearchToken>;
setTokens: (tokens: SearchToken[]) => void;
}

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ export class TagSearchStore {

const [parser, parsedToken] = results;
const tokens = parser.add(this.store.tokens, parsedToken);
this.store.tokens = observable(tokens);
this.store.setTokens(tokens);
};

@action
Expand All @@ -116,6 +116,6 @@ export class TagSearchStore {

const [parser, parsedToken] = results;
const tokens = parser.remove(this.store.tokens.slice(), parsedToken);
this.store.tokens = observable(tokens);
this.store.setTokens(tokens);
};
}
8 changes: 3 additions & 5 deletions src/views/documents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ function DocumentsContainer() {
console.log('Documents.index.useEffect')
const tokens = params.getAll('search');

// Ok, this does not trigger initial search reaction because there are no
// does not trigger initial search reaction because there are no
// tokens and the change is based on length, and there fireImmediately is false
// This whole thing is dumb.
// Make this more elegant.
if (tokens.length) {
console.log('Documents.index passing tokens to searchStore', tokens)
searchStore.addTokens(tokens);
} else {
console.log('Documents.index calling search directly')
searchStore.search();
}
}, [])
Expand All @@ -51,7 +49,7 @@ function DocumentsContainer() {
}

// todo: Improve error handling
// case: can re-attempt search when searchStore has error (works)
// test case: can re-attempt search when searchStore has error (works)
if (searchStore.error) {
return (
<Layout store={searchStore}>
Expand Down
9 changes: 2 additions & 7 deletions src/views/documents/search/parsers/before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ export class BeforeTokenParser {
};

add = (tokens: SearchToken[], token: BeforeToken) => {
// TODO: I dont remember what these comments were about
// replace existing after token
// this.remove(tokens, token);
// TRY: What if URL syncing does "has" before "add"?


const existing = tokens.find(t => t.type === 'before');
if (existing) {
if (existing.value !== token.value) {
Expand All @@ -43,8 +38,8 @@ export class BeforeTokenParser {

remove = (tokens: SearchToken[], token: BeforeToken) => {
return tokens.filter((t) => {
// since we can have only one after token; keep
// everything that isn't an after token
// since we can have only one before token; keep
// everything that isn't an before token
return t.type !== "before";
});
};
Expand Down

0 comments on commit 0c1358c

Please sign in to comment.