Skip to content

Commit

Permalink
Strip user profiles to whitelisted fields for /people page
Browse files Browse the repository at this point in the history
  • Loading branch information
zoul committed Nov 20, 2024
1 parent 1748e1f commit 4446913
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
22 changes: 4 additions & 18 deletions app/people/SearchablePeopleBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {
UserProfileCard,
UserProfileContainer,
} from "~/components/UserProfileCard";
import { compareByName, type UserProfile } from "~/src/data/user-profile";
import { compareByName } from "~/src/data/user-profile";

import { match, type PublicUserProfile } from "./matching";

type Props = {
allUserProfiles: ReadonlyArray<UserProfile>;
allUserProfiles: ReadonlyArray<PublicUserProfile>;
};

export const SearchablePeopleBox = ({ allUserProfiles }: Props) => {
Expand Down Expand Up @@ -38,19 +40,3 @@ export const SearchablePeopleBox = ({ allUserProfiles }: Props) => {
</div>
);
};

const normalize = (s: string) =>
s
.toLocaleLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");

const match = (profile: UserProfile, query: string): boolean => {
const scalarMatches = (value: string | undefined) =>
value ? normalize(value).includes(normalize(query)) : false;
return (
scalarMatches(profile.name) ||
scalarMatches(profile.tags) ||
scalarMatches(profile.bio)
);
};
25 changes: 25 additions & 0 deletions app/people/matching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type UserProfile } from "~/src/data/user-profile";
import { subset } from "~/src/utils";

const publicProps = ["id", "name", "bio", "tags", "profilePictureUrl"] as const;

export type PublicUserProfile = Pick<UserProfile, (typeof publicProps)[number]>;

export const stripNonPublicFields = (profile: UserProfile) =>
subset(profile, publicProps) as PublicUserProfile;

export const match = (profile: PublicUserProfile, query: string): boolean => {
const scalarMatches = (value: string | undefined) =>
value ? normalize(value).includes(normalize(query)) : false;
return (
scalarMatches(profile.name) ||
scalarMatches(profile.tags) ||
scalarMatches(profile.bio)
);
};

const normalize = (s: string) =>
s
.toLocaleLowerCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");
4 changes: 3 additions & 1 deletion app/people/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Metadata } from "next";

import { stripNonPublicFields } from "~/app/people/matching";
import { SearchablePeopleBox } from "~/app/people/SearchablePeopleBox";
import { Breadcrumbs } from "~/components/Breadcrumbs";
import { getAllUserProfiles } from "~/src/data/user-profile";
Expand All @@ -18,12 +19,13 @@ export const metadata: Metadata = {
/** People address book */
async function Page() {
const profiles = await getAllUserProfiles("Public Profiles");
const strippedProfiles = profiles.map(stripNonPublicFields);
return (
<main className="m-auto max-w-content px-7 py-20">
<Breadcrumbs currentPage="Lidé" />
<h1 className="typo-title mb-10 mt-7">Lidé</h1>
<p className="mb-10 max-w-prose">{metadata.description}</p>
<SearchablePeopleBox allUserProfiles={profiles} />
<SearchablePeopleBox allUserProfiles={strippedProfiles} />
</main>
);
}
Expand Down
6 changes: 4 additions & 2 deletions src/data/user-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ export async function createUserProfile(
*
* Uses `String.localeCompare` on the last word of the name.
*/
export const compareByName = (a: UserProfile, b: UserProfile) =>
compareNames(a.name, b.name);
export const compareByName = (
a: Pick<UserProfile, "name">,
b: Pick<UserProfile, "name">,
) => compareNames(a.name, b.name);

/** Locale compare strings according to their last words */
export const compareNames = (a: string, b: string) =>
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export function map<T, U>(
}

/** Return object subset with given keys */
export const subset = <T extends object>(obj: T, keys: (keyof T)[]) =>
export const subset = <T extends object>(
obj: T,
keys: ReadonlyArray<keyof T>,
) =>
Object.fromEntries(
keys.filter((key) => key in obj).map((key) => [key, obj[key]]),
);
Expand Down

0 comments on commit 4446913

Please sign in to comment.