Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ studymap/
│ └── lib/ # pure TypeScript utilities (no JSX)
├── supabase/
│ └── migrations/ # SQL for the optional, sign-in-gated tables (user_places,
│ # user_home, user_events, competition_saves,
│ # user_home, user_events, saved_places, competition_saves,
│ # competition_stats), run once by hand - see SELF-HOSTING.md
├── ARCHITECTURE.md
├── CHANGELOG.md
Expand Down Expand Up @@ -208,6 +208,7 @@ Pure TypeScript modules. No JSX, no React imports. Each file has a single respon
| `competitions-share.ts` | Encodes and decodes competition filters to and from a shareable URL query |
| `competition-events.ts` | Flattens a competition's milestones into calendar-friendly events |
| `competition-saves.ts` | Save, unsave and count saved competitions (`competition_saves`, `competition_stats` tables) |
| `saved-places.ts` | The visitor's starred places: browser storage, the `?saved=` link, account sync |
| `calendar-feed.ts` | Resolves a calendar-feed token to the competitions that user saved, without exposing who they are |
| `ics.ts` | Builds RFC 5545 iCalendar text for calendar feeds |
| `user-profile.ts` | Onboarding answers (board, field, country, referral source) and their `user_profiles` row |
Expand Down
4 changes: 3 additions & 1 deletion SELF-HOSTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ a Supabase project:
3. In the Supabase SQL editor, run every file in `supabase/migrations/`, in filename order. Each
one creates its tables with row-level security already scoped to `auth.uid()`, so users can
only ever read or write their own rows. This includes `user_places`, `user_home`, and
`user_events` (the three original sign-in-gated tables), plus `competition_saves` and
`user_events` (the three original sign-in-gated tables), `saved_places` (the stars on public
places, private per user; without it saving still works, kept in the browser only), plus
`competition_saves` and
`competition_stats` for the competitions vertical: saves are private per user, and
`competition_stats` is a public save-count aggregate kept in sync by a trigger, readable by
anyone but writable only by that trigger, never directly. It also includes `user_profiles`,
Expand Down
6 changes: 6 additions & 0 deletions src/app/docs/(article)/privacy-data/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const TABLES: { name: string; stores: string; note?: string }[] = [
name: "user_events",
stores: "Personal calendar events you've added on top of the public exam calendar.",
},
{
name: "saved_places",
stores:
"Which public places you've starred on the map - just a list of ids, nothing else.",
note: "Also kept in this browser, so it works without an account; signing in merges the two.",
},
{
name: "competition_saves",
stores: "Which competitions you've saved - just a list of ids, nothing else.",
Expand Down
78 changes: 77 additions & 1 deletion src/components/map/map-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as React from "react";
import Link from "next/link";
import { Code2, MapPinPlus, Search, Share2, X } from "lucide-react";
import { Code2, MapPinPlus, Search, Share2, Star, X } from "lucide-react";

import type { City, PlaceType, Place } from "@/lib/types";
import { Button } from "@/components/ui/button";
Expand Down Expand Up @@ -44,6 +44,18 @@ export interface MyPlacesProps {
onEditHome: () => void;
}

/** The "Saved" filter, the Share action, and the banner for a shared shortlist (#355). */
export interface SavedControls {
/** How many of the visitor's own saved places still exist in the dataset. */
count: number;
/** True while the list is narrowed to the visitor's saved places. */
active: boolean;
onToggle: () => void;
onShare: () => void;
/** Set while a `?saved=` link is open: a read-only shortlist from someone else. */
shared: { count: number; onSaveAll: () => void; onExit: () => void } | null;
}

interface MapPanelProps {
filters: PlaceFilters;
onFiltersChange: (filters: PlaceFilters) => void;
Expand All @@ -65,6 +77,7 @@ interface MapPanelProps {
activeFilters: string[];
onResetFilters: () => void;
resultsToggle?: { label: string; onClick: () => void } | null;
savedControls?: SavedControls;
onSelectPlace: (place: Place) => void;
onShowDetails?: (place: Place) => void;
compare?: CompareControls;
Expand Down Expand Up @@ -102,6 +115,7 @@ export function MapPanel({
activeFilters,
onResetFilters,
resultsToggle,
savedControls,
onSelectPlace,
onShowDetails,
compare,
Expand Down Expand Up @@ -230,6 +244,68 @@ export function MapPanel({
Suggest a place
</Button>

{savedControls && (
<div className="flex flex-col gap-1.5">
{savedControls.shared ? (
<div
role="status"
className="rounded-md border border-border bg-muted/50 px-3 py-2 text-xs"
>
<p className="text-foreground">
A shortlist of {savedControls.shared.count}{" "}
{savedControls.shared.count === 1 ? "place" : "places"} was shared with
you. It is read only: your own saved places are not changed.
</p>
<div className="mt-2 flex flex-wrap gap-2">
<Button type="button" size="sm" onClick={savedControls.shared.onSaveAll}>
Save all to my list
</Button>
<Button
type="button"
size="sm"
variant="ghost"
onClick={savedControls.shared.onExit}
>
Show all places
</Button>
</div>
</div>
) : (
<div className="flex items-center justify-between gap-2">
<button
type="button"
aria-pressed={savedControls.active}
onClick={savedControls.onToggle}
className={`inline-flex items-center gap-1.5 rounded-full border px-3 py-1.5 text-[13px] font-medium transition-colors ${
savedControls.active
? "border-amber-500 bg-amber-500/15 text-foreground"
: "border-border bg-card text-muted-foreground hover:bg-muted hover:text-foreground"
}`}
>
<Star
className="size-3.5"
fill={savedControls.active ? "currentColor" : "none"}
aria-hidden
/>
Saved
<span className="tabular-nums text-[11px] text-muted-foreground">
{savedControls.count}
</span>
</button>
{savedControls.count > 0 && (
<button
type="button"
onClick={savedControls.onShare}
className="text-xs text-primary underline-offset-2 hover:underline"
>
Share list
</button>
)}
</div>
)}
</div>
)}

{myPlaces && <MyPlacesPanel {...myPlaces} />}

<Separator />
Expand Down
2 changes: 2 additions & 0 deletions src/components/map/place-detail-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Place } from "@/lib/types";
import { humanizeCity, PLACE_TYPE_LABELS } from "@/lib/types";
import { CategoryIcon } from "@/components/map/category-icon";
import { PlacePhotoGallery } from "@/components/map/place-photo-gallery";
import { SaveStar } from "@/components/map/save-star";
import { SharePlaceCard } from "@/components/map/share-place-card";
import { PlaceContact } from "@/components/pins/place-contact";
import { VerifiedBadge } from "@/components/pins/verified-badge";
Expand Down Expand Up @@ -40,6 +41,7 @@ export function PlaceDetailDrawer({ place, open, onOpenChange }: PlaceDetailDraw
<SheetTitle className="flex items-center gap-2">
{place.name}
<VerifiedBadge place={place} />
<SaveStar placeId={place.id} placeName={place.name} className="ml-auto" />
</SheetTitle>
<SheetDescription>{humanizeCity(place.city)}</SheetDescription>
</SheetHeader>
Expand Down
148 changes: 148 additions & 0 deletions src/components/map/places-map.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,151 @@ describe("PlacesMap list follows the map (#351)", () => {
expect(window.location.search).not.toContain("bounds");
});
});

describe("PlacesMap saved places (#355)", () => {
afterEach(() => window.localStorage.clear());

function renderMap() {
return render(
<PlacesMap
places={PLACES}
cities={CITIES}
defaultCenter={[19.08, 72.95]}
defaultZoom={11}
/>,
);
}

const star = (name: string) => screen.getByRole("button", { name: `Save ${name}` });
const stored = () =>
JSON.parse(window.localStorage.getItem("studymap:saved-places") ?? "[]");

it("stars a place from the results list and keeps it after a reload", () => {
const { unmount } = renderMap();
fireEvent.click(star("City Library"));

expect(
screen
.getByRole("button", { name: "Remove City Library from saved" })
.getAttribute("aria-pressed"),
).toBe("true");
expect(stored()).toEqual(["city-library"]);

unmount();
renderMap();
expect(
screen.getByRole("button", { name: "Remove City Library from saved" }),
).toBeTruthy();
});

it("shows how many places are saved, and a Saved filter that narrows the list to them", () => {
renderMap();
fireEvent.click(star("City Library"));
fireEvent.click(star("City Airport"));

const filter = screen.getByRole("button", { name: /^Saved\s*2$/ });
expect(filter.getAttribute("aria-pressed")).toBe("false");
fireEvent.click(filter);

expect(
screen.getByRole("button", { name: /^Saved\s*2$/ }).getAttribute("aria-pressed"),
).toBe("true");
expect(screen.getByText("City Library")).toBeTruthy();
expect(screen.getByText("City Airport")).toBeTruthy();
expect(screen.queryByText("Thane Library")).toBeNull();
expect(screen.queryByText("Riverside Reading Room")).toBeNull();

fireEvent.click(screen.getByRole("button", { name: /^Saved\s*2$/ }));
expect(screen.getByText("Thane Library")).toBeTruthy();
});

it("says why the list is empty when the Saved filter is on and nothing is saved", () => {
renderMap();
fireEvent.click(screen.getByRole("button", { name: /^Saved\s*0$/ }));

expect(screen.getByRole("status").textContent).toContain("saved places only");
});

it("copies a link that opens the saved places as a shortlist", () => {
renderMap();
fireEvent.click(star("City Library"));
fireEvent.click(star("City Airport"));

fireEvent.click(screen.getByRole("button", { name: "Share list" }));

expect(writeText).toHaveBeenCalledOnce();
const url = new URL(writeText.mock.calls[0][0]);
expect(url.searchParams.get("saved")).toBe("city-library,airport");
});

it("offers to share only once something is saved", () => {
renderMap();
expect(screen.queryByRole("button", { name: "Share list" })).toBeNull();
fireEvent.click(star("City Library"));
expect(screen.getByRole("button", { name: "Share list" })).toBeTruthy();
});

describe("opening a shared link", () => {
it("shows that shortlist read only, and drops places that no longer exist", () => {
window.history.replaceState(
null,
"",
"/map?saved=city-library,airport,deleted-place",
);
renderMap();

expect(screen.getByRole("status").textContent).toContain("A shortlist of 2 places");
expect(screen.getByText("City Library")).toBeTruthy();
expect(screen.getByText("City Airport")).toBeTruthy();
expect(screen.queryByText("Thane Library")).toBeNull();
// Read only: the visitor's own list is untouched.
expect(stored()).toEqual([]);
expect(screen.queryByRole("button", { name: "Share list" })).toBeNull();
});

it("keeps the shortlist in the address bar", () => {
window.history.replaceState(null, "", "/map?saved=city-library,airport");
renderMap();
advance(500);

expect(params().get("saved")).toBe("city-library,airport");
});

it("adds the shortlist to the visitor's own list on request, without duplicates", () => {
window.localStorage.setItem(
"studymap:saved-places",
JSON.stringify(["city-library"]),
);
window.history.replaceState(null, "", "/map?saved=city-library,airport");
renderMap();

fireEvent.click(screen.getByRole("button", { name: "Save all to my list" }));
advance(500);

expect(stored()).toEqual(["city-library", "airport"]);
expect(screen.queryByRole("status")).toBeNull();
expect(screen.getByText("Thane Library")).toBeTruthy();
expect(params().get("saved")).toBeNull();
});

it("returns to every place without saving anything", () => {
window.history.replaceState(null, "", "/map?saved=city-library");
renderMap();

fireEvent.click(screen.getByRole("button", { name: "Show all places" }));
advance(500);

expect(screen.getByText("Thane Library")).toBeTruthy();
expect(stored()).toEqual([]);
expect(params().get("saved")).toBeNull();
});

it("ignores a link whose places are all gone", () => {
window.history.replaceState(null, "", "/map?saved=gone-1,gone-2");
renderMap();

expect(screen.queryByRole("status")).toBeNull();
expect(screen.getByText("Thane Library")).toBeTruthy();
});
});
});
Loading
Loading