Favourite pages that follow the user, and a narrow icon-only sidebar
A star beside each page in the sidebar pins it under "Favourites" at the top of the menu; the list is saved with the user (users.favorites, migration 0020), so it follows them to any browser, is offered first in the search box, and can be changed from there too. A button at the foot of the sidebar, or the [ key, narrows it to a strip of icons, where a group's pages open in a small menu beside its icon and the alert and maintenance counts stay visible; the choice is kept per browser and applies only on wide screens. Menu text on a coloured (environment) sidebar is full strength for contrast, and menu badges sit in line on a phone. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
57fd4f8c52
commit
65263a64e3
17 files changed
+2736
-86
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import { api } from "../api/client";
|
||||
import { readableError } from "./errors";
|
||||
import { toast } from "./toast";
|
||||
|
||||
// The pages this person pinned to the top of the menu. They live on the user's record on the server, so they follow the person to
|
||||
// any browser; here they are kept in step so the sidebar, the command palette and the star buttons all show the same list at once.
|
||||
|
||||
/** The same limit the server enforces. */
|
||||
export const MAX_FAVORITES = 24;
|
||||
|
||||
let favorites: string[] = [];
|
||||
const listeners = new Set<() => void>();
|
||||
/** Only the answer to the latest change may overwrite the list, however the replies are ordered. */
|
||||
let latest = 0;
|
||||
|
||||
function set(next: string[]) {
|
||||
favorites = next;
|
||||
for (const l of listeners) l();
|
||||
}
|
||||
|
||||
function subscribe(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
/** Loads the list the server sent with the signed-in user. Call before the menu is first drawn. */
|
||||
export function initFavorites(list: string[] | undefined): void {
|
||||
set([...(list ?? [])]);
|
||||
}
|
||||
|
||||
export function useFavorites(): string[] {
|
||||
return useSyncExternalStore(subscribe, () => favorites);
|
||||
}
|
||||
|
||||
/** Pins a page, or unpins it. Shown at once; put back, with a message, if the server can't save it. */
|
||||
export async function toggleFavorite(path: string): Promise<void> {
|
||||
const before = favorites;
|
||||
const adding = !before.includes(path);
|
||||
if (adding && before.length >= MAX_FAVORITES) {
|
||||
toast.info(`You can pin up to ${MAX_FAVORITES} pages. Unpin one first.`);
|
||||
return;
|
||||
}
|
||||
const next = adding ? [...before, path] : before.filter((p) => p !== path);
|
||||
set(next);
|
||||
const mine = ++latest;
|
||||
try {
|
||||
const res = await api.me.setFavorites(next);
|
||||
if (mine === latest) set(res.favorites);
|
||||
} catch (err) {
|
||||
if (mine === latest) {
|
||||
set(before);
|
||||
toast.error(`Couldn't save your favourites: ${readableError(err)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user