Skip to content

Commit

Permalink
SortName is now a zod type. And it also loses ''.
Browse files Browse the repository at this point in the history
Part of #673.
  • Loading branch information
jkomoros committed Nov 12, 2023
1 parent e9c005b commit a0bf64b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 97 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,
SORT_NAME_STARS,
TEXT_FIELD_TITLE
} from '../type_constants.js';

Expand Down Expand Up @@ -299,7 +298,7 @@ const FALLBACK_TITLES = [
const selectGoodTitles = (state : State, count = 20) : string[] => {
//TODO: memoize
const contentFilter : CardType = 'content';
const description = new CollectionDescription('everything', [contentFilter, limitFilter(count)], SORT_NAME_STARS);
const description = new CollectionDescription('everything', [contentFilter, limitFilter(count)], 'stars');
const collection = description.collection(selectCollectionConstructorArguments(state));
const titles = collection.sortedCards.map(card => card.title);
return [...titles, ...FALLBACK_TITLES].slice(0,count);
Expand Down
9 changes: 2 additions & 7 deletions src/actions/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ import {
limitFilter
} from '../filters.js';

import {
SORT_NAME_DEFAULT,
SORT_NAME_RANDOM
} from '../type_constants.js';

import {
Collection,
CollectionDescription,
Expand Down Expand Up @@ -291,7 +286,7 @@ export const canonicalizeURL = () : ThunkSomeAction => (dispatch, getState) => {
}

//TODO: it's weird to recreate the logic of CollectionDescription.serialize() here.
if (description.sort != SORT_NAME_DEFAULT || description.sortReversed) {
if (description.sort != 'default' || description.sortReversed) {
result.push(SORT_URL_KEYWORD);
if(description.sortReversed) {
result.push(SORT_REVERSED_URL_KEYWORD);
Expand Down Expand Up @@ -426,7 +421,7 @@ export const showCard = (requestedCard : CardID = PLACEHOLDER_CARD_ID_CHARACTER)
}
};

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

const randomizeSalt = () : SomeAction => {
return {
Expand Down
21 changes: 8 additions & 13 deletions src/collection_description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import {
SET_INFOS,
} from './filters.js';

import {
SORT_NAME_DEFAULT,
SORT_NAME_RANDOM
} from './type_constants.js';

import {
TypedObject
} from './typed_object.js';
Expand Down Expand Up @@ -70,9 +65,9 @@ const extractFilterNamesSortAndView = (parts : URLPart[]) : [FilterName[], SortN
//returns the filter names, the sort name, and whether the sort is reversed
//parts is all of the unconsumed portions of the path that aren't the set
//name or the card name.
if (!parts.length) return [[], SORT_NAME_DEFAULT, false, 'list', ''];
if (!parts.length) return [[], 'default', false, 'list', ''];
const filters : FilterName[] = [];
let sortName : SortName = SORT_NAME_DEFAULT;
let sortName : SortName = 'default';
let sortReversed = false;
let viewMode : ViewMode = 'list';
let viewModeExtra = '';
Expand Down Expand Up @@ -278,7 +273,7 @@ export class CollectionDescription {
setName = 'main';
setNameExplicitlySet = false;
}
if (!sortName) sortName = SORT_NAME_DEFAULT;
if (!sortName) sortName = 'default';
if (!sortReversed) sortReversed = false;
if (!filterNames) filterNames = [];
if (!viewMode) viewMode = 'list';
Expand Down Expand Up @@ -344,7 +339,7 @@ export class CollectionDescription {
}

get sortConfig() {
return SORTS[this.sort] || SORTS[SORT_NAME_DEFAULT];
return SORTS[this.sort];
}

//IF the collection wants to limit how many items to return, this will
Expand All @@ -355,7 +350,7 @@ export class CollectionDescription {
}

get isRandom() {
return this.sort == SORT_NAME_RANDOM;
return this.sort == 'random';
}

//IF the collection wants to offset how many items to return, this will
Expand Down Expand Up @@ -389,7 +384,7 @@ export class CollectionDescription {

result = result.concat(filterNames);

if (this.sort != SORT_NAME_DEFAULT || this.sortReversed) {
if (this.sort != 'default' || this.sortReversed) {
result.push(SORT_URL_KEYWORD);
if (this.sortReversed) result.push(SORT_REVERSED_URL_KEYWORD);
result.push(this.sort);
Expand Down Expand Up @@ -426,7 +421,7 @@ export class CollectionDescription {

result = result.concat(filterNames);

if (this.sort != SORT_NAME_DEFAULT || this.sortReversed) {
if (this.sort != 'default' || this.sortReversed) {
result.push(SORT_URL_KEYWORD);
if (this.sortReversed) result.push(SORT_REVERSED_URL_KEYWORD);
result.push(this.sort);
Expand Down Expand Up @@ -884,7 +879,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 == 'main' && 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 == 'default' && !this._description.sortReversed && (!this._sortExtras || Object.keys(this._sortExtras).length == 0)) {
return collection;
}
const sortInfo = this._sortInfo;
Expand Down
46 changes: 16 additions & 30 deletions src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,7 @@ import {

import {
REFERENCE_TYPE_CONCEPT,
REFERENCE_TYPE_LINK,
SORT_NAME_DEFAULT,
SORT_NAME_RECENT,
SORT_NAME_STARS,
SORT_NAME_ORIGINAL_ORDER,
SORT_NAME_LINK_COUNT,
SORT_NAME_UPDATED,
SORT_NAME_CREATED,
SORT_NAME_COMMENTED,
SORT_NAME_LAST_TWEETED,
SORT_NAME_TWEET_COUNT,
SORT_NAME_TWEET_ORDER,
SORT_NAME_TODO_DIFFICULTY,
SORT_NAME_RANDOM,
SORT_NAME_CARD_RANK
REFERENCE_TYPE_LINK
} from './type_constants.js';

import {
Expand Down Expand Up @@ -1360,7 +1346,7 @@ export const SORTS : SortConfigurationMap = {
//sortValues, in which case it uses those. Note that
//collection._makeSortedCards has logic tailored to this to know when it can
//bail out early
[SORT_NAME_DEFAULT]: {
'default': {
extractor: (card, sections, _, sortExtra) : [number, string] => {
if (!sortExtra || Object.keys(sortExtra).length == 0) return [0, sectionNameForCard(card, sections)];
//Pick whatever is the first key stored, which will be the first
Expand Down Expand Up @@ -1388,49 +1374,49 @@ export const SORTS : SortConfigurationMap = {
},
reorderable: (sortExtra) => !sortExtra || Object.keys(sortExtra).length == 0
},
[SORT_NAME_ORIGINAL_ORDER]: {
'original-order': {
extractor: (card, sections) => [0, sectionNameForCard(card, sections)],
description: 'The default order of the cards within each section in order',
labelName: 'Section',
reorderable: () => true
},
[SORT_NAME_LINK_COUNT]: {
'link-count': {
extractor: (card) => {
const inbound_links = references(card).inboundLinksArray();
return [inbound_links.length, '' + inbound_links.length];
},
description: 'In descending order by number of inbound links',
labelName: 'Link Count',
},
[SORT_NAME_UPDATED]: {
'updated': {
extractor: (card) => {
const timestamp = card.updated_substantive;
return [timestamp ? timestamp.seconds : 0, prettyTime(timestamp)];
},
description: 'In descending order by when each card was last substantively updated',
labelName:'Updated',
},
[SORT_NAME_CREATED]: {
'created': {
extractor: (card) => {
const timestamp = card.updated_substantive;
return [timestamp ? timestamp.seconds : 0, prettyTime(timestamp)];
},
description: 'In descending order by when each card was created',
labelName:'Created',
},
[SORT_NAME_STARS]: {
'stars': {
extractor: (card) => [card.star_count || 0, ''],
description: 'In descending order by number of stars',
},
[SORT_NAME_COMMENTED]: {
'commented': {
extractor: (card) => {
const timestamp = card.updated_message;
return [timestamp ? timestamp.seconds : 0, prettyTime(timestamp)];
},
description: 'In descending order by when each card last had a new message',
labelName: 'Commented',
},
[SORT_NAME_RECENT]: {
'recent': {
extractor: (card) => {
const messageValue = card.updated_message ? card.updated_message.seconds : 0;
const updatedValue = card.updated_substantive ? card.updated_substantive.seconds : 0;
Expand All @@ -1442,39 +1428,39 @@ export const SORTS : SortConfigurationMap = {
description: 'In descending order by when each card was last updated or had a new message',
labelName: 'Last Activity',
},
[SORT_NAME_LAST_TWEETED]: {
'last-tweeted': {
extractor: (card) => {
return [card.last_tweeted.seconds, prettyTime(card.last_tweeted)];
},
description: 'In descending order of when they were last auto-tweeted',
labelName: 'Tweeted'
},
[SORT_NAME_TWEET_COUNT]: {
'tweet-count': {
extractor: (card) => [card.tweet_count, '' + card.tweet_count],
description: 'In descending order of how many times the card has been tweeted',
labelName: 'Tweet Count',
},
[SORT_NAME_TWEET_ORDER]: {
'tweet-order': {
extractor: tweetOrderExtractor as (card : ProcessedCard, sections : Sections, allCards : ProcessedCards) => [number, string],
description: 'In descending order of the ones that are most deserving of a tweet',
labelName: 'Tweet Worthiness',
},
[SORT_NAME_TODO_DIFFICULTY]: {
'todo-difficulty': {
extractor: (card : ProcessedCard) => {
const result = MAX_TOTAL_TODO_DIFFICULTY - cardTODOConfigKeys(card).map(key => TODO_DIFFICULTY_MAP[key]).reduce((prev, curr) => prev + curr, 0.0);
return [result, '' + result];
},
description: 'In ascending order of how difficult remaining TODOs are',
labelName: 'TODO Difficulty'
},
[SORT_NAME_RANDOM]: {
'random': {
extractor: (card, _, __, ___, filterExtras) => {
return [hash(card.id + filterExtras.randomSalt), ''];
},
description: 'A random order',
labelName: 'Random Order'
},
[SORT_NAME_CARD_RANK]: {
'card-rank': {
extractor: (card, _, cards) => {
//This is memoized so as long as cards is the same it won't be re-run.
const ranks = pageRank(cards);
Expand Down Expand Up @@ -1784,7 +1770,7 @@ const INITIAL_STATE_FILTERS = Object.assign(
export const INITIAL_STATE : CollectionState = {
activeSetName: 'main',
activeFilterNames: [],
activeSortName: SORT_NAME_DEFAULT,
activeSortName: 'default',
activeSortReversed: false,
activeViewMode: 'list',
activeViewModeExtra: '',
Expand Down
8 changes: 3 additions & 5 deletions src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ import {
} from './card_fields.js';

import {
SORT_NAME_RECENT,
SORT_NAME_DEFAULT,
AI_DIALOG_TYPE_CARD_SUMMARY
} from './type_constants.js';

Expand Down Expand Up @@ -210,7 +208,7 @@ export const selectImageBrowserDialogIndex = (state : State) => state.editor ? s
export const selectActiveRenderOffset = (state : State) => state.collection ? state.collection.activeRenderOffset : 0;
const selectActiveSetName = (state : State) : SetName => state.collection ? state.collection.activeSetName : 'main';
const selectActiveFilterNames = (state : State) => state.collection ? state.collection.activeFilterNames : [];
const selectActiveSortName = (state : State) => state.collection ? state.collection.activeSortName : '';
const selectActiveSortName = (state : State) : SortName => state.collection ? state.collection.activeSortName : 'default';
const selectActiveSortReversed = (state : State) => state.collection ? state.collection.activeSortReversed : false;
const selectActiveViewMode = (state : State) : ViewMode => state.collection ? state.collection.activeViewMode : 'list';
const selectActiveViewModeExtra = (state : State) => state.collection ? state.collection.activeViewModeExtra : '';
Expand Down Expand Up @@ -1604,7 +1602,7 @@ export const selectCollectionDescriptionForQuery = createSelector(
//If it's a generic search, we don't want similar cards to
//current card (which might be a boring section title card), we
//just want recent cards.
sort = SORT_NAME_RECENT;
sort = 'recent';
} else {
//If it's a search to find a card to link etc we do want it to
//be related to the card we're on.
Expand All @@ -1615,7 +1613,7 @@ export const selectCollectionDescriptionForQuery = createSelector(
return new CollectionDescription('everything', baseFilters, sort);
}
const queryFilter = queryConfigurableFilterText(wordsAndFilters[0]);
return new CollectionDescription('everything',[...baseFilters, queryFilter, ...wordsAndFilters[1]], sortByRecent ? SORT_NAME_RECENT : SORT_NAME_DEFAULT);
return new CollectionDescription('everything',[...baseFilters, queryFilter, ...wordsAndFilters[1]], sortByRecent ? 'recent' : 'default');
}
);

Expand Down
8 changes: 3 additions & 5 deletions src/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
import * as icons from './components/my-icons.js';

import {
SORT_NAME_RECENT,
SORT_NAME_STARS,
TAB_CONFIG_DEFAULT_TABS,
TAB_CONFIG_DEFAULT_END_TABS,
TAB_CONFIG_SECTIONS,
Expand Down Expand Up @@ -166,7 +164,7 @@ const EXPANSION_ITEMS : {[name in TabConfigName]+?: TabConfig} = {
icon: icons.INSIGHTS_ICON,
display_name: 'Popular',
//TODO: this should be DEFAULT_SET_NAME, but if you click on the tab with DEFAULT_SET_NAME and a sort and no filters, it breaks
collection: new CollectionDescription('everything',[], SORT_NAME_STARS, false),
collection: new CollectionDescription('everything',[], 'stars', false),
//If any section has default set to true first, it will be default. This is thus a fallback.
default:true,
}
Expand All @@ -175,7 +173,7 @@ const EXPANSION_ITEMS : {[name in TabConfigName]+?: TabConfig} = {
{
icon: icons.SCHEDULE_ICON,
display_name: 'Recent',
collection: new CollectionDescription('everything', ['has-content'], SORT_NAME_RECENT, false),
collection: new CollectionDescription('everything', ['has-content'], 'recent', false),
}
],
[TAB_CONFIG_READING_LIST]: [
Expand Down Expand Up @@ -208,7 +206,7 @@ const EXPANSION_ITEMS : {[name in TabConfigName]+?: TabConfig} = {
{
icon: icons.INSERT_DRIVE_FILE_ICON,
display_name: 'Working note cards',
collection: new CollectionDescription('everything', [cardTypeFilter('working-notes'), 'unpublished'], SORT_NAME_RECENT, false),
collection: new CollectionDescription('everything', [cardTypeFilter('working-notes'), 'unpublished'], 'recent', false),
count:true,
hideIfEmpty: true,
}
Expand Down
32 changes: 0 additions & 32 deletions src/type_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,38 +117,6 @@ export const EDITOR_CONTENT_TAB_TYPES = {
[EDITOR_TAB_TODO]: true,
};

export const SORT_NAME_DEFAULT = 'default';
export const SORT_NAME_RECENT = 'recent';
export const SORT_NAME_STARS = 'stars';
export const SORT_NAME_ORIGINAL_ORDER = 'original-order';
export const SORT_NAME_LINK_COUNT = 'link-count';
export const SORT_NAME_UPDATED = 'updated';
export const SORT_NAME_CREATED = 'created';
export const SORT_NAME_COMMENTED = 'commented';
export const SORT_NAME_LAST_TWEETED = 'last-tweeted';
export const SORT_NAME_TWEET_COUNT = 'tweet-count';
export const SORT_NAME_TWEET_ORDER = 'tweet-order';
export const SORT_NAME_TODO_DIFFICULTY = 'todo-difficulty';
export const SORT_NAME_RANDOM = 'random';
export const SORT_NAME_CARD_RANK = 'card-rank';

export const SORT_NAME_TYPES = {
[SORT_NAME_DEFAULT]: true,
[SORT_NAME_RECENT]: true,
[SORT_NAME_STARS]: true,
[SORT_NAME_ORIGINAL_ORDER]: true,
[SORT_NAME_LINK_COUNT]: true,
[SORT_NAME_UPDATED]: true,
[SORT_NAME_CREATED]: true,
[SORT_NAME_COMMENTED]: true,
[SORT_NAME_LAST_TWEETED]: true,
[SORT_NAME_TWEET_COUNT]: true,
[SORT_NAME_TWEET_ORDER]: true,
[SORT_NAME_TODO_DIFFICULTY]: true,
[SORT_NAME_RANDOM]: true,
[SORT_NAME_CARD_RANK]: true
};

export const TAB_CONFIG_DEFAULT_TABS = 'default_tabs';
export const TAB_CONFIG_DEFAULT_END_TABS = 'default_end_tabs';
export const TAB_CONFIG_SECTIONS = 'sections';
Expand Down
Loading

0 comments on commit a0bf64b

Please sign in to comment.