| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | /* | 
					
						
							|  |  |  | 	GoToSocial | 
					
						
							| 
									
										
										
										
											2023-03-12 18:49:06 +01:00
										 |  |  | 	Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | 	SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | 	it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | 	the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | 	(at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | 	but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | 	GNU Affero General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const React = require("react"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | const query = require("../lib/query"); | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | const { | 
					
						
							|  |  |  | 	useTextInput, | 
					
						
							|  |  |  | 	useFileInput, | 
					
						
							| 
									
										
										
										
											2023-06-13 12:21:26 +02:00
										 |  |  | 	useBoolInput, | 
					
						
							|  |  |  | 	useFieldArrayInput | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | } = require("../lib/form"); | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | const useFormSubmit = require("../lib/form/submit"); | 
					
						
							| 
									
										
										
										
											2023-06-13 12:21:26 +02:00
										 |  |  | const { useWithFormContext, FormContext } = require("../lib/form/context"); | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const { | 
					
						
							|  |  |  | 	TextInput, | 
					
						
							|  |  |  | 	TextArea, | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 	FileInput, | 
					
						
							|  |  |  | 	Checkbox | 
					
						
							|  |  |  | } = require("../components/form/inputs"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const FormWithData = require("../lib/form/form-with-data"); | 
					
						
							|  |  |  | const FakeProfile = require("../components/fake-profile"); | 
					
						
							|  |  |  | const MutationButton = require("../components/form/mutation-button"); | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = function UserProfile() { | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 	return ( | 
					
						
							|  |  |  | 		<FormWithData | 
					
						
							|  |  |  | 			dataQuery={query.useVerifyCredentialsQuery} | 
					
						
							|  |  |  | 			DataForm={UserProfileForm} | 
					
						
							|  |  |  | 		/> | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | function UserProfileForm({ data: profile }) { | 
					
						
							|  |  |  | 	/* | 
					
						
							|  |  |  | 		User profile update form keys | 
					
						
							|  |  |  | 		- bool bot | 
					
						
							|  |  |  | 		- bool locked | 
					
						
							|  |  |  | 		- string display_name | 
					
						
							|  |  |  | 		- string note | 
					
						
							|  |  |  | 		- file avatar | 
					
						
							|  |  |  | 		- file header | 
					
						
							|  |  |  | 		- bool enable_rss | 
					
						
							|  |  |  | 		- string custom_css (if enabled) | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 	const { data: instance } = query.useInstanceQuery(); | 
					
						
							| 
									
										
										
										
											2023-06-13 12:21:26 +02:00
										 |  |  | 	const instanceConfig = React.useMemo(() => { | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			allowCustomCSS: instance?.configuration?.accounts?.allow_custom_css === true, | 
					
						
							|  |  |  | 			maxPinnedFields: instance?.configuration?.accounts?.max_profile_fields ?? 6 | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 	}, [instance]); | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 	const form = { | 
					
						
							|  |  |  | 		avatar: useFileInput("avatar", { withPreview: true }), | 
					
						
							|  |  |  | 		header: useFileInput("header", { withPreview: true }), | 
					
						
							| 
									
										
										
										
											2023-02-06 09:19:56 +01:00
										 |  |  | 		displayName: useTextInput("display_name", { source: profile }), | 
					
						
							|  |  |  | 		note: useTextInput("note", { source: profile, valueSelector: (p) => p.source?.note }), | 
					
						
							|  |  |  | 		customCSS: useTextInput("custom_css", { source: profile }), | 
					
						
							|  |  |  | 		bot: useBoolInput("bot", { source: profile }), | 
					
						
							|  |  |  | 		locked: useBoolInput("locked", { source: profile }), | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 		discoverable: useBoolInput("discoverable", { source: profile}), | 
					
						
							| 
									
										
										
										
											2023-02-06 09:19:56 +01:00
										 |  |  | 		enableRSS: useBoolInput("enable_rss", { source: profile }), | 
					
						
							| 
									
										
										
										
											2023-06-13 12:21:26 +02:00
										 |  |  | 		fields: useFieldArrayInput("fields_attributes", { | 
					
						
							|  |  |  | 			defaultValue: profile?.source?.fields, | 
					
						
							|  |  |  | 			length: instanceConfig.maxPinnedFields | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 12:21:26 +02:00
										 |  |  | 	const [submitForm, result] = useFormSubmit(form, query.useUpdateCredentialsMutation(), { | 
					
						
							|  |  |  | 		onFinish: () => { | 
					
						
							|  |  |  | 			form.avatar.reset(); | 
					
						
							|  |  |  | 			form.header.reset(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return ( | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 		<form className="user-profile" onSubmit={submitForm}> | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 			<h1>Profile</h1> | 
					
						
							|  |  |  | 			<div className="overview"> | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 				<FakeProfile | 
					
						
							|  |  |  | 					avatar={form.avatar.previewValue ?? profile.avatar} | 
					
						
							|  |  |  | 					header={form.header.previewValue ?? profile.header} | 
					
						
							|  |  |  | 					display_name={form.displayName.value ?? profile.username} | 
					
						
							|  |  |  | 					username={profile.username} | 
					
						
							| 
									
										
										
										
											2023-05-13 12:17:22 +02:00
										 |  |  | 					role={profile.role} | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 				/> | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 				<div className="files"> | 
					
						
							|  |  |  | 					<div> | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 						<FileInput | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 							label="Header" | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 							field={form.header} | 
					
						
							|  |  |  | 							accept="image/*" | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 						/> | 
					
						
							|  |  |  | 					</div> | 
					
						
							|  |  |  | 					<div> | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 						<FileInput | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 							label="Avatar" | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 							field={form.avatar} | 
					
						
							|  |  |  | 							accept="image/*" | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 						/> | 
					
						
							|  |  |  | 					</div> | 
					
						
							|  |  |  | 				</div> | 
					
						
							|  |  |  | 			</div> | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			<div className="form-section-docs"> | 
					
						
							|  |  |  | 				<h3>Basic Information</h3> | 
					
						
							|  |  |  | 				<a | 
					
						
							|  |  |  | 					href="https://docs.gotosocial.org/en/latest/user_guide/settings/#basic-information" | 
					
						
							|  |  |  | 					target="_blank" | 
					
						
							|  |  |  | 					className="docslink" | 
					
						
							|  |  |  | 					rel="noreferrer" | 
					
						
							|  |  |  | 				> | 
					
						
							|  |  |  | 					Learn more about these settings (opens in a new tab) | 
					
						
							|  |  |  | 				</a> | 
					
						
							|  |  |  | 			</div> | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 			<TextInput | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 				field={form.displayName} | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 				label="Display name" | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 				placeholder="A GoToSocial user" | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 			/> | 
					
						
							|  |  |  | 			<TextArea | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 				field={form.note} | 
					
						
							|  |  |  | 				label="Bio" | 
					
						
							|  |  |  | 				placeholder="Just trying out GoToSocial, my pronouns are they/them and I like sloths." | 
					
						
							|  |  |  | 				rows={8} | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 			/> | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 			<b>Profile fields</b> | 
					
						
							|  |  |  | 			<ProfileFields | 
					
						
							|  |  |  | 				field={form.fields} | 
					
						
							|  |  |  | 			/> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			<div className="form-section-docs"> | 
					
						
							|  |  |  | 				<h3>Visibility and privacy</h3> | 
					
						
							|  |  |  | 				<a | 
					
						
							|  |  |  | 					href="https://docs.gotosocial.org/en/latest/user_guide/settings/#visibility-and-privacy" | 
					
						
							|  |  |  | 					target="_blank" | 
					
						
							|  |  |  | 					className="docslink" | 
					
						
							|  |  |  | 					rel="noreferrer" | 
					
						
							|  |  |  | 				> | 
					
						
							|  |  |  | 					Learn more about these settings (opens in a new tab) | 
					
						
							|  |  |  | 				</a> | 
					
						
							|  |  |  | 			</div> | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 			<Checkbox | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 				field={form.locked} | 
					
						
							|  |  |  | 				label="Manually approve follow requests" | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 			/> | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 			<Checkbox | 
					
						
							|  |  |  | 				field={form.discoverable} | 
					
						
							|  |  |  | 				label="Mark account as discoverable by search engines and directories" | 
					
						
							|  |  |  | 			/> | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 			<Checkbox | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 				field={form.enableRSS} | 
					
						
							|  |  |  | 				label="Enable RSS feed of Public posts" | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 			/> | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			<div className="form-section-docs"> | 
					
						
							|  |  |  | 				<h3>Advanced</h3> | 
					
						
							|  |  |  | 				<a | 
					
						
							|  |  |  | 					href="https://docs.gotosocial.org/en/latest/user_guide/settings/#advanced" | 
					
						
							|  |  |  | 					target="_blank" | 
					
						
							|  |  |  | 					className="docslink" | 
					
						
							|  |  |  | 					rel="noreferrer" | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 				> | 
					
						
							| 
									
										
										
										
											2023-09-29 13:01:36 +02:00
										 |  |  | 					Learn more about these settings (opens in a new tab) | 
					
						
							|  |  |  | 				</a> | 
					
						
							|  |  |  | 			</div> | 
					
						
							|  |  |  | 			<TextArea | 
					
						
							|  |  |  | 				field={form.customCSS} | 
					
						
							|  |  |  | 				label="Custom CSS" | 
					
						
							|  |  |  | 				className="monospace" | 
					
						
							|  |  |  | 				rows={8} | 
					
						
							|  |  |  | 				disabled={!instanceConfig.allowCustomCSS} | 
					
						
							|  |  |  | 			/> | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | 			<MutationButton label="Save profile info" result={result} /> | 
					
						
							|  |  |  | 		</form> | 
					
						
							| 
									
										
										
										
											2022-09-29 12:02:41 +02:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2023-06-13 12:21:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function ProfileFields({ field: formField }) { | 
					
						
							|  |  |  | 	return ( | 
					
						
							|  |  |  | 		<div className="fields"> | 
					
						
							|  |  |  | 			<FormContext.Provider value={formField.ctx}> | 
					
						
							|  |  |  | 				{formField.value.map((data, i) => ( | 
					
						
							|  |  |  | 					<Field | 
					
						
							|  |  |  | 						key={i} | 
					
						
							|  |  |  | 						index={i} | 
					
						
							|  |  |  | 						data={data} | 
					
						
							|  |  |  | 					/> | 
					
						
							|  |  |  | 				))} | 
					
						
							|  |  |  | 			</FormContext.Provider> | 
					
						
							|  |  |  | 		</div> | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function Field({ index, data }) { | 
					
						
							|  |  |  | 	const form = useWithFormContext(index, { | 
					
						
							|  |  |  | 		name: useTextInput("name", { defaultValue: data.name }), | 
					
						
							|  |  |  | 		value: useTextInput("value", { defaultValue: data.value }) | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ( | 
					
						
							|  |  |  | 		<div className="entry"> | 
					
						
							|  |  |  | 			<TextInput | 
					
						
							|  |  |  | 				field={form.name} | 
					
						
							|  |  |  | 				placeholder="Name" | 
					
						
							|  |  |  | 			/> | 
					
						
							|  |  |  | 			<TextInput | 
					
						
							|  |  |  | 				field={form.value} | 
					
						
							|  |  |  | 				placeholder="Value" | 
					
						
							|  |  |  | 			/> | 
					
						
							|  |  |  | 		</div> | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2023-01-18 14:45:14 +01:00
										 |  |  | } |