Add option for query id routing (#31)

This commit is contained in:
Ahmed Waleed
2025-06-29 12:47:24 +03:00
committed by GitHub
parent 7dc3edb5fd
commit 335c418c54
5 changed files with 64 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ import Editor from './components/Editor';
import usePreference from './hooks/usePreference.ts';
import themes, { Themes } from './style/themes.ts';
import { loadFromBytebin } from './util/storage';
import { useQueryRouting } from './util/constants';
const INITIAL = Symbol();
const LOADING = Symbol();
@@ -81,10 +82,10 @@ function get404Message(pasteId: string) {
}
function getPasteIdFromUrl() {
const path = window.location.pathname;
if (path && /^\/[a-zA-Z0-9]+$/.test(path)) {
return path.substring(1);
} else {
return undefined;
if (useQueryRouting) {
return new URLSearchParams(window.location.search).get('id') ?? undefined;
}
const path = window.location.pathname;
return /^\/[a-zA-Z0-9]+$/.test(path) ? path.substring(1) : undefined;
}

View File

@@ -6,6 +6,7 @@ import styled from 'styled-components';
import themes, { Themes } from '../style/themes';
import { languages } from '../util/highlighting';
import { saveToBytebin } from '../util/storage';
import { useQueryRouting } from '../util/constants';
import Button from './Button';
import { ResetFunction } from './Editor';
import MenuButton from './MenuButton';
@@ -59,9 +60,15 @@ export default function EditorControls({
setSaving(false);
setRecentlySaved(true);
if (pasteId) {
history.replace({
pathname: pasteId,
});
if (useQueryRouting) {
history.replace({
search: `?id=${pasteId}`,
});
} else {
history.replace({
pathname: pasteId,
});
}
copy(window.location.href);
document.title = 'paste | ' + pasteId;
}

View File

@@ -1,3 +1,5 @@
export const bytebinUrl =
import.meta.env.VITE_BYTEBIN_URL || 'https://bytebin.lucko.me/';
export const postUrl = bytebinUrl + 'post';
export const useQueryRouting =
import.meta.env.VITE_USE_QUERY_ROUTING === 'true';

1
src/vite-env.d.ts vendored
View File

@@ -2,6 +2,7 @@
interface ImportMetaEnv {
readonly VITE_BYTEBIN_URL?: string;
readonly VITE_USE_QUERY_ROUTING?: string;
}
interface ImportMeta {