Skip to content

Commit

Permalink
file
Browse files Browse the repository at this point in the history
  • Loading branch information
Slouchwind committed Jun 28, 2023
1 parent 3879066 commit 26b49f6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
9 changes: 4 additions & 5 deletions components/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ export function fillBlank(i18n: string, ...fills: (string | undefined)[]): strin
}

export function useLocale<T extends i18nContents, K extends keyof T>(i18n: T) {
const [userLo, setUserLo] = useState('zh-CN');
useEffect(() => setUserLo(window.navigator.language), []);
const { setting } = useSetting();
const lo = setting.locale || userLo;
const [lo, setLo] = useState('');
const { getSetting } = useSetting();
useEffect(() => setLo(getSetting().locale || window.navigator.language), []);
function locale(key: K, loc?: string) {
return (i18n as Record<K, Record<string, string>>)[key][loc || lo];
}
function localeType<Type extends string>(key: Type, loc?: string) {
return (i18n as Record<Type, Record<string, string>>)[key][loc || lo];
}
return { lo, locale, localeType, userLo };
return { lo, locale, localeType };
}
4 changes: 2 additions & 2 deletions components/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function MainNode({ children, onBodyClick }: {
children: React.ReactNode;
onBodyClick?: React.MouseEventHandler;
}) {
const { lo, locale, localeType, userLo } = useLocale(main);
const { lo, locale, localeType } = useLocale(main);

const { setting, setSetting } = useSetting({
locale: lo,
Expand Down Expand Up @@ -125,7 +125,7 @@ export default function MainNode({ children, onBodyClick }: {
display={display}
/>
));
}, [userLo]);
}, [lo]);

return (
<div id={styles.main} onClick={onBodyClick}>
Expand Down
1 change: 1 addition & 0 deletions components/students/studentsMethods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fileInfo, schaleInfo, studentsJson, studentInfo } from './students';

export async function getStudentsJson(locale: string): Promise<studentsJson> {
if (locale.length < 5) return new Promise(() => { });
const fileJson: fileInfo[] = await fetch('../students.json').then(r => r.json());
const schaleJson: schaleInfo[] = await fetch(`https://schale.gg/data/${locale.slice(3).toLowerCase()}/students.min.json`).then(r => r.json());
return { fileJson, schaleJson };
Expand Down
23 changes: 16 additions & 7 deletions pages/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import { SettingState, useSetting } from '@/components/setting';
const StatesContext = createContext<{
allWindow: AllWindow;
listState: ListState;
setListState: SetStateFun<ListState>;
chatState: ChatState;
setChatState: SetStateFun<ChatState>;
}>({
allWindow: {},
listState: {},
setListState: () => { },
chatState: {},
setChatState: () => { },
});
Expand Down Expand Up @@ -170,6 +172,8 @@ interface LoaderState {

function Loader({ type }: LoaderState) {
const {
listState: { studentsList },
setListState,
chatState: { studentsChat },
setChatState,
} = useContext(StatesContext);
Expand All @@ -188,12 +192,16 @@ function Loader({ type }: LoaderState) {
reader.onload = e => {
const result = e.target?.result;
if (typeof result !== 'string') return;
const json = JSON.parse(result);
setChatState({ studentsChat: json });
const json: {
studentsList: ListState['studentsList'];
studentsChat: ChatState['studentsChat'];
} = JSON.parse(result);
setListState({ studentsList: json.studentsList })
setChatState({ studentsChat: json.studentsChat });
setSetting({ fileName: file.name.split('.')[0] });
}
});
if (type === 'down') downloadFile(studentsChat || {}, `${getSetting().fileName}.json`);
if (type === 'down') downloadFile({ studentsList, studentsChat } || {}, `${getSetting().fileName}.json`);
}}
/>
);
Expand Down Expand Up @@ -257,9 +265,9 @@ interface SendMessageArg {
}

export default function Chat() {
const { lo, locale, userLo } = useLocale(chat);
const { lo, locale } = useLocale(chat);

const { setting, setSetting } = useSetting();
//const { getSetting } = useSetting();

const [listState, setListState] = getClassState(useState<ListState>({
student: 0,
Expand Down Expand Up @@ -298,7 +306,7 @@ export default function Chat() {

useEffect(() => {
getStudentsJson(lo).then(r => setListState({ studentsJson: { data: r } }));
}, [userLo]);
}, [lo]);

//Window
const {
Expand Down Expand Up @@ -421,7 +429,7 @@ export default function Chat() {
display={display}
/>
));
}, [userLo]);
}, [lo]);

useEffect(() => {
listState.studentsList?.forEach(id => {
Expand Down Expand Up @@ -501,6 +509,7 @@ export default function Chat() {
<StatesContext.Provider value={{
allWindow,
listState,
setListState,
chatState,
setChatState,
}}>
Expand Down
4 changes: 2 additions & 2 deletions pages/info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ interface State {
}

export default function Info() {
const { lo, locale, userLo } = useLocale(info);
const { lo, locale } = useLocale(info);
const [state, setState] = getClassState(useState<State>({
student: 0,
studentsJson: { data: {} }
}));

useEffect(() => {
getStudentsJson(lo).then(r => setState({ studentsJson: { data: r } }));
}, [userLo]);
}, [lo]);

return (
<MainNode>
Expand Down

1 comment on commit 26b49f6

@vercel
Copy link

@vercel vercel bot commented on 26b49f6 Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.