2024-03-07 01:28:18 +08:00
|
|
|
import { useQuery } from '@apollo/client';
|
2024-04-24 22:18:58 +08:00
|
|
|
import React, { useEffect } from 'react';
|
2024-07-17 01:40:49 +08:00
|
|
|
import { UserMetadataResponse, getUserMetadata } from './queries';
|
2024-04-24 22:18:58 +08:00
|
|
|
import { setUserSettings } from '/imports/ui/core/local-states/useUserSettings';
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
interface CustomUsersSettingsProps {
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CustomUsersSettings: React.FC<CustomUsersSettingsProps> = ({
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const {
|
2024-07-17 01:40:49 +08:00
|
|
|
data: userMetadataData,
|
|
|
|
loading: userMetadataLoading,
|
|
|
|
error: userMetadataError,
|
|
|
|
} = useQuery<UserMetadataResponse>(getUserMetadata);
|
2024-03-07 01:28:18 +08:00
|
|
|
const [allowToRender, setAllowToRender] = React.useState(false);
|
|
|
|
useEffect(() => {
|
2024-07-17 01:40:49 +08:00
|
|
|
if (userMetadataData && !userMetadataLoading) {
|
|
|
|
const filteredData = userMetadataData.user_metadata.map((uc) => {
|
2024-03-07 01:28:18 +08:00
|
|
|
const { parameter, value } = uc;
|
2024-04-25 04:18:21 +08:00
|
|
|
let parsedValue: string | boolean | string[] = '';
|
|
|
|
try {
|
|
|
|
parsedValue = JSON.parse(uc.value);
|
|
|
|
} catch {
|
|
|
|
parsedValue = value;
|
|
|
|
}
|
|
|
|
return { [parameter]: parsedValue };
|
2024-03-07 01:28:18 +08:00
|
|
|
});
|
2024-04-24 22:18:58 +08:00
|
|
|
setUserSettings(filteredData.reduce((acc, item) => ({ ...acc, ...item }), {}));
|
|
|
|
setAllowToRender(true);
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
|
|
|
}, [
|
2024-07-17 01:40:49 +08:00
|
|
|
userMetadataData,
|
|
|
|
userMetadataLoading,
|
2024-03-07 01:28:18 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-07-17 01:40:49 +08:00
|
|
|
if (userMetadataError) {
|
|
|
|
throw new Error(`Error on requesting custom parameter data: ${userMetadataError}`);
|
2024-03-07 01:28:18 +08:00
|
|
|
}
|
2024-07-17 01:40:49 +08:00
|
|
|
}, [userMetadataError]);
|
2024-03-07 01:28:18 +08:00
|
|
|
|
|
|
|
return allowToRender ? <>{children}</> : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CustomUsersSettings;
|