Skip to content

Commit

Permalink
feat(Next > Search): Add country or province to search keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Oct 31, 2023
1 parent 3d3a680 commit 5377101
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
6 changes: 3 additions & 3 deletions next/pages/[gallery].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ServerSideAlbumItem extends GalleryAlbum {
corpus: string;
}

type Props = {
type ComponentProps = {
gallery: NonNullable<AlbumMeta['gallery']>;
albums: ServerSideAlbumItem[];
indexedKeywords: object[];
Expand All @@ -27,7 +27,7 @@ interface Params extends ParsedUrlQuery {
gallery: NonNullable<AlbumMeta['gallery']>
}

export const getStaticProps: GetStaticProps<Props, Params> = async (context) => {
export const getStaticProps: GetStaticProps<ComponentProps, Params> = async (context) => {
const params = context.params!
const { albums } = await getAlbums(params.gallery)
const preparedAlbums = albums.map((album): ServerSideAlbumItem => ({
Expand Down Expand Up @@ -74,7 +74,7 @@ const AlbumYear = styled.h3`
color: #8B5A2B;
`

function AlbumsPage({ gallery, albums, indexedKeywords }: Props) {
function AlbumsPage({ gallery, albums, indexedKeywords }: ComponentProps) {
const {
filtered,
searchBox,
Expand Down
11 changes: 6 additions & 5 deletions next/pages/[gallery]/[album].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import AlbumPageComponent from '../../src/components/AlbumPage'
import getAlbum from '../../src/lib/album'
import getAlbums from '../../src/lib/albums'
import getGalleries from '../../src/lib/galleries'
import indexKeywords from '../../src/lib/search'
import indexKeywords, { addGeographyToSearch } from '../../src/lib/search'
import type { AlbumMeta, Item } from '../../src/types/common'

async function buildStaticPaths() {
Expand All @@ -21,7 +21,7 @@ interface ServerSideAlbumItem extends Item {
corpus: string;
}

type Props = {
type ComponentProps = {
items?: ServerSideAlbumItem[];
meta: AlbumMeta;
indexedKeywords: object[];
Expand All @@ -32,16 +32,17 @@ interface Params extends ParsedUrlQuery {
album: NonNullable<AlbumMeta['albumName']>
}

export const getStaticProps: GetStaticProps<Props, Params> = async (context) => {
export const getStaticProps: GetStaticProps<ComponentProps, Params> = async (context) => {
const params = context.params!
const { album: { items, meta } } = await getAlbum(params.gallery, params.album)
const preparedItems = items.map((item) => ({
...item,
search: addGeographyToSearch(item),
corpus: [item.description, item.caption, item.location, item.city, item.search].join(' '),
}))

return {
props: { items: preparedItems, meta, ...indexKeywords(items) },
props: { items: preparedItems, meta, ...indexKeywords(preparedItems) },
}
}

Expand All @@ -53,7 +54,7 @@ export const getStaticPaths: GetStaticPaths = async () => (
}
)

function AlbumPage({ items = [], meta, indexedKeywords }: Props) {
function AlbumPage({ items = [], meta, indexedKeywords }: ComponentProps) {
return <AlbumPageComponent items={items} meta={meta} indexedKeywords={indexedKeywords} />
}

Expand Down
9 changes: 5 additions & 4 deletions next/pages/[gallery]/all.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import config from '../../../config.json'
import getAlbum from '../../src/lib/album'
import getAlbums from '../../src/lib/albums'
import getGalleries from '../../src/lib/galleries'
import indexKeywords from '../../src/lib/search'
import indexKeywords, { addGeographyToSearch } from '../../src/lib/search'

import AlbumContext from '../../src/components/Context'
import Img from '../../src/components/Img'
Expand All @@ -34,7 +34,7 @@ interface ServerSideAllItem extends Item {
coordinateAccuracy: NonNullable<AlbumMeta['geo']>['zoom'];
}

type Props = {
type ComponentProps = {
items: ServerSideAllItem[];
indexedKeywords: object[];
}
Expand All @@ -43,7 +43,7 @@ interface Params extends ParsedUrlQuery {
gallery: NonNullable<AlbumMeta['gallery']>
}

export const getStaticProps: GetStaticProps<Props, Params> = async (context) => {
export const getStaticProps: GetStaticProps<ComponentProps, Params> = async (context) => {
const params = context.params!
const { albums } = await getAlbums(params.gallery)

Expand All @@ -60,6 +60,7 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (context) =>
album: albumName,
corpus: [item.description, item.caption, item.location, item.city, item.search].join(' '),
coordinateAccuracy: item.coordinateAccuracy ?? albumCoordinateAccuracy,
search: addGeographyToSearch(item),
}))

// reverse order for albums in ascending order (oldest on top)
Expand Down Expand Up @@ -90,7 +91,7 @@ export const getStaticPaths: GetStaticPaths = async () => {
}
}

function AllPage({ items = [], indexedKeywords }: Props) {
function AllPage({ items = [], indexedKeywords }: ComponentProps) {
const refImageGallery = useRef<ReactImageGallery>(null)
const [memoryIndex, setMemoryIndex] = useState(0)
const {
Expand Down
9 changes: 5 additions & 4 deletions next/pages/[gallery]/today.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import config from '../../../config.json'
import getAlbum from '../../src/lib/album'
import getAlbums from '../../src/lib/albums'
import getGalleries from '../../src/lib/galleries'
import indexKeywords from '../../src/lib/search'
import indexKeywords, { addGeographyToSearch } from '../../src/lib/search'

import AlbumPageComponent from '../../src/components/AlbumPage'
import type { AlbumMeta, Item } from '../../src/types/common'
Expand All @@ -16,7 +16,7 @@ interface ServerSideTodayItem extends Item {
coordinateAccuracy: NonNullable<AlbumMeta['geo']>['zoom'];
}

type Props = {
type ComponentProps = {
items: ServerSideTodayItem[];
indexedKeywords: object[];
}
Expand All @@ -25,7 +25,7 @@ interface Params extends ParsedUrlQuery {
gallery: NonNullable<AlbumMeta['gallery']>
}

export const getStaticProps: GetStaticProps<Props, Params> = async (context) => {
export const getStaticProps: GetStaticProps<ComponentProps, Params> = async (context) => {
const params = context.params!
const { albums } = await getAlbums(params.gallery)

Expand All @@ -41,6 +41,7 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (context) =>
album: albumName,
corpus: [item.description, item.caption, item.location, item.city, item.search].join(' '),
coordinateAccuracy: item.coordinateAccuracy ?? albumCoordinateAccuracy,
search: addGeographyToSearch(item),
}))

// reverse order for albums in ascending order (oldest on top)
Expand Down Expand Up @@ -73,7 +74,7 @@ export const getStaticPaths: GetStaticPaths = async () => {
}
}

function Today({ items, indexedKeywords }: Props) {
function Today({ items, indexedKeywords }: ComponentProps) {
return <AlbumPageComponent items={items} indexedKeywords={indexedKeywords} />
}

Expand Down
6 changes: 3 additions & 3 deletions next/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { Fragment } from 'react'
import Link from '../src/components/Link'
import getGalleries, { type Gallery } from '../src/lib/galleries'

type Props = {
type ComponentProps = {
galleries: { id: Gallery; gallery: Gallery }[]
}

export const getStaticProps: GetStaticProps<Props> = async () => {
export const getStaticProps: GetStaticProps<ComponentProps> = async () => {
const { galleries } = await getGalleries()

return {
Expand All @@ -20,7 +20,7 @@ export const getStaticProps: GetStaticProps<Props> = async () => {
}
}

function Home({ galleries }: Props) {
function Home({ galleries }: ComponentProps) {
return (
<>
<Head>
Expand Down
7 changes: 7 additions & 0 deletions next/src/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ function indexKeywords(items: { search: Item['search'] | GalleryAlbum['search']
}

export default indexKeywords

export function addGeographyToSearch(item: Item) {
const hasComma = item.city.lastIndexOf(',') !== -1
const country = hasComma ? item.city.substring(item.city.lastIndexOf(', ') + 1).trim() : item.city.trim()

return item.search === null ? country : `${item.search}, ${country}`
}

0 comments on commit 5377101

Please sign in to comment.