Changeset View
Changeset View
Standalone View
Standalone View
web/url-utils.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import _keyBy from 'lodash/fp/keyBy.js'; | import _keyBy from 'lodash/fp/keyBy.js'; | ||||
import type { AccountUserInfo } from 'lib/types/user-types.js'; | |||||
import { | import { | ||||
startDateForYearAndMonth, | startDateForYearAndMonth, | ||||
endDateForYearAndMonth, | endDateForYearAndMonth, | ||||
} from 'lib/utils/date-utils.js'; | } from 'lib/utils/date-utils.js'; | ||||
import { infoFromURL } from 'lib/utils/url-utils.js'; | import { infoFromURL, type URLInfo } from 'lib/utils/url-utils.js'; | ||||
import { yearExtractor, monthExtractor } from './selectors/nav-selectors.js'; | import { yearExtractor, monthExtractor } from './selectors/nav-selectors.js'; | ||||
import type { NavInfo } from './types/nav-types.js'; | import type { NavInfo } from './types/nav-types.js'; | ||||
function canonicalURLFromReduxState( | function canonicalURLFromReduxState( | ||||
navInfo: NavInfo, | navInfo: NavInfo, | ||||
currentURL: string, | currentURL: string, | ||||
loggedIn: boolean, | loggedIn: boolean, | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | ): string { | ||||
} | } | ||||
return newURL; | return newURL; | ||||
} | } | ||||
// Given a URL, this function parses out a navInfo object, leaving values as | // Given a URL, this function parses out a navInfo object, leaving values as | ||||
// default if they are unspecified. | // default if they are unspecified. | ||||
function navInfoFromURL( | function navInfoFromURL( | ||||
url: string, | urlInfo: URLInfo, | ||||
backupInfo: { now?: Date, navInfo?: NavInfo }, | backupInfo: { | ||||
+now?: Date, | |||||
+userInfos?: { +[id: string]: AccountUserInfo }, | |||||
+navInfo?: NavInfo, | |||||
}, | |||||
): NavInfo { | ): NavInfo { | ||||
const urlInfo = infoFromURL(url); | |||||
const { navInfo } = backupInfo; | const { navInfo } = backupInfo; | ||||
const now = backupInfo.now ? backupInfo.now : new Date(); | const now = backupInfo.now ? backupInfo.now : new Date(); | ||||
let year = urlInfo.year; | let year = urlInfo.year; | ||||
if (!year && navInfo) { | if (!year && navInfo) { | ||||
year = yearExtractor(navInfo.startDate, navInfo.endDate); | year = yearExtractor(navInfo.startDate, navInfo.endDate); | ||||
} | } | ||||
if (!year) { | if (!year) { | ||||
Show All 32 Lines | const newNavInfo: NavInfo = { | ||||
startDate: startDateForYearAndMonth(year, month), | startDate: startDateForYearAndMonth(year, month), | ||||
endDate: endDateForYearAndMonth(year, month), | endDate: endDateForYearAndMonth(year, month), | ||||
activeChatThreadID, | activeChatThreadID, | ||||
chatMode, | chatMode, | ||||
}; | }; | ||||
if (urlInfo.selectedUserList) { | if (urlInfo.selectedUserList) { | ||||
const selectedUsers = _keyBy('id')(navInfo?.selectedUserList ?? []); | const selectedUsers = _keyBy('id')(navInfo?.selectedUserList ?? []); | ||||
const userInfos = backupInfo.userInfos ?? {}; | |||||
newNavInfo.selectedUserList = urlInfo.selectedUserList | newNavInfo.selectedUserList = urlInfo.selectedUserList | ||||
?.map(id => selectedUsers[id]) | ?.map(id => selectedUsers[id] ?? userInfos[id]) | ||||
?.filter(Boolean); | ?.filter(Boolean); | ||||
} | } | ||||
if (urlInfo.settings) { | if (urlInfo.settings) { | ||||
newNavInfo.settingsSection = urlInfo.settings; | newNavInfo.settingsSection = urlInfo.settings; | ||||
} | } | ||||
if (urlInfo.inviteSecret) { | if (urlInfo.inviteSecret) { | ||||
newNavInfo.inviteSecret = urlInfo.inviteSecret; | newNavInfo.inviteSecret = urlInfo.inviteSecret; | ||||
} | } | ||||
return newNavInfo; | return newNavInfo; | ||||
} | } | ||||
export { canonicalURLFromReduxState, navInfoFromURL }; | export { canonicalURLFromReduxState, navInfoFromURL }; |