diff --git a/packages/gatsby-theme-project-portal/gatsby-node.js b/packages/gatsby-theme-project-portal/gatsby-node.js index d51e11cf3..6ba0a9a51 100644 --- a/packages/gatsby-theme-project-portal/gatsby-node.js +++ b/packages/gatsby-theme-project-portal/gatsby-node.js @@ -1,6 +1,6 @@ const { withDefaults } = require(`./utils/default-options`) const fs = require("fs") -const { createSearchIndex } = require(`./utils/search`) +const { createSearchIndex, searchQuery } = require(`./utils/search`) const { siteMetadataTypeDefs, projectTypeDefs, @@ -35,55 +35,9 @@ exports.createPages = async ({ graphql, actions, reporter }) => { query { allProject { nodes { - title - agency - topics { - title - } - slug - summary - statusOfData - status - startDate - requirement - question - purpose - projectTeam { - name - employer - } - priorResearch - opportunityCloses - mainContact { - name - } - fundingInfo - expertise - faq { - text - title - } - deliverable - emailContent - endDate slug } } - allGeneralPage { - nodes { - slug - lede - faq { - text - title - } - aims { - text - title - } - title - } - } allCardPage { nodes { slug @@ -195,61 +149,7 @@ exports.createPages = async ({ graphql, actions, reporter }) => { } exports.onPreBuild = async ({ reporter, basePath, pathPrefix, graphql }) => { - const result = await graphql(` - query { - allProject { - nodes { - title - agency - topics { - title - } - slug - summary - statusOfData - status - startDate - requirement - question - purpose - projectTeam { - name - employer - } - priorResearch - opportunityCloses - mainContact { - name - } - fundingInfo - expertise - faq { - text - title - } - deliverable - emailContent - endDate - slug - } - } - allGeneralPage { - nodes { - slug - lede - faq { - text - title - } - aims { - text - title - } - title - } - } - } - `) + const result = await graphql(searchQuery) const { allProject, allGeneralPage } = result.data const [index, documents] = createSearchIndex({ allProject, allGeneralPage }) @@ -263,6 +163,20 @@ exports.onPreBuild = async ({ reporter, basePath, pathPrefix, graphql }) => { if (err) console.error(err) } ) + // this is a function which grabs the page from the original documents + // lunr tosses this info for SPEED + const reduceDocuments = documents.reduce(function (page, document) { + page[document.slug] = document + return page + }, {}) + + await fs.writeFile( + "static/documents-reduced.json", + JSON.stringify(reduceDocuments), + (err) => { + if (err) console.error(err) + } + ) reporter.info( `Site was built with basePath: ${basePath} & pathPrefix: ${pathPrefix}` diff --git a/packages/gatsby-theme-project-portal/src/components/ProjectDetail.stories.tsx b/packages/gatsby-theme-project-portal/src/components/ProjectDetail.stories.tsx index 3b7df2d21..62a4fefed 100644 --- a/packages/gatsby-theme-project-portal/src/components/ProjectDetail.stories.tsx +++ b/packages/gatsby-theme-project-portal/src/components/ProjectDetail.stories.tsx @@ -87,7 +87,7 @@ export const LongTitle: Story = { faq: [ { title: "Question 1?", text: "Answer 1!" }, { - title: "This is another question?", + title: "This is another different question?", text: "Here's an answer for that question! Yay!", }, ], @@ -165,6 +165,58 @@ export const Minimum: Story = { }, } +export const Maximum: Story = { + args: { + question: "Is there a difference between brown sugar and white sugar?", + summary: + "Contrary to common belief, they are nutritionally similar. Nutritionally speaking, all natural sugars have relatively comparable nutritional value with approximately 15 calories per teaspoon (4.2 g).\n", + title: "Sugar Investigations in the US", + mainContact: { + name: "Contact Name", + title: "Title", + employer: "Employer", + email: "some-email@example.com", + }, + + status: "open", + opportunityCloses: new Date("2022-03-04"), + startDate: new Date("2022-01-03"), + endDate: new Date("2022-03-04"), + agency: "Sugar Agency", + topics: [ + { slug: "conspiracy", title: "conspiracy" }, + { slug: "investigation", title: "investigation" }, + ], + lastModified: new Date("2022-05-27T16:34:04.000Z"), + slug: "project/sugar-investigations", + emailContent: + "Dearest community, Are you interested in knowing about sugars? So are we! Join our search!", + deliverable: + "Analysis of existing sugars in the United States. Summary of and detailed recommendations and potential action steps to inform the public about the sugars they are consuming.  \\n* See project summary for additional information.", + purpose: + "Results will be shared with the FDA and whoever else wants these results. We have no oversight and thus may do as we wish.", + expertise: + "While our team does not have previous experience in organic chemistry, we are committed to uncovering the truth.", + requirement: "I do not understand this question. Next.", + keyDates: "Publish data at some point in time.", + priorResearch: "Prior research is published in many reputable journals.", + statusOfData: "Data collection has not begun.", + fundingInfo: + "Our team will evaluate whether or not a project needs money, and we will most likely need the project team to figure out funding.", + openText: "Are you interested in collaborating?", + ongoingText: "This project is ongoing.", + completeText: "This project is complete.", + projectTeam: ProjectTeamStories.Primary.args.contacts, + faq: [ + { title: "Question 1?", text: "Answer 1!" }, + { + title: "This is another question?", + text: "Here's an answer for that question! Yay!", + }, + ], + }, +} + export const TailwindXSWindow: Story = { args: Primary.args, parameters: { viewport: { defaultViewport: "tailwindXS" } }, diff --git a/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx b/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx index 61abab1a0..d47e3c317 100644 --- a/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx +++ b/packages/gatsby-theme-project-portal/src/components/SiteSearch.stories.tsx @@ -23,6 +23,7 @@ export const manyProjects = { { ...ProjectDetailStories.Open.args }, { ...ProjectDetailStories.Ongoing.args }, { ...ProjectDetailStories.Completed.args }, + { ...ProjectDetailStories.Maximum.args }, ], } @@ -30,15 +31,15 @@ export const oneProject = { nodes: [{ ...ProjectDetailStories.LongTitle.args }], } -export const twoGeneralPages = { +export const twoGenPages = { nodes: [ { slug: "about", - ...AboutPageStories.NoImage.args.data.generalPage, + ...AboutPageStories.Primary.args.data.generalPage, }, { slug: "contact", - ...ContactPageStories.NoImage.args.data.generalPage, + ...ContactPageStories.Primary.args.data.generalPage, }, ], } @@ -47,18 +48,15 @@ export const oneGeneralPage = { nodes: [ { slug: "about", - ...AboutPageStories.NoImage.args.data.generalPage, + ...AboutPageStories.Primary.args.data.generalPage, }, ], } -export const noProjectPage = { - nodes: [{}], -} - -export const noGeneralPage = { +export const noPage = { nodes: [], } + // Valid search function const validSearches = async ({ canvasElement }) => { const canvas = within(canvasElement) @@ -129,7 +127,7 @@ export const manyData = { // One general page, no project pages const [indexOneGen, documentsOneGen] = createSearchIndex({ - noProjectPage, + noPage, oneGeneralPage, }) const dbOneGen = documentsOneGen.reduce(function (acc, document) { @@ -146,7 +144,7 @@ export const oneData = { // No general pages, one project page const [indexOneProj, documentsOneProj] = createSearchIndex({ oneProject, - noGeneralPage, + noPage, }) const dbOneProj = documentsOneProj.reduce(function (acc, document) { acc[document.slug] = document @@ -159,7 +157,6 @@ export const oneProjectData = { db: dbOneProj, } -// export const Primary: Story = { args: manyData, } diff --git a/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx b/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx index 5af823202..902b5b48a 100644 --- a/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx +++ b/packages/gatsby-theme-project-portal/src/components/SiteSearch.tsx @@ -66,9 +66,7 @@ export const SiteSearch: FunctionComponent = ({ const item = (
-

- {db[result.ref].question || db[result.ref].slug} -

+

{db[result.ref].title}

    Match found in: diff --git a/packages/gatsby-theme-project-portal/src/components/SiteSearchWrapper.tsx b/packages/gatsby-theme-project-portal/src/components/SiteSearchWrapper.tsx index 1ab2ccd89..cd236eb1c 100644 --- a/packages/gatsby-theme-project-portal/src/components/SiteSearchWrapper.tsx +++ b/packages/gatsby-theme-project-portal/src/components/SiteSearchWrapper.tsx @@ -4,31 +4,20 @@ import SiteSearch from "./SiteSearch" export interface SearchWrapperProps { siteUrl: string - index?: lunr.Index - dataBase?: Function } -export const SearchWrapper: FunctionComponent = ({ +export const SiteSearchWrapper: FunctionComponent = ({ siteUrl, - index, - dataBase, }: SearchWrapperProps) => { const [idx, setIdx] = useState() const [db, setDb] = useState() useEffect(() => { const getIndex = async () => { const savedIndex = await (await fetch("/lunr-index.json")).json() - const documents = await (await fetch("/documents.json")).json() + const db = await (await fetch("/documents-reduced.json")).json() setIdx(lunr.Index.load(savedIndex)) - // this is a function which grabs the slug from the original documents - // lunr tosses this info for SPEED - setDb( - documents.reduce(function (acc, document) { - acc[document.slug] = document - return acc - }, {}) - ) + setDb(db) } // Because Gatsby runs this on build, we need to say to ignore getting the json files @@ -39,4 +28,3 @@ export const SearchWrapper: FunctionComponent = ({ return
    {}
    } -export default SearchWrapper diff --git a/packages/gatsby-theme-project-portal/src/components/index.tsx b/packages/gatsby-theme-project-portal/src/components/index.tsx index db4f1b166..c4a7a6edc 100644 --- a/packages/gatsby-theme-project-portal/src/components/index.tsx +++ b/packages/gatsby-theme-project-portal/src/components/index.tsx @@ -31,3 +31,4 @@ export { Accordion } from "./Accordion" export { KeyDate } from "./KeyDate" export { NavbarItem } from "./NavbarItem" export { PageLayout } from "./PageLayout" +export { SiteSearchWrapper } from "./SiteSearchWrapper" diff --git a/packages/gatsby-theme-project-portal/src/fragments/site-search.js b/packages/gatsby-theme-project-portal/src/fragments/site-search.js deleted file mode 100644 index 0416df574..000000000 --- a/packages/gatsby-theme-project-portal/src/fragments/site-search.js +++ /dev/null @@ -1,63 +0,0 @@ -import { graphql } from "gatsby" - -export const searchFragment = graphql` - fragment SearchData on Query { - allProject { - nodes { - slug - question - title - summary - status - opportunityCloses - startDate - endDate - lastModified - agency - topics { - ...TopicDetails - } - deliverable - purpose - expertise - requirement - keyDates - priorResearch - statusOfData - fundingInfo - emailContent - mainContact { - name - title - employer - email - } - projectTeam { - name - title - employer - email - } - faq { - text - title - } - } - } - allGeneralPage { - nodes { - slug - lede - faq { - text - title - } - aims { - text - title - } - title - } - } - } -` diff --git a/packages/gatsby-theme-project-portal/src/layouts/AboutPageLayout.stories.tsx b/packages/gatsby-theme-project-portal/src/layouts/AboutPageLayout.stories.tsx index d362bb74d..f6c0103b6 100644 --- a/packages/gatsby-theme-project-portal/src/layouts/AboutPageLayout.stories.tsx +++ b/packages/gatsby-theme-project-portal/src/layouts/AboutPageLayout.stories.tsx @@ -7,7 +7,8 @@ const meta: Meta = { component: AboutPageLayout, tags: ["autodocs"], } - +const backgroundImage = loadImage("image/background.jpg") +console.log(backgroundImage) export default meta type Story = StoryObj @@ -17,7 +18,7 @@ export const Primary: Story = { data: { generalPage: { pageName: null, - title: null, + title: "About the Site", header: "What we're here to do", image: loadImage("image/background.jpg"), aims: [ @@ -43,8 +44,9 @@ export const NoImage: Story = { data: { generalPage: { pageName: null, - title: null, + title: "About No Images", header: "What we're here to do", + image: undefined, aims: [ { title: "About Aim", diff --git a/packages/gatsby-theme-project-portal/src/layouts/SearchPageLayout.tsx b/packages/gatsby-theme-project-portal/src/layouts/SearchPageLayout.tsx index e4cbec5ce..6a233caba 100644 --- a/packages/gatsby-theme-project-portal/src/layouts/SearchPageLayout.tsx +++ b/packages/gatsby-theme-project-portal/src/layouts/SearchPageLayout.tsx @@ -1,7 +1,6 @@ import React, { FunctionComponent } from "react" import { graphql } from "gatsby" -import SiteSearchWrapper from "../components/SiteSearchWrapper" -import { HeaderWithImage } from "../components" +import { HeaderWithImage, SiteSearchWrapper } from "../components" import { ImageDataLike } from "gatsby-plugin-image" export interface SearchLayoutProps { diff --git a/packages/gatsby-theme-project-portal/utils/search.js b/packages/gatsby-theme-project-portal/utils/search.js index c5f776aca..622a41e2f 100644 --- a/packages/gatsby-theme-project-portal/utils/search.js +++ b/packages/gatsby-theme-project-portal/utils/search.js @@ -4,43 +4,44 @@ function createSearchIndex(searchNodes) { // searchNodes = {allProject[], allGeneralPage[], siteUrl} let documents = [] Object.keys(searchNodes).forEach((page) => { - if (page === "siteUrl") { - return - } else { - searchNodes[page].nodes.forEach((node) => { - let tempNode = structuredClone(node) - if (page === "allProject") { - if (!node.slug.includes("/")) { - tempNode.slug = `project/${node.slug}` - } + searchNodes[page].nodes.forEach((node) => { + let tempNode = structuredClone(node) + if (page === "allProject") { + if (!node.slug.includes("/")) { + tempNode.slug = `project/${node.slug}` } - const newItem = Object.values(tempNode).map((field) => { - if (lodash.isNull(field)) { - return null - } - - if (typeof field === "object") { - return Object.values(field) - .filter((value) => !lodash.isNull(value)) - .map((value) => - typeof value === "string" - ? value - : Object.values(value) - .filter((k) => !lodash.isNull(k)) - .join(" ") - ) - .join(" ") - } else { - return field - } - }) + } + const newItem = Object.values(tempNode).map((field) => { + if (lodash.isNull(field)) { + return null + } + // extract this part out, test it with a bunch of object cases + if (typeof field === "object") { + return Object.values(field) + .filter((value) => !lodash.isNull(value)) + .map((value) => { + if (typeof value === "string") { + return value + } else if (value == undefined) { + // loose assignment for null + return value + } else { + Object.values(value) + .filter((k) => !lodash.isNull(k)) + .join(" ") + } + }) + .join(" ") + } else { + return field + } + }) - Object.keys(tempNode).forEach((page, i) => { - tempNode[page] = newItem[i] - }) - documents.push(tempNode) + Object.keys(tempNode).forEach((page, i) => { + tempNode[page] = newItem[i] }) - } + documents.push(tempNode) + }) }) // ask lunr to ignore these words within a search @@ -65,9 +66,6 @@ function createSearchIndex(searchNodes) { "been", "but", "by", - "can", - "cannot", - "could", "dear", "did", "do", @@ -81,14 +79,11 @@ function createSearchIndex(searchNodes) { "get", "got", "had", - "has", - "have", "he", "her", "hers", "him", "his", - "how", "however", "i", "if", @@ -101,7 +96,6 @@ function createSearchIndex(searchNodes) { "least", "let", "like", - "likely", "may", "me", "might", @@ -126,7 +120,6 @@ function createSearchIndex(searchNodes) { "say", "says", "she", - "should", "since", "so", "some", @@ -145,21 +138,12 @@ function createSearchIndex(searchNodes) { "too", "twas", "us", - "wants", "was", "we", "were", - "what", - "when", - "where", - "which", "while", - "who", - "whom", - "why", "will", "with", - "would", "yet", "you", "your", @@ -199,4 +183,60 @@ function createSearchIndex(searchNodes) { return [index, documents] } -module.exports = { createSearchIndex } +const searchQuery = ` + query { + allProject { + nodes { + title + agency + topics { + title + } + slug + summary + statusOfData + status + startDate + requirement + question + purpose + projectTeam { + name + employer + } + priorResearch + opportunityCloses + mainContact { + name + } + fundingInfo + expertise + faq { + text + title + } + deliverable + emailContent + endDate + slug + } + } + allGeneralPage { + nodes { + slug + lede + faq { + text + title + } + aims { + text + title + } + title + } + } + } + ` + +module.exports = { createSearchIndex, searchQuery }