el.xwx.moe/pages/settings/account.tsx

386 lines
12 KiB
TypeScript
Raw Normal View History

2023-10-18 16:50:55 -05:00
import { useState, useEffect } from "react";
import useAccountStore from "@/store/account";
import { AccountSettings } from "@/types/global";
import { toast } from "react-hot-toast";
import SettingsLayout from "@/layouts/SettingsLayout";
import TextInput from "@/components/TextInput";
import { resizeImage } from "@/lib/client/resizeImage";
import ProfilePhoto from "@/components/ProfilePhoto";
import SubmitButton from "@/components/SubmitButton";
import React from "react";
import { MigrationFormat, MigrationRequest } from "@/types/global";
import Link from "next/link";
import Checkbox from "@/components/Checkbox";
2024-01-14 09:09:09 -06:00
import { dropdownTriggerer } from "@/lib/client/utils";
2023-10-18 16:50:55 -05:00
2023-10-18 23:09:28 -05:00
export default function Account() {
2023-10-18 16:50:55 -05:00
const emailEnabled = process.env.NEXT_PUBLIC_EMAIL_PROVIDER;
const [submitLoader, setSubmitLoader] = useState(false);
const { account, updateAccount } = useAccountStore();
const [user, setUser] = useState<AccountSettings>(
!objectIsEmpty(account)
? account
: ({
// @ts-ignore
id: null,
name: "",
username: "",
email: "",
emailVerified: null,
image: "",
2023-10-18 16:50:55 -05:00
isPrivate: true,
// @ts-ignore
createdAt: null,
whitelistedUsers: [],
} as unknown as AccountSettings)
);
function objectIsEmpty(obj: object) {
return Object.keys(obj).length === 0;
}
useEffect(() => {
if (!objectIsEmpty(account)) setUser({ ...account });
}, [account]);
const handleImageUpload = async (e: any) => {
const file: File = e.target.files[0];
const fileExtension = file.name.split(".").pop()?.toLowerCase();
const allowedExtensions = ["png", "jpeg", "jpg"];
if (allowedExtensions.includes(fileExtension as string)) {
const resizedFile = await resizeImage(file);
if (
resizedFile.size < 1048576 // 1048576 Bytes == 1MB
) {
const reader = new FileReader();
reader.onload = () => {
setUser({ ...user, image: reader.result as string });
2023-10-18 16:50:55 -05:00
};
reader.readAsDataURL(resizedFile);
} else {
toast.error("Please select a PNG or JPEG file thats less than 1MB.");
}
} else {
toast.error("Invalid file format.");
}
};
const submit = async () => {
setSubmitLoader(true);
const load = toast.loading("Applying...");
const response = await updateAccount({
...user,
});
toast.dismiss(load);
if (response.ok) {
toast.success("Settings Applied!");
} else toast.error(response.data as string);
setSubmitLoader(false);
};
const importBookmarks = async (e: any, format: MigrationFormat) => {
2023-12-19 16:20:09 -06:00
setSubmitLoader(true);
2023-10-18 16:50:55 -05:00
const file: File = e.target.files[0];
if (file) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = async function (e) {
const load = toast.loading("Importing...");
const request: string = e.target?.result as string;
const body: MigrationRequest = {
format,
data: request,
};
const response = await fetch("/api/v1/migration", {
2023-10-18 16:50:55 -05:00
method: "POST",
body: JSON.stringify(body),
});
const data = await response.json();
toast.dismiss(load);
2023-12-19 16:20:09 -06:00
if (response.ok) {
toast.success("Imported the Bookmarks! Reloading the page...");
setTimeout(() => {
location.reload();
}, 2000);
} else toast.error(data.response as string);
2023-10-18 16:50:55 -05:00
};
reader.onerror = function (e) {
console.log("Error:", e);
};
}
2023-12-19 16:20:09 -06:00
setSubmitLoader(false);
2023-10-18 16:50:55 -05:00
};
const [whitelistedUsersTextbox, setWhiteListedUsersTextbox] = useState("");
useEffect(() => {
setWhiteListedUsersTextbox(account?.whitelistedUsers?.join(", "));
}, [account]);
useEffect(() => {
setUser({
...user,
whitelistedUsers: stringToArray(whitelistedUsersTextbox),
});
}, [whitelistedUsersTextbox]);
const stringToArray = (str: string) => {
const stringWithoutSpaces = str?.replace(/\s+/g, "");
const wordsArray = stringWithoutSpaces?.split(",");
return wordsArray;
};
return (
<SettingsLayout>
2023-11-20 11:48:41 -06:00
<p className="capitalize text-3xl font-thin inline">Account Settings</p>
<div className="divider my-3"></div>
2023-11-20 11:48:41 -06:00
<div className="flex flex-col gap-5">
2023-10-18 16:50:55 -05:00
<div className="grid sm:grid-cols-2 gap-3 auto-rows-auto">
<div className="flex flex-col gap-3">
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Display Name</p>
2023-10-18 16:50:55 -05:00
<TextInput
value={user.name || ""}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2023-10-18 16:50:55 -05:00
onChange={(e) => setUser({ ...user, name: e.target.value })}
/>
</div>
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Username</p>
2023-10-18 16:50:55 -05:00
<TextInput
value={user.username || ""}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2023-10-18 16:50:55 -05:00
onChange={(e) => setUser({ ...user, username: e.target.value })}
/>
</div>
{emailEnabled ? (
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Email</p>
2023-11-02 23:09:50 -05:00
{user.email !== account.email &&
process.env.NEXT_PUBLIC_STRIPE === "true" ? (
2023-11-25 04:39:56 -06:00
<p className="text-neutral mb-2 text-sm">
2023-11-02 23:09:50 -05:00
Updating this field will change your billing email as well
</p>
) : undefined}
2023-10-18 16:50:55 -05:00
<TextInput
value={user.email || ""}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2023-10-18 16:50:55 -05:00
onChange={(e) => setUser({ ...user, email: e.target.value })}
/>
</div>
) : undefined}
</div>
<div className="sm:row-span-2 sm:justify-self-center mx-auto my-3">
2023-11-24 07:39:55 -06:00
<p className="mb-2 text-center">Profile Photo</p>
2023-10-18 16:50:55 -05:00
<div className="w-28 h-28 flex items-center justify-center rounded-full relative">
<ProfilePhoto
priority={true}
src={user.image ? user.image : undefined}
2023-12-17 22:32:33 -06:00
large={true}
2023-10-18 16:50:55 -05:00
/>
{user.image && (
2023-10-18 16:50:55 -05:00
<div
onClick={() =>
setUser({
...user,
image: "",
2023-10-18 16:50:55 -05:00
})
}
2023-12-01 13:00:52 -06:00
className="absolute top-1 left-1 btn btn-xs btn-circle btn-neutral btn-outline bg-base-100"
2023-10-18 16:50:55 -05:00
>
2023-12-17 22:32:33 -06:00
<i className="bi-x"></i>
2023-10-18 16:50:55 -05:00
</div>
)}
<div className="absolute -bottom-3 left-0 right-0 mx-auto w-fit text-center">
<label className="btn btn-xs btn-neutral btn-outline bg-base-100">
2023-10-18 16:50:55 -05:00
Browse...
<input
type="file"
name="photo"
id="upload-photo"
accept=".png, .jpeg, .jpg"
className="hidden"
onChange={handleImageUpload}
/>
</label>
</div>
</div>
</div>
</div>
<div>
<div className="flex items-center gap-2 w-full rounded-md h-8">
2023-11-24 07:39:55 -06:00
<p className="truncate w-full pr-7 text-3xl font-thin">
2023-10-18 16:50:55 -05:00
Import & Export
</p>
</div>
<div className="divider my-3"></div>
2023-10-18 16:50:55 -05:00
<div className="flex gap-3 flex-col">
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Import your data from other platforms.</p>
<div className="dropdown dropdown-bottom">
<div
tabIndex={0}
role="button"
2024-01-14 09:09:09 -06:00
onMouseDown={dropdownTriggerer}
2023-12-07 11:29:45 -06:00
className="flex gap-2 text-sm btn btn-outline btn-neutral group"
2023-10-18 16:50:55 -05:00
id="import-dropdown"
>
2023-12-17 22:32:33 -06:00
<i className="bi-cloud-upload text-xl duration-100"></i>
2023-12-07 11:29:45 -06:00
<p>Import From</p>
</div>
<ul className="shadow menu dropdown-content z-[1] bg-base-200 border border-neutral-content rounded-box mt-1 w-60">
2023-12-01 13:00:52 -06:00
<li>
<label
tabIndex={0}
role="button"
2023-12-01 13:00:52 -06:00
htmlFor="import-linkwarden-file"
title="JSON File"
>
From Linkwarden
<input
type="file"
name="photo"
id="import-linkwarden-file"
accept=".json"
className="hidden"
onChange={(e) =>
importBookmarks(e, MigrationFormat.linkwarden)
}
/>
</label>
</li>
<li>
<label
tabIndex={0}
role="button"
2023-12-01 13:00:52 -06:00
htmlFor="import-html-file"
title="HTML File"
>
From Bookmarks HTML file
<input
type="file"
name="photo"
id="import-html-file"
accept=".html"
className="hidden"
onChange={(e) =>
importBookmarks(e, MigrationFormat.htmlFile)
}
/>
</label>
</li>
</ul>
</div>
2023-10-18 16:50:55 -05:00
</div>
<div>
2023-11-24 07:39:55 -06:00
<p className="mb-2">Download your data instantly.</p>
<Link className="w-fit" href="/api/v1/migration">
2023-12-07 11:29:45 -06:00
<div className="flex w-fit gap-2 text-sm btn btn-outline btn-neutral group">
2023-12-17 22:32:33 -06:00
<i className="bi-cloud-download text-xl duration-100"></i>
2023-12-07 11:29:45 -06:00
<p>Export Data</p>
2023-10-18 16:50:55 -05:00
</div>
</Link>
</div>
</div>
</div>
<div>
<div className="flex items-center gap-2 w-full rounded-md h-8">
2023-11-24 07:39:55 -06:00
<p className="truncate w-full pr-7 text-3xl font-thin">
2023-10-18 16:50:55 -05:00
Profile Visibility
</p>
</div>
<div className="divider my-3"></div>
2023-10-18 16:50:55 -05:00
<Checkbox
label="Make profile private"
state={user.isPrivate}
onClick={() => setUser({ ...user, isPrivate: !user.isPrivate })}
/>
2023-11-25 04:39:56 -06:00
<p className="text-neutral text-sm">
2023-11-24 12:28:47 -06:00
This will limit who can find and add you to new Collections.
2023-10-18 16:50:55 -05:00
</p>
{user.isPrivate && (
<div className="pl-5">
2023-11-24 07:39:55 -06:00
<p className="mt-2">Whitelisted Users</p>
2023-11-25 04:39:56 -06:00
<p className="text-neutral text-sm mb-3">
2023-10-18 16:50:55 -05:00
Please provide the Username of the users you wish to grant
visibility to your profile. Separated by comma.
</p>
<textarea
2023-12-01 13:00:52 -06:00
className="w-full resize-none border rounded-md duration-100 bg-base-200 p-2 outline-none border-neutral-content focus:border-primary"
2023-10-18 16:50:55 -05:00
placeholder="Your profile is hidden from everyone right now..."
value={whitelistedUsersTextbox}
onChange={(e) => setWhiteListedUsersTextbox(e.target.value)}
/>
</div>
)}
</div>
<SubmitButton
onClick={submit}
loading={submitLoader}
label="Save"
className="mt-2 mx-auto lg:mx-0"
/>
<div>
<div className="flex items-center gap-2 w-full rounded-md h-8">
<p className="text-red-500 dark:text-red-500 truncate w-full pr-7 text-3xl font-thin">
Delete Account
</p>
</div>
<div className="divider my-3"></div>
<p>
This will permanently delete ALL the Links, Collections, Tags, and
archived data you own.{" "}
{process.env.NEXT_PUBLIC_STRIPE
? "It will also cancel your subscription. "
: undefined}{" "}
You will be prompted to enter your password before the deletion
process.
</p>
</div>
<Link
href="/settings/delete"
className="mx-auto lg:mx-0 text-white flex items-center gap-2 py-1 px-3 rounded-md text-lg tracking-wide select-none font-semibold duration-100 w-fit bg-red-500 hover:bg-red-400 cursor-pointer"
>
<p className="text-center w-full">Delete Your Account</p>
</Link>
2023-10-18 16:50:55 -05:00
</div>
</SettingsLayout>
);
}