From 6eb4561e02f4959cd0f4deea24be1fae2379d0a6 Mon Sep 17 00:00:00 2001 From: riccardoperra Date: Mon, 11 Dec 2023 21:19:46 +0100 Subject: [PATCH] version store changelog new file checker --- .../components/Changelog/resolveChangelog.ts | 16 ++++---- .../src/state/version/version.store.ts | 37 +++++++++++++------ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/apps/codeimage/src/components/Changelog/resolveChangelog.ts b/apps/codeimage/src/components/Changelog/resolveChangelog.ts index a3d2f0536..95e67d97e 100644 --- a/apps/codeimage/src/components/Changelog/resolveChangelog.ts +++ b/apps/codeimage/src/components/Changelog/resolveChangelog.ts @@ -5,18 +5,20 @@ interface ChangelogEntry { path: string; component: () => Promise<{default: Component}>; } + +export function parseChangelogFilePath(path: string) { + const [version, date] = (path.split('/').at(-1) as string) + .replace('.mdx', '') + .split('_'); + return {version: version.replaceAll('-', '.'), date}; +} export function resolveChangelog(props: ChangelogEntry) { const Component = (mdxProps: MDXProps) => createComponent(lazy(props.component), mdxProps); - const data = () => { - const [version, date] = (props.path.split('/').at(-1) as string) - .replace('.mdx', '') - .split('_'); - return {version, date}; - }; + const data = () => parseChangelogFilePath(props.path); - const version = () => data().version.replaceAll('-', '.'); + const version = () => data().version; const date = () => { const [month, day, year] = data() diff --git a/apps/codeimage/src/state/version/version.store.ts b/apps/codeimage/src/state/version/version.store.ts index 17ea13723..7c9442656 100644 --- a/apps/codeimage/src/state/version/version.store.ts +++ b/apps/codeimage/src/state/version/version.store.ts @@ -4,7 +4,6 @@ import {getUmami} from '@core/constants/umami'; import {createControlledDialog} from '@core/hooks/createControlledDialog'; import { Accessor, - batch, createEffect, createSignal, getOwner, @@ -16,6 +15,7 @@ import { import {unwrap} from 'solid-js/store'; import {defineStore} from 'statebuilder'; import {Changelog} from '../../components/Changelog/Changelog'; +import {parseChangelogFilePath} from '../../components/Changelog/resolveChangelog'; interface Feature { name: string; @@ -39,6 +39,10 @@ interface VersionStore { export type FeatureName = 'fontPicker' | 'windowStylePicker'; +const ChangelogFiles = Object.keys( + import.meta.glob('../../../changelog/*.mdx'), +).map(parseChangelogFilePath); + function initialValue(): VersionStore { return { appVersion: appEnvironment.version, @@ -62,9 +66,13 @@ export const VersionStore = defineStore(initialValue) const [ready, setReady] = createSignal(false); const controlledDialog = createControlledDialog(); - function seeLatestVersion() { - const currentVersion = appEnvironment.version; - _.set('seen', seen => [...new Set([...seen, currentVersion])]); + function updateWithLatestVersionSeen( + currentVersion: string, + data: VersionStore, + ): VersionStore { + const versionSeen = new Set([...data.seen, currentVersion]); + data.seen = [...versionSeen]; + return data; } onMount(() => { @@ -89,15 +97,22 @@ export const VersionStore = defineStore(initialValue) }); } if (isFirstTime || hasNewUpdate) { - controlledDialog(Changelog, {latest: false}); - batch(() => { - seeLatestVersion(); + const fileVersions = ChangelogFiles.map(({version}) => version); + const hasNewVersionToSee = fileVersions.includes(data.appVersion); + console.log(isFirstTime, hasNewUpdate, hasNewVersionToSee); + + if (isFirstTime || hasNewVersionToSee) { + controlledDialog(Changelog, {latest: true}); + const updatedData = updateWithLatestVersionSeen( + currentVersion, + data, + ); + _.set(() => updatedData); + } else { _.set(() => data); - }); + } } else { - const versionSeen = new Set([...data.seen, currentVersion]); - data.seen = [...versionSeen]; - _.set(() => data); + _.set(() => updateWithLatestVersionSeen(currentVersion, data)); } setReady(true); }