Skip to content

Commit 17d74dc

Browse files
timngynhbuchel
andauthored
Update how we display page last updated (aws-amplify#6110)
* Rename script to add last updated dates * Update to use imports * Add when page was last updated to left menu * Run script to flatten directory during prebuild command; add null check for directory data * Update amplify.yml to not run script to get last updated dates * Remove old LastUpdatedProvider * Remove old custom frontmatter remark plugin since we're no longer using it * Update src/styles/page-last-updated.scss Co-authored-by: Heather Buchel <[email protected]> * Add back layout changes (mistake from rebase) * Add page prop to show/hide page last updated date * Hide page last updated on home and overview pages --------- Co-authored-by: Heather Buchel <[email protected]>
1 parent 0766254 commit 17d74dc

20 files changed

+124
-191
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ amplify/backend/function/submissionsLambda/src/controllers
7070
public/**/**/nextImageExportOptimizer/
7171
public/next-image-export-optimizer-hashes.json
7272
src/directory/directory.json
73+
src/directory/flatDirectory.json
7374

7475
#amplify-do-not-edit-begin
7576
amplify/\#current-cloud-backend

amplify.yml

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ frontend:
44
preBuild:
55
commands:
66
- yarn install
7-
- node tasks/addLastUpdatedToMdxFiles.js
87
build:
98
commands:
109
- echo "BUILD_ENV=$BUILD_ENV" >> .env.custom

next.config.mjs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export default async () => {
1010
const withMDX = createMDX({
1111
extension: /\.mdx$/,
1212
options: {
13-
// TODO: Fix this frontmatter plugin
14-
// remarkPlugins: [frontmatterPlugin],
1513
rehypePlugins: [[rehypeImgSize, { dir: 'public' }]]
1614
}
1715
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,6 @@
153153
"next-start": "next start",
154154
"prepare": "husky install",
155155
"analyze": "ANALYZE=true yarn next-build",
156-
"prebuild": "node src/directory/generateDirectory.mjs"
156+
"prebuild": "node src/directory/generateDirectory.mjs && node src/directory/generateFlatDirectory.mjs"
157157
}
158158
}

src/components/Fragments/index.tsx

-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
import { Fragment } from 'react';
22
import FilterChildren from '../FilterChildren';
3-
import { useLastUpdatedDatesContext } from '../LastUpdatedProvider';
43

54
type MdxFrontmatterType = {
65
lastUpdated: string;
76
};
87

98
export default function Fragments({ fragments }) {
109
const children: React.ReactNode[] = [];
11-
let frontmatter: MdxFrontmatterType;
1210

13-
const { state, dispatch } = useLastUpdatedDatesContext();
1411
let filterKey = '';
1512

1613
for (const key in fragments) {
1714
if (!filterKey) filterKey = key;
1815
const fragment = fragments[key]([]);
19-
frontmatter = fragment.props.frontmatter;
20-
21-
if (frontmatter && frontmatter.lastUpdated) {
22-
if (
23-
state.files[key] === undefined ||
24-
(state.files[key] &&
25-
!state.files[key].includes(frontmatter.lastUpdated))
26-
) {
27-
dispatch({
28-
type: 'update',
29-
key: key,
30-
lastUpdated: frontmatter.lastUpdated
31-
});
32-
}
33-
}
3416

3517
children.push(<Fragment key={key}>{fragment}</Fragment>);
3618
}

src/components/LastUpdatedProvider/index.tsx

-69
This file was deleted.

src/components/Layout/Layout.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import { TableOfContents } from '@/components/TableOfContents';
3131
import type { Heading } from '@/components/TableOfContents/TableOfContents';
3232
import { PlatformNavigator } from '@/components/PlatformNavigator';
3333
import directory from 'src/directory/directory.json';
34+
import flatDirectory from 'src/directory/flatDirectory.json';
3435
import { PageNode } from 'src/directory/directory';
3536
import { Breadcrumbs } from '@/components/Breadcrumbs';
3637
import { debounce } from '@/utils/debounce';
38+
import { PageLastUpdated } from '../PageLastUpdated';
3739

3840
export const Layout = ({
3941
children,
@@ -42,7 +44,8 @@ export const Layout = ({
4244
pageTitle,
4345
pageType = 'inner',
4446
platform,
45-
url
47+
url,
48+
showLastUpdatedDate = true
4649
}: {
4750
children: any;
4851
hasTOC?: boolean;
@@ -51,6 +54,7 @@ export const Layout = ({
5154
pageType?: 'home' | 'inner';
5255
platform?: Platform;
5356
url?: string;
57+
showLastUpdatedDate: boolean;
5458
}) => {
5559
const [menuOpen, toggleMenuOpen] = useState(false);
5660
const [tocHeadings, setTocHeadings] = useState<Heading[]>([]);
@@ -261,6 +265,14 @@ export const Layout = ({
261265
}}
262266
/>
263267
)}
268+
<div className="layout-sidebar-feedback">
269+
[ Feedback widget goes here ]
270+
</div>
271+
{showLastUpdatedDate && (
272+
<PageLastUpdated
273+
directoryData={flatDirectory[router.pathname]}
274+
/>
275+
)}
264276
</div>
265277
</View>
266278
</View>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Text, View } from '@aws-amplify/ui-react';
2+
import { PageNode } from 'src/directory/directory';
3+
4+
type PageLastUpdatedProps = {
5+
directoryData: PageNode;
6+
};
7+
8+
export function PageLastUpdated({ directoryData }: PageLastUpdatedProps) {
9+
if (directoryData) {
10+
const lastUpdated = directoryData['lastUpdated'];
11+
12+
if (!lastUpdated) {
13+
return <></>;
14+
}
15+
16+
const date = toReadableDate(lastUpdated);
17+
18+
return (
19+
<View className="page-last-updated">
20+
<View className="page-last-updated__inner">
21+
<Text fontSize="14px">Page updated {date}</Text>{' '}
22+
</View>
23+
</View>
24+
);
25+
}
26+
27+
return <></>;
28+
}
29+
30+
function toReadableDate(date) {
31+
const dateOptions: Intl.DateTimeFormatOptions = {
32+
year: 'numeric',
33+
month: 'short',
34+
day: 'numeric'
35+
};
36+
37+
return new Date(date).toLocaleDateString('en-US', dateOptions);
38+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { PageLastUpdated } from './PageLastUpdated';

src/directory/directory.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ export type PageNode = {
4040
* Root url for the home page
4141
*/
4242
url?: string;
43+
44+
/**
45+
* String representing when file was last edited
46+
*/
47+
lastUpdated?: string;
4348
};

src/directory/generateDirectory.mjs

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'path';
55
import JSON5 from 'json5';
66
import { directory } from './directory.mjs';
77
import { writeFile } from 'fs/promises';
8+
import { getLastModifiedDate } from 'git-jiggy';
89

910
/**
1011
* Helper function to use RegEx to grab the "meta" object
@@ -51,6 +52,11 @@ async function traverseDirectoryObject(directoryNode) {
5152
directoryNode[key] = metaObj[key];
5253
}
5354

55+
// Get the last updated date
56+
directoryNode['lastUpdated'] = await getLastModifiedDate(
57+
directoryNode.path
58+
);
59+
5460
const relativePath = path.relative(rootPath, directoryNode.path);
5561
const parsedPath = path.parse(relativePath);
5662

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { fileURLToPath } from 'url';
2+
import path from 'path';
3+
import { writeFile } from 'fs/promises';
4+
import directory from './directory.json' assert { type: 'json' };
5+
6+
function flattenJSON(jsonObj, flattened = {}) {
7+
if (Array.isArray(jsonObj.children)) {
8+
jsonObj.children.forEach((child) => flattenJSON(child, flattened));
9+
}
10+
if (jsonObj.route) {
11+
flattened[jsonObj.route] = { ...jsonObj };
12+
// Remove the 'children' property since we don't need it in the "flat" structure
13+
delete flattened[jsonObj.route].children;
14+
}
15+
return flattened;
16+
}
17+
18+
/**
19+
* Generate a "flat" version of directory.json
20+
*/
21+
async function generateFlatDirectory() {
22+
const flatDirectory = flattenJSON(directory);
23+
24+
try {
25+
const __filename = fileURLToPath(import.meta.url);
26+
const __dirname = path.dirname(__filename);
27+
28+
const filePath = path.join(__dirname, 'flatDirectory.json');
29+
30+
const json = JSON.stringify(flatDirectory, null, 2);
31+
32+
await writeFile(filePath, json, 'utf-8');
33+
34+
console.log('Directory object has been written to', filePath);
35+
} catch (err) {
36+
throw new Error(`Error saving to flatDirectory.json: ${err}`);
37+
}
38+
}
39+
40+
console.log('Generating flatDirectory.json...');
41+
generateFlatDirectory();

src/pages/[platform]/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function getStaticProps(context) {
3232
props: {
3333
platform: context.params.platform,
3434
hasTOC: false,
35+
showLastUpdatedDate: false,
3536
pageType: 'home',
3637
meta
3738
}

src/pages/_app.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { useRouter } from 'next/router';
88

99
function MyApp({ Component, pageProps }) {
1010
const router = useRouter();
11-
const { meta, platform, url, hasTOC, pageType } = pageProps;
11+
const { meta, platform, url, hasTOC, pageType, showLastUpdatedDate } =
12+
pageProps;
1213
const getLayout =
1314
Component.getLayout ||
1415
((page) => (
@@ -20,6 +21,7 @@ function MyApp({ Component, pageProps }) {
2021
url={url}
2122
platform={platform ? platform : ''}
2223
hasTOC={hasTOC}
24+
showLastUpdatedDate={showLastUpdatedDate}
2325
>
2426
{page}
2527
</Layout>

src/pages/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function getStaticProps() {
2222
return {
2323
props: {
2424
hasTOC: false,
25+
showLastUpdatedDate: false,
2526
pageType: 'home',
2627
meta
2728
}

0 commit comments

Comments
 (0)