Skip to content

Commit

Permalink
Change SetName to use zod types.
Browse files Browse the repository at this point in the history
Part of #673.
  • Loading branch information
jkomoros committed Nov 12, 2023
1 parent 3189386 commit 15af0f2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 81 deletions.
3 changes: 1 addition & 2 deletions src/actions/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import {
AI_DIALOG_TYPE_CARD_SUMMARY,
AI_DIALOG_TYPE_MISSING_CONCEPTS,
AI_DIALOG_TYPE_SUGGEST_TITLE,
EVERYTHING_SET_NAME,
SORT_NAME_STARS,
TEXT_FIELD_TITLE
} from '../type_constants.js';
Expand Down Expand Up @@ -300,7 +299,7 @@ const FALLBACK_TITLES = [
const selectGoodTitles = (state : State, count = 20) : string[] => {
//TODO: memoize
const contentFilter : CardType = 'content';
const description = new CollectionDescription(EVERYTHING_SET_NAME, [contentFilter, limitFilter(count)], SORT_NAME_STARS);
const description = new CollectionDescription('everything', [contentFilter, limitFilter(count)], SORT_NAME_STARS);
const collection = description.collection(selectCollectionConstructorArguments(state));
const titles = collection.sortedCards.map(card => card.title);
return [...titles, ...FALLBACK_TITLES].slice(0,count);
Expand Down
8 changes: 3 additions & 5 deletions src/actions/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
} from '../filters.js';

import {
DEFAULT_SET_NAME,
EVERYTHING_SET_NAME,
DEFAULT_VIEW_MODE,
SORT_NAME_DEFAULT,
SORT_NAME_RANDOM
Expand Down Expand Up @@ -146,7 +144,7 @@ export const updateCardSelector = (cardSelector : string) : ThunkSomeAction => (
if (CARD_TYPE_CONFIGURATION[card.card_type].orphanedByDefault && !description.setNameExplicitlySet) {
//If it's a working notes card then by default we'll view it
//in the collection including all of its other cards.
set = EVERYTHING_SET_NAME;
set = 'everything';
filters = [card.card_type];
} else {
filters = [NONE_FILTER_NAME];
Expand Down Expand Up @@ -280,7 +278,7 @@ export const canonicalizeURL = () : ThunkSomeAction => (dispatch, getState) => {
//We need to show the set name if it's not the default set, or if its
//the default set and there are no filters active (e.g.
//`c/main/sort/recent/_`)
if (description.set != DEFAULT_SET_NAME || description.filters.length == 0) {
if (description.set != 'main' || description.filters.length == 0) {
result.push(description.set);
}

Expand Down Expand Up @@ -429,7 +427,7 @@ export const showCard = (requestedCard : CardID = PLACEHOLDER_CARD_ID_CHARACTER)
}
};

export const RANDOM_CARD_COLLECTION = new CollectionDescription(EVERYTHING_SET_NAME, [limitFilter(1)], SORT_NAME_RANDOM, false);
export const RANDOM_CARD_COLLECTION = new CollectionDescription('everything', [limitFilter(1)], SORT_NAME_RANDOM, false);

const randomizeSalt = () : SomeAction => {
return {
Expand Down
16 changes: 9 additions & 7 deletions src/collection_description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
SORT_REVERSED_URL_KEYWORD,
SET_NAMES,
UNION_FILTER_DELIMITER,
FILTER_EQUIVALENTS_FOR_SET,
CONFIGURABLE_FILTER_URL_PARTS,
CONFIGURABLE_FILTER_NAMES,
LIMIT_FILTER_NAME,
Expand All @@ -15,10 +14,10 @@ import {
makeConfigurableFilter,
queryConfigurableFilterText,
queryTextFromQueryFilter,
SET_INFOS,
} from './filters.js';

import {
DEFAULT_SET_NAME,
DEFAULT_VIEW_MODE,
VIEW_MODE_WEB,
SORT_NAME_DEFAULT,
Expand Down Expand Up @@ -277,7 +276,7 @@ export class CollectionDescription {
constructor(setName? : SetName, filterNames? : FilterName[], sortName? : SortName, sortReversed? : boolean, viewMode? : ViewMode, viewModeExtra? : string) {
let setNameExplicitlySet = true;
if (!setName) {
setName = DEFAULT_SET_NAME;
setName = 'main';
setNameExplicitlySet = false;
}
if (!sortName) sortName = SORT_NAME_DEFAULT;
Expand Down Expand Up @@ -384,7 +383,7 @@ export class CollectionDescription {
//all items are in a canonical sorted order but the URL is optimized to stay
//as the user wrote it.
_serialize(unsorted? : boolean) : string {
let result = [this.set];
let result : string[] = [this.set];

const filterNames = [...this.filters];
if (!unsorted) filterNames.sort();
Expand Down Expand Up @@ -421,7 +420,7 @@ export class CollectionDescription {
_serializeShort(unsorted? : boolean) : string {
let result = [];

if (this.set != DEFAULT_SET_NAME) result.push(this.set);
if (this.set != 'main') result.push(this.set);

const filterNames = [...this.filters];
if (!unsorted) filterNames.sort();
Expand Down Expand Up @@ -847,7 +846,10 @@ export class Collection {
//cards and then filter them down to the list that was in the set
//originally. This is OK because we're returning a set, not an array,
//from this method, so order doesn't matter.
const filterEquivalentForActiveSet = FILTER_EQUIVALENTS_FOR_SET[this._description.set];

if (this._description.set == '') throw new Error('Empty set name');

const filterEquivalentForActiveSet = SET_INFOS[this._description.set].filterEquivalent;
if (filterEquivalentForActiveSet) filterDefinition = [...filterDefinition, filterEquivalentForActiveSet];

const [currentFilterFunc,,] = combinedFilterForFilterDefinition(filterDefinition, makeExtrasForFilterFunc(this._filtersSnapshot, this._cardsForFiltering, this._keyCardID, this._editingCard, this._userID, this._randomSalt, this._cardSimilarity));
Expand Down Expand Up @@ -885,7 +887,7 @@ export class Collection {
this._ensureSortInfo();
//Skip the work of sorting in the default case, as everything is already
//sorted. No-op collections still might be created and should be fast.
if (this._description.set == DEFAULT_SET_NAME && this._description.sort == SORT_NAME_DEFAULT && !this._description.sortReversed && (!this._sortExtras || Object.keys(this._sortExtras).length == 0)) {
if (this._description.set == 'main' && this._description.sort == SORT_NAME_DEFAULT && !this._description.sortReversed && (!this._sortExtras || Object.keys(this._sortExtras).length == 0)) {
return collection;
}
const sortInfo = this._sortInfo;
Expand Down
29 changes: 13 additions & 16 deletions src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import {
import {
REFERENCE_TYPE_CONCEPT,
REFERENCE_TYPE_LINK,
DEFAULT_SET_NAME,
READING_LIST_SET_NAME,
EVERYTHING_SET_NAME,
VIEW_MODE_WEB,
DEFAULT_VIEW_MODE,
SORT_NAME_DEFAULT,
Expand Down Expand Up @@ -107,7 +104,9 @@ import {
ConcreteFilterName,
CardSimilarityMap,
FilterFuncResult,
ConfigurableFilterResult
ConfigurableFilterResult,
SetName,
setName
} from './types.js';

import {
Expand Down Expand Up @@ -168,23 +167,21 @@ const DIFFERENT_TYPE_FILTER_NAME = 'different-type';
* description - the description for the set, to be shown to potentially all
users.
*/
export const SET_INFOS = {
[DEFAULT_SET_NAME]: {
export const SET_INFOS : {[name in Exclude<SetName, ''>]: {filterEquivalent: FilterName, description: string}} = {
'main': {
filterEquivalent: 'in-all-set',
description: 'The default set, typically containing only content cards that are specifically included in a section'
},
[READING_LIST_SET_NAME]: {
'reading-list': {
filterEquivalent: 'in-reading-list',
description: 'This user\'s list of cards they\'ve put on their reading list',
},
[EVERYTHING_SET_NAME]: {
'everything': {
filterEquivalent: 'in-everything-set',
description: 'Every single card of every type, including cards that aren\'t in any section (orphaned)'
}
};

export const FILTER_EQUIVALENTS_FOR_SET = Object.fromEntries(Object.entries(SET_INFOS).map(entry => [entry[0], entry[1].filterEquivalent]));

//If filter names have this character in them then they're actually a union of
//the filters
export const UNION_FILTER_DELIMITER = '+';
Expand All @@ -207,7 +204,7 @@ export const LEGAL_VIEW_MODES : {[mode in ViewMode]+?: boolean} = {
[VIEW_MODE_WEB]: true,
};

export const collectionDescription = (...parts : FilterName[]) : CollectionDescription => new CollectionDescription(EVERYTHING_SET_NAME, parts);
export const collectionDescription = (...parts : FilterName[]) : CollectionDescription => new CollectionDescription('everything', parts);

export const referencesFilter = (direction : 'inbound' | 'outbound' | 'both', referenceType : ReferenceType | ReferenceType[], invertReferencesTypes? : boolean) : ConfigurableFilterName => {
let filter = '';
Expand Down Expand Up @@ -1626,7 +1623,7 @@ const CARD_FILTER_CONFIGS : CardFilterConfigMap = Object.assign(
//Mined is always flagged on cards that it might be autoapplied to. The only way to make it go away is to add a true to the auto_todo_overrides for it.
//To find cards that are _partially_ mined, use the 'has-inbound-mined-from-references/not-mined' filters.
'content-mined': [['mined-for-content', 'not-mined-for-content', 'does-not-need-to-be-mined-for-content', 'needs-to-be-mined-for-content'], () => false, TODO_TYPE_AUTO_WORKING_NOTES, 2.0, 'Whether the card has had its insights \'mined\' into other cards. Only automatically applied to working-notes cards. The only way to clear it is to add a force TODO disable for it'],
[EVERYTHING_SET_NAME]: [defaultNonTodoCardFilterName(FILTER_EQUIVALENTS_FOR_SET[EVERYTHING_SET_NAME]), () => true, TODO_TYPE_NA, 0.0, 'Every card is in the everything set'],
[setName('everything')]: [defaultNonTodoCardFilterName(SET_INFOS['everything'].filterEquivalent), () => true, TODO_TYPE_NA, 0.0, 'Every card is in the everything set'],
//note: a number of things rely on `has-body` filter which is derived from this configuration
'body': [defaultCardFilterName('body'), (card : Card) => card && BODY_CARD_TYPES[card.card_type], TODO_TYPE_NA, 0.0, 'Cards that are of a type that has a body field'],
'substantive-references': [defaultCardFilterName('substantive-references'), (card : Card) => references(card).substantiveArray().length, TODO_TYPE_NA, 0.0, 'Whether the card has any substantive references of any type'],
Expand Down Expand Up @@ -1713,7 +1710,7 @@ export const INVERSE_FILTER_NAMES = Object.assign(
[ALL_FILTER_NAME]: NONE_FILTER_NAME,
[TODO_COMBINED_INVERSE_FILTER_NAME]: TODO_COMBINED_FILTER_NAME,
},
Object.fromEntries(Object.entries(FILTER_EQUIVALENTS_FOR_SET).map(entry => ['not-' + entry[1], entry[1]])),
Object.fromEntries(Object.entries(SET_INFOS).map(entry => ['not-' + entry[1].filterEquivalent, entry[1].filterEquivalent])),
//extend with ones for all of the card filters badsed on that config
Object.fromEntries(Object.entries(CARD_FILTER_CONFIGS).map(entry => [entry[1][0][1], entry[1][0][0]])),
//Add the inverse need filters (skipping ones htat are not a TODO)
Expand Down Expand Up @@ -1762,7 +1759,7 @@ const CARD_NON_INVERTED_FILTER_DESCRIPTIONS = Object.assign(
[NONE_FILTER_NAME]: 'Matches no cards',
'read': 'Cards that you have read',
},
Object.fromEntries(Object.entries(FILTER_EQUIVALENTS_FOR_SET).map(entry => [entry[1], 'A filter equivalent of the set ' + entry[0]])),
Object.fromEntries(Object.entries(SET_INFOS).map(entry => [entry[1].filterEquivalent, 'A filter equivalent of the set ' + entry[0]])),
Object.fromEntries(Object.entries(CONFIGURABLE_FILTER_INFO).map(entry => [entry[0], entry[1].description])),
);

Expand All @@ -1780,14 +1777,14 @@ const INITIAL_STATE_FILTERS = Object.assign(
starred: {},
read: {},
},
Object.fromEntries(Object.entries(FILTER_EQUIVALENTS_FOR_SET).map(entry => [entry[1], {}])),
Object.fromEntries(Object.entries(SET_INFOS).map(entry => [entry[1].filterEquivalent, {}])),
//note: `in-everything-set` will be included in the above set and this next
//one, but that's OK, they'll both be the same.
Object.fromEntries(Object.entries(CARD_FILTER_FUNCS).map(entry => [entry[0], {}])),
);

export const INITIAL_STATE : CollectionState = {
activeSetName: DEFAULT_SET_NAME,
activeSetName: 'main',
activeFilterNames: [],
activeSortName: SORT_NAME_DEFAULT,
activeSortReversed: false,
Expand Down
11 changes: 3 additions & 8 deletions src/reducers/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ import {

import {
INITIAL_STATE,
FILTER_EQUIVALENTS_FOR_SET,
CARD_FILTER_FUNCS,
SET_INFOS,
} from '../filters.js';

import {
DEFAULT_SET_NAME,
READING_LIST_SET_NAME,
} from '../type_constants.js';

import {
CollectionState,
Filters,
Expand Down Expand Up @@ -123,7 +118,7 @@ const app = (state : CollectionState = INITIAL_STATE, action : SomeAction) : Col

const makeFilterFromReadingList = (readingList : CardID[]) : {[filterName : string] : FilterMap} => {
return {
[FILTER_EQUIVALENTS_FOR_SET[READING_LIST_SET_NAME]]: Object.fromEntries(readingList.map(id => [id, true]))
[SET_INFOS['reading-list'].filterEquivalent]: Object.fromEntries(readingList.map(id => [id, true]))
};
};

Expand All @@ -139,7 +134,7 @@ const makeFilterFromSection = (sections : Sections, includeDefaultSet? : boolean
});
result[key] = filter;
}
if (includeDefaultSet) result[FILTER_EQUIVALENTS_FOR_SET[DEFAULT_SET_NAME]] = combinedSet;
if (includeDefaultSet) result[SET_INFOS.main.filterEquivalent] = combinedSet;
return result;
};

Expand Down
42 changes: 24 additions & 18 deletions src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ import {
} from './card_fields.js';

import {
DEFAULT_SET_NAME,
READING_LIST_SET_NAME,
EVERYTHING_SET_NAME,
SORT_NAME_RECENT,
SORT_NAME_DEFAULT,
AI_DIALOG_TYPE_CARD_SUMMARY
Expand Down Expand Up @@ -158,7 +155,8 @@ import {
ExpandedTabConfig,
SortName,
AIDialogType,
AIModelName
AIModelName,
SetName
} from './types.js';

import {
Expand Down Expand Up @@ -1153,7 +1151,7 @@ export const selectActiveSectionId = createSelector(
(collectionDescription, sections) => {
//The activeSectionId is only true if it's the default set and there
//is precisely one filter who is also a set.
if(collectionDescription.set != DEFAULT_SET_NAME) return '';
if(collectionDescription.set != 'main') return '';
if (collectionDescription.filters.length != 1) return '';
return sections[collectionDescription.filters[0]] ? collectionDescription.filters[0] : '';
}
Expand All @@ -1169,7 +1167,7 @@ const selectUserMayEditActiveSection = createSelector(
export const selectActiveCollectionCardTypeToAdd = createSelector(
selectActiveCollectionDescription,
(collectionDescription) : CardType => {
if (collectionDescription.set != EVERYTHING_SET_NAME) return DEFAULT_CARD_TYPE;
if (collectionDescription.set != 'everything') return DEFAULT_CARD_TYPE;
if (collectionDescription.filters.length != 1) return DEFAULT_CARD_TYPE;
//Note: we aren't sure that the first filter is a CardType, but it's
//safe to try because we're just using it to index.
Expand Down Expand Up @@ -1268,7 +1266,7 @@ export const selectActiveTagId = createSelector(
(collectionDescription, tags) => {
//The activeSectionId is only true if it's the default set and there
//is precisely one filter who is also a set.
if( collectionDescription.set != DEFAULT_SET_NAME) return '';
if( collectionDescription.set != 'main') return '';
if (collectionDescription.filters.length != 1) return '';
return tags[collectionDescription.filters[0]] ? collectionDescription.filters[0] : '';
}
Expand Down Expand Up @@ -1314,16 +1312,21 @@ const selectEverythingSetSnapshot = createSelector(
makeEverythingSetFromCards,
);

type SetCollection = {
[set in Exclude<SetName, ''>]: CardID[]
};

const selectAllSets = createSelector(
selectDefaultSet,
selectUserReadingList,
selectEverythingSet,
(defaultSet, readingListSet, everythingSet) => {
return {
[DEFAULT_SET_NAME]: defaultSet,
[READING_LIST_SET_NAME]: readingListSet,
[EVERYTHING_SET_NAME]: everythingSet,
const result : SetCollection = {
'main': defaultSet,
'reading-list': readingListSet,
'everything': everythingSet,
};
return result;
}
);

Expand All @@ -1334,11 +1337,14 @@ const selectSetsSnapshot = createSelector(
selectAllSets,
selectEverythingSetSnapshot,
selectUserReadingListSnapshot,
(allSets, everythingSetSnapshot, readingListSet) => ({
...allSets,
[EVERYTHING_SET_NAME]: everythingSetSnapshot,
[READING_LIST_SET_NAME]: readingListSet,
})
(allSets, everythingSetSnapshot, readingListSet) => {
const result : SetCollection = {
...allSets,
'everything': everythingSetSnapshot,
'reading-list': readingListSet,
};
return result;
}
);


Expand Down Expand Up @@ -1605,10 +1611,10 @@ export const selectCollectionDescriptionForQuery = createSelector(
}
baseFilters.push(limitFilter(10));
//If there's no query, return the similar cards to the current card
return new CollectionDescription(EVERYTHING_SET_NAME, baseFilters, sort);
return new CollectionDescription('everything', baseFilters, sort);
}
const queryFilter = queryConfigurableFilterText(wordsAndFilters[0]);
return new CollectionDescription(EVERYTHING_SET_NAME,[...baseFilters, queryFilter, ...wordsAndFilters[1]], sortByRecent ? SORT_NAME_RECENT : SORT_NAME_DEFAULT);
return new CollectionDescription('everything',[...baseFilters, queryFilter, ...wordsAndFilters[1]], sortByRecent ? SORT_NAME_RECENT : SORT_NAME_DEFAULT);
}
);

Expand Down
Loading

0 comments on commit 15af0f2

Please sign in to comment.