Skip to content

Commit

Permalink
Add parameters for all reasonable app state and default “darkMode” to…
Browse files Browse the repository at this point in the history
… User-Agent preference
  • Loading branch information
ntninja committed Jul 20, 2024
1 parent 8946cc8 commit fe33cf0
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,54 @@ function generateHue() {
return Math.floor(Math.random() * 360);
}

function useSearchParamOrStorage(key: String, generator: () => String)
function useParamOrElse<T>(
key: string, type: "string" | "boolean", makeState: () => [T, (T) => null]
): [T, (T) => null]
{
let searchString = window.location.hash.split("?")[1];
let searchParams = new URLSearchParams(typeof(searchString) === "string" ? searchString : "");
if (searchParams.has(key)) {
return useState(searchParams.get(key));
let value = searchParams.get(key);
if (type === "boolean") {
value = value !== "false";
} else if (type === "number") {
value = Number(value);
}
return useState(value);
} else {
return useStorage(key, generator);
return makeState();
}
}

function useParamOrState<T>(
key: string, type: "string" | "boolean", generator: () => T
): [T, (T) => null]
{
return useParamOrElse(key, type, () => useState(generator));
}

function useParamOrStorage<T>(
key: string, type: "string" | "boolean", generator: () => T
): [T, (T) => null]
{
return useParamOrElse(key, type, () => useStorage(key, generator));
}

function App() {
const id = useHash(); // Normalizes URL

const toast = useToast();
const [language, setLanguage] = useState("plaintext");
const [language, setLanguage] = useParamOrState("language", "string", "plaintext");
const [connection, setConnection] = useState<
"connected" | "disconnected" | "desynchronized"
>("disconnected");
const [users, setUsers] = useState<Record<number, UserInfo>>({});
const [name, setName] = useSearchParamOrStorage("userName", generateName);
const [hue, setHue] = useStorage("hue", generateHue);
const [name, setName] = useParamOrStorage("userName", "string", generateName);
const [hue, setHue] = useParamOrStorage("userHue", "number", generateHue);
const [editor, setEditor] = useState<editor.IStandaloneCodeEditor>();
const [darkMode, setDarkMode] = useStorage("darkMode", () => false);
const [darkMode, setDarkMode] = useParamOrStorage(
"darkMode", "boolean", () => window.matchMedia("(prefers-color-scheme: dark)")
);
const rustpad = useRef<Rustpad>();

useEffect(() => {
Expand Down

0 comments on commit fe33cf0

Please sign in to comment.