Skip to content

Commit

Permalink
Update how we display page last updated (#6110)
Browse files Browse the repository at this point in the history
* 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 <hbuchel@gmail.com>

* 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 <hbuchel@gmail.com>
  • Loading branch information
timngyn and hbuchel authored Oct 31, 2023
1 parent 0766254 commit 17d74dc
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 191 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ amplify/backend/function/submissionsLambda/src/controllers
public/**/**/nextImageExportOptimizer/
public/next-image-export-optimizer-hashes.json
src/directory/directory.json
src/directory/flatDirectory.json

#amplify-do-not-edit-begin
amplify/\#current-cloud-backend
Expand Down
1 change: 0 additions & 1 deletion amplify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ frontend:
preBuild:
commands:
- yarn install
- node tasks/addLastUpdatedToMdxFiles.js
build:
commands:
- echo "BUILD_ENV=$BUILD_ENV" >> .env.custom
Expand Down
2 changes: 0 additions & 2 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export default async () => {
const withMDX = createMDX({
extension: /\.mdx$/,
options: {
// TODO: Fix this frontmatter plugin
// remarkPlugins: [frontmatterPlugin],
rehypePlugins: [[rehypeImgSize, { dir: 'public' }]]
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@
"next-start": "next start",
"prepare": "husky install",
"analyze": "ANALYZE=true yarn next-build",
"prebuild": "node src/directory/generateDirectory.mjs"
"prebuild": "node src/directory/generateDirectory.mjs && node src/directory/generateFlatDirectory.mjs"
}
}
18 changes: 0 additions & 18 deletions src/components/Fragments/index.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
import { Fragment } from 'react';
import FilterChildren from '../FilterChildren';
import { useLastUpdatedDatesContext } from '../LastUpdatedProvider';

type MdxFrontmatterType = {
lastUpdated: string;
};

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

const { state, dispatch } = useLastUpdatedDatesContext();
let filterKey = '';

for (const key in fragments) {
if (!filterKey) filterKey = key;
const fragment = fragments[key]([]);
frontmatter = fragment.props.frontmatter;

if (frontmatter && frontmatter.lastUpdated) {
if (
state.files[key] === undefined ||
(state.files[key] &&
!state.files[key].includes(frontmatter.lastUpdated))
) {
dispatch({
type: 'update',
key: key,
lastUpdated: frontmatter.lastUpdated
});
}
}

children.push(<Fragment key={key}>{fragment}</Fragment>);
}
Expand Down
69 changes: 0 additions & 69 deletions src/components/LastUpdatedProvider/index.tsx

This file was deleted.

14 changes: 13 additions & 1 deletion src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import { TableOfContents } from '@/components/TableOfContents';
import type { Heading } from '@/components/TableOfContents/TableOfContents';
import { PlatformNavigator } from '@/components/PlatformNavigator';
import directory from 'src/directory/directory.json';
import flatDirectory from 'src/directory/flatDirectory.json';
import { PageNode } from 'src/directory/directory';
import { Breadcrumbs } from '@/components/Breadcrumbs';
import { debounce } from '@/utils/debounce';
import { PageLastUpdated } from '../PageLastUpdated';

export const Layout = ({
children,
Expand All @@ -42,7 +44,8 @@ export const Layout = ({
pageTitle,
pageType = 'inner',
platform,
url
url,
showLastUpdatedDate = true
}: {
children: any;
hasTOC?: boolean;
Expand All @@ -51,6 +54,7 @@ export const Layout = ({
pageType?: 'home' | 'inner';
platform?: Platform;
url?: string;
showLastUpdatedDate: boolean;
}) => {
const [menuOpen, toggleMenuOpen] = useState(false);
const [tocHeadings, setTocHeadings] = useState<Heading[]>([]);
Expand Down Expand Up @@ -261,6 +265,14 @@ export const Layout = ({
}}
/>
)}
<div className="layout-sidebar-feedback">
[ Feedback widget goes here ]
</div>
{showLastUpdatedDate && (
<PageLastUpdated
directoryData={flatDirectory[router.pathname]}
/>
)}
</div>
</View>
</View>
Expand Down
38 changes: 38 additions & 0 deletions src/components/PageLastUpdated/PageLastUpdated.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Text, View } from '@aws-amplify/ui-react';
import { PageNode } from 'src/directory/directory';

type PageLastUpdatedProps = {
directoryData: PageNode;
};

export function PageLastUpdated({ directoryData }: PageLastUpdatedProps) {
if (directoryData) {
const lastUpdated = directoryData['lastUpdated'];

if (!lastUpdated) {
return <></>;
}

const date = toReadableDate(lastUpdated);

return (
<View className="page-last-updated">
<View className="page-last-updated__inner">
<Text fontSize="14px">Page updated {date}</Text>{' '}
</View>
</View>
);
}

return <></>;
}

function toReadableDate(date) {
const dateOptions: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
day: 'numeric'
};

return new Date(date).toLocaleDateString('en-US', dateOptions);
}
1 change: 1 addition & 0 deletions src/components/PageLastUpdated/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PageLastUpdated } from './PageLastUpdated';
5 changes: 5 additions & 0 deletions src/directory/directory.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ export type PageNode = {
* Root url for the home page
*/
url?: string;

/**
* String representing when file was last edited
*/
lastUpdated?: string;
};
6 changes: 6 additions & 0 deletions src/directory/generateDirectory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'path';
import JSON5 from 'json5';
import { directory } from './directory.mjs';
import { writeFile } from 'fs/promises';
import { getLastModifiedDate } from 'git-jiggy';

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

// Get the last updated date
directoryNode['lastUpdated'] = await getLastModifiedDate(
directoryNode.path
);

const relativePath = path.relative(rootPath, directoryNode.path);
const parsedPath = path.parse(relativePath);

Expand Down
41 changes: 41 additions & 0 deletions src/directory/generateFlatDirectory.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { fileURLToPath } from 'url';
import path from 'path';
import { writeFile } from 'fs/promises';
import directory from './directory.json' assert { type: 'json' };

function flattenJSON(jsonObj, flattened = {}) {
if (Array.isArray(jsonObj.children)) {
jsonObj.children.forEach((child) => flattenJSON(child, flattened));
}
if (jsonObj.route) {
flattened[jsonObj.route] = { ...jsonObj };
// Remove the 'children' property since we don't need it in the "flat" structure
delete flattened[jsonObj.route].children;
}
return flattened;
}

/**
* Generate a "flat" version of directory.json
*/
async function generateFlatDirectory() {
const flatDirectory = flattenJSON(directory);

try {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const filePath = path.join(__dirname, 'flatDirectory.json');

const json = JSON.stringify(flatDirectory, null, 2);

await writeFile(filePath, json, 'utf-8');

console.log('Directory object has been written to', filePath);
} catch (err) {
throw new Error(`Error saving to flatDirectory.json: ${err}`);
}
}

console.log('Generating flatDirectory.json...');
generateFlatDirectory();
1 change: 1 addition & 0 deletions src/pages/[platform]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function getStaticProps(context) {
props: {
platform: context.params.platform,
hasTOC: false,
showLastUpdatedDate: false,
pageType: 'home',
meta
}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { useRouter } from 'next/router';

function MyApp({ Component, pageProps }) {
const router = useRouter();
const { meta, platform, url, hasTOC, pageType } = pageProps;
const { meta, platform, url, hasTOC, pageType, showLastUpdatedDate } =
pageProps;
const getLayout =
Component.getLayout ||
((page) => (
Expand All @@ -20,6 +21,7 @@ function MyApp({ Component, pageProps }) {
url={url}
platform={platform ? platform : ''}
hasTOC={hasTOC}
showLastUpdatedDate={showLastUpdatedDate}
>
{page}
</Layout>
Expand Down
1 change: 1 addition & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function getStaticProps() {
return {
props: {
hasTOC: false,
showLastUpdatedDate: false,
pageType: 'home',
meta
}
Expand Down
Loading

0 comments on commit 17d74dc

Please sign in to comment.