Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the permalink system to determine version redirect based on date rather than version number #109

Open
wants to merge 7 commits into
base: theseus
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/latest/data/releaseDates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
48 changes: 46 additions & 2 deletions src/latest/scripts/interpreter/permalink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import compatRaw from "../../data/compat.json?raw";
import datesRaw from "../../data/releaseDates.json?raw";
import { gunzipString, gzipString } from "gzip-utils";

const compat = JSON.parse(compatRaw);
const releaseDates = JSON.parse(datesRaw);
const LATEST_VYXAL_VERSION_CONSTANT_RETURNED_FROM_DETERMINE_VERSION = "latest";

type OldPermalinks = {
format: 2,
Expand All @@ -24,7 +27,45 @@
version: string,
};

function decodeVersion(version: string): string {

// This is what happens when you let lyxal write a hack
// solution to something that probably required a bigger
// refactoring. (Comment written by, surprisingly, lyxal)

// Version will either be `x.y.z` or a unix timestamp. So
// determine which one it is. If `x.y.z`, simply return
// it.

if (version.includes(".")) {
return version;
}

// It's a timestamp, which means fun because that's the whole
// point of this function. The version can be determined
// by finding the first release on a date after the given
// date.

// But first, convert the string to a date object.
// Multiply the "version" by 1000 to convert it to
// milliseconds.
const timestamp = Number.parseInt(version);

// Now, iterate through the release dates and find the
// first release after the given date.

const parsedDates: [string, number][] = Object.entries(releaseDates).map(([v, d]) => [v, Number.parseInt(d as string)]);
const candidates = parsedDates.filter(([_, d]) => d > timestamp);
if (candidates.length === 0) {
return LATEST_VYXAL_VERSION_CONSTANT_RETURNED_FROM_DETERMINE_VERSION;
}
return candidates[0][0];
}

function incompatible(permalinkVersion: string) {
if (permalinkVersion === LATEST_VYXAL_VERSION_CONSTANT_RETURNED_FROM_DETERMINE_VERSION) {
return false;
}
return compat[permalinkVersion] ?? false;
}

Expand Down Expand Up @@ -84,8 +125,11 @@
}
}
try {
if (incompatible(permalink.version)) {
return { compatible: false, version: permalink.version };
const realVersion = decodeVersion(permalink.version);
console.log("Real version:", realVersion);
console.log("Permalink version:", permalink.version);
if (incompatible(realVersion)) {
return { compatible: false, version: realVersion };
}
switch (permalink.format) {
case 2: {
Expand Down
1 change: 1 addition & 0 deletions src/latest/scripts/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fallback.hidden = true;
const permalink = window.location.hash.length ? await decodeHash(window.location.hash.slice(1)) : null;
if (permalink != null && !permalink.compatible) {
window.location.replace(`https://vyxal.github.io/versions/v${permalink.version}#${location.hash.substring(1)}`);

} else {
// @ts-expect-error DATA_URI gets replaced by Webpack
const elementData = parseElementData(await fetch(`${DATA_URI}/theseus.json`).then((r) => r.json()));
Expand Down
11 changes: 6 additions & 5 deletions src/latest/scripts/ui/Theseus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@

const [state, setState] = useState<RunState>({ name: autorun ? "starting" : "idle" });
const [lastFocusedEditor, setLastFocusedEditor] = useState<ReactCodeMirrorRef | null>(null);

const runnerRef = useRef<VyTerminalRef | null>(null);
const snowflakesRef = useRef<Snowflakes | null>(null);

useEffect(() => {
switch (settingsState.theme) {
case Theme.Dark:
Expand Down Expand Up @@ -109,13 +109,14 @@
}, [settingsState]);

useEffect(() => {
const date = new Date();
encodeHash({
header,
code,
footer,
flags: [...serializeFlags(elementData.flagDefs, flags)],
inputs: inputs.map(({ name, inputs }) => [name, inputs.map(({ input }) => input)]),
version: elementData.version,
version: `${Date.now()}`,
}).then((hash) => history.replaceState(undefined, "", "#" + hash));
}, [header, code, footer, flags, inputs, elementData]);

Expand All @@ -134,7 +135,7 @@
utilWorker.formatBytecount(code, literate).then(setBytecount);
}, [code, flags, utilWorker, literate]);

const literateToSbcs = useCallback(async() => {
const literateToSbcs = useCallback(async () => {
runnerRef.current?.showMessage(`\x1b[1mSBCS translation:\x1b[0m\n${await utilWorker.sbcsify(code)}`);
}, [code, runnerRef, utilWorker]);

Expand Down Expand Up @@ -174,7 +175,7 @@
if (view != null) {
view.dispatch(view.state.replaceSelection(char));
}
}}
}}
/>
<div className="w-100 h-100 vstack">
<Header
Expand Down
Loading