Skip to content

Commit

Permalink
Fix URL not updating on back/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Dec 28, 2018
1 parent 7519ab0 commit 7f30f30
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.referencesCodeLens.enabled": true,
// "editor.formatOnSave": true,
"cSpell.words": ["Quantizer", "readwrite"]
"cSpell.words": [
"Quantizer",
"firstload",
"readwrite"
]
}
24 changes: 12 additions & 12 deletions src/db-worker/handle-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function handleMessage(
payload: {
entry: processEntry(entries[0]),
firstLoad: false,
updateHash: true,
},
});
postMessage({
Expand All @@ -68,20 +69,18 @@ export async function handleMessage(
);
return;
case 'OPEN':
let entry: PaletteEntry | null = null;
if (!Number.isNaN(action.payload.timestamp)) {
const entry = await loadItemFromDB(
action.payload.timestamp,
);
if (entry != null) {
postMessage({
type: 'DISPLAY',
payload: {
entry,
firstLoad: action.payload.firstLoad,
},
});
}
entry = await loadItemFromDB(action.payload.timestamp);
}
postMessage({
type: 'DISPLAY',
payload: {
entry,
firstLoad: action.payload.firstLoad,
updateHash: false,
},
});
return;
case 'DELETE':
if (!Number.isNaN(action.payload.timestamp)) {
Expand All @@ -97,6 +96,7 @@ export async function handleMessage(
payload: {
entry: otherEntry,
firstLoad: false,
updateHash: true,
},
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/page/__tests__/handle-messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('handleMessage', () => {
},
},
firstLoad: false,
updateHash: false,
},
});
expect(displayMainPalette).toBeCalledWith({
Expand All @@ -87,6 +88,7 @@ describe('handleMessage', () => {
},
},
firstLoad: false,
updateHash: false,
});
});
});
2 changes: 2 additions & 0 deletions src/page/handle-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface DisplayAction {
payload: {
entry: PaletteEntry | null;
firstLoad: boolean;
updateHash: boolean;
};
}

Expand All @@ -40,6 +41,7 @@ export function handleMessage(action: UiAction) {
displayMainPalette({
data: action.payload.entry,
firstLoad: action.payload.firstLoad,
updateHash: action.payload.updateHash,
});
return;
case 'ERROR':
Expand Down
18 changes: 17 additions & 1 deletion src/page/main-palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ import { renderImage } from './render-image';
import { renderPalette } from './render-palette';

interface DisplayMainPaletteProps {
/** Data to display on the main palette */
data: PaletteEntry | null;
/**
* True if this image was loaded when the document loaded.
* Changes back button behavior so that it doesn't navigate away
* from the page accidentally.
*/
firstLoad: boolean;
/**
* True if the page URL should be updated with this image's ID.
* Unnecessary if a link was clicked to open this image.
*/
updateHash: boolean;
}

const MAIN_PALETTE_ELEMENT = document.getElementById('palette')!;
Expand Down Expand Up @@ -56,7 +67,12 @@ export function displayMainPalette(props: DisplayMainPaletteProps) {
listener = render;

MAIN_PALETTE_ELEMENT.classList.add('is-open'); // Open on mobile
document.title = `${props.data.name} | ${TITLE}`;
const title = `${props.data.name} | ${TITLE}`;
document.title = title;

if (props.updateHash) {
history.replaceState(true, title, `#${props.data.timestamp}`);
}
} else {
renderPalette(
{
Expand Down

0 comments on commit 7f30f30

Please sign in to comment.