diff options
author | Miodec <[email protected]> | 2024-07-18 17:35:05 +0200 |
---|---|---|
committer | Miodec <[email protected]> | 2024-07-18 17:35:05 +0200 |
commit | 99e2ae4433198e09f3e841c1034159421ec51be3 (patch) | |
tree | 931db3f7396b6774bd0a8090628ab8d7dab97de5 | |
parent | 708be53ee3ad0f325962fe78cc20d5c8319aa376 (diff) | |
download | monkeytype-99e2ae4433198e09f3e841c1034159421ec51be3.tar.gz monkeytype-99e2ae4433198e09f3e841c1034159421ec51be3.zip |
almost all frontend types
57 files changed, 792 insertions, 791 deletions
diff --git a/frontend/src/ts/ape/endpoints/configs.ts b/frontend/src/ts/ape/endpoints/configs.ts index 5dc2b8741..5742af78f 100644 --- a/frontend/src/ts/ape/endpoints/configs.ts +++ b/frontend/src/ts/ape/endpoints/configs.ts @@ -1,3 +1,5 @@ +import { Config } from "@monkeytype/shared-types/config"; + const BASE_PATH = "/configs"; export default class Configs { @@ -9,9 +11,7 @@ export default class Configs { return await this.httpClient.get(BASE_PATH); } - async save( - config: SharedTypes.Config - ): Ape.EndpointResponse<Ape.Configs.PostConfig> { + async save(config: Config): Ape.EndpointResponse<Ape.Configs.PostConfig> { return await this.httpClient.patch(BASE_PATH, { payload: { config } }); } } diff --git a/frontend/src/ts/ape/endpoints/configuration.ts b/frontend/src/ts/ape/endpoints/configuration.ts index 39f8ad7db..19013364d 100644 --- a/frontend/src/ts/ape/endpoints/configuration.ts +++ b/frontend/src/ts/ape/endpoints/configuration.ts @@ -1,9 +1,11 @@ +import { Configuration } from "@monkeytype/shared-types"; + export default class Root { constructor(private httpClient: Ape.HttpClient) { this.httpClient = httpClient; } - async get(): Ape.EndpointResponse<SharedTypes.Configuration> { + async get(): Ape.EndpointResponse<Configuration> { return await this.httpClient.get("/configuration"); } } diff --git a/frontend/src/ts/ape/endpoints/psas.ts b/frontend/src/ts/ape/endpoints/psas.ts index 55b9989ea..6477a7878 100644 --- a/frontend/src/ts/ape/endpoints/psas.ts +++ b/frontend/src/ts/ape/endpoints/psas.ts @@ -1,3 +1,5 @@ +import { PSA } from "@monkeytype/shared-types"; + const BASE_PATH = "/psas"; export default class Psas { @@ -5,7 +7,7 @@ export default class Psas { this.httpClient = httpClient; } - async get(): Ape.EndpointResponse<SharedTypes.PSA[]> { + async get(): Ape.EndpointResponse<PSA[]> { return await this.httpClient.get(BASE_PATH); } } diff --git a/frontend/src/ts/ape/endpoints/public.ts b/frontend/src/ts/ape/endpoints/public.ts index 243e74a0d..5f4b417d3 100644 --- a/frontend/src/ts/ape/endpoints/public.ts +++ b/frontend/src/ts/ape/endpoints/public.ts @@ -1,3 +1,5 @@ +import { PublicTypingStats, SpeedHistogram } from "@monkeytype/shared-types"; + const BASE_PATH = "/public"; type SpeedStatsQuery = { @@ -13,13 +15,13 @@ export default class Public { async getSpeedHistogram( searchQuery: SpeedStatsQuery - ): Ape.EndpointResponse<SharedTypes.SpeedHistogram> { + ): Ape.EndpointResponse<SpeedHistogram> { return await this.httpClient.get(`${BASE_PATH}/speedHistogram`, { searchQuery, }); } - async getTypingStats(): Ape.EndpointResponse<SharedTypes.PublicTypingStats> { + async getTypingStats(): Ape.EndpointResponse<PublicTypingStats> { return await this.httpClient.get(`${BASE_PATH}/typingStats`); } } diff --git a/frontend/src/ts/ape/endpoints/results.ts b/frontend/src/ts/ape/endpoints/results.ts index 0cc1e69c3..1502c0969 100644 --- a/frontend/src/ts/ape/endpoints/results.ts +++ b/frontend/src/ts/ape/endpoints/results.ts @@ -1,3 +1,6 @@ +import { DBResult, Result } from "@monkeytype/shared-types"; +import { Mode } from "@monkeytype/shared-types/config"; + const BASE_PATH = "/results"; export default class Results { @@ -5,14 +8,12 @@ export default class Results { this.httpClient = httpClient; } - async get( - offset?: number - ): Ape.EndpointResponse<SharedTypes.DBResult<SharedTypes.Config.Mode>[]> { + async get(offset?: number): Ape.EndpointResponse<DBResult<Mode>[]> { return await this.httpClient.get(BASE_PATH, { searchQuery: { offset } }); } async save( - result: SharedTypes.Result<SharedTypes.Config.Mode> + result: Result<Mode> ): Ape.EndpointResponse<Ape.Results.PostResult> { return await this.httpClient.post(BASE_PATH, { payload: { result }, diff --git a/frontend/src/ts/ape/endpoints/users.ts b/frontend/src/ts/ape/endpoints/users.ts index f6b095b83..add9516f8 100644 --- a/frontend/src/ts/ape/endpoints/users.ts +++ b/frontend/src/ts/ape/endpoints/users.ts @@ -1,3 +1,13 @@ +import { + CountByYearAndDay, + CustomTheme, + ResultFilters, + UserProfile, + UserProfileDetails, + UserTag, +} from "@monkeytype/shared-types"; +import { Mode, Mode2 } from "@monkeytype/shared-types/config"; + const BASE_PATH = "/users"; export default class Users { @@ -48,9 +58,9 @@ export default class Users { }); } - async updateLeaderboardMemory<M extends SharedTypes.Config.Mode>( + async updateLeaderboardMemory<M extends Mode>( mode: string, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, language: string, rank: number ): Ape.EndpointResponse<null> { @@ -89,7 +99,7 @@ export default class Users { } async addResultFilterPreset( - filter: SharedTypes.ResultFilters + filter: ResultFilters ): Ape.EndpointResponse<string> { return await this.httpClient.post(`${BASE_PATH}/resultFilterPresets`, { payload: filter, @@ -103,7 +113,7 @@ export default class Users { ); } - async createTag(tagName: string): Ape.EndpointResponse<SharedTypes.UserTag> { + async createTag(tagName: string): Ape.EndpointResponse<UserTag> { return await this.httpClient.post(`${BASE_PATH}/tags`, { payload: { tagName }, }); @@ -130,7 +140,7 @@ export default class Users { ); } - async getCustomThemes(): Ape.EndpointResponse<SharedTypes.CustomTheme[]> { + async getCustomThemes(): Ape.EndpointResponse<CustomTheme[]> { return await this.httpClient.get(`${BASE_PATH}/customThemes`); } @@ -161,7 +171,7 @@ export default class Users { async addCustomTheme( newTheme: Partial<MonkeyTypes.CustomTheme> - ): Ape.EndpointResponse<SharedTypes.CustomTheme> { + ): Ape.EndpointResponse<CustomTheme> { const payload = { name: newTheme.name, colors: newTheme.colors }; return await this.httpClient.post(`${BASE_PATH}/customThemes`, { payload }); } @@ -204,24 +214,20 @@ export default class Users { }); } - async getProfileByUid( - uid: string - ): Ape.EndpointResponse<SharedTypes.UserProfile> { + async getProfileByUid(uid: string): Ape.EndpointResponse<UserProfile> { const encoded = encodeURIComponent(uid); return await this.httpClient.get(`${BASE_PATH}/${encoded}/profile?isUid`); } - async getProfileByName( - name: string - ): Ape.EndpointResponse<SharedTypes.UserProfile> { + async getProfileByName(name: string): Ape.EndpointResponse<UserProfile> { const encoded = encodeURIComponent(name); return await this.httpClient.get(`${BASE_PATH}/${encoded}/profile`); } async updateProfile( - profileUpdates: Partial<SharedTypes.UserProfileDetails>, + profileUpdates: Partial<UserProfileDetails>, selectedBadgeId?: number - ): Ape.EndpointResponse<SharedTypes.UserProfileDetails> { + ): Ape.EndpointResponse<UserProfileDetails> { return await this.httpClient.patch(`${BASE_PATH}/profile`, { payload: { ...profileUpdates, @@ -281,7 +287,7 @@ export default class Users { return await this.httpClient.post(`${BASE_PATH}/revokeAllTokens`); } - async getTestActivity(): Ape.EndpointResponse<SharedTypes.CountByYearAndDay> { + async getTestActivity(): Ape.EndpointResponse<CountByYearAndDay> { return await this.httpClient.get(`${BASE_PATH}/testActivity`); } } diff --git a/frontend/src/ts/ape/server-configuration.ts b/frontend/src/ts/ape/server-configuration.ts index 1873c9737..3d76c411d 100644 --- a/frontend/src/ts/ape/server-configuration.ts +++ b/frontend/src/ts/ape/server-configuration.ts @@ -1,8 +1,9 @@ +import { Configuration } from "@monkeytype/shared-types"; import Ape from "."; -let config: SharedTypes.Configuration | undefined = undefined; +let config: Configuration | undefined = undefined; -export function get(): SharedTypes.Configuration | undefined { +export function get(): Configuration | undefined { return config; } diff --git a/frontend/src/ts/ape/types/ape-keys.d.ts b/frontend/src/ts/ape/types/ape-keys.d.ts index a93c99884..7b973f40f 100644 --- a/frontend/src/ts/ape/types/ape-keys.d.ts +++ b/frontend/src/ts/ape/types/ape-keys.d.ts @@ -1,11 +1,11 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ // for some reason when using the dot notaion, the types are not being recognized as used declare namespace Ape.ApeKeys { - type GetApeKeys = Record<string, SharedTypes.ApeKey>; + type GetApeKeys = Record<string, ApeKey>; type GenerateApeKey = { apeKey: string; apeKeyId: string; - apeKeyDetails: SharedTypes.ApeKey; + apeKeyDetails: ApeKey; }; } diff --git a/frontend/src/ts/ape/types/configs.d.ts b/frontend/src/ts/ape/types/configs.d.ts index cd3c633cf..54780ea1d 100644 --- a/frontend/src/ts/ape/types/configs.d.ts +++ b/frontend/src/ts/ape/types/configs.d.ts @@ -4,7 +4,7 @@ declare namespace Ape.Configs { type GetConfig = { _id: string; uid: string; - config: Partial<SharedTypes.Config>; + config: Partial<Config>; }; type PostConfig = null; } diff --git a/frontend/src/ts/ape/types/leaderboards.d.ts b/frontend/src/ts/ape/types/leaderboards.d.ts index 2dca27928..fdceb8467 100644 --- a/frontend/src/ts/ape/types/leaderboards.d.ts +++ b/frontend/src/ts/ape/types/leaderboards.d.ts @@ -3,7 +3,7 @@ declare namespace Ape.Leaderboards { type Query = { language: string; - mode: SharedTypes.Config.Mode; + mode: Config.Mode; mode2: string; isDaily?: boolean; daysBefore?: number; @@ -14,12 +14,12 @@ declare namespace Ape.Leaderboards { limit?: number; } & Query; - type GetLeaderboard = SharedTypes.LeaderboardEntry[]; + type GetLeaderboard = LeaderboardEntry[]; type GetRank = { minWpm: number; count: number; rank: number | null; - entry: SharedTypes.LeaderboardEntry | null; + entry: LeaderboardEntry | null; }; } diff --git a/frontend/src/ts/ape/types/presets.d.ts b/frontend/src/ts/ape/types/presets.d.ts index aff31b201..eeb90687b 100644 --- a/frontend/src/ts/ape/types/presets.d.ts +++ b/frontend/src/ts/ape/types/presets.d.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ // for some reason when using the dot notaion, the types are not being recognized as used declare namespace Ape.Presets { - type GetPresets = SharedTypes.DBConfigPreset[]; + type GetPresets = DBConfigPreset[]; type PostPreset = { presetId: string; }; diff --git a/frontend/src/ts/ape/types/results.d.ts b/frontend/src/ts/ape/types/results.d.ts index d0b0a040f..6fe156f7a 100644 --- a/frontend/src/ts/ape/types/results.d.ts +++ b/frontend/src/ts/ape/types/results.d.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ // for some reason when using the dot notaion, the types are not being recognized as used declare namespace Ape.Results { - type PostResult = SharedTypes.PostResultResponse; + type PostResult = PostResultResponse; type PatchResult = { tagPbs: string[]; }; diff --git a/frontend/src/ts/ape/types/users.d.ts b/frontend/src/ts/ape/types/users.d.ts index 44e0f8b6b..c2f45ac0f 100644 --- a/frontend/src/ts/ape/types/users.d.ts +++ b/frontend/src/ts/ape/types/users.d.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ // for some reason when using the dot notaion, the types are not being recognized as used declare namespace Ape.Users { - type GetUser = SharedTypes.User & { + type GetUser = User & { inboxUnreadSize: number; isPremium: boolean; }; @@ -13,6 +13,6 @@ declare namespace Ape.Users { discordAvatar: string; }; type GetInbox = { - inbox: SharedTypes.MonkeyMail[] | undefined; + inbox: MonkeyMail[] | undefined; }; } diff --git a/frontend/src/ts/config.ts b/frontend/src/ts/config.ts index 84bddc669..1c87e7656 100644 --- a/frontend/src/ts/config.ts +++ b/frontend/src/ts/config.ts @@ -16,8 +16,9 @@ import { canSetFunboxWithConfig, } from "./test/funbox/funbox-validation"; import { reloadAfter } from "./utils/misc"; +import * as ConfigTypes from "@monkeytype/shared-types/config"; -export let localStorageConfig: SharedTypes.Config; +export let localStorageConfig: ConfigTypes.Config; let loadDone: (value?: unknown) => void; @@ -25,7 +26,7 @@ const config = { ...DefaultConfig, }; -let configToSend = {} as SharedTypes.Config; +let configToSend = {} as ConfigTypes.Config; const saveToDatabase = debounce(1000, () => { if (Object.keys(configToSend).length > 0) { AccountButton.loading(true); @@ -33,11 +34,11 @@ const saveToDatabase = debounce(1000, () => { AccountButton.loading(false); }); } - configToSend = {} as SharedTypes.Config; + configToSend = {} as ConfigTypes.Config; }); function saveToLocalStorage( - key: keyof SharedTypes.Config, + key: keyof ConfigTypes.Config, nosave = false, noDbCheck = false ): void { @@ -103,10 +104,7 @@ export function setPunctuation(punc: boolean, nosave?: boolean): boolean { return true; } -export function setMode( - mode: SharedTypes.Config.Mode, - nosave?: boolean -): boolean { +export function setMode(mode: ConfigTypes.Mode, nosave?: boolean): boolean { if ( !isConfigValueValid("mode", mode, [ ["time", "words", "quote", "zen", "custom"], @@ -139,7 +137,7 @@ export function setMode( } export function setPlaySoundOnError( - val: SharedTypes.Config.PlaySoundOnError, + val: ConfigTypes.PlaySoundOnError, nosave?: boolean ): boolean { if ( @@ -158,7 +156,7 @@ export function setPlaySoundOnError( } export function setPlaySoundOnClick( - val: SharedTypes.Config.PlaySoundOnClick, + val: ConfigTypes.PlaySoundOnClick, nosave?: boolean ): boolean { if ( @@ -194,7 +192,7 @@ export function setPlaySoundOnClick( } export function setSoundVolume( - val: SharedTypes.Config.SoundVolume, + val: ConfigTypes.SoundVolume, nosave?: boolean ): boolean { if (!isConfigValueValid("sound volume", val, [["0.1", "0.5", "1.0"]])) { @@ -210,7 +208,7 @@ export function setSoundVolume( //difficulty export function setDifficulty( - diff: SharedTypes.Config.Difficulty, + diff: ConfigTypes.Difficulty, nosave?: boolean ): boolean { if ( @@ -299,7 +297,7 @@ export function setBlindMode(blind: boolean, nosave?: boolean): boolean { } function setAccountChart( - array: SharedTypes.Config.AccountChart, + array: ConfigTypes.AccountChart, nosave?: boolean ): boolean { if ( @@ -380,7 +378,7 @@ export function setAccountChartAvg100( } export function setStopOnError( - soe: SharedTypes.Config.StopOnError, + soe: ConfigTypes.StopOnError, nosave?: boolean ): boolean { if (!isConfigValueValid("stop on error", soe, [["off", "word", "letter"]])) { @@ -417,7 +415,7 @@ export function setAlwaysShowDecimalPlaces( } export function setTypingSpeedUnit( - val: SharedTypes.Config.TypingSpeedUnit, + val: ConfigTypes.TypingSpeedUnit, nosave?: boolean ): boolean { if ( @@ -454,7 +452,7 @@ export function setShowOutOfFocusWarning( //pace caret export function setPaceCaret( - val: SharedTypes.Config.PaceCaret, + val: ConfigTypes.PaceCaret, nosave?: boolean ): boolean { if ( @@ -509,7 +507,7 @@ export function setRepeatedPace(pace: boolean, nosave?: boolean): boolean { //min wpm export function setMinWpm( - minwpm: SharedTypes.Config.MinimumWordsPerMinute, + minwpm: ConfigTypes.MinimumWordsPerMinute, nosave?: boolean ): boolean { if (!isConfigValueValid("min speed", minwpm, [["off", "custom"]])) { @@ -537,7 +535,7 @@ export function setMinWpmCustomSpeed(val: number, nosave?: boolean): boolean { //min acc export function setMinAcc( - min: SharedTypes.Config.MinimumAccuracy, + min: ConfigTypes.MinimumAccuracy, nosave?: boolean ): boolean { if (!isConfigValueValid("min acc", min, [["off", "custom"]])) return false; @@ -562,7 +560,7 @@ export function setMinAccCustom(val: number, nosave?: boolean): boolean { //min burst export function setMinBurst( - min: SharedTypes.Config.MinimumBurst, + min: ConfigTypes.MinimumBurst, nosave?: boolean ): boolean { if (!isConfigValueValid("min burst", min, [["off", "fixed", "flex"]])) { @@ -606,7 +604,7 @@ export function setAlwaysShowWordsHistory( //single list command line export function setSingleListCommandLine( - option: SharedTypes.Config.SingleListCommandLine, + option: ConfigTypes.SingleListCommandLine, nosave?: boolean ): boolean { if ( @@ -658,7 +656,7 @@ export function setQuickEnd(qe: boolean, nosave?: boolean): boolean { return true; } -export function setAds(val: SharedTypes.Config.Ads, nosave?: boolean): boolean { +export function setAds(val: ConfigTypes.Ads, nosave?: boolean): boolean { if (!isConfigValueValid("ads", val, [["off", "result", "on", "sellout"]])) { return false; } @@ -675,7 +673,7 @@ export function setAds(val: SharedTypes.Config.Ads, nosave?: boolean): boolean { } export function setRepeatQuotes( - val: SharedTypes.Config.RepeatQuotes, + val: ConfigTypes.RepeatQuotes, nosave?: boolean ): boolean { if (!isConfigValueValid("repeat quotes", val, [["off", "typing"]])) { @@ -724,7 +722,7 @@ export function setStrictSpace(val: boolean, nosave?: boolean): boolean { //opposite shift space export function setOppositeShiftMode( - val: SharedTypes.Config.OppositeShiftMode, + val: ConfigTypes.OppositeShiftMode, nosave?: boolean ): boolean { if ( @@ -741,7 +739,7 @@ export function setOppositeShiftMode( } export function setCaretStyle( - caretStyle: SharedTypes.Config.CaretStyle, + caretStyle: ConfigTypes.CaretStyle, nosave?: boolean ): boolean { if ( @@ -783,7 +781,7 @@ export function setCaretStyle( } export function setPaceCaretStyle( - caretStyle: SharedTypes.Config.CaretStyle, + caretStyle: ConfigTypes.CaretStyle, nosave?: boolean ): boolean { if ( @@ -823,7 +821,7 @@ export function setPaceCaretStyle( } export function setShowAverage( - value: SharedTypes.Config.ShowAverage, + value: ConfigTypes.ShowAverage, nosave?: boolean ): boolean { if ( @@ -842,7 +840,7 @@ export function setShowAverage( } export function setHighlightMode( - mode: SharedTypes.Config.HighlightMode, + mode: ConfigTypes.HighlightMode, nosave?: boolean ): boolean { if ( @@ -872,7 +870,7 @@ export function setHighlightMode( } export function setTapeMode( - mode: SharedTypes.Config.TapeMode, + mode: ConfigTypes.TapeMode, nosave?: boolean ): boolean { if (!isConfigValueValid("tape mode", mode, [["off", "letter", "word"]])) { @@ -901,7 +899,7 @@ export function setHideExtraLetters(val: boolean, nosave?: boolean): boolean { } export function setTimerStyle( - style: SharedTypes.Config.TimerStyle, + style: ConfigTypes.TimerStyle, nosave?: boolean ): boolean { if ( @@ -918,7 +916,7 @@ export function setTimerStyle( } export function setLiveSpeedStyle( - style: SharedTypes.Config.LiveSpeedAccBurstStyle, + style: ConfigTypes.LiveSpeedAccBurstStyle, nosave?: boolean ): boolean { if ( @@ -935,7 +933,7 @@ export function setLiveSpeedStyle( } export function setLiveAccStyle( - style: SharedTypes.Config.LiveSpeedAccBurstStyle, + style: ConfigTypes.LiveSpeedAccBurstStyle, nosave?: boolean ): boolean { if (!isConfigValueValid("live acc style", style, [["off", "text", "mini"]])) { @@ -950,7 +948,7 @@ export function setLiveAccStyle( } export function setLiveBurstStyle( - style: SharedTypes.Config.LiveSpeedAccBurstStyle, + style: ConfigTypes.LiveSpeedAccBurstStyle, nosave?: boolean ): boolean { if ( @@ -967,7 +965,7 @@ export function setLiveBurstStyle( } export function setTimerColor( - color: SharedTypes.Config.TimerColor, + color: ConfigTypes.TimerColor, nosave?: boolean ): boolean { if ( @@ -986,7 +984,7 @@ export function setTimerColor( return true; } export function setTimerOpacity( - opacity: SharedTypes.Config.TimerOpacity, + opacity: ConfigTypes.TimerOpacity, nosave?: boolean ): boolean { if ( @@ -1040,7 +1038,7 @@ export function setTimeConfig(time: number, nosave?: boolean): boolean { //quote length export function setQuoteLength( - len: SharedTypes.Config.QuoteLength[] | SharedTypes.Config.QuoteLength, + len: ConfigTypes.QuoteLength[] | ConfigTypes.QuoteLength, nosave?: boolean, multipleMode?: boolean ): boolean { @@ -1062,7 +1060,7 @@ export function setQuoteLength( if (len === null || isNaN(len) || len < -3 || len > 3) { len = 1; } - len = parseInt(len.toString()) as SharedTypes.Config.QuoteLength; + len = parseInt(len.toString()) as ConfigTypes.QuoteLength; if (len === -1) { config.quoteLength = [0, 1, 2, 3]; @@ -1105,7 +1103,7 @@ export function setWordCount(wordCount: number, nosave?: boolean): boolean { //caret export function setSmoothCaret( - mode: SharedTypes.Config["smoothCaret"], + mode: ConfigTypes.Config["smoothCaret"], nosave?: boolean ): boolean { if ( @@ -1223,7 +1221,7 @@ export function setFreedomMode(freedom: boolean, nosave?: boolean): boolean { } export function setConfidenceMode( - cm: SharedTypes.Config.ConfidenceMode, + cm: ConfigTypes.ConfidenceMode, nosave?: boolean ): boolean { if (!isConfigValueValid("confidence mode", cm, [["off", "on", "max"]])) { @@ -1244,7 +1242,7 @@ export function setConfidenceMode( } export function setIndicateTypos( - value: SharedTypes.Config.IndicateTypos, + value: ConfigTypes.IndicateTypos, nosave?: boolean ): boolean { if ( @@ -1351,7 +1349,7 @@ function setThemes( } export function setRandomTheme( - val: SharedTypes.Config.RandomTheme, + val: ConfigTypes.RandomTheme, nosave?: boolean ): boolean { if ( @@ -1461,7 +1459,7 @@ export function setMonkey(monkey: boolean, nosave?: boolean): boolean { } export function setKeymapMode( - mode: SharedTypes.Config.KeymapMode, + mode: ConfigTypes.KeymapMode, nosave?: boolean ): boolean { if ( @@ -1482,7 +1480,7 @@ export function setKeymapMode( } export function setKeymapLegendStyle( - style: SharedTypes.Config.KeymapLegendStyle, + style: ConfigTypes.KeymapLegendStyle, nosave?: boolean ): boolean { if ( @@ -1524,7 +1522,7 @@ export function setKeymapLegendStyle( } export function setKeymapStyle( - style: SharedTypes.Config.KeymapStyle, + style: ConfigTypes.KeymapStyle, nosave?: boolean ): boolean { if ( @@ -1562,7 +1560,7 @@ export function setKeymapLayout(layout: string, nosave?: boolean): boolean { } export function setKeymapShowTopRow( - show: SharedTypes.Config.KeymapShowTopRow, + show: ConfigTypes.KeymapShowTopRow, nosave?: boolean ): boolean { if ( @@ -1709,7 +1707,7 @@ export async function setCustomLayoutfluid( const customLayoutfluid = trimmed.replace( / /g, "#" - ) as SharedTypes.Config.CustomLayoutFluid; + ) as ConfigTypes.CustomLayoutFluid; config.customLayoutfluid = customLayoutfluid; saveToLocalStorage("customLayoutfluid", nosave); @@ -1719,7 +1717,7 @@ export async function setCustomLayoutfluid( } export function setCustomBackgroundSize( - value: SharedTypes.Config.CustomBackgroundSize, + value: ConfigTypes.CustomBackgroundSize, nosave?: boolean ): boolean { if ( @@ -1738,7 +1736,7 @@ export function setCustomBackgroundSize( } export function setCustomBackgroundFilter( - array: SharedTypes.Config.CustomBackgroundFilter, + array: ConfigTypes.CustomBackgroundFilter, nosave?: boolean ): boolean { if (!isConfigValueValid("custom background filter", array, ["numberArray"])) { @@ -1753,7 +1751,7 @@ export function setCustomBackgroundFilter( } export function setMonkeyPowerLevel( - level: SharedTypes.Config.MonkeyPowerLevel, + level: ConfigTypes.MonkeyPowerLevel, nosave?: boolean ): boolean { if ( @@ -1786,7 +1784,7 @@ export function setBurstHeatmap(value: boolean, nosave?: boolean): boolean { } export async function apply( - configToApply: SharedTypes.Config | MonkeyTypes.ConfigChanges + configToApply: ConfigTypes.Config | MonkeyTypes.ConfigChanges ): Promise<void> { if (configToApply === undefined) return; @@ -1794,8 +1792,8 @@ export async function apply( configToApply = replaceLegacyValues(configToApply); - const configObj = configToApply as SharedTypes.Config; - (Object.keys(DefaultConfig) as (keyof SharedTypes.Config)[]).forEach( + const configObj = configToApply as ConfigTypes.Config; + (Object.keys(DefaultConfig) as (keyof ConfigTypes.Config)[]).forEach( (configKey) => { if (configObj[configKey] === undefined) { const newValue = DefaultConfig[configKey]; @@ -1911,7 +1909,7 @@ export async function reset(): Promise<void> { export async function loadFromLocalStorage(): Promise<void> { console.log("loading localStorage config"); const newConfigString = window.localStorage.getItem("config"); - let newConfig: SharedTypes.Config; + let newConfig: ConfigTypes.Config; if ( newConfigString !== undefined && newConfigString !== null && @@ -1920,7 +1918,7 @@ export async function loadFromLocalStorage(): Promise<void> { try { newConfig = JSON.parse(newConfigString); } catch (e) { - newConfig = {} as SharedTypes.Config; + newConfig = {} as ConfigTypes.Config; } await apply(newConfig); localStorageConfig = newConfig; @@ -1933,9 +1931,9 @@ export async function loadFromLocalStorage(): Promise<void> { } function replaceLegacyValues( - configToApply: SharedTypes.Config | MonkeyTypes.ConfigChanges -): SharedTypes.Config | MonkeyTypes.ConfigChanges { - const configObj = configToApply as SharedTypes.Config; + configToApply: ConfigTypes.Config | MonkeyTypes.ConfigChanges +): ConfigTypes.Config | MonkeyTypes.ConfigChanges { + const configObj = configToApply as ConfigTypes.Config; //@ts-expect-error if (configObj.quickTab === true) { @@ -1972,7 +1970,7 @@ function replaceLegacyValues( //@ts-expect-error if (configObj.showLiveWpm === true) { - let val: SharedTypes.Config.LiveSpeedAccBurstStyle = "mini"; + let val: ConfigTypes.LiveSpeedAccBurstStyle = "mini"; if (configObj.timerStyle !== "bar" && configObj.timerStyle !== "off") { val = configObj.timerStyle; } @@ -1981,7 +1979,7 @@ function replaceLegacyValues( //@ts-expect-error if (configObj.showLiveBurst === true) { - let val: SharedTypes.Config.LiveSpeedAccBurstStyle = "mini"; + let val: ConfigTypes.LiveSpeedAccBurstStyle = "mini"; if (configObj.timerStyle !== "bar" && configObj.timerStyle !== "off") { val = configObj.timerStyle; } @@ -1990,7 +1988,7 @@ function replaceLegacyValues( //@ts-expect-error if (configObj.showLiveAcc === true) { - let val: SharedTypes.Config.LiveSpeedAccBurstStyle = "mini"; + let val: ConfigTypes.LiveSpeedAccBurstStyle = "mini"; if (configObj.timerStyle !== "bar" && configObj.timerStyle !== "off") { val = configObj.timerStyle; } @@ -2002,7 +2000,7 @@ function replaceLegacyValues( export function getConfigChanges(): MonkeyTypes.PresetConfig { const configChanges = {} as MonkeyTypes.PresetConfig; - (Object.keys(config) as (keyof SharedTypes.Config)[]) + (Object.keys(config) as (keyof ConfigTypes.Config)[]) .filter((key) => { return config[key] !== DefaultConfig[key]; }) diff --git a/frontend/src/ts/constants/default-config.ts b/frontend/src/ts/constants/default-config.ts index e19fbe898..7ff9404f9 100644 --- a/frontend/src/ts/constants/default-config.ts +++ b/frontend/src/ts/constants/default-config.ts @@ -1,3 +1,5 @@ +import { Config } from "@monkeytype/shared-types/config"; + export default { theme: "serika_dark", themeLight: "serika", @@ -94,4 +96,4 @@ export default { showAverage: "off", tapeMode: "off", maxLineWidth: 0, -} as SharedTypes.Config; +} as Config; diff --git a/frontend/src/ts/controllers/challenge-controller.ts b/frontend/src/ts/controllers/challenge-controller.ts index 51787079f..238f44801 100644 --- a/frontend/src/ts/controllers/challenge-controller.ts +++ b/frontend/src/ts/controllers/challenge-controller.ts @@ -9,6 +9,16 @@ import * as TestUI from "../test/test-ui"; import * as ConfigEvent from "../observables/config-event"; import * as TestState from "../test/test-state"; import * as Loader from "../elements/loader"; +import { + CustomTextLimitMode, + CustomTextMode, + Result, +} from "@monkeytype/shared-types"; +import { + Config as ConfigType, + Difficulty, + Mode, +} from "@monkeytype/shared-types/config"; let challengeLoading = false; @@ -23,9 +33,7 @@ export function clearActive(): void { } } -export function verify( - result: SharedTypes.Result<SharedTypes.Config.Mode> -): string | null { +export function verify(result: Result<Mode>): string | null { try { if (TestState.activeChallenge) { const afk = (result.afkDuration / result.testDuration) * 100; @@ -156,9 +164,7 @@ export function verify( } else if (requirementType === "config") { for (const configKey in requirementValue) { const configValue = requirementValue[configKey]; - if ( - Config[configKey as keyof SharedTypes.Config] !== configValue - ) { + if (Config[configKey as keyof ConfigType] !== configValue) { requirementsMet = false; failReasons.push(`${configKey} not set to ${configValue}`); } @@ -251,11 +257,9 @@ export async function setup(challengeName: string): Promise<boolean> { UpdateConfig.setDifficulty("normal", true); } else if (challenge.type === "customText") { CustomText.setText((challenge.parameters[0] as string).split(" ")); - CustomText.setMode(challenge.parameters[1] as SharedTypes.CustomTextMode); + CustomText.setMode(challenge.parameters[1] as CustomTextMode); CustomText.setLimitValue(challenge.parameters[2] as number); - CustomText.setLimitMode( - challenge.parameters[3] as SharedTypes.CustomTextLimitMode - ); + CustomText.setLimitMode(challenge.parameters[3] as CustomTextLimitMode); CustomText.setPipeDelimiter(challenge.parameters[4] as boolean); UpdateConfig.setMode("custom", true); UpdateConfig.setDifficulty("normal", true); @@ -294,15 +298,9 @@ export async function setup(challengeName: string): Promise<boolean> { } else if (challenge.parameters[1] === "time") { UpdateConfig.setTimeConfig(challenge.parameters[2] as number, true); } - UpdateConfig.setMode( - challenge.parameters[1] as SharedTypes.Config.Mode, - true - ); + UpdateConfig.setMode(challenge.parameters[1] as Mode, true); if (challenge.parameters[3] !== undefined) { - UpdateConfig.setDifficulty( - challenge.parameters[3] as SharedTypes.Config.Difficulty, - true - ); + UpdateConfig.setDifficulty(challenge.parameters[3] as Difficulty, true); } } else if (challenge.type === "special") { if (challenge.name === "semimak") { diff --git a/frontend/src/ts/controllers/page-controller.ts b/frontend/src/ts/controllers/page-controller.ts index db0423113..752af8c03 100644 --- a/frontend/src/ts/controllers/page-controller.ts +++ b/frontend/src/ts/controllers/page-controller.ts @@ -88,7 +88,6 @@ export async function change( await previousPage?.afterHide(); await nextPage?.beforeShow({ params: options.params, - // @ts-expect-error data: options.data, }); } diff --git a/frontend/src/ts/controllers/sound-controller.ts b/frontend/src/ts/controllers/sound-controller.ts index 7bcf6f9a8..dda92b455 100644 --- a/frontend/src/ts/controllers/sound-controller.ts +++ b/frontend/src/ts/controllers/sound-controller.ts @@ -8,6 +8,7 @@ import { capsState } from "../test/caps-warning"; import * as Notifications from "../elements/notifications"; import type { Howl } from "howler"; +import { PlaySoundOnClick } from "@monkeytype/shared-types/config"; async function gethowler(): Promise<typeof import("howler")> { return await import("howler"); @@ -478,10 +479,7 @@ const codeToNote: Record<string, GetNoteFrequencyCallback> = { BracketRight: bindToNote(notes.G, 2), }; -type DynamicClickSounds = Extract< - SharedTypes.Config.PlaySoundOnClick, - "8" | "9" | "10" | "11" ->; +type DynamicClickSounds = Extract<PlaySoundOnClick, "8" | "9" | "10" | "11">; type SupportedOscillatorTypes = Exclude<OscillatorType, "custom">; const clickSoundIdsToOscillatorType: Record< @@ -551,7 +549,7 @@ const defaultScaleData: ScaleData = { }; export const scaleConfigurations: Record< - Extract<SharedTypes.Config.PlaySoundOnClick, "12" | "13">, + Extract<PlaySoundOnClick, "12" | "13">, ScaleMeta > = { "12": { diff --git a/frontend/src/ts/db.ts b/frontend/src/ts/db.ts index fce4aae17..e2236eaa2 100644 --- a/frontend/src/ts/db.ts +++ b/frontend/src/ts/db.ts @@ -14,6 +14,14 @@ import { ModifiableTestActivityCalendar, } from "./elements/test-activity-calendar"; import * as Loader from "./elements/loader"; +import { PersonalBest, PersonalBests } from "@monkeytype/shared-types/user"; +import { Badge, DBResult, Result } from "@monkeytype/shared-types"; +import { + Config, + Difficulty, + Mode, + Mode2, +} from "@monkeytype/shared-types/config"; let dbSnapshot: MonkeyTypes.Snapshot | undefined; @@ -112,7 +120,7 @@ export async function initSnapshot(): Promise< }; for (const mode of ["time", "words", "quote", "zen", "custom"]) { - snap.personalBests[mode as keyof SharedTypes.PersonalBests] ??= {}; + snap.personalBests[mode as keyof PersonalBests] ??= {}; } snap.banned = userData.banned; @@ -188,7 +196,7 @@ export async function initSnapshot(): Promise< // }; // for (const mode of ["time", "words", "quote", "zen", "custom"]) { - // tag.personalBests[mode as keyof SharedTypes.PersonalBests] ??= {}; + // tag.personalBests[mode as keyof PersonalBests] ??= {}; // } // }); @@ -272,8 +280,7 @@ export async function getUserResults(offset?: number): Promise<boolean> { return false; } - const results = - response.data as SharedTypes.DBResult<SharedTypes.Config.Mode>[]; + const results = response.data as DBResult<Mode>[]; results?.sort((a, b) => b.timestamp - a.timestamp); results.forEach((result) => { if (result.bailedOut === undefined) result.bailedOut = false; @@ -311,11 +318,10 @@ export async function getUserResults(offset?: number): Promise<boolean> { (it) => it.timestamp < oldestTimestamp ); dbSnapshot.results.push( - ...(resultsWithoutDuplicates as unknown as SharedTypes.Result<SharedTypes.Config.Mode>[]) + ...(resultsWithoutDuplicates as unknown as Result<Mode>[]) ); } else { - dbSnapshot.results = - results as unknown as SharedTypes.Result<SharedTypes.Config.Mode>[]; + dbSnapshot.results = results as unknown as Result<Mode>[]; } return true; } @@ -417,13 +423,13 @@ export async function deleteCustomTheme(themeId: string): Promise<boolean> { return true; } -export async function getUserAverage10<M extends SharedTypes.Config.Mode>( +export async function getUserAverage10<M extends Mode>( mode: M, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, punctuation: boolean, numbers: boolean, language: string, - difficulty: SharedTypes.Config.Difficulty, + difficulty: Difficulty, lazyMode: boolean ): Promise<[number, number]> { const snapshot = getSnapshot(); @@ -502,13 +508,13 @@ export async function getUserAverage10<M extends SharedTypes.Config.Mode>( return retval; } -export async function getUserDailyBest<M extends SharedTypes.Config.Mode>( +export async function getUserDailyBest<M extends Mode>( mode: M, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, punctuation: boolean, numbers: boolean, language: string, - difficulty: SharedTypes.Config.Difficulty, + difficulty: Difficulty, lazyMode: boolean ): Promise<number> { const snapshot = getSnapshot(); @@ -567,13 +573,13 @@ export async function getUserDailyBest<M extends SharedTypes.Config.Mode>( return retval; } -export async function getLocalPB<M extends SharedTypes.Config.Mode>( +export async function getLocalPB<M extends Mode>( mode: M, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, punctuation: boolean, numbers: boolean, language: string, - difficulty: SharedTypes.Config.Difficulty, + difficulty: Difficulty, lazyMode: boolean, funbox: string ): Promise<number> { @@ -586,9 +592,7 @@ export async function getLocalPB<M extends SharedTypes.Config.Mode>( } if (dbSnapshot === null || dbSnapshot?.personalBests === null) return 0; - const bestsByMode = dbSnapshot?.personalBests[mode][ - mode2 - ] as SharedTypes.PersonalBest[]; + const bestsByMode = dbSnapshot?.personalBests[mode][mode2] as PersonalBest[]; if (bestsByMode === undefined) return 0; @@ -604,13 +608,13 @@ export async function getLocalPB<M extends SharedTypes.Config.Mode>( ); } -export async function saveLocalPB<M extends SharedTypes.Config.Mode>( +export async function saveLocalPB<M extends Mode>( mode: M, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, punctuation: boolean, numbers: boolean, language: string, - difficulty: SharedTypes.Config.Difficulty, + difficulty: Difficulty, lazyMode: boolean, wpm: number, acc: number, @@ -636,12 +640,10 @@ export async function saveLocalPB<M extends SharedTypes.Config.Mode>( }; dbSnapshot.personalBests[mode][mode2] ??= - [] as unknown as SharedTypes.PersonalBests[M][SharedTypes.Config.Mode2<M>]; + [] as unknown as PersonalBests[M][Mode2<M>]; ( - dbSnapshot.personalBests[mode][ - mode2 - ] as unknown as SharedTypes.PersonalBest[] + dbSnapshot.personalBests[mode][mode2] as unknown as PersonalBest[] ).forEach((pb) => { if ( (pb.punctuation ?? false) === punctuation && @@ -661,22 +663,20 @@ export async function saveLocalPB<M extends SharedTypes.Config.Mode>( }); if (!found) { //nothing found - ( - dbSnapshot.personalBests[mode][ - mode2 - ] as unknown as SharedTypes.PersonalBest[] - ).push({ - language, - difficulty, - lazyMode, - punctuation, - numbers, - wpm, - acc, - raw, - timestamp: Date.now(), - consistency, - }); + (dbSnapshot.personalBests[mode][mode2] as unknown as PersonalBest[]).push( + { + language, + difficulty, + lazyMode, + punctuation, + numbers, + wpm, + acc, + raw, + timestamp: Date.now(), + consistency, + } + ); } } @@ -685,14 +685,14 @@ export async function saveLocalPB<M extends SharedTypes.Config.Mode>( } } -export async function getLocalTagPB<M extends SharedTypes.Config.Mode>( +export async function getLocalTagPB<M extends Mode>( tagId: string, mode: M, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, punctuation: boolean, numbers: boolean, language: string, - difficulty: SharedTypes.Config.Difficulty, + difficulty: Difficulty, lazyMode: boolean ): Promise<number> { if (dbSnapshot === null) return 0; @@ -718,10 +718,10 @@ export async function getLocalTagPB<M extends SharedTypes.Config.Mode>( }; filteredtag.personalBests[mode][mode2] ??= - [] as unknown as SharedTypes.PersonalBests[M][SharedTypes.Config.Mode2<M>]; + [] as unknown as PersonalBests[M][Mode2<M>]; const personalBests = (filteredtag.personalBests[mode][mode2] ?? - []) as SharedTypes.PersonalBest[]; + []) as PersonalBest[]; ret = personalBests.find( @@ -736,14 +736,14 @@ export async function getLocalTagPB<M extends SharedTypes.Config.Mode>( return ret; } -export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>( +export async function saveLocalTagPB<M extends Mode>( tagId: string, mode: M, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, punctuation: boolean, numbers: boolean, language: string, - difficulty: SharedTypes.Config.Difficulty, + difficulty: Difficulty, lazyMode: boolean, wpm: number, acc: number, @@ -770,15 +770,13 @@ export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>( }; filteredtag.personalBests[mode][mode2] ??= - [] as unknown as SharedTypes.PersonalBests[M][SharedTypes.Config.Mode2<M>]; + [] as unknown as PersonalBests[M][Mode2<M>]; try { let found = false; ( - filteredtag.personalBests[mode][ - mode2 - ] as unknown as SharedTypes.PersonalBest[] + filteredtag.personalBests[mode][mode2] as unknown as PersonalBest[] ).forEach((pb) => { if ( (pb.punctuation ?? false) === punctuation && @@ -799,9 +797,7 @@ export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>( if (!found) { //nothing found ( - filteredtag.personalBests[mode][ - mode2 - ] as unknown as SharedTypes.PersonalBest[] + filteredtag.personalBests[mode][mode2] as unknown as PersonalBest[] ).push({ language, difficulty, @@ -837,7 +833,7 @@ export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>( timestamp: Date.now(), consistency: consistency, }, - ] as unknown as SharedTypes.PersonalBests[M][SharedTypes.Config.Mode2<M>]; + ] as unknown as PersonalBests[M][Mode2<M>]; } } @@ -848,9 +844,9 @@ export async function saveLocalTagPB<M extends SharedTypes.Config.Mode>( return; } -export async function updateLbMemory<M extends SharedTypes.Config.Mode>( +export async function updateLbMemory<M extends Mode>( mode: M, - mode2: SharedTypes.Config.Mode2<M>, + mode2: Mode2<M>, language: string, rank: number, api = false @@ -890,7 +886,7 @@ export async function updateLbMemory<M extends SharedTypes.Config.Mode>( } } -export async function saveConfig(config: SharedTypes.Config): Promise<void> { +export async function saveConfig(config: Config): Promise<void> { if (isAuthenticated()) { const response = await Ape.configs.save(config); if (response.status !== 200) { @@ -899,9 +895,7 @@ export async function saveConfig(config: SharedTypes.Config): Promise<void> { } } -export function saveLocalResult( - result: SharedTypes.Result<SharedTypes.Config.Mode> -): void { +export function saveLocalResult(result: Result<Mode>): void { const snapshot = getSnapshot(); if (!snapshot) return; @@ -946,7 +940,7 @@ export function addXp(xp: number): void { setSnapshot(snapshot); } -export function addBadge(badge: SharedTypes.Badge): void { +export function addBadge(badge: Badge): void { const snapshot = getSnapshot(); if (!snapshot) return; diff --git a/frontend/src/ts/elements/account/mini-result-chart.ts b/frontend/src/ts/elements/account/mini-result-chart.ts index 41fae6a76..4c694c034 100644 --- a/frontend/src/ts/elements/account/mini-result-chart.ts +++ b/frontend/src/ts/elements/account/mini-result-chart.ts @@ -2,6 +2,7 @@ import * as ChartController from "../../controllers/chart-controller"; import Config from "../../config"; import * as Misc from "../../utils/misc"; import * as Arrays from "../../utils/arrays"; +import { ChartData } from "@monkeytype/shared-types"; export function updatePosition(x: number, y: number): void { $(".pageAccount .miniResultChartWrapper").css({ top: y, left: x }); @@ -17,7 +18,7 @@ function hide(): void { $(".pageAccount .miniResultChartBg").stop(true, true).fadeOut(125); } -export function updateData(data: SharedTypes.ChartData): void { +export function updateData(data: ChartData): void { // let data = filteredResults[filteredId].chartData; let labels = []; for (let i = 1; i <= data.wpm.length; i++) { diff --git a/frontend/src/ts/elements/account/pb-tables.ts b/frontend/src/ts/elements/account/pb-tables.ts index 54bab58be..9db2321ea 100644 --- a/frontend/src/ts/elements/account/pb-tables.ts +++ b/frontend/src/ts/elements/account/pb-tables.ts @@ -1,6 +1,9 @@ import Config from "../../config"; import { format as dateFormat } from "date-fns/format"; import Format from "../../utils/format"; +import { PersonalBests } from "@monkeytype/shared-types/user"; +import { Mode2 } from "@monkeytype/shared-types/config"; +import { StringNumber } from "@monkeytype/shared-types/util"; function clearTables(isProfile: boolean): void { const source = isProfile ? "Profile" : "Account"; @@ -79,10 +82,7 @@ function clearTables(isProfile: boolean): void { `); } -export function update( - personalBests?: SharedTypes.PersonalBests, - isProfile = false -): void { +export function update(personalBests?: PersonalBests, isProfile = false): void { clearTables(isProfile); if (personalBests === undefined) return; @@ -93,18 +93,8 @@ export function update( $(`.page${source} .profile .pbsTime`).html(""); $(`.page${source} .profile .pbsWords`).html(""); - const timeMode2s: SharedTypes.Config.Mode2<"time">[] = [ - "15", - "30", - "60", - "120", - ]; - const wordMode2s: SharedTypes.Config.Mode2<"words">[] = [ - "10", - "25", - "50", - "100", - ]; + const timeMode2s: Mode2<"time">[] = ["15", "30", "60", "120"]; + const wordMode2s: Mode2<"words">[] = ["10", "25", "50", "100"]; timeMode2s.forEach((mode2) => { text += buildPbHtml(personalBests, "time", mode2); @@ -131,9 +121,9 @@ export function update( } function buildPbHtml( - pbs: SharedTypes.PersonalBests, + pbs: PersonalBests, mode: "time" | "words", - mode2: SharedTypes.StringNumber + mode2: StringNumber ): string { let retval = ""; let dateText = ""; diff --git a/frontend/src/ts/elements/account/result-filters.ts b/frontend/src/ts/elements/account/result-filters.ts index 7d627f4be..075acf4f0 100644 --- a/frontend/src/ts/elements/account/result-filters.ts +++ b/frontend/src/ts/elements/account/result-filters.ts @@ -7,6 +7,8 @@ import * as Notifications from "../notifications"; import Ape from "../../ape/index"; import * as Loader from "../loader"; import SlimSelect from "slim-select"; +import { ResultFilters } from "@monkeytype/shared-types"; +import { QuoteLength } from "@monkeytype/shared-types/config"; type Option = { id: string; @@ -26,11 +28,9 @@ type Option = { }; const groupsUsingSelect = ["language", "funbox", "tags"]; -const groupSelects: Partial< - Record<keyof SharedTypes.ResultFilters, SlimSelect> -> = {}; +const groupSelects: Partial<Record<keyof ResultFilters, SlimSelect>> = {}; -export const defaultResultFilters: SharedTypes.ResultFilters = { +export const defaultResultFilters: ResultFilters = { _id: "default-result-filters-id", name: "default result filters", pb: { @@ -219,13 +219,11 @@ export async function setFilterPreset(id: string): Promise<void> { ).addClass("active"); } -function deepCopyFilter( - filter: SharedTypes.ResultFilters -): SharedTypes.ResultFilters { +function deepCopyFilter(filter: ResultFilters): ResultFilters { return JSON.parse(JSON.stringify(filter)); } -function addFilterPresetToSnapshot(filter: SharedTypes.ResultFilters): void { +function addFilterPresetToSnapshot(filter: ResultFilters): void { const snapshot = DB.getSnapshot(); if (!snapshot) return; DB.setSnapshot({ @@ -285,13 +283,11 @@ function deSelectFilterPreset(): void { ).removeClass("active"); } -function getFilters(): SharedTypes.ResultFilters { +function getFilters(): ResultFilters { return filters; } -function getGroup<G extends keyof SharedTypes.ResultFilters>( - group: G -): SharedTypes.ResultFilters[G] { +function getGroup<G extends keyof ResultFilters>(group: G): ResultFilters[G] { return filters[group]; } @@ -299,25 +295,22 @@ function getGroup<G extends keyof SharedTypes.ResultFilters>( // filters[group][filter] = value; // } -export function getFilter<G extends keyof SharedTypes.ResultFilters>( +export function getFilter<G extends keyof ResultFilters>( group: G, filter: MonkeyTypes.Filter<G> -): SharedTypes.ResultFilters[G][MonkeyTypes.Filter<G>] { +): ResultFilters[G][MonkeyTypes.Filter<G>] { return filters[group][filter]; } function setFilter( - group: keyof SharedTypes.ResultFilters, + group: keyof ResultFilters, filter: MonkeyTypes.Filter<typeof group>, value: boolean ): void { filters[group][filter as keyof typeof filters[typeof group]] = value as never; } -function setAllFilters( - group: keyof SharedTypes.ResultFilters, - value: boolean -): void { +function setAllFilters(group: keyof ResultFilters, value: boolean): void { Object.keys(getGroup(group)).forEach((filter) => { filters[group][filter as keyof typeof filters[typeof group]] = value as never; @@ -336,7 +329,7 @@ export function reset(): void { } type AboveChartDisplay = Partial< - Record<keyof SharedTypes.ResultFilters, { all: boolean; array?: string[] }> + Record<keyof ResultFilters, { all: boolean; array?: string[] }> >; export function updateActive(): void { @@ -398,7 +391,7 @@ export function updateActive(): void { for (const [id, select] of Object.entries(groupSelects)) { const ss = select; - const group = getGroup(id as keyof SharedTypes.ResultFilters); + const group = getGroup(id as keyof ResultFilters); const everythingSelected = Object.values(group).every((v) => v === true); const newData = ss.store.getData(); @@ -442,7 +435,7 @@ export function updateActive(): void { }, 0); } - function addText(group: keyof SharedTypes.ResultFilters): string { + function addText(group: keyof ResultFilters): string { let ret = ""; ret += "<div class='group'>"; if (group === "difficulty") { @@ -540,7 +533,7 @@ export function updateActive(): void { }, 0); } -function toggle<G extends keyof SharedTypes.ResultFilters>( +function toggle<G extends keyof ResultFilters>( group: G, filter: MonkeyTypes.Filter<G> ): void { @@ -554,7 +547,7 @@ function toggle<G extends keyof SharedTypes.ResultFilters>( const currentValue = filters[group][filter] as unknown as boolean; const newValue = !currentValue; filters[group][filter] = - newValue as unknown as SharedTypes.ResultFilters[G][MonkeyTypes.Filter<G>]; + newValue as unknown as ResultFilters[G][MonkeyTypes.Filter<G>]; save(); } catch (e) { Notifications.add( @@ -573,7 +566,7 @@ $( ).on("click", "button", (e) => { const group = $(e.target) .parents(".buttons") - .attr("group") as keyof SharedTypes.ResultFilters; + .attr("group") as keyof ResultFilters; const filter = $(e.target).attr("filter") as MonkeyTypes.Filter<typeof group>; if ($(e.target).hasClass("allFilters")) { Misc.typedKeys(getFilters()).forEach((group) => { @@ -671,9 +664,7 @@ $(".pageAccount .topFilters button.currentConfigFilter").on("click", () => { "thicc", ]; filterName.forEach((ql, index) => { - if ( - Config.quoteLength.includes(index as SharedTypes.Config.QuoteLength) - ) { + if (Config.quoteLength.includes(index as QuoteLength)) { filters.quoteLength[ql] = true; } else { filters.quoteLength[ql] = false; @@ -726,7 +717,7 @@ $(".pageAccount .topFilters button.toggleAdvancedFilters").on("click", () => { }); function adjustScrollposition( - group: keyof SharedTypes.ResultFilters, + group: keyof ResultFilters, topItem: number = 0 ): void { const slimSelect = groupSelects[group]; @@ -738,7 +729,7 @@ function adjustScrollposition( } function selectBeforeChangeFn( - group: keyof SharedTypes.ResultFilters, + group: keyof ResultFilters, selectedOptions: Option[], oldSelectedOptions: Option[] ): void | boolean { @@ -986,12 +977,10 @@ $(".group.presetFilterButtons .filterBtns").on( } ); -function verifyResultFiltersStructure( - filterIn: SharedTypes.ResultFilters -): SharedTypes.ResultFilters { +function verifyResultFiltersStructure(filterIn: ResultFilters): ResultFilters { const filter = deepCopyFilter(filterIn); Object.entries(defaultResultFilters).forEach((entry) => { - const key = entry[0] as keyof SharedTypes.ResultFilters; + const key = entry[0] as keyof ResultFilters; const value = entry[1]; if (filter[key] === undefined) { filter[key] = value; diff --git a/frontend/src/ts/elements/custom-background-filter.ts b/frontend/src/ts/elements/custom-background-filter.ts index 9698e3788..8374bd098 100644 --- a/frontend/src/ts/elements/custom-background-filter.ts +++ b/frontend/src/ts/elements/custom-background-filter.ts @@ -1,3 +1,4 @@ +import { CustomBackgroundFilter } from "@monkeytype/shared-types/config"; import * as UpdateConfig from "../config"; import * as ConfigEvent from "../observables/config-event"; import { debounce } from "throttle-debounce"; @@ -82,7 +83,7 @@ export function updateUI(): void { updateNumbers(); } -function loadConfig(config: SharedTypes.Config.CustomBackgroundFilter): void { +function loadConfig(config: CustomBackgroundFilter): void { filters.blur.value = config[0]; filters.brightness.value = config[1]; filters.saturate.value = config[2]; @@ -152,13 +153,13 @@ $(".section[data-config-name='customBackgroundFilter'] input").on( const debouncedSave = debounce(2000, async () => { const arr = Object.keys(filters).map( (filterKey) => filters[filterKey as keyof typeof filters].value - ) as SharedTypes.Config.CustomBackgroundFilter; + ) as CustomBackgroundFilter; UpdateConfig.setCustomBackgroundFilter(arr, false); }); ConfigEvent.subscribe((eventKey, eventValue) => { if (eventKey === "customBackgroundFilter" && (eventValue as boolean)) { - loadConfig(eventValue as SharedTypes.Config.CustomBackgroundFilter); + loadConfig(eventValue as CustomBackgroundFilter); apply(); } }); diff --git a/frontend/src/ts/elements/leaderboards.ts b/frontend/src/ts/elements/leaderboards.ts index 56aeb8260..7551c18bd 100644 --- a/frontend/src/ts/elements/leaderboards.ts +++ b/frontend/src/ts/elements/leaderboards.ts @@ -16,6 +16,7 @@ import { debounce } from "throttle-debounce"; import Format from "../utils/format"; import SlimSelect from "slim-select"; import { getHtmlByUserFlags } from "../controllers/user-flag-controller"; +import { LeaderboardEntry } from "@monkeytype/shared-types"; const wrapperId = "leaderboardsWrapper"; @@ -26,7 +27,7 @@ let showingYesterday = false; type LbKey = "15" | "60"; let currentData: { - [key in LbKey]: SharedTypes.LeaderboardEntry[]; + [key in LbKey]: LeaderboardEntry[]; } = { "15": [], "60": [], diff --git a/frontend/src/ts/elements/profile.ts b/frontend/src/ts/elements/profile.ts index b5fd6298c..24be4480a 100644 --- a/frontend/src/ts/elements/profile.ts +++ b/frontend/src/ts/elements/profile.ts @@ -11,9 +11,10 @@ import * as ActivePage from "../states/active-page"; import { formatDistanceToNowStrict } from "date-fns/formatDistanceToNowStrict"; import { getHtmlByUserFlags } from "../controllers/user-flag-controller"; import Format from "../utils/format"; +import { RankAndCount, UserProfile } from "@monkeytype/shared-types"; type ProfileViewPaths = "profile" | "account"; -type UserProfileOrSnapshot = SharedTypes.UserProfile | MonkeyTypes.Snapshot; +type UserProfileOrSnapshot = UserProfile | MonkeyTypes.Snapshot; //this is probably the dirtiest code ive ever written @@ -439,7 +440,7 @@ $(window).on("resize", () => { throttledEvent(); }); -function formatTopPercentage(lbRank: SharedTypes.RankAndCount): string { +function formatTopPercentage(lbRank: RankAndCount): string { if (lbRank.rank === undefined) return "-"; if (lbRank.rank === 1) return "GOAT"; return "Top " + Numbers.roundTo2((lbRank.rank / lbRank.count) * 100) + "%"; diff --git a/frontend/src/ts/elements/psa.ts b/frontend/src/ts/elements/psa.ts index 023514d4d..9075b1908 100644 --- a/frontend/src/ts/elements/psa.ts +++ b/frontend/src/ts/elements/psa.ts @@ -4,6 +4,7 @@ import { secondsToString } from "../utils/date-and-time"; import * as Notifications from "./notifications"; import { format } from "date-fns/format"; import * as Alerts from "./alerts"; +import { PSA } from "@monkeytype/shared-types"; function clearMemory(): void { window.localStorage.setItem("confirmedPSAs", JSON.stringify([])); @@ -19,7 +20,7 @@ function setMemory(id: string): void { window.localStorage.setItem("confirmedPSAs", JSON.stringify(list)); } -async function getLatest(): Promise<SharedTypes.PSA[] | null> { +async function getLatest(): Promise<PSA[] | null> { const response = await Ape.psas.get(); if (response.status === 500) { diff --git a/frontend/src/ts/elements/settings/settings-group.ts b/frontend/src/ts/elements/settings/settings-group.ts index cefb61e46..f87e85a4d 100644 --- a/frontend/src/ts/elements/settings/settings-group.ts +++ b/frontend/src/ts/elements/settings/settings-group.ts @@ -1,8 +1,9 @@ +import { ConfigValue } from "@monkeytype/shared-types/config"; import Config from "../../config"; import * as Notifications from "../notifications"; import SlimSelect from "slim-select"; -export default class SettingsGroup<T extends SharedTypes.ConfigValue> { +export default class SettingsGroup<T extends ConfigValue> { public configName: string; public configValue: T; public configFunction: (param: T, nosave?: boolean) => boolean; diff --git a/frontend/src/ts/modals/ape-keys.ts b/frontend/src/ts/modals/ape-keys.ts index 9c3358ada..a5f21d5e1 100644 --- a/frontend/src/ts/modals/ape-keys.ts +++ b/frontend/src/ts/modals/ape-keys.ts @@ -5,6 +5,7 @@ import { format } from "date-fns/format"; import * as ConnectionState from "../states/connection"; import AnimatedModal, { ShowOptions } from "../utils/animated-modal"; import { showPopup } from "./simple-modals"; +import { ApeKey } from "@monkeytype/shared-types"; let apeKeys: Ape.ApeKeys.GetApeKeys | null = {}; @@ -34,7 +35,7 @@ function refreshList(): void { return; } apeKeyIds.forEach((apeKeyId) => { - const key = data[apeKeyId] as SharedTypes.ApeKey; + const key = data[apeKeyId] as ApeKey; table.append(` <tr keyId="${apeKeyId}"> <td> diff --git a/frontend/src/ts/modals/custom-text.ts b/frontend/src/ts/modals/custom-text.ts index b72d31674..a6fbb5d05 100644 --- a/frontend/src/ts/modals/custom-text.ts +++ b/frontend/src/ts/modals/custom-text.ts @@ -10,6 +10,7 @@ import * as Notifications from "../elements/notifications"; import * as SavedTextsPopup from "./saved-texts"; import * as SaveCustomTextPopup from "./save-custom-text"; import AnimatedModal, { ShowOptions } from "../utils/animated-modal"; +import { CustomTextMode } from "@monkeytype/shared-types"; const popup = "#customTextModal .modal"; @@ -18,7 +19,7 @@ type State = { longCustomTextWarning: boolean; challengeWarning: boolean; - customTextMode: "simple" | SharedTypes.CustomTextMode; + customTextMode: "simple" | CustomTextMode; customTextLimits: { word: string; time: string; diff --git a/frontend/src/ts/modals/edit-profile.ts b/frontend/src/ts/modals/edit-profile.ts index b179968fb..3da573c09 100644 --- a/frontend/src/ts/modals/edit-profile.ts +++ b/frontend/src/ts/modals/edit-profile.ts @@ -7,6 +7,7 @@ import * as ConnectionState from "../states/connection"; import AnimatedModal from "../utils/animated-modal"; import * as Profile from "../elements/profile"; import { CharacterCounter } from "../elements/character-counter"; +import { Badge, UserProfileDetails } from "@monkeytype/shared-types"; export function show(): void { if (!ConnectionState.get()) { @@ -59,7 +60,7 @@ function hydrateInputs(): void { websiteInput.val(socialProfiles?.website ?? ""); badgeIdsSelect.html(""); - badges?.forEach((badge: SharedTypes.Badge) => { + badges?.forEach((badge: Badge) => { if (badge.selected) { currentSelectedBadgeId = badge.id; } @@ -96,14 +97,14 @@ function initializeCharacterCounters(): void { new CharacterCounter(keyboardInput, 75); } -function buildUpdatesFromInputs(): SharedTypes.UserProfileDetails { +function buildUpdatesFromInputs(): UserProfileDetails { const bio = (bioInput.val() ?? "") as string; const keyboard = (keyboardInput.val() ?? "") as string; const twitter = (twitterInput.val() ?? "") as string; const github = (githubInput.val() ?? "") as string; const website = (websiteInput.val() ?? "") as string; - const profileUpdates: SharedTypes.UserProfileDetails = { + const profileUpdates: UserProfileDetails = { bio, keyboard, socialProfiles: { diff --git a/frontend/src/ts/modals/edit-result-tags.ts b/frontend/src/ts/modals/edit-result-tags.ts index 6c373e876..f18b75729 100644 --- a/frontend/src/ts/modals/edit-result-tags.ts +++ b/frontend/src/ts/modals/edit-result-tags.ts @@ -5,8 +5,10 @@ import * as Notifications from "../elements/notifications"; import * as AccountPage from "../pages/account"; import * as ConnectionState from "../states/connection"; import { areUnsortedArraysEqual } from "../utils/arrays"; -import * as Result from "../test/result"; +import * as TestResult from "../test/result"; import AnimatedModal from "../utils/animated-modal"; +import { Mode } from "@monkeytype/shared-types/config"; +import { Result } from "@monkeytype/shared-types"; type State = { resultId: string; @@ -126,18 +128,16 @@ async function save(): Promise<void> { duration: 2, }); - DB.getSnapshot()?.results?.forEach( - (result: SharedTypes.Result<SharedTypes.Config.Mode>) => { - if (result._id === state.resultId) { - result.tags = state.tags; - } + DB.getSnapshot()?.results?.forEach((result: Result<Mode>) => { + if (result._id === state.resultId) { + result.tags = state.tags; } - ); + }); if (state.source === "accountPage") { AccountPage.updateTagsForResult(state.resultId, state.tags); } else if (state.source === "resultPage") { - Result.updateTagsAfterEdit(state.tags, responseTagPbs); + TestResult.updateTagsAfterEdit(state.tags, responseTagPbs); } } diff --git a/frontend/src/ts/modals/mobile-test-config.ts b/frontend/src/ts/modals/mobile-test-config.ts index facf94253..145ca7cc1 100644 --- a/frontend/src/ts/modals/mobile-test-config.ts +++ b/frontend/src/ts/modals/mobile-test-config.ts @@ -6,6 +6,7 @@ import * as CustomTestDurationPopup from "./custom-test-duration"; import * as QuoteSearchModal from "./quote-search"; import * as CustomTextPopup from "./custom-text"; import AnimatedModal from "../utils/animated-modal"; +import { Mode, QuoteLength } from "@monkeytype/shared-types/config"; function update(): void { const el = $("#mobileTestConfigModal"); @@ -95,7 +96,7 @@ async function setup(modalEl: HTMLElement): Promise<void> { const target = e.currentTarget as HTMLElement; const mode = target.getAttribute("data-mode"); if (mode === Config.mode) return; - UpdateConfig.setMode(mode as SharedTypes.Config.Mode); + UpdateConfig.setMode(mode as Mode); ManualRestart.set(); TestLogic.restart(); }); @@ -136,9 +137,7 @@ async function setup(modalEl: HTMLElement): Promise<void> { newVal = [0, 1, 2, 3]; } UpdateConfig.setQuoteLength( - newVal as - | SharedTypes.Config.QuoteLength - | SharedTypes.Config.QuoteLength[], + newVal as QuoteLength | QuoteLength[], false, (e as MouseEvent).shiftKey ); diff --git a/frontend/src/ts/modals/pb-tables.ts b/frontend/src/ts/modals/pb-tables.ts index 270ea57cf..ee277dc0c 100644 --- a/frontend/src/ts/modals/pb-tables.ts +++ b/frontend/src/ts/modals/pb-tables.ts @@ -4,12 +4,14 @@ import { getLanguageDisplayString } from "../utils/strings"; import Config from "../config"; import Format from "../utils/format"; import AnimatedModal from "../utils/animated-modal"; +import { PersonalBest } from "@monkeytype/shared-types/user"; +import { Mode, Mode2 } from "@monkeytype/shared-types/config"; -type PersonalBest = { - mode2: SharedTypes.Config.Mode2<SharedTypes.Config.Mode>; -} & SharedTypes.PersonalBest; +type PBWithMode2 = { + mode2: Mode2<Mode>; +} & PersonalBest; -function update(mode: SharedTypes.Config.Mode): void { +function update(mode: Mode): void { const modalEl = modal.getModal(); (modalEl.querySelector("table tbody") as HTMLElement).innerHTML = ""; @@ -23,15 +25,13 @@ function update(mode: SharedTypes.Config.Mode): void { if (!snapshot) return; const allmode2 = snapshot.personalBests?.[mode] as - | Record<string, PersonalBest[]> + | Record<string, PBWithMode2[]> | undefined; if (allmode2 === undefined) return; - const list: PersonalBest[] = []; - ( - Object.keys(allmode2) as SharedTypes.Config.Mode2<SharedTypes.Config.Mode>[] - ).forEach(function (key) { + const list: PBWithMode2[] = []; + (Object.keys(allmode2) as Mode2<Mode>[]).forEach(function (key) { let pbs = allmode2[key] ?? []; pbs = pbs.sort(function (a, b) { return b.wpm - a.wpm; @@ -42,7 +42,7 @@ function update(mode: SharedTypes.Config.Mode): void { }); }); - let mode2memory: SharedTypes.Config.Mode2<SharedTypes.Config.Mode>; + let mode2memory: Mode2<Mode>; list.forEach((pb) => { let dateText = `-<br><span class="sub">-</span>`; @@ -82,7 +82,7 @@ function update(mode: SharedTypes.Config.Mode): void { }); } -export function show(mode: SharedTypes.Config.Mode): void { +export function show(mode: Mode): void { void modal.show({ beforeAnimation: async () => { update(mode); diff --git a/frontend/src/ts/modals/quote-search.ts b/frontend/src/ts/modals/quote-search.ts index 4fdaf2e95..3f45836a5 100644 --- a/frontend/src/ts/modals/quote-search.ts +++ b/frontend/src/ts/modals/quote-search.ts @@ -21,6 +21,7 @@ import * as TestState from "../test/test-state"; import AnimatedModal, { ShowOptions } from "../utils/animated-modal"; import * as TestLogic from "../test/test-logic"; import { createErrorMessage } from "../utils/misc"; +import { QuoteLength } from "@monkeytype/shared-types/config"; const searchServiceCache: Record<string, SearchService<MonkeyTypes.Quote>> = {}; @@ -327,7 +328,7 @@ function apply(val: number): void { ); } if (val !== null && !isNaN(val) && val >= 0) { - UpdateConfig.setQuoteLength(-2 as SharedTypes.Config.QuoteLength, false); + UpdateConfig.setQuoteLength(-2 as QuoteLength, false); TestState.setSelectedQuoteId(val); ManualRestart.set(); } else { diff --git a/frontend/src/ts/modals/share-test-settings.ts b/frontend/src/ts/modals/share-test-settings.ts index ccc61a902..b294a9cf0 100644 --- a/frontend/src/ts/modals/share-test-settings.ts +++ b/frontend/src/ts/modals/share-test-settings.ts @@ -4,19 +4,21 @@ import { getMode2 } from "../utils/misc"; import * as CustomText from "../test/custom-text"; import { compressToURI } from "lz-ts"; import AnimatedModal, { ShowOptions } from "../utils/animated-modal"; +import { Difficulty, Mode, Mode2 } from "@monkeytype/shared-types/config"; +import { CustomTextData } from "@monkeytype/shared-types"; function getCheckboxValue(checkbox: string): boolean { return $(`#shareTestSettingsModal label.${checkbox} input`).prop("checked"); } type SharedTestSettings = [ - SharedTypes.Config.Mode | null, - SharedTypes.Config.Mode2<SharedTypes.Config.Mode> | null, - SharedTypes.CustomTextData | null, + Mode | null, + Mode2<Mode> | null, + CustomTextData | null, boolean | null, boolean | null, string | null, - SharedTypes.Config.Difficulty | null, + Difficulty | null, string | null ]; @@ -38,10 +40,7 @@ function updateURL(): void { } if (getCheckboxValue("mode2")) { - settings[1] = getMode2( - Config, - currentQuote - ) as SharedTypes.Config.Mode2<SharedTypes.Config.Mode>; + settings[1] = getMode2(Config, currentQuote) as Mode2<Mode>; } if (getCheckboxValue("customText")) { diff --git a/frontend/src/ts/observables/config-event.ts b/frontend/src/ts/observables/config-event.ts index 5981a5214..62b7c386d 100644 --- a/frontend/src/ts/observables/config-event.ts +++ b/frontend/src/ts/observables/config-event.ts @@ -1,9 +1,11 @@ +import { Config, ConfigValue } from "@monkeytype/shared-types/config"; + type SubscribeFunction = ( key: string, - newValue?: SharedTypes.ConfigValue, + newValue?: ConfigValue, nosave?: boolean, - previousValue?: SharedTypes.ConfigValue, - fullConfig?: SharedTypes.Config + previousValue?: ConfigValue, + fullConfig?: Config ) => void; const subscribers: SubscribeFunction[] = []; @@ -14,10 +16,10 @@ export function subscribe(fn: SubscribeFunction): void { export function dispatch( key: string, - newValue?: SharedTypes.ConfigValue, + newValue?: ConfigValue, nosave?: boolean, - previousValue?: SharedTypes.ConfigValue, - fullConfig?: SharedTypes.Config + previousValue?: ConfigValue, + fullConfig?: Config ): void { subscribers.forEach((fn) => { try { diff --git a/frontend/src/ts/pages/about.ts b/frontend/src/ts/pages/about.ts index f376723b2..6e93ebf05 100644 --- a/frontend/src/ts/pages/about.ts +++ b/frontend/src/ts/pages/about.ts @@ -8,6 +8,7 @@ import * as ChartController from "../controllers/chart-controller"; import * as ConnectionState from "../states/connection"; import { intervalToDuration } from "date-fns/intervalToDuration"; import * as Skeleton from "../utils/skeleton"; +import { PublicTypingStats, SpeedHistogram } from "@monkeytype/shared-types"; function reset(): void { $(".pageAbout .contributors").empty(); @@ -17,8 +18,8 @@ function reset(): void { void ChartController.globalSpeedHistogram.updateColors(); } -let speedHistogramResponseData: SharedTypes.SpeedHistogram | null; -let typingStatsResponseData: SharedTypes.PublicTypingStats | null; +let speedHistogramResponseData: SpeedHistogram | null; +let typingStatsResponseData: PublicTypingStats | null; function updateStatsAndHistogram(): void { if (speedHistogramResponseData) { diff --git a/frontend/src/ts/pages/account.ts b/frontend/src/ts/pages/account.ts index 03a75b759..c89b78f30 100644 --- a/frontend/src/ts/pages/account.ts +++ b/frontend/src/ts/pages/account.ts @@ -27,6 +27,9 @@ import * as Loader from "../elements/loader"; import * as ResultBatches from "../elements/result-batches"; import Format from "../utils/format"; import * as TestActivity from "../elements/test-activity"; +import { ChartData, Result } from "@monkeytype/shared-types"; +import { Mode, Mode2, Mode2Custom } from "@monkeytype/shared-types/config"; +import { PersonalBests } from "@monkeytype/shared-types/user"; let filterDebug = false; //toggle filterdebug @@ -37,7 +40,7 @@ export function toggleFilterDebug(): void { } } -let filteredResults: SharedTypes.Result<SharedTypes.Config.Mode>[] = []; +let filteredResults: Result<Mode>[] = []; let visibleTableLines = 0; function loadMoreLines(lineIndex?: number): void { @@ -268,391 +271,385 @@ async function fillContent(): Promise<void> { filteredResults = []; $(".pageAccount .history table tbody").empty(); - DB.getSnapshot()?.results?.forEach( - (result: SharedTypes.Result<SharedTypes.Config.Mode>) => { - // totalSeconds += tt; + DB.getSnapshot()?.results?.forEach((result: Result<Mode>) => { + // totalSeconds += tt; - //apply filters - try { - if (!ResultFilters.getFilter("pb", result.isPb ? "yes" : "no")) { - if (filterDebug) { - console.log(`skipping result due to pb filter`, result); - } - return; + //apply filters + try { + if (!ResultFilters.getFilter("pb", result.isPb ? "yes" : "no")) { + if (filterDebug) { + console.log(`skipping result due to pb filter`, result); } + return; + } - let resdiff = result.difficulty; - if (resdiff === undefined) { - resdiff = "normal"; + let resdiff = result.difficulty; + if (resdiff === undefined) { + resdiff = "normal"; + } + if (!ResultFilters.getFilter("difficulty", resdiff)) { + if (filterDebug) { + console.log(`skipping result due to difficulty filter`, result); } - if (!ResultFilters.getFilter("difficulty", resdiff)) { + return; + } + if (!ResultFilters.getFilter("mode", result.mode)) { + if (filterDebug) { + console.log(`skipping result due to mode filter`, result); + } + return; + } + + if (result.mode === "time") { + let timefilter: Mode2<"time"> | "custom" = "custom"; + if ( + ["15", "30", "60", "120"].includes( + `${result.mode2}` //legacy results could have a number in mode2 + ) + ) { + timefilter = `${result.mode2}` as `${number}`; + } + if ( + !ResultFilters.getFilter( + "time", + timefilter as "custom" | "15" | "30" | "60" | "120" + ) + ) { if (filterDebug) { - console.log(`skipping result due to difficulty filter`, result); + console.log(`skipping result due to time filter`, result); } return; } - if (!ResultFilters.getFilter("mode", result.mode)) { + } else if (result.mode === "words") { + let wordfilter: Mode2Custom<"words"> = "custom"; + if ( + ["10", "25", "50", "100", "200"].includes( + `${result.mode2}` //legacy results could have a number in mode2 + ) + ) { + wordfilter = `${result.mode2}` as `${number}`; + } + if ( + !ResultFilters.getFilter( + "words", + wordfilter as "custom" | "10" | "25" | "50" | "100" + ) + ) { if (filterDebug) { - console.log(`skipping result due to mode filter`, result); + console.log(`skipping result due to word filter`, result); } return; } + } - if (result.mode === "time") { - let timefilter: SharedTypes.Config.Mode2<"time"> | "custom" = - "custom"; - if ( - ["15", "30", "60", "120"].includes( - `${result.mode2}` //legacy results could have a number in mode2 - ) - ) { - timefilter = `${result.mode2}` as `${number}`; - } - if ( - !ResultFilters.getFilter( - "time", - timefilter as "custom" | "15" | "30" | "60" | "120" - ) - ) { - if (filterDebug) { - console.log(`skipping result due to time filter`, result); - } - return; - } - } else if (result.mode === "words") { - let wordfilter: SharedTypes.Config.Mode2Custom<"words"> = "custom"; - if ( - ["10", "25", "50", "100", "200"].includes( - `${result.mode2}` //legacy results could have a number in mode2 - ) - ) { - wordfilter = `${result.mode2}` as `${number}`; - } - if ( - !ResultFilters.getFilter( - "words", - wordfilter as "custom" | "10" | "25" | "50" | "100" - ) - ) { - if (filterDebug) { - console.log(`skipping result due to word filter`, result); - } - return; - } - } - - if (result.quoteLength !== null) { - let filter: MonkeyTypes.QuoteModes | undefined = undefined; - if (result.quoteLength === 0) { - filter = "short"; - } else if (result.quoteLength === 1) { - filter = "medium"; - } else if (result.quoteLength === 2) { - filter = "long"; - } else if (result.quoteLength === 3) { - filter = "thicc"; - } - if ( - filter !== undefined && - !ResultFilters.getFilter("quoteLength", filter) - ) { - if (filterDebug) { - console.log(`skipping result due to quoteLength filter`, result); - } - return; - } + if (result.quoteLength !== null) { + let filter: MonkeyTypes.QuoteModes | undefined = undefined; + if (result.quoteLength === 0) { + filter = "short"; + } else if (result.quoteLength === 1) { + filter = "medium"; + } else if (result.quoteLength === 2) { + filter = "long"; + } else if (result.quoteLength === 3) { + filter = "thicc"; } - - let langFilter = ResultFilters.getFilter( - "language", - result.language ?? "english" - ); - if ( - result.language === "english_expanded" && - ResultFilters.getFilter("language", "english_1k") + filter !== undefined && + !ResultFilters.getFilter("quoteLength", filter) ) { - langFilter = true; - } - if (!langFilter) { if (filterDebug) { - console.log(`skipping result due to language filter`, result); + console.log(`skipping result due to quoteLength filter`, result); } return; } + } - let puncfilter: MonkeyTypes.Filter<"punctuation"> = "off"; - if (result.punctuation) { - puncfilter = "on"; + let langFilter = ResultFilters.getFilter( + "language", + result.language ?? "english" + ); + + if ( + result.language === "english_expanded" && + ResultFilters.getFilter("language", "english_1k") + ) { + langFilter = true; + } + if (!langFilter) { + if (filterDebug) { + console.log(`skipping result due to language filter`, result); } - if (!ResultFilters.getFilter("punctuation", puncfilter)) { + return; + } + + let puncfilter: MonkeyTypes.Filter<"punctuation"> = "off"; + if (result.punctuation) { + puncfilter = "on"; + } + if (!ResultFilters.getFilter("punctuation", puncfilter)) { + if (filterDebug) { + console.log(`skipping result due to punctuation filter`, result); + } + return; + } + + let numfilter: MonkeyTypes.Filter<"numbers"> = "off"; + if (result.numbers) { + numfilter = "on"; + } + if (!ResultFilters.getFilter("numbers", numfilter)) { + if (filterDebug) { + console.log(`skipping result due to numbers filter`, result); + } + return; + } + + if (result.funbox === "none" || result.funbox === undefined) { + if (!ResultFilters.getFilter("funbox", "none")) { if (filterDebug) { - console.log(`skipping result due to punctuation filter`, result); + console.log(`skipping result due to funbox filter`, result); } return; } - - let numfilter: MonkeyTypes.Filter<"numbers"> = "off"; - if (result.numbers) { - numfilter = "on"; + } else { + let counter = 0; + for (const f of result.funbox.split("#")) { + if (ResultFilters.getFilter("funbox", f)) { + counter++; + break; + } } - if (!ResultFilters.getFilter("numbers", numfilter)) { + if (counter === 0) { if (filterDebug) { - console.log(`skipping result due to numbers filter`, result); + console.log(`skipping result due to funbox filter`, result); } return; } + } - if (result.funbox === "none" || result.funbox === undefined) { - if (!ResultFilters.getFilter("funbox", "none")) { - if (filterDebug) { - console.log(`skipping result due to funbox filter`, result); - } - return; - } + let tagHide = true; + if (result.tags === undefined || result.tags.length === 0) { + //no tags, show when no tag is enabled + if ((DB.getSnapshot()?.tags?.length ?? 0) > 0) { + if (ResultFilters.getFilter("tags", "none")) tagHide = false; } else { - let counter = 0; - for (const f of result.funbox.split("#")) { - if (ResultFilters.getFilter("funbox", f)) { - counter++; - break; - } - } - if (counter === 0) { - if (filterDebug) { - console.log(`skipping result due to funbox filter`, result); - } - return; - } + tagHide = false; } - - let tagHide = true; - if (result.tags === undefined || result.tags.length === 0) { - //no tags, show when no tag is enabled - if ((DB.getSnapshot()?.tags?.length ?? 0) > 0) { - if (ResultFilters.getFilter("tags", "none")) tagHide = false; + } else { + //tags exist + const validTags = DB.getSnapshot()?.tags?.map((t) => t._id); + + if (validTags === undefined) return; + + result.tags.forEach((tag) => { + //check if i even need to check tags anymore + if (!tagHide) return; + //check if tag is valid + if (validTags?.includes(tag)) { + //tag valid, check if filter is on + if (ResultFilters.getFilter("tags", tag)) tagHide = false; } else { - tagHide = false; + //tag not found in valid tags, meaning probably deleted + if (ResultFilters.getFilter("tags", "none")) tagHide = false; } - } else { - //tags exist - const validTags = DB.getSnapshot()?.tags?.map((t) => t._id); - - if (validTags === undefined) return; - - result.tags.forEach((tag) => { - //check if i even need to check tags anymore - if (!tagHide) return; - //check if tag is valid - if (validTags?.includes(tag)) { - //tag valid, check if filter is on - if (ResultFilters.getFilter("tags", tag)) tagHide = false; - } else { - //tag not found in valid tags, meaning probably deleted - if (ResultFilters.getFilter("tags", "none")) tagHide = false; - } - }); - } + }); + } - if (tagHide) { - if (filterDebug) { - console.log(`skipping result due to tag filter`, result); - } - return; + if (tagHide) { + if (filterDebug) { + console.log(`skipping result due to tag filter`, result); } + return; + } - const timeSinceTest = Math.abs(result.timestamp - Date.now()) / 1000; + const timeSinceTest = Math.abs(result.timestamp - Date.now()) / 1000; - let datehide = true; + let datehide = true; - if ( - ResultFilters.getFilter("date", "all") || - (ResultFilters.getFilter("date", "last_day") && - timeSinceTest <= 86400) || - (ResultFilters.getFilter("date", "last_week") && - timeSinceTest <= 604800) || - (ResultFilters.getFilter("date", "last_month") && - timeSinceTest <= 2592000) || - (ResultFilters.getFilter("date", "last_3months") && - timeSinceTest <= 7776000) - ) { - datehide = false; - } + if ( + ResultFilters.getFilter("date", "all") || + (ResultFilters.getFilter("date", "last_day") && + timeSinceTest <= 86400) || + (ResultFilters.getFilter("date", "last_week") && + timeSinceTest <= 604800) || + (ResultFilters.getFilter("date", "last_month") && + timeSinceTest <= 2592000) || + (ResultFilters.getFilter("date", "last_3months") && + timeSinceTest <= 7776000) + ) { + datehide = false; + } - if (datehide) { - if (filterDebug) { - console.log(`skipping result due to date filter`, result); - } - return; + if (datehide) { + if (filterDebug) { + console.log(`skipping result due to date filter`, result); } - - filteredResults.push(result); - } catch (e) { - Notifications.add( - "Something went wrong when filtering. Resetting filters.", - 0 - ); - console.log(result); - console.error(e); - ResultFilters.reset(); - ResultFilters.updateActive(); - void update(); return; } - //filters done - //======================================= - totalEstimatedWords += Math.round( - (result.wpm / 60) * result.testDuration + filteredResults.push(result); + } catch (e) { + Notifications.add( + "Something went wrong when filtering. Resetting filters.", + 0 ); - - const resultDate = new Date(result.timestamp); - resultDate.setSeconds(0); - resultDate.setMinutes(0); - resultDate.setHours(0); - resultDate.setMilliseconds(0); - const resultTimestamp = resultDate.getTime(); - - const dataForTimestamp = activityChartData[resultTimestamp]; - - if (dataForTimestamp !== undefined) { - dataForTimestamp.amount++; - dataForTimestamp.time += + console.log(result); + console.error(e); + ResultFilters.reset(); + ResultFilters.updateActive(); + void update(); + return; + } + //filters done + //======================================= + + totalEstimatedWords += Math.round((result.wpm / 60) * result.testDuration); + + const resultDate = new Date(result.timestamp); + resultDate.setSeconds(0); + resultDate.setMinutes(0); + resultDate.setHours(0); + resultDate.setMilliseconds(0); + const resultTimestamp = resultDate.getTime(); + + const dataForTimestamp = activityChartData[resultTimestamp]; + + if (dataForTimestamp !== undefined) { + dataForTimestamp.amount++; + dataForTimestamp.time += + result.testDuration + + result.incompleteTestSeconds - + (result.afkDuration ?? 0); + dataForTimestamp.totalWpm += result.wpm; + } else { + activityChartData[resultTimestamp] = { + amount: 1, + time: result.testDuration + result.incompleteTestSeconds - - (result.afkDuration ?? 0); - dataForTimestamp.totalWpm += result.wpm; - } else { - activityChartData[resultTimestamp] = { - amount: 1, - time: - result.testDuration + - result.incompleteTestSeconds - - (result.afkDuration ?? 0), - totalWpm: result.wpm, - }; - } - - const bucketSize = typingSpeedUnit.histogramDataBucketSize; - const bucket = Math.floor( - Math.round(typingSpeedUnit.fromWpm(result.wpm)) / bucketSize - ); + (result.afkDuration ?? 0), + totalWpm: result.wpm, + }; + } - //grow array if needed - if (histogramChartData.length <= bucket) { - for (let i = histogramChartData.length; i <= bucket; i++) { - histogramChartData.push(0); - } - } - histogramChartData[bucket]++; + const bucketSize = typingSpeedUnit.histogramDataBucketSize; + const bucket = Math.floor( + Math.round(typingSpeedUnit.fromWpm(result.wpm)) / bucketSize + ); - let tt = 0; - if ( - result.testDuration === undefined && - result.mode2 !== "custom" && - result.mode2 !== "zen" - ) { - //test finished before testDuration field was introduced - estimate - if (result.mode === "time") { - tt = parseInt(result.mode2); - } else if (result.mode === "words") { - tt = (parseInt(result.mode2) / result.wpm) * 60; - } - } else { - tt = parseFloat(result.testDuration as unknown as string); //legacy results could have a string here + //grow array if needed + if (histogramChartData.length <= bucket) { + for (let i = histogramChartData.length; i <= bucket; i++) { + histogramChartData.push(0); } - if (result.incompleteTestSeconds !== undefined) { - tt += result.incompleteTestSeconds; - } else if (result.restartCount !== undefined && result.restartCount > 0) { - tt += (tt / 4) * result.restartCount; + } + (histogramChartData[bucket] as number)++; + + let tt = 0; + if ( + result.testDuration === undefined && + result.mode2 !== "custom" && + result.mode2 !== "zen" + ) { + //test finished before testDuration field was introduced - estimate + if (result.mode === "time") { + tt = parseInt(result.mode2); + } else if (result.mode === "words") { + tt = (parseInt(result.mode2) / result.wpm) * 60; } + } else { + tt = parseFloat(result.testDuration as unknown as string); //legacy results could have a string here + } + if (result.incompleteTestSeconds !== undefined) { + tt += result.incompleteTestSeconds; + } else if (result.restartCount !== undefined && result.restartCount > 0) { + tt += (tt / 4) * result.restartCount; + } - // if (result.incompleteTestSeconds !== undefined) { - // tt += result.incompleteTestSeconds; - // } else if (result.restartCount !== undefined && result.restartCount > 0) { - // tt += (tt / 4) * result.restartCount; - // } - totalSecondsFiltered += tt; - - if (last10 < 10) { - last10++; - wpmLast10total += result.wpm; - totalAcc10 += result.acc; - result.consistency !== undefined - ? (totalCons10 += result.consistency) - : 0; - } - testCount++; + // if (result.incompleteTestSeconds !== undefined) { + // tt += result.incompleteTestSeconds; + // } else if (result.restartCount !== undefined && result.restartCount > 0) { + // tt += (tt / 4) * result.restartCount; + // } + totalSecondsFiltered += tt; + + if (last10 < 10) { + last10++; + wpmLast10total += result.wpm; + totalAcc10 += result.acc; + result.consistency !== undefined + ? (totalCons10 += result.consistency) + : 0; + } + testCount++; - if (result.consistency !== undefined) { - consCount++; - totalCons += result.consistency; - if (result.consistency > topCons) { - topCons = result.consistency; - } + if (result.consistency !== undefined) { + consCount++; + totalCons += result.consistency; + if (result.consistency > topCons) { + topCons = result.consistency; } + } - if (result.rawWpm !== null) { - if (rawWpm.last10Count < 10) { - rawWpm.last10Count++; - rawWpm.last10Total += result.rawWpm; - } - rawWpm.total += result.rawWpm; - rawWpm.count++; - if (result.rawWpm > rawWpm.max) { - rawWpm.max = result.rawWpm; - } + if (result.rawWpm !== null) { + if (rawWpm.last10Count < 10) { + rawWpm.last10Count++; + rawWpm.last10Total += result.rawWpm; } - - if (result.acc > topAcc) { - topAcc = result.acc; + rawWpm.total += result.rawWpm; + rawWpm.count++; + if (result.rawWpm > rawWpm.max) { + rawWpm.max = result.rawWpm; } + } + + if (result.acc > topAcc) { + topAcc = result.acc; + } - totalAcc += result.acc; + totalAcc += result.acc; - if (result.restartCount !== undefined) { - testRestarts += result.restartCount; - } + if (result.restartCount !== undefined) { + testRestarts += result.restartCount; + } - chartData.push({ - x: filteredResults.length, - y: Numbers.roundTo2(typingSpeedUnit.fromWpm(result.wpm)), - wpm: Numbers.roundTo2(typingSpeedUnit.fromWpm(result.wpm)), - acc: result.acc, - mode: result.mode, - mode2: result.mode2, - punctuation: result.punctuation as boolean, - language: result.language, - timestamp: result.timestamp, - difficulty: result.difficulty, - raw: Numbers.roundTo2(typingSpeedUnit.fromWpm(result.rawWpm)), - isPb: result.isPb ?? false, - }); + chartData.push({ + x: filteredResults.length, + y: Numbers.roundTo2(typingSpeedUnit.fromWpm(result.wpm)), + wpm: Numbers.roundTo2(typingSpeedUnit.fromWpm(result.wpm)), + acc: result.acc, + mode: result.mode, + mode2: result.mode2, + punctuation: result.punctuation as boolean, + language: result.language, + timestamp: result.timestamp, + difficulty: result.difficulty, + raw: Numbers.roundTo2(typingSpeedUnit.fromWpm(result.rawWpm)), + isPb: result.isPb ?? false, + }); - wpmChartData.push(result.wpm); + wpmChartData.push(result.wpm); - accChartData.push({ - x: filteredResults.length, - y: result.acc, - errorRate: 100 - result.acc, - }); + accChartData.push({ + x: filteredResults.length, + y: result.acc, + errorRate: 100 - result.acc, + }); - if (result.wpm > topWpm) { - const puncsctring = result.punctuation ? ",<br>with punctuation" : ""; - const numbsctring = result.numbers - ? ",<br> " + (result.punctuation ? "&" : "") + "with numbers" - : ""; - topWpm = result.wpm; - if (result.mode === "custom") topMode = result.mode; - else { - topMode = - result.mode + " " + result.mode2 + puncsctring + numbsctring; - } + if (result.wpm > topWpm) { + const puncsctring = result.punctuation ? ",<br>with punctuation" : ""; + const numbsctring = result.numbers + ? ",<br> " + (result.punctuation ? "&" : "") + "with numbers" + : ""; + topWpm = result.wpm; + if (result.mode === "custom") topMode = result.mode; + else { + topMode = result.mode + " " + result.mode2 + puncsctring + numbsctring; } - - totalWpm += result.wpm; } - ); + + totalWpm += result.wpm; + }); $(".pageAccount .group.history table thead tr td:nth-child(2)").text( Config.typingSpeedUnit @@ -1094,9 +1091,7 @@ function sortAndRefreshHistory( temp.push(filteredResults[idx]); parsedIndexes.push(idx); } - filteredResults = temp as SharedTypes.Result< - keyof SharedTypes.PersonalBests - >[]; + filteredResults = temp as Result<keyof PersonalBests>[]; $(".pageAccount .history table tbody").empty(); visibleTableLines = 0; @@ -1145,7 +1140,7 @@ $(".pageAccount").on("click", ".miniResultChartButton", (event) => { const filteredId = $(event.currentTarget).attr("filteredResultsId"); if (filteredId === undefined) return; MiniResultChart.updateData( - filteredResults[parseInt(filteredId)]?.chartData as SharedTypes.ChartData + filteredResults[parseInt(filteredId)]?.chartData as ChartData ); MiniResultChart.show(); MiniResultChart.updatePosition( diff --git a/frontend/src/ts/pages/profile.ts b/frontend/src/ts/pages/profile.ts index 56698a486..4043d5b9a 100644 --- a/frontend/src/ts/pages/profile.ts +++ b/frontend/src/ts/pages/profile.ts @@ -6,6 +6,8 @@ import * as Notifications from "../elements/notifications"; import { checkIfGetParameterExists } from "../utils/misc"; import * as UserReportModal from "../modals/user-report"; import * as Skeleton from "../utils/skeleton"; +import { UserProfile } from "@monkeytype/shared-types"; +import { PersonalBests } from "@monkeytype/shared-types/user"; function reset(): void { $(".page.pageProfile .preloader").removeClass("hidden"); @@ -156,7 +158,7 @@ function reset(): void { type UpdateOptions = { uidOrName?: string; - data?: undefined | SharedTypes.UserProfile; + data?: undefined | UserProfile; }; async function update(options: UpdateOptions): Promise<void> { @@ -166,7 +168,7 @@ async function update(options: UpdateOptions): Promise<void> { await Profile.update("profile", options.data); PbTables.update( // this cast is fine because pb tables can handle the partial data inside user profiles - options.data.personalBests as unknown as SharedTypes.PersonalBests, + options.data.personalBests as unknown as PersonalBests, true ); } else if (options.uidOrName !== undefined && options.uidOrName !== "") { @@ -193,7 +195,7 @@ async function update(options: UpdateOptions): Promise<void> { await Profile.update("profile", response.data); // this cast is fine because pb tables can handle the partial data inside user profiles PbTables.update( - response.data.personalBests as unknown as SharedTypes.PersonalBests, + response.data.personalBests as unknown as PersonalBests, true ); } @@ -211,7 +213,7 @@ $(".page.pageProfile").on("click", ".profile .userReportButton", () => { void UserReportModal.show({ uid, name, lbOptOut }); }); -export const page = new Page<undefined | SharedTypes.UserProfile>({ +export const page = new Page<undefined | UserProfile>({ name: "profile", element: $(".page.pageProfile"), path: "/profile", diff --git a/frontend/src/ts/pages/settings.ts b/frontend/src/ts/pages/settings.ts index bf4d47fee..85a947676 100644 --- a/frontend/src/ts/pages/settings.ts +++ b/frontend/src/ts/pages/settings.ts @@ -22,13 +22,11 @@ import SlimSelect from "slim-select"; import * as Skeleton from "../utils/skeleton"; import * as CustomBackgroundFilter from "../elements/custom-background-filter"; +import { ConfigValue } from "@monkeytype/shared-types/config"; -type SettingsGroups<T extends SharedTypes.ConfigValue> = Record< - string, - SettingsGroup<T> ->; +type SettingsGroups<T extends ConfigValue> = Record<string, SettingsGroup<T>>; -export const groups: SettingsGroups<SharedTypes.ConfigValue> = {}; +export const groups: SettingsGroups<ConfigValue> = {}; async function initGroups(): Promise<void> { await UpdateConfig.loadPromise; @@ -36,22 +34,22 @@ async function initGroups(): Promise<void> { "smoothCaret", UpdateConfig.setSmoothCaret, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["difficulty"] = new SettingsGroup( "difficulty", UpdateConfig.setDifficulty, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["quickRestart"] = new SettingsGroup( "quickRestart", UpdateConfig.setQuickRestartMode, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["showAverage"] = new SettingsGroup( "showAverage", UpdateConfig.setShowAverage, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["keymapMode"] = new SettingsGroup( "keymapMode", UpdateConfig.setKeymapMode, @@ -88,27 +86,27 @@ async function initGroups(): Promise<void> { ).removeClass("hidden"); } } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["keymapMatrix"] = new SettingsGroup( "keymapStyle", UpdateConfig.setKeymapStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["keymapLayout"] = new SettingsGroup( "keymapLayout", UpdateConfig.setKeymapLayout, "select" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["keymapLegendStyle"] = new SettingsGroup( "keymapLegendStyle", UpdateConfig.setKeymapLegendStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["keymapShowTopRow"] = new SettingsGroup( "keymapShowTopRow", UpdateConfig.setKeymapShowTopRow, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["showKeyTips"] = new SettingsGroup( "showKeyTips", UpdateConfig.setKeyTips, @@ -121,7 +119,7 @@ async function initGroups(): Promise<void> { $(".pageSettings .tip").addClass("hidden"); } } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["freedomMode"] = new SettingsGroup( "freedomMode", UpdateConfig.setFreedomMode, @@ -129,17 +127,17 @@ async function initGroups(): Promise<void> { () => { groups["confidenceMode"]?.updateUI(); } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["strictSpace"] = new SettingsGroup( "strictSpace", UpdateConfig.setStrictSpace, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["oppositeShiftMode"] = new SettingsGroup( "oppositeShiftMode", UpdateConfig.setOppositeShiftMode, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["confidenceMode"] = new SettingsGroup( "confidenceMode", UpdateConfig.setConfidenceMode, @@ -148,87 +146,87 @@ async function initGroups(): Promise<void> { groups["freedomMode"]?.updateUI(); groups["stopOnError"]?.updateUI(); } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["indicateTypos"] = new SettingsGroup( "indicateTypos", UpdateConfig.setIndicateTypos, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["hideExtraLetters"] = new SettingsGroup( "hideExtraLetters", UpdateConfig.setHideExtraLetters, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["blindMode"] = new SettingsGroup( "blindMode", UpdateConfig.setBlindMode, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["quickEnd"] = new SettingsGroup( "quickEnd", UpdateConfig.setQuickEnd, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["repeatQuotes"] = new SettingsGroup( "repeatQuotes", UpdateConfig.setRepeatQuotes, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["ads"] = new SettingsGroup( "ads", UpdateConfig.setAds, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["alwaysShowWordsHistory"] = new SettingsGroup( "alwaysShowWordsHistory", UpdateConfig.setAlwaysShowWordsHistory, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["britishEnglish"] = new SettingsGroup( "britishEnglish", UpdateConfig.setBritishEnglish, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["singleListCommandLine"] = new SettingsGroup( "singleListCommandLine", UpdateConfig.setSingleListCommandLine, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["capsLockWarning"] = new SettingsGroup( "capsLockWarning", UpdateConfig.setCapsLockWarning, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["flipTestColors"] = new SettingsGroup( "flipTestColors", UpdateConfig.setFlipTestColors, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["showOutOfFocusWarning"] = new SettingsGroup( "showOutOfFocusWarning", UpdateConfig.setShowOutOfFocusWarning, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["colorfulMode"] = new SettingsGroup( "colorfulMode", UpdateConfig.setColorfulMode, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["startGraphsAtZero"] = new SettingsGroup( "startGraphsAtZero", UpdateConfig.setStartGraphsAtZero, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["autoSwitchTheme"] = new SettingsGroup( "autoSwitchTheme", UpdateConfig.setAutoSwitchTheme, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["randomTheme"] = new SettingsGroup( "randomTheme", UpdateConfig.setRandomTheme, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["stopOnError"] = new SettingsGroup( "stopOnError", UpdateConfig.setStopOnError, @@ -236,12 +234,12 @@ async function initGroups(): Promise<void> { () => { groups["confidenceMode"]?.updateUI(); } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["soundVolume"] = new SettingsGroup( "soundVolume", UpdateConfig.setSoundVolume, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["playSoundOnError"] = new SettingsGroup( "playSoundOnError", UpdateConfig.setPlaySoundOnError, @@ -249,7 +247,7 @@ async function initGroups(): Promise<void> { () => { if (Config.playSoundOnError !== "off") void Sound.playError(); } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["playSoundOnClick"] = new SettingsGroup( "playSoundOnClick", UpdateConfig.setPlaySoundOnClick, @@ -257,117 +255,117 @@ async function initGroups(): Promise<void> { () => { if (Config.playSoundOnClick !== "off") void Sound.playClick("KeyQ"); } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["showAllLines"] = new SettingsGroup( "showAllLines", UpdateConfig.setShowAllLines, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["paceCaret"] = new SettingsGroup( "paceCaret", UpdateConfig.setPaceCaret, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["repeatedPace"] = new SettingsGroup( "repeatedPace", UpdateConfig.setRepeatedPace, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["minWpm"] = new SettingsGroup( "minWpm", UpdateConfig.setMinWpm, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["minAcc"] = new SettingsGroup( "minAcc", UpdateConfig.setMinAcc, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["minBurst"] = new SettingsGroup( "minBurst", UpdateConfig.setMinBurst, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["smoothLineScroll"] = new SettingsGroup( "smoothLineScroll", UpdateConfig.setSmoothLineScroll, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["lazyMode"] = new SettingsGroup( "lazyMode", UpdateConfig.setLazyMode, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["layout"] = new SettingsGroup( "layout", UpdateConfig.setLayout, "select" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["language"] = new SettingsGroup( "language", UpdateConfig.setLanguage, "select" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["fontSize"] = new SettingsGroup( "fontSize", UpdateConfig.setFontSize, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["maxLineWidth"] = new SettingsGroup( "maxLineWidth", UpdateConfig.setMaxLineWidth, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["caretStyle"] = new SettingsGroup( "caretStyle", UpdateConfig.setCaretStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["paceCaretStyle"] = new SettingsGroup( "paceCaretStyle", UpdateConfig.setPaceCaretStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["timerStyle"] = new SettingsGroup( "timerStyle", UpdateConfig.setTimerStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["liveSpeedStyle"] = new SettingsGroup( "liveSpeedStyle", UpdateConfig.setLiveSpeedStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["liveAccStyle"] = new SettingsGroup( "liveAccStyle", UpdateConfig.setLiveAccStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["liveBurstStyle"] = new SettingsGroup( "liveBurstStyle", UpdateConfig.setLiveBurstStyle, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["highlightMode"] = new SettingsGroup( "highlightMode", UpdateConfig.setHighlightMode, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["tapeMode"] = new SettingsGroup( "tapeMode", UpdateConfig.setTapeMode, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["timerOpacity"] = new SettingsGroup( "timerOpacity", UpdateConfig.setTimerOpacity, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["timerColor"] = new SettingsGroup( "timerColor", UpdateConfig.setTimerColor, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["fontFamily"] = new SettingsGroup( "fontFamily", UpdateConfig.setFontFamily, @@ -388,22 +386,22 @@ async function initGroups(): Promise<void> { customButton.text("Custom"); } } - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["alwaysShowDecimalPlaces"] = new SettingsGroup( "alwaysShowDecimalPlaces", UpdateConfig.setAlwaysShowDecimalPlaces, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["typingSpeedUnit"] = new SettingsGroup( "typingSpeedUnit", UpdateConfig.setTypingSpeedUnit, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; groups["customBackgroundSize"] = new SettingsGroup( "customBackgroundSize", UpdateConfig.setCustomBackgroundSize, "button" - ) as SettingsGroup<SharedTypes.ConfigValue>; + ) as SettingsGroup<ConfigValue>; // groups.customLayoutfluid = new SettingsGroup( // "customLayoutfluid", // UpdateConfig.setCustomLayoutfluid diff --git a/frontend/src/ts/test/custom-text.ts b/frontend/src/ts/test/custom-text.ts index b106e7da2..cb3fd1533 100644 --- a/frontend/src/ts/test/custom-text.ts +++ b/frontend/src/ts/test/custom-text.ts @@ -1,3 +1,10 @@ +import { + CustomTextData, + CustomTextLimit, + CustomTextLimitMode, + CustomTextMode, +} from "@monkeytype/shared-types"; + let text: string[] = [ "The", "quick", @@ -10,8 +17,8 @@ let text: string[] = [ "dog", ]; -let mode: SharedTypes.CustomTextMode = "repeat"; -const limit: SharedTypes.CustomTextLimit = { +let mode: CustomTextMode = "repeat"; +const limit: CustomTextLimit = { value: 9, mode: "word", }; @@ -26,16 +33,16 @@ export function setText(txt: string[]): void { limit.value = text.length; } -export function getMode(): SharedTypes.CustomTextMode { +export function getMode(): CustomTextMode { return mode; } -export function setMode(val: SharedTypes.CustomTextMode): void { +export function setMode(val: CustomTextMode): void { mode = val; limit.value = text.length; } -export function getLimit(): SharedTypes.CustomTextLimit { +export function getLimit(): CustomTextLimit { return limit; } @@ -43,7 +50,7 @@ export function getLimitValue(): number { return limit.value; } -export function getLimitMode(): SharedTypes.CustomTextLimitMode { +export function getLimitMode(): CustomTextLimitMode { return limit.mode; } @@ -51,7 +58,7 @@ export function setLimitValue(val: number): void { limit.value = val; } -export function setLimitMode(val: SharedTypes.CustomTextLimitMode): void { +export function setLimitMode(val: CustomTextLimitMode): void { limit.mode = val; } @@ -63,7 +70,7 @@ export function setPipeDelimiter(val: boolean): void { pipeDelimiter = val; } -export function getData(): SharedTypes.CustomTextData { +export function getData(): CustomTextData { return { text, mode, diff --git a/frontend/src/ts/test/funbox/funbox-memory.ts b/frontend/src/ts/test/funbox/funbox-memory.ts index d65302c49..1ba9d6e94 100644 --- a/frontend/src/ts/test/funbox/funbox-memory.ts +++ b/frontend/src/ts/test/funbox/funbox-memory.ts @@ -1,3 +1,5 @@ +import { ConfigValue } from "@monkeytype/shared-types/config"; + type SetFunction<T> = (param: T, nosave?: boolean) => boolean; type ValueAndSetFunction<T> = { @@ -7,24 +9,22 @@ type ValueAndSetFunction<T> = { type SettingsMemory<T> = Record<string, ValueAndSetFunction<T>>; -let settingsMemory: SettingsMemory<SharedTypes.ConfigValue> = {}; +let settingsMemory: SettingsMemory<ConfigValue> = {}; -export function save<T extends SharedTypes.ConfigValue>( +export function save<T extends ConfigValue>( settingName: string, value: T, setFunction: SetFunction<T> ): void { settingsMemory[settingName] ??= { value, - setFunction: setFunction as SetFunction<SharedTypes.ConfigValue>, + setFunction: setFunction as SetFunction<ConfigValue>, }; } export function load(): void { Object.keys(settingsMemory).forEach((setting) => { - const memory = settingsMemory[ - setting - ] as ValueAndSetFunction<SharedTypes.ConfigValue>; + const memory = settingsMemory[setting] as ValueAndSetFunction<ConfigValue>; memory.setFunction(memory.value, true); }); settingsMemory = {}; diff --git a/frontend/src/ts/test/funbox/funbox-validation.ts b/frontend/src/ts/test/funbox/funbox-validation.ts index 7a4deb008..8eab9cb76 100644 --- a/frontend/src/ts/test/funbox/funbox-validation.ts +++ b/frontend/src/ts/test/funbox/funbox-validation.ts @@ -2,14 +2,15 @@ import * as FunboxList from "./funbox-list"; import * as Notifications from "../../elements/notifications"; import * as Arrays from "../../utils/arrays"; import * as Strings from "../../utils/strings"; +import { Config, ConfigValue } from "@monkeytype/shared-types/config"; export function checkFunboxForcedConfigs( key: string, - value: SharedTypes.ConfigValue, + value: ConfigValue, funbox: string ): { result: boolean; - forcedConfigs?: SharedTypes.ConfigValue[]; + forcedConfigs?: ConfigValue[]; } { if (FunboxList.get(funbox).length === 0) return { result: true }; @@ -33,20 +34,18 @@ export function checkFunboxForcedConfigs( return { result: true }; } } else { - const forcedConfigs: Record<string, SharedTypes.ConfigValue[]> = {}; + const forcedConfigs: Record<string, ConfigValue[]> = {}; // collect all forced configs for (const fb of FunboxList.get(funbox)) { if (fb.forcedConfig) { //push keys to forcedConfigs, if they don't exist. if they do, intersect the values for (const key in fb.forcedConfig) { if (forcedConfigs[key] === undefined) { - forcedConfigs[key] = fb.forcedConfig[ - key - ] as SharedTypes.ConfigValue[]; + forcedConfigs[key] = fb.forcedConfig[key] as ConfigValue[]; } else { forcedConfigs[key] = Arrays.intersect( - forcedConfigs[key] as SharedTypes.ConfigValue[], - fb.forcedConfig[key] as SharedTypes.ConfigValue[], + forcedConfigs[key] as ConfigValue[], + fb.forcedConfig[key] as ConfigValue[], true ); } @@ -62,9 +61,7 @@ export function checkFunboxForcedConfigs( throw new Error("No intersection of forced configs"); } return { - result: (forcedConfigs[key] ?? []).includes( - value as SharedTypes.ConfigValue - ), + result: (forcedConfigs[key] ?? []).includes(value as ConfigValue), forcedConfigs: forcedConfigs[key], }; } @@ -76,7 +73,7 @@ export function checkFunboxForcedConfigs( // if it returns false, show a notification and return false export function canSetConfigWithCurrentFunboxes( key: string, - value: SharedTypes.ConfigValue, + value: ConfigValue, funbox: string, noNotification = false ): boolean { @@ -157,7 +154,7 @@ export function canSetConfigWithCurrentFunboxes( export function canSetFunboxWithConfig( funbox: string, - config: SharedTypes.Config + config: Config ): boolean { console.log("cansetfunboxwithconfig", funbox, config.mode); let funboxToCheck = config.funbox; @@ -306,8 +303,8 @@ export function areFunboxesCompatible( if (allowedConfig[key]) { if ( Arrays.intersect( - allowedConfig[key] as SharedTypes.ConfigValue[], - f.forcedConfig[key] as SharedTypes.ConfigValue[], + allowedConfig[key] as ConfigValue[], + f.forcedConfig[key] as ConfigValue[], true ).length === 0 ) { @@ -315,7 +312,7 @@ export function areFunboxesCompatible( break; } } else { - allowedConfig[key] = f.forcedConfig[key] as SharedTypes.ConfigValue[]; + allowedConfig[key] = f.forcedConfig[key] as ConfigValue[]; } } } diff --git a/frontend/src/ts/test/funbox/funbox.ts b/frontend/src/ts/test/funbox/funbox.ts index a0916db3f..fbcf100c4 100644 --- a/frontend/src/ts/test/funbox/funbox.ts +++ b/frontend/src/ts/test/funbox/funbox.ts @@ -26,6 +26,7 @@ import { import { Wordset } from "../wordset"; import * as LayoutfluidFunboxTimer from "./layoutfluid-funbox-timer"; import * as DDR from "../../utils/ddr"; +import { HighlightMode, Mode } from "@monkeytype/shared-types/config"; const prefixSize = 2; @@ -40,7 +41,7 @@ class CharDistribution { public addChar(char: string): void { this.count++; if (char in this.chars) { - this.chars[char]++; + (this.chars[char] as number)++; } else { this.chars[char] = 1; } @@ -646,9 +647,7 @@ export async function activate(funbox?: string): Promise<boolean | undefined> { if (!check.result) { if (check.forcedConfigs && check.forcedConfigs.length > 0) { if (configKey === "mode") { - UpdateConfig.setMode( - check.forcedConfigs[0] as SharedTypes.Config.Mode - ); + UpdateConfig.setMode(check.forcedConfigs[0] as Mode); } if (configKey === "words") { UpdateConfig.setWordCount(check.forcedConfigs[0] as number); @@ -664,7 +663,7 @@ export async function activate(funbox?: string): Promise<boolean | undefined> { } if (configKey === "highlightMode") { UpdateConfig.setHighlightMode( - check.forcedConfigs[0] as SharedTypes.Config.HighlightMode + check.forcedConfigs[0] as HighlightMode ); } } else { diff --git a/frontend/src/ts/test/pace-caret.ts b/frontend/src/ts/test/pace-caret.ts index b92e99126..2d400ef81 100644 --- a/frontend/src/ts/test/pace-caret.ts +++ b/frontend/src/ts/test/pace-caret.ts @@ -8,6 +8,7 @@ import * as Numbers from "../utils/numbers"; import * as JSONData from "../utils/json-data"; import * as TestState from "./test-state"; import * as ConfigEvent from "../observables/config-event"; +import { Mode2 } from "@monkeytype/shared-types/config"; type Settings = { wpm: number; @@ -66,10 +67,9 @@ async function resetCaretPosition(): Promise<void> { export async function init(): Promise<void> { $("#paceCaret").addClass("hidden"); - const mode2 = Misc.getMode2( - Config, - TestWords.currentQuote - ) as SharedTypes.Config.Mode2<typeof Config.mode>; + const mode2 = Misc.getMode2(Config, TestWords.currentQuote) as Mode2< + typeof Config.mode + >; let wpm; if (Config.paceCaret === "pb") { wpm = await DB.getLocalPB( diff --git a/frontend/src/ts/test/practise-words.ts b/frontend/src/ts/test/practise-words.ts index e3488037a..71fe47efa 100644 --- a/frontend/src/ts/test/practise-words.ts +++ b/frontend/src/ts/test/practise-words.ts @@ -7,14 +7,16 @@ import * as ConfigEvent from "../observables/config-event"; import { setCustomTextName } from "../states/custom-text-name"; import * as Skeleton from "../utils/skeleton"; import { isPopupVisible } from "../utils/misc"; +import { Mode } from "@monkeytype/shared-types/config"; +import { CustomTextData } from "@monkeytype/shared-types"; const wrapperId = "practiseWordsPopupWrapper"; type Before = { - mode: SharedTypes.Config.Mode | null; + mode: Mode | null; punctuation: boolean | null; numbers: boolean | null; - customText: SharedTypes.CustomTextData | null; + customText: CustomTextData | null; }; export const before: Before = { diff --git a/frontend/src/ts/test/result.ts b/frontend/src/ts/test/result.ts index 3ff7acb66..9c53bf804 100644 --- a/frontend/src/ts/test/result.ts +++ b/frontend/src/ts/test/result.ts @@ -36,8 +36,10 @@ import Format from "../utils/format"; import confetti from "canvas-confetti"; import type { AnnotationOptions } from "chartjs-plugin-annotation"; import Ape from "../ape"; +import { Result } from "@monkeytype/shared-types"; +import { Mode } from "@monkeytype/shared-types/config"; -let result: SharedTypes.Result<SharedTypes.Config.Mode>; +let result: Result<Mode>; let maxChartVal: number; let useUnsmoothedRaw = false; @@ -817,7 +819,7 @@ function updateQuoteSource(randomQuote: MonkeyTypes.Quote | null): void { } export async function update( - res: SharedTypes.Result<SharedTypes.Config.Mode>, + res: Result<Mode>, difficultyFailed: boolean, failReason: string, afkDetected: boolean, diff --git a/frontend/src/ts/test/test-config.ts b/frontend/src/ts/test/test-config.ts index fdb0a557d..f3fdb95b3 100644 --- a/frontend/src/ts/test/test-config.ts +++ b/frontend/src/ts/test/test-config.ts @@ -1,3 +1,8 @@ +import { + ConfigValue, + Mode, + QuoteLength, +} from "@monkeytype/shared-types/config"; import Config from "../config"; import * as ConfigEvent from "../observables/config-event"; import * as ActivePage from "../states/active-page"; @@ -56,10 +61,7 @@ export async function instantUpdate(): Promise<void> { updateExtras("punctuation", Config.punctuation); } -export async function update( - previous: SharedTypes.Config.Mode, - current: SharedTypes.Config.Mode -): Promise<void> { +export async function update(previous: Mode, current: Mode): Promise<void> { if (previous === current) return; $("#testConfig .mode .textButton").removeClass("active"); $("#testConfig .mode .textButton[mode='" + current + "']").addClass("active"); @@ -198,10 +200,7 @@ export async function update( ); } -export function updateExtras( - key: string, - value: SharedTypes.ConfigValue -): void { +export function updateExtras(key: string, value: ConfigValue): void { if (key === "time") { $("#testConfig .time .textButton").removeClass("active"); const timeCustom = ![15, 30, 60, 120].includes(value as number) @@ -222,7 +221,7 @@ export function updateExtras( ).addClass("active"); } else if (key === "quoteLength") { $("#testConfig .quoteLength .textButton").removeClass("active"); - (value as SharedTypes.Config.QuoteLength[]).forEach((ql) => { + (value as QuoteLength[]).forEach((ql) => { $( "#testConfig .quoteLength .textButton[quoteLength='" + ql + "']" ).addClass("active"); @@ -253,10 +252,7 @@ export function hideFavoriteQuoteLength(): void { ConfigEvent.subscribe((eventKey, eventValue, _nosave, eventPreviousValue) => { if (ActivePage.get() !== "test") return; if (eventKey === "mode") { - void update( - eventPreviousValue as SharedTypes.Config.Mode, - eventValue as SharedTypes.Config.Mode - ); + void update(eventPreviousValue as Mode, eventValue as Mode); let m2; diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index 8f84d3e84..097bff522 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -58,11 +58,16 @@ import * as KeymapEvent from "../observables/keymap-event"; import * as LayoutfluidFunboxTimer from "../test/funbox/layoutfluid-funbox-timer"; import * as ArabicLazyMode from "../states/arabic-lazy-mode"; import Format from "../utils/format"; +import { + CompletedEvent, + CustomTextDataWithTextLen, +} from "@monkeytype/shared-types"; +import { Mode, QuoteLength } from "@monkeytype/shared-types/config"; let failReason = ""; const koInputVisual = document.getElementById("koInputVisual") as HTMLElement; -export let notSignedInLastResult: SharedTypes.CompletedEvent | null = null; +export let notSignedInLastResult: CompletedEvent | null = null; export function clearNotSignedInResult(): void { notSignedInLastResult = null; @@ -600,7 +605,7 @@ export async function addWord(): Promise<void> { } type RetrySaving = { - completedEvent: SharedTypes.CompletedEvent | null; + completedEvent: CompletedEvent | null; canRetry: boolean; }; @@ -639,9 +644,7 @@ export async function retrySavingResult(): Promise<void> { await saveResult(completedEvent, true); } -function buildCompletedEvent( - difficultyFailed: boolean -): SharedTypes.CompletedEvent { +function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent { //build completed event object let stfk = Numbers.roundTo2( TestInput.keypressTimings.spacing.first - TestStats.start @@ -733,7 +736,7 @@ function buildCompletedEvent( const wpmCons = Numbers.roundTo2(Misc.kogasa(stddev3 / avg3)); const wpmConsistency = isNaN(wpmCons) ? 0 : wpmCons; - let customText: SharedTypes.CustomTextDataWithTextLen | null = null; + let customText: CustomTextDataWithTextLen | null = null; if (Config.mode === "custom") { const temp = CustomText.getData(); customText = { @@ -804,7 +807,7 @@ function buildCompletedEvent( testDuration: duration, afkDuration: afkDuration, stopOnLetter: Config.stopOnError === "letter", - } as SharedTypes.CompletedEvent; + } as CompletedEvent; if (completedEvent.mode !== "custom") delete completedEvent.customText; if (completedEvent.mode !== "quote") delete completedEvent.quoteLength; @@ -1098,7 +1101,7 @@ export async function finish(difficultyFailed = false): Promise<void> { } async function saveResult( - completedEvent: SharedTypes.CompletedEvent, + completedEvent: CompletedEvent, isRetrying: boolean ): Promise<void> { if (!TestState.savingEnabled) { @@ -1312,8 +1315,7 @@ $(".pageTest").on("click", "#restartTestButtonWithSameWordset", () => { $(".pageTest").on("click", "#testConfig .mode .textButton", (e) => { if (TestUI.testRestarting) return; if ($(e.currentTarget).hasClass("active")) return; - const mode = ($(e.currentTarget).attr("mode") ?? - "time") as SharedTypes.Config.Mode; + const mode = ($(e.currentTarget).attr("mode") ?? "time") as Mode; if (mode === undefined) return; UpdateConfig.setMode(mode); ManualRestart.set(); @@ -1342,10 +1344,9 @@ $(".pageTest").on("click", "#testConfig .time .textButton", (e) => { $(".pageTest").on("click", "#testConfig .quoteLength .textButton", (e) => { if (TestUI.testRestarting) return; - let len: SharedTypes.Config.QuoteLength | SharedTypes.Config.QuoteLength[] = - parseInt( - $(e.currentTarget).attr("quoteLength") ?? "1" - ) as SharedTypes.Config.QuoteLength; + let len: QuoteLength | QuoteLength[] = parseInt( + $(e.currentTarget).attr("quoteLength") ?? "1" + ) as QuoteLength; if (len !== -2) { if (len === -1) { len = [0, 1, 2, 3]; diff --git a/frontend/src/ts/test/test-stats.ts b/frontend/src/ts/test/test-stats.ts index 1578b5a8b..03ccf3028 100644 --- a/frontend/src/ts/test/test-stats.ts +++ b/frontend/src/ts/test/test-stats.ts @@ -6,6 +6,8 @@ import * as TestWords from "./test-words"; import * as FunboxList from "./funbox/funbox-list"; import * as TestState from "./test-state"; import * as Numbers from "../utils/numbers"; +import { IncompleteTest, Result } from "@monkeytype/shared-types"; +import { Mode } from "@monkeytype/shared-types/config"; type CharCount = { spaces: number; @@ -37,11 +39,9 @@ export let start2: number, end2: number; export let start3: number, end3: number; export let lastSecondNotRound = false; -export let lastResult: SharedTypes.Result<SharedTypes.Config.Mode>; +export let lastResult: Result<Mode>; -export function setLastResult( - result: SharedTypes.Result<SharedTypes.Config.Mode> -): void { +export function setLastResult(result: Result<Mode>): void { lastResult = result; } @@ -108,7 +108,7 @@ export function restart(): void { export let restartCount = 0; export let incompleteSeconds = 0; -export let incompleteTests: SharedTypes.IncompleteTest[] = []; +export let incompleteTests: IncompleteTest[] = []; export function incrementRestartCount(): void { restartCount++; diff --git a/frontend/src/ts/test/test-ui.ts b/frontend/src/ts/test/test-ui.ts index f60465a09..186b10df6 100644 --- a/frontend/src/ts/test/test-ui.ts +++ b/frontend/src/ts/test/test-ui.ts @@ -28,6 +28,7 @@ import * as ActivePage from "../states/active-page"; import Format from "../utils/format"; import * as Loader from "../elements/loader"; import { getHtmlByUserFlags } from "../controllers/user-flag-controller"; +import { TimerColor, TimerOpacity } from "@monkeytype/shared-types/config"; async function gethtml2canvas(): Promise<typeof import("html2canvas").default> { return (await import("html2canvas")).default; @@ -1448,14 +1449,14 @@ function updateWordsWidth(): void { } } -function updateLiveStatsOpacity(value: SharedTypes.Config.TimerOpacity): void { +function updateLiveStatsOpacity(value: TimerOpacity): void { $("#barTimerProgress").css("opacity", parseFloat(value as string)); $("#liveStatsTextTop").css("opacity", parseFloat(value as string)); $("#liveStatsTextBottom").css("opacity", parseFloat(value as string)); $("#liveStatsMini").css("opacity", parseFloat(value as string)); } -function updateLiveStatsColor(value: SharedTypes.Config.TimerColor): void { +function updateLiveStatsColor(value: TimerColor): void { $("#barTimerProgress").removeClass("timerSub"); $("#barTimerProgress").removeClass("timerText"); $("#barTimerProgress").removeClass("timerMain"); @@ -1615,9 +1616,9 @@ ConfigEvent.subscribe((key, value) => { updateWordsWidth(); } if (key === "timerOpacity") { - updateLiveStatsOpacity(value as SharedTypes.Config.TimerOpacity); + updateLiveStatsOpacity(value as TimerOpacity); } if (key === "timerColor") { - updateLiveStatsColor(value as SharedTypes.Config.TimerColor); + updateLiveStatsColor(value as TimerColor); } }); diff --git a/frontend/src/ts/utils/config.ts b/frontend/src/ts/utils/config.ts index 169e04def..60df40e55 100644 --- a/frontend/src/ts/utils/config.ts +++ b/frontend/src/ts/utils/config.ts @@ -1,13 +1,11 @@ +import { Config, ConfigValue } from "@monkeytype/shared-types/config"; import DefaultConfig from "../constants/default-config"; import { typedKeys } from "./misc"; -export function mergeWithDefaultConfig( - config: Partial<SharedTypes.Config> -): SharedTypes.Config { - const mergedConfig = {} as SharedTypes.Config; +export function mergeWithDefaultConfig(config: Partial<Config>): Config { + const mergedConfig = {} as Config; for (const key of typedKeys(DefaultConfig)) { - const newValue = - config[key] ?? (DefaultConfig[key] as SharedTypes.ConfigValue); + const newValue = config[key] ?? (DefaultConfig[key] as ConfigValue); //@ts-expect-error cant be bothered to deal with this mergedConfig[key] = newValue; } diff --git a/frontend/src/ts/utils/format.ts b/frontend/src/ts/utils/format.ts index 02e520585..1d6bd4ca1 100644 --- a/frontend/src/ts/utils/format.ts +++ b/frontend/src/ts/utils/format.ts @@ -1,6 +1,7 @@ -import Config from "../config"; import { get as getTypingSpeedUnit } from "../utils/typing-speed-units"; import * as Numbers from "../utils/numbers"; +import { Config as ConfigType } from "@monkeytype/shared-types/config"; +import Config from "../config"; export type FormatOptions = { showDecimalPlaces?: boolean; @@ -20,7 +21,7 @@ export type FallbackOptions = { }; export class Formatting { - constructor(private config: SharedTypes.Config) {} + constructor(private config: ConfigType) {} typingSpeed( wpm: number | null | undefined, diff --git a/frontend/src/ts/utils/misc.ts b/frontend/src/ts/utils/misc.ts index e81a25985..9106883a0 100644 --- a/frontend/src/ts/utils/misc.ts +++ b/frontend/src/ts/utils/misc.ts @@ -2,6 +2,9 @@ import * as Loader from "../elements/loader"; import { envConfig } from "../constants/env-config"; import { lastElementFromArray } from "./arrays"; import * as JSONData from "./json-data"; +import { CustomTextData, Result } from "@monkeytype/shared-types"; +import { PersonalBests } from "@monkeytype/shared-types/user"; +import { Config, Mode, Mode2 } from "@monkeytype/shared-types/config"; export function kogasa(cov: number): number { return ( @@ -217,7 +220,7 @@ export function canQuickRestart( mode: string, words: number, time: number, - CustomText: SharedTypes.CustomTextData, + CustomText: CustomTextData, customTextIsLong: boolean ): boolean { const wordsLong = mode === "words" && (words >= 1000 || words === 0); @@ -375,10 +378,10 @@ export async function swapElements( return; } -export function getMode2<M extends keyof SharedTypes.PersonalBests>( - config: SharedTypes.Config, +export function getMode2<M extends keyof PersonalBests>( + config: Config, randomQuote: MonkeyTypes.Quote | null -): SharedTypes.Config.Mode2<M> { +): Mode2<M> { const mode = config.mode; let retVal: string; @@ -396,12 +399,10 @@ export function getMode2<M extends keyof SharedTypes.PersonalBests>( throw new Error("Invalid mode"); } - return retVal as SharedTypes.Config.Mode2<M>; + return retVal as Mode2<M>; } -export async function downloadResultsCSV( - array: SharedTypes.Result<SharedTypes.Config.Mode>[] -): Promise<void> { +export async function downloadResultsCSV(array: Result<Mode>[]): Promise<void> { Loader.show(); const csvString = [ [ @@ -430,7 +431,7 @@ export async function downloadResultsCSV( "tags", "timestamp", ], - ...array.map((item: SharedTypes.Result<SharedTypes.Config.Mode>) => [ + ...array.map((item: Result<Mode>) => [ item._id, item.isPb, item.wpm, diff --git a/frontend/src/ts/utils/typing-speed-units.ts b/frontend/src/ts/utils/typing-speed-units.ts index c31b97f7c..ce532bf5f 100644 --- a/frontend/src/ts/utils/typing-speed-units.ts +++ b/frontend/src/ts/utils/typing-speed-units.ts @@ -1,12 +1,14 @@ +import { TypingSpeedUnit } from "@monkeytype/shared-types/config"; + class Unit implements MonkeyTypes.TypingSpeedUnitSettings { - unit: SharedTypes.Config.TypingSpeedUnit; + unit: TypingSpeedUnit; convertFactor: number; fullUnitString: string; histogramDataBucketSize: number; historyStepSize: number; constructor( - unit: SharedTypes.Config.TypingSpeedUnit, + unit: TypingSpeedUnit, convertFactor: number, fullUnitString: string, histogramDataBucketSize: number, @@ -28,7 +30,7 @@ class Unit implements MonkeyTypes.TypingSpeedUnitSettings { } } -const typingSpeedUnits: Record<SharedTypes.Config.TypingSpeedUnit, Unit> = { +const typingSpeedUnits: Record<TypingSpeedUnit, Unit> = { wpm: new Unit("wpm", 1, "Words per Minute", 10, 10), cpm: new Unit("cpm", 5, "Characters per Minute", 50, 100), wps: new Unit("wps", 1 / 60, "Words per Second", 0.5, 2), @@ -37,7 +39,7 @@ const typingSpeedUnits: Record<SharedTypes.Config.TypingSpeedUnit, Unit> = { }; export function get( - unit: SharedTypes.Config.TypingSpeedUnit + unit: TypingSpeedUnit ): MonkeyTypes.TypingSpeedUnitSettings { return typingSpeedUnits[unit]; } diff --git a/frontend/src/ts/utils/url-handler.ts b/frontend/src/ts/utils/url-handler.ts index 87de976d8..708899e87 100644 --- a/frontend/src/ts/utils/url-handler.ts +++ b/frontend/src/ts/utils/url-handler.ts @@ -12,6 +12,8 @@ import * as Loader from "../elements/loader"; import * as AccountButton from "../elements/account-button"; import { restart as restartTest } from "../test/test-logic"; import * as ChallengeController from "../controllers/challenge-controller"; +import { Difficulty, Mode, Mode2 } from "@monkeytype/shared-types/config"; +import { CustomTextData } from "@monkeytype/shared-types"; export async function linkDiscord(hashOverride: string): Promise<void> { if (!hashOverride) return; @@ -109,13 +111,13 @@ export function loadCustomThemeFromUrl(getOverride?: string): void { } type SharedTestSettings = [ - SharedTypes.Config.Mode | null, - SharedTypes.Config.Mode2<SharedTypes.Config.Mode> | null, - SharedTypes.CustomTextData | null, + Mode | null, + Mode2<Mode> | null, + CustomTextData | null, boolean | null, boolean | null, string | null, - SharedTypes.Config.Difficulty | null, + Difficulty | null, string | null ]; diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json index d4de7464f..a74452381 100644 --- a/frontend/tsconfig.json +++ b/frontend/tsconfig.json @@ -9,6 +9,10 @@ "target": "ES6", "noEmit": true }, - "include": ["./src/**/*.ts", "./scripts/**/*.ts"], + "include": [ + "./src/**/*.ts", + "./scripts/**/*.ts", + "./src/ts/ape/types/*.d.ts" + ], "exclude": ["node_modules", "build", "setup-tests.ts", "**/*.spec.ts"] } |