diff --git a/components/sidebar.html b/components/sidebar.html index c61233aa..d4788085 100644 --- a/components/sidebar.html +++ b/components/sidebar.html @@ -159,41 +159,6 @@ data-state="collapsed" > - - - About - - - - Guidelines - - - - Getting Started - Docs - - - Upload Content - - - - Roadmap - Beta - - - Links - { if (logger && typeof logger === "object") { return { @@ -32,6 +41,7 @@ export function createHashChangeHandler({ } = {}) { const devLog = createLoggerFacade(devLogger, noopLogger); const userLog = createLoggerFacade(userLogger, noopLogger); + let currentViewName = null; const getReadyPromise = () => { if (typeof getApplicationReady === "function") { @@ -82,7 +92,14 @@ export function createHashChangeHandler({ const defaultViewName = resolveIsLoggedIn() ? "for-you" : "most-recent-videos"; + + if (defaultViewName === currentViewName) { + return; + } + await loadView(`views/${defaultViewName}.html`); + currentViewName = defaultViewName; + const initFn = viewInitRegistry?.[defaultViewName]; if (typeof initFn === "function") { await initFn(); @@ -91,11 +108,24 @@ export function createHashChangeHandler({ } const viewName = match[1]; + + if (Object.prototype.hasOwnProperty.call(LEGACY_DOCS_VIEWS, viewName)) { + const docSlug = LEGACY_DOCS_VIEWS[viewName]; + window.location.hash = `#view=docs&doc=${docSlug}`; + return; + } + + if (viewName === currentViewName) { + return; + } + if (typeof viewName === "string" && viewName.toLowerCase() === "history") { } const viewUrl = `views/${viewName}.html`; await loadView(viewUrl); + currentViewName = viewName; + const initFn = viewInitRegistry?.[viewName]; if (typeof initFn === "function") { await initFn(); diff --git a/tests/unit/hashChangeHandler.test.mjs b/tests/unit/hashChangeHandler.test.mjs new file mode 100644 index 00000000..277d2ad8 --- /dev/null +++ b/tests/unit/hashChangeHandler.test.mjs @@ -0,0 +1,64 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { createHashChangeHandler } from '../../js/hashChangeHandler.js'; + +test('createHashChangeHandler', async (t) => { + // Mock window + const mockLocation = { hash: '' }; + global.window = { + location: mockLocation, + history: { + replaceState: () => {} + } + }; + + // Mock dependencies + let loadedView = null; + const mockLoadView = async (url) => { + loadedView = url; + }; + + const mockViewInitRegistry = { + docs: async () => {}, + "for-you": async () => {} + }; + + const handler = createHashChangeHandler({ + getApplication: () => ({ isUserLoggedIn: () => false }), + getApplicationReady: () => Promise.resolve(), + loadView: mockLoadView, + viewInitRegistry: mockViewInitRegistry, + userLogger: { error: () => {}, warn: () => {} }, + devLogger: { log: () => {} } + }); + + await t.test('loads default view if hash is empty', async () => { + loadedView = null; + mockLocation.hash = ''; + await handler(); + // Default is most-recent-videos if not logged in + assert.equal(loadedView, 'views/most-recent-videos.html'); + }); + + await t.test('redirects legacy view', async () => { + loadedView = null; + mockLocation.hash = '#view=about'; + await handler(); + // It should update hash and return + assert.equal(mockLocation.hash, '#view=docs&doc=about'); + assert.equal(loadedView, null); + }); + + await t.test('skips redundant reload', async () => { + // First load docs + loadedView = null; + mockLocation.hash = '#view=docs'; + await handler(); + assert.equal(loadedView, 'views/docs.html'); + + // Call again with same hash + loadedView = null; + await handler(); + assert.equal(loadedView, null, 'Should not reload view if same'); + }); +}); diff --git a/views/about.html b/views/about.html deleted file mode 100644 index 65f0bc7b..00000000 --- a/views/about.html +++ /dev/null @@ -1,74 +0,0 @@ - - - - - - About bitvid - - - Loading content... - - - - - - diff --git a/views/community-guidelines.html b/views/community-guidelines.html deleted file mode 100644 index 8b1ff7d3..00000000 --- a/views/community-guidelines.html +++ /dev/null @@ -1,78 +0,0 @@ - - - - - - Community Guidelines - - - Loading content... - - - - - - diff --git a/views/getting-started.html b/views/getting-started.html deleted file mode 100644 index d46edd52..00000000 --- a/views/getting-started.html +++ /dev/null @@ -1,79 +0,0 @@ - - - - - - Getting Started - - - Loading content... - - - - - - diff --git a/views/links.html b/views/links.html deleted file mode 100644 index d72ac7c4..00000000 --- a/views/links.html +++ /dev/null @@ -1,74 +0,0 @@ - - - - - - bitvid Links - - - Loading content... - - - - - - diff --git a/views/roadmap.html b/views/roadmap.html deleted file mode 100644 index 3414fb81..00000000 --- a/views/roadmap.html +++ /dev/null @@ -1,75 +0,0 @@ - - - - - - Roadmap - - - Loading content... - - - - - - diff --git a/views/upload-content.html b/views/upload-content.html deleted file mode 100644 index e4246b8a..00000000 --- a/views/upload-content.html +++ /dev/null @@ -1,77 +0,0 @@ - - - - - - - - Upload Content - - - - Loading content... - - - - - -
Loading content...