Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
import { useEffect, useMemo, useRef, useState } from "react";
import { useLocation } from "react-router-dom";
import {
DEFAULT_SETTING_SECTION,
isSettingSectionKey,
SETTINGS_SECTIONS,
type SettingSectionKey,
} from "@/components/Settings/settingSections";
import { useInstance } from "@/contexts/InstanceContext";
import useCurrentUser from "@/hooks/useCurrentUser";
import { User_Role } from "@/types/proto/api/user_service_pb";
const Setting = () => {
const location = useLocation();
const user = useCurrentUser();
const { fetchSettings } = useInstance();
const [selectedSection, setSelectedSection] = useState<SettingSectionKey>(DEFAULT_SETTING_SECTION);
const isHost = user?.role === User_Role.ADMIN;
const sectionGroups = useMemo(() => {
const visibleSections = SETTINGS_SECTIONS.filter((section) => section.scope === "basic" || isHost);
return {
admin: visibleSections.filter((section) => section.scope === "admin"),
all: visibleSections,
};
}, [isHost]);
const visibleSectionKeys = useMemo(() => new Set(sectionGroups.all.map((section) => section.key)), [sectionGroups.all]);
useEffect(() => {
const hash = location.hash.slice(1);
const nextSection = isSettingSectionKey(hash) && visibleSectionKeys.has(hash) ? hash : DEFAULT_SETTING_SECTION;
setSelectedSection(nextSection);
}, [location.hash, visibleSectionKeys]);
// Jump back to the top when switching sections; skip the initial hash sync so
// scroll restoration on back-navigation still wins.
const prevSectionRef = useRef<SettingSectionKey | null>(null);
useEffect(() => {
if (prevSectionRef.current && prevSectionRef.current !== selectedSection) {
window.scrollTo({ top: 0 });
}
prevSectionRef.current = selectedSection;
}, [selectedSection]);
useEffect(() => {
if (!isHost) {
return;
}
const preloadSettingKeys = new Set(sectionGroups.admin.flatMap((section) => section.preloadSettingKeys ?? []));
void fetchSettings([...preloadSettingKeys]);
}, [fetchSettings, isHost, sectionGroups.admin]);
const selectedSectionDefinition =
sectionGroups.all.find((section) => section.key === selectedSection) ??
SETTINGS_SECTIONS.find((section) => section.key === DEFAULT_SETTING_SECTION) ??
SETTINGS_SECTIONS[0];
const ActiveSection = selectedSectionDefinition.component;
return (
<section className="w-full min-h-full">
<div className="mx-auto w-full max-w-4xl px-4 pb-12 pt-4 sm:px-6 md:pt-8">
<div className="min-w-0">
<ActiveSection />
</div>
</div>
</section>
);
};
export default Setting;