Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
54 changes: 47 additions & 7 deletions src/pages/ai-atlas.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GetStaticProps } from 'next';
import { useRouter } from 'next/router';
import React, {
useEffect,
useLayoutEffect,
Expand Down Expand Up @@ -868,6 +869,14 @@ type ViewMode = 'environment' | 'security';
/* The header tabs: the map, and the two readings that sit beside it. */
type Tab = 'atlas' | 'article' | 'ladder';
const READING_TABS: Tab[] = ['article', 'ladder'];
/* Each reading has its own address on keepsimple.io: /ai-atlas/story and
/ai-atlas/ladder. The map stays at /ai-atlas. */
export const TAB_SLUGS: Record<string, string> = {
article: 'story',
ladder: 'ladder',
};
export const tabPath = (tab: Tab) =>
tab === 'atlas' ? '/ai-atlas' : '/ai-atlas/' + TAB_SLUGS[tab];
const readings = readingsData as Readings;

function DoctrinePanel({ t }: { t: T }) {
Expand Down Expand Up @@ -1262,6 +1271,11 @@ export function AiAtlasApp({
initialGuide = null,
initialView = 'environment',
initialFocus = null,
initialTab = 'atlas',
/* On keepsimple.io a tab is its own page and this navigates to it. Without
it (the Terminal's frame) the tabs switch in place and the hash holds
the tab. */
onTabChange = null,
}: any = {}) {
const [data] = useState<any>(() =>
initialGuide ? adaptGuide(initialGuide) : null,
Expand All @@ -1272,7 +1286,7 @@ export function AiAtlasApp({
const [hoverNode, setHoverNode] = useState<string | null>(null);
const [linkHoverNode, setLinkHoverNode] = useState<string | null>(null);
const [viewMode, setViewMode] = useState<ViewMode>(initialView);
const [tab, setTab] = useState<Tab>('atlas');
const [tab, setTab] = useState<Tab>(initialTab);
const tabPending = useRef(false);
const changeTab = (next: Tab) => {
if (next === tab || tabPending.current) return;
Expand All @@ -1281,6 +1295,10 @@ export function AiAtlasApp({
stage?.classList.add('is-leaving');
setTimeout(
() => {
if (onTabChange) {
onTabChange(next);
return;
}
setTab(next);
setFocusedNode(null);
stage?.classList.remove('is-leaving');
Expand Down Expand Up @@ -1321,10 +1339,16 @@ export function AiAtlasApp({
const sync = () => {
const hash = window.location.hash.replace(/^#/, '').toLowerCase();
if ((READING_TABS as string[]).includes(hash)) {
setTab(hash as Tab);
setFocusedNode(null);
/* An old #article / #ladder link opens the reading's own page. */
if (onTabChange) onTabChange(hash as Tab, true);
else {
setTab(hash as Tab);
setFocusedNode(null);
}
return;
}
/* A reading page has no map state to restore from the hash. */
if (onTabChange && initialTab !== 'atlas') return;
setTab('atlas');
if (hash === 'security') {
setViewMode('security');
Expand All @@ -1350,13 +1374,15 @@ export function AiAtlasApp({
useEffect(() => {
if (typeof window === 'undefined') return;
let desired = '';
if (tab !== 'atlas') desired = '#' + tab;
if (tab !== 'atlas') desired = onTabChange ? '' : '#' + tab;
else if (viewMode === 'security') desired = '#security';
else if (focusedNode) desired = '#' + focusedNode;
const current = window.location.hash;
if (current !== desired) {
const url = window.location.pathname + window.location.search + desired;
window.history.replaceState(null, '', url);
/* Keep the router's own entry state, so Back between the tab pages
still navigates. */
window.history.replaceState(window.history.state, '', url);
}
}, [tab, viewMode, focusedNode]);

Expand Down Expand Up @@ -2182,7 +2208,16 @@ export function AiAtlasApp({
);
}

export default function AiAtlasPage({ guide }: { guide: any }) {
export default function AiAtlasPage({
guide,
tab = 'atlas',
}: {
guide: any;
tab?: Tab;
}) {
const router = useRouter();
const goTo = (next: Tab, replace?: boolean) =>
replace ? router.replace(tabPath(next)) : router.push(tabPath(next));
/* Words pushed with the guide replace the built-in ones, SEO included. */
const words = { ...copy, ...PAGE_TEXT_DEFAULTS, ...(guide?.copy || {}) };
return (
Expand Down Expand Up @@ -2212,7 +2247,12 @@ export default function AiAtlasPage({ guide }: { guide: any }) {
}}
/>
<div className="ai-atlas-root">
<AiAtlasApp initialGuide={guide} />
<AiAtlasApp
key={tab}
initialGuide={guide}
initialTab={tab}
onTabChange={goTo}
/>
</div>
</>
);
Expand Down
31 changes: 31 additions & 0 deletions src/pages/ai-atlas/[tab].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { GetStaticPaths, GetStaticProps } from 'next';

import AiAtlasPage, {
getStaticProps as atlasStaticProps,
TAB_SLUGS,
} from '../ai-atlas';

/* /ai-atlas/story and /ai-atlas/ladder: the Atlas page opened on one of its
readings. Same guide, same words, its own address. */
const TAB_BY_SLUG: Record<string, string> = Object.fromEntries(
Object.entries(TAB_SLUGS).map(([tab, slug]) => [slug, tab]),
);

export const getStaticPaths: GetStaticPaths = async ({ locales = [] }) => ({
paths: Object.values(TAB_SLUGS).flatMap(tab =>
(locales.length ? locales : [undefined]).map(locale => ({
params: { tab },
locale,
})),
),
fallback: false,
});

export const getStaticProps: GetStaticProps = async context => {
const tab = TAB_BY_SLUG[String(context.params?.tab)];
if (!tab) return { notFound: true };
const atlas: any = await atlasStaticProps(context);
return { ...atlas, props: { ...atlas.props, tab } };
};

export default AiAtlasPage;
8 changes: 7 additions & 1 deletion src/pages/api/ai-atlas/guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ import { stripGuide } from '@lib/aiAtlas/stripGuide';

export const config = { api: { bodyParser: { sizeLimit: '1mb' } } };

const PAGES = ['/ai-atlas', '/ru/ai-atlas', '/hy/ai-atlas'];
/* The map and its two readings (/ai-atlas/story, /ai-atlas/ladder), in
every locale: they share the pushed words. */
const PAGES = ['', '/ru', '/hy'].flatMap(prefix =>
['/ai-atlas', '/ai-atlas/story', '/ai-atlas/ladder'].map(
path => prefix + path,
),
);

const digest = (value: string) => createHash('sha256').update(value).digest();

Expand Down
Loading