Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browser nav & remove the need to double-ENTER on the searchbar #298

Merged
merged 29 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3832725
make back nav more intuitive (every time you click search it adds to …
AbhiramTadepalli Oct 31, 2024
e9e6e10
Dynamically change page title based on searchTerms. Helps make back n…
AbhiramTadepalli Nov 1, 2024
57f4566
lint check
AbhiramTadepalli Nov 1, 2024
1ce794d
Trigger new search when clicking ENTER to select add an autocomplete …
AbhiramTadepalli Nov 2, 2024
c93e6a9
Fix formatting of the header (trailing comma, add 'Results - ' at the…
AbhiramTadepalli Nov 2, 2024
673bbf4
Remove state
TyHil Nov 8, 2024
11e2a2f
Bring the meta property for link preview into dashboard/index.tsx so …
AbhiramTadepalli Nov 8, 2024
9dc3e06
(revert later) add a test to diagnose link preview bug
AbhiramTadepalli Nov 8, 2024
17f3293
Revert "Remove state"
AbhiramTadepalli Nov 8, 2024
143f080
Reapply "Remove state"
AbhiramTadepalli Nov 8, 2024
a782977
Revert "(revert later) add a test to diagnose link preview bug"
AbhiramTadepalli Nov 8, 2024
96afc30
try to directly make the meta tag from the url
AbhiramTadepalli Nov 10, 2024
357a222
Merge branch 'develop' of https://github.com/UTDNebula/UTD-Trends int…
AbhiramTadepalli Nov 10, 2024
25280ab
small changes based on inline comments
AbhiramTadepalli Nov 19, 2024
e26529b
testing SSRajmouli for link previews
AbhiramTadepalli Nov 19, 2024
9557009
Try to enable dynamic page titles alongside SSR
AbhiramTadepalli Nov 20, 2024
0bdbf6c
Try again to enable dynamic page titles alongside SSR
AbhiramTadepalli Nov 20, 2024
45dd32f
Revert "Try again to enable dynamic page titles alongside SSR"
AbhiramTadepalli Nov 20, 2024
9ac1e75
Add a dynamicPageTitle state that is initialized to the SSR-generated…
AbhiramTadepalli Nov 20, 2024
543d7e3
order searchterms properly in link preview (courses first and then pr…
AbhiramTadepalli Nov 20, 2024
6aed954
c'mon I forgot linting
AbhiramTadepalli Nov 20, 2024
47ecdb5
FORMAAAAAT
AbhiramTadepalli Nov 20, 2024
7b2aeef
i figured it out maybe
AbhiramTadepalli Nov 20, 2024
47384d2
refactor: moved search term processing to a function as it is called …
AbhiramTadepalli Nov 20, 2024
760af45
format and lint
AbhiramTadepalli Nov 20, 2024
b0f55e2
refactor: remove a dynamicPageTitle state; build the page title via f…
AbhiramTadepalli Nov 20, 2024
d50301c
Update description meta tag
AbhiramTadepalli Nov 20, 2024
99ac32e
remove redundant onChange_internal() in updateValue()
AbhiramTadepalli Nov 20, 2024
932d252
Merge branch 'develop' into fix-browser-nav
AbhiramTadepalli Nov 21, 2024
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
42 changes: 8 additions & 34 deletions src/components/search/SearchBar/searchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ interface SearchProps {
*
* Styled for the splash page
*/
let wasEmpty = false; // tracks if the searchbar was empty before the new entry (to create a new browser navigation entry push())
const SearchBar = ({
manageQuery,
onSelect,
Expand Down Expand Up @@ -76,25 +75,13 @@ const SearchBar = ({
} else {
delete newQuery.searchTerms;
}
if (wasEmpty) {
// if the searchbar was cleared before this entry,
router.push(
{
query: router.query,
},
undefined,
{ shallow: true },
);
wasEmpty = false;
} //otherwise, just update the current navigation entry query
else
router.replace(
{
query: newQuery,
},
undefined,
{ shallow: true },
);
router.push(
{
query: router.query,
},
undefined,
{ shallow: true },
);
}
}

Expand Down Expand Up @@ -162,9 +149,6 @@ const SearchBar = ({

//update parent and queries
function onChange_internal(newValue: SearchQuery[]) {
if (newValue.length == 0) {
wasEmpty = true; // so that the next search creates a new navigation entry (push())
}
if (manageQuery === 'onChange') {
updateQueries(newValue);
}
Expand All @@ -183,7 +167,7 @@ const SearchBar = ({
function updateValue(newValue: SearchQuery[]) {
if (newValue.length) setErrorTooltip(false); //close the tooltip if there is at least 1 valid search term
setValue(newValue);
onChange_internal(newValue);
onSelect_internal(newValue); // clicking enter to select a autocomplete suggestion triggers a new search (it also 'Enters' for the searchbar)
}

//update parent and queries
Expand All @@ -197,15 +181,6 @@ const SearchBar = ({
}
}

//returns results on enter
function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
if (event.key === 'Enter' && inputValue === '') {
event.preventDefault();
event.stopPropagation();
onSelect_internal(value);
}
}

useEffect(() => {
fetch('/api/autocomplete');
}, []);
Expand Down Expand Up @@ -258,7 +233,6 @@ const SearchBar = ({
loadNewOptions(newInputValue);
}}
renderInput={(params) => {
params.inputProps.onKeyDown = handleKeyDown;
return (
<TextField
{...params}
Expand Down
3 changes: 2 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ function MyApp({ Component, pageProps }: AppProps) {
<>
<GoogleAnalytics gaId="G-CC86XR1562" />
<Head>
<title>UTD Trends</title>
<title>UTD TRENDS</title>
<meta key="og:title" property="og:title" content="UTD TRENDS" />
<link
rel="icon"
type="image/png"
Expand Down
6 changes: 2 additions & 4 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ function Document() {
<Head prefix="og: http://ogp.me/ns#">
<meta
name="description"
content="A data visualization tool built to help students view historical course and section data."
content="Choose the perfect classes for you: Nebula Labs's data analytics platform to help you make informed decisions about your coursework with grade and Rate My Professors data."
/>
<meta
name="theme-color"
content={tailwindConfig.theme.extend.colors.royal}
/>

<meta property="og:title" content="UTD Trends" />
<meta
property="og:description"
content="A data visualization tool built to help students view historical course and section data."
content="Choose the perfect classes for you: Nebula Labs's data analytics platform to help you make informed decisions about your coursework with grade and Rate My Professors data."
/>
<meta property="og:type" content="website" />
<meta
Expand Down
106 changes: 83 additions & 23 deletions src/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Card, Grid2 as Grid, useMediaQuery } from '@mui/material';
import type { NextPage } from 'next';
import type { NextPage, NextPageContext } from 'next';
import Head from 'next/head';
import { useRouter } from 'next/router';
import React, { useEffect, useRef, useState } from 'react';
Expand Down Expand Up @@ -247,7 +247,77 @@ function createColorMap(courses: SearchQuery[]): { [key: string]: string } {
return colorMap;
}

export const Dashboard: NextPage = () => {
/**
* Seperates courses and professors from a string of comma-delimited searchTerms or string[] of searchTerms
* @param searchTermInput
* @returns an array of courseSearchTerms and professorSearchTerms
*/
function getSearchTerms(searchTermInput: string | string[] | undefined): {
courseSearchTerms: SearchQuery[];
professorSearchTerms: SearchQuery[];
} {
let array = searchTermInput ?? [];
if (!Array.isArray(array)) {
array = array.split(','); // if searchTermsInput is a comma-delimited string, make it an array
}
const searchTerms = array.map((el) => decodeSearchQueryLabel(el)); // convert an array of strings to an array of SearchQuery's

const courseSearchTerms: SearchQuery[] = [];
const professorSearchTerms: SearchQuery[] = [];

// split the search terms into professors and courses
searchTerms.map((searchTerm) => {
if (typeof searchTerm.profLast !== 'undefined') {
professorSearchTerms.push(searchTerm);
}
if (typeof searchTerm.prefix !== 'undefined') {
courseSearchTerms.push(searchTerm);
}
});

return { courseSearchTerms, professorSearchTerms };
}

/**
*
* @param courseSearchTerms
* @param professorSearchTerms
* @returns an empty string or a comma-delimited list of courses and professors, ending with a " - "
*/
function buildPageTitle(
courseSearchTerms: SearchQuery[],
professorSearchTerms: SearchQuery[],
): string {
let pageTitle = '';
courseSearchTerms.map((term) => {
pageTitle += searchQueryLabel(term) + ', ';
});
professorSearchTerms.map((term) => {
pageTitle += searchQueryLabel(term) + ', ';
});
pageTitle = pageTitle.slice(0, -2) + (pageTitle.length > 0 ? ' - ' : '');
return pageTitle;
}

export async function getServerSideProps(
context: NextPageContext,
): Promise<{ props: { pageTitle: string } }> {
const { courseSearchTerms, professorSearchTerms } = getSearchTerms(
context.query.searchTerms,
);

return {
props: {
pageTitle: buildPageTitle(courseSearchTerms, professorSearchTerms),
},
};
}

export const Dashboard: NextPage<{ pageTitle: string }> = ({
pageTitle,
}: {
pageTitle: string;
}): React.ReactNode => {
const router = useRouter();

//Searches seperated into courses and professors to create combos
Expand All @@ -262,24 +332,9 @@ export const Dashboard: NextPage = () => {
//On search change, seperate into courses and profs, clear data, and fetch new results
useEffect(() => {
if (router.isReady) {
let array = router.query.searchTerms ?? [];
if (!Array.isArray(array)) {
array = array.split(',');
}
const searchTerms = array.map((el) => decodeSearchQueryLabel(el));

const courseSearchTerms: SearchQuery[] = [];
const professorSearchTerms: SearchQuery[] = [];

// split the search terms into professors and courses
searchTerms.map((searchTerm) => {
if (typeof searchTerm.profLast !== 'undefined') {
professorSearchTerms.push(searchTerm);
}
if (typeof searchTerm.prefix !== 'undefined') {
courseSearchTerms.push(searchTerm);
}
});
const { courseSearchTerms, professorSearchTerms } = getSearchTerms(
router.query.searchTerms,
);
setCourses(courseSearchTerms);
setProfessors(professorSearchTerms);

Expand Down Expand Up @@ -836,17 +891,22 @@ export const Dashboard: NextPage = () => {
);
}

/* Final page */

return (
<>
<Head>
<title>Results - UTD Trends</title>
<title>
{'Results - ' + buildPageTitle(courses, professors) + 'UTD TRENDS'}
</title>
<link
rel="canonical"
href="https://trends.utdnebula.com/dashboard"
key="canonical"
/>
<meta
key="og:title"
property="og:title"
content={'Results - ' + pageTitle + 'UTD TRENDS'}
/>
<meta
property="og:url"
content="https://trends.utdnebula.com/dashboard"
Expand Down
Loading