random food word nicknames!

This commit is contained in:
legop3
2026-01-15 17:26:31 -05:00
parent 2252527d89
commit d4ce6e9092
8 changed files with 321 additions and 48 deletions
+49
View File
@@ -0,0 +1,49 @@
import { useEffect, useRef } from 'react';
import { useSettingsNamespace } from '../settings/index.js';
import { useSession } from '../context/SessionContext.jsx';
import wordListText from '../assets/wordlist.txt?raw';
function capitalize(word) {
if (!word) return '';
return word.charAt(0).toUpperCase() + word.slice(1);
}
function pickRandomWord() {
const words = String(wordListText || '')
.split(/\r?\n/)
.map((word) => word.trim())
.filter(Boolean);
if (!words.length) return '';
const selected = words[Math.floor(Math.random() * words.length)] || '';
return selected.trim();
}
export default function useDefaultNickname() {
const { setNickname } = useSession();
const { value, status, save } = useSettingsNamespace('profile', { nickname: '' });
const attemptedRef = useRef(false);
useEffect(() => {
if (status !== 'ready') return;
if (attemptedRef.current) return;
const existing = (value.nickname || '').trim();
if (existing) return;
attemptedRef.current = true;
const generated = capitalize(pickRandomWord()).slice(0, 32);
if (!generated) return;
const applyNickname = async () => {
try {
await setNickname(generated);
} catch (err) {
// Ignore server errors; still persist locally to avoid re-rolling.
} finally {
save({ nickname: generated });
}
};
applyNickname();
}, [save, setNickname, status, value.nickname]);
}