🐛 Debouncing password input on public link manager

This commit is contained in:
Montassar Ghanmy
2024-01-26 09:38:32 +01:00
committed by GitHub
parent 2d78a26051
commit f6b67565b7
2 changed files with 41 additions and 19 deletions
@@ -19,7 +19,14 @@ export const useDriveItem = (id: string) => {
const item = useRecoilValue(DriveItemAtom(id)); const item = useRecoilValue(DriveItemAtom(id));
const children = useRecoilValue(DriveItemChildrenAtom(id)); const children = useRecoilValue(DriveItemChildrenAtom(id));
const [loading, setLoading] = useRecoilState(LoadingStateInitTrue('useDriveItem-' + id)); const [loading, setLoading] = useRecoilState(LoadingStateInitTrue('useDriveItem-' + id));
const { refresh: refreshItem, create, update: _update, updateLevel: _updateLevel, remove: _remove, restore: _restore } = useDriveActions(); const {
refresh: refreshItem,
create,
update: _update,
updateLevel: _updateLevel,
remove: _remove,
restore: _restore,
} = useDriveActions();
const { uploadVersion: _uploadVersion } = useDriveUpload(); const { uploadVersion: _uploadVersion } = useDriveUpload();
const refresh = useCallback( const refresh = useCallback(
@@ -55,8 +62,8 @@ export const useDriveItem = (id: string) => {
}, [id, setLoading, refresh, item?.item?.parent_id]); }, [id, setLoading, refresh, item?.item?.parent_id]);
const update = useCallback( const update = useCallback(
async (update: Partial<DriveItem>) => { async (update: Partial<DriveItem>, skipLoading = false) => {
setLoading(true); if (!skipLoading) setLoading(true);
try { try {
await _update(update, id, item?.item?.parent_id || ''); await _update(update, id, item?.item?.parent_id || '');
} catch (e) { } catch (e) {
@@ -93,8 +100,11 @@ export const useDriveItem = (id: string) => {
[companyId, id, setLoading, refresh, item?.item?.parent_id], [companyId, id, setLoading, refresh, item?.item?.parent_id],
); );
const inTrash = id.includes('trash') || item?.path?.some(i => i?.parent_id?.includes('trash')) || item?.item?.is_in_trash; const inTrash =
const sharedWithMe = id =="shared_with_me"; id.includes('trash') ||
item?.path?.some(i => i?.parent_id?.includes('trash')) ||
item?.item?.is_in_trash;
const sharedWithMe = id == 'shared_with_me';
return { return {
sharedWithMe, sharedWithMe,
@@ -8,19 +8,19 @@ import { Input } from 'app/atoms/input/input-text';
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { AccessLevel } from './common'; import { AccessLevel } from './common';
import moment from 'moment'; import moment from 'moment';
import 'moment/min/locales' import 'moment/min/locales';
import Languages from 'features/global/services/languages-service'; import Languages from 'features/global/services/languages-service';
import { debounce } from 'lodash';
export const PublicLinkManager = ({ id, disabled }: { id: string; disabled?: boolean }) => { export const PublicLinkManager = ({ id, disabled }: { id: string; disabled?: boolean }) => {
const { item, loading, update } = useDriveItem(id); const { item, loading, update } = useDriveItem(id);
const publicLink = getPublicLink(item); const publicLink = getPublicLink(item);
return ( return (
<> <>
<Base className="block mt-2 mb-1">{Languages.t('components.public-link-acess.public_link_acess')}</Base> <Base className="block mt-2 mb-1">
{Languages.t('components.public-link-acess.public_link_acess')}
</Base>
<div className="p-4 rounded-md border"> <div className="p-4 rounded-md border">
<div className="flex flex-row overflow-hidden w-full"> <div className="flex flex-row overflow-hidden w-full">
<div className="grow"> <div className="grow">
@@ -75,7 +75,7 @@ export const PublicLinkManager = ({ id, disabled }: { id: string; disabled?: boo
password: password || '', password: password || '',
}, },
}, },
}); }, true);
}} }}
onChangeExpiration={(expiration: number) => { onChangeExpiration={(expiration: number) => {
update({ update({
@@ -109,10 +109,19 @@ const PublicLinkOptions = (props: {
const handlePasswordBlur = () => { const handlePasswordBlur = () => {
props.onChangePassword(password); props.onChangePassword(password);
}; };
const debouncedOnChangePassword = debounce(passwordValue => {
props.onChangePassword(passwordValue);
}, 500); // 500ms delay
useEffect(() => { useEffect(() => {
props.onChangePassword(usePassword ? password : ''); // Ensure the effect runs only if usePassword or password changes
debouncedOnChangePassword(usePassword ? password : '');
// Cleanup function to cancel the debounced call if the component is unmounted or the dependencies change
return () => {
debouncedOnChangePassword.cancel();
};
}, [usePassword, password]); }, [usePassword, password]);
useEffect(() => { useEffect(() => {
@@ -126,7 +135,9 @@ const PublicLinkOptions = (props: {
return ( return (
<> <>
<Subtitle className="block mt-4 mb-1">{Languages.t('components.public-link-security')}</Subtitle> <Subtitle className="block mt-4 mb-1">
{Languages.t('components.public-link-security')}
</Subtitle>
<div className="flex items-center justify-center w-full h-10"> <div className="flex items-center justify-center w-full h-10">
<Checkbox <Checkbox
disabled={props.disabled} disabled={props.disabled}
@@ -147,15 +158,14 @@ const PublicLinkOptions = (props: {
// saves and copies password // saves and copies password
onClick={() => { onClick={() => {
if (password) copyToClipboard(password); if (password) copyToClipboard(password);
ToasterService.success(Languages.t('components.public-link-security_password_copied')); ToasterService.success(
Languages.t('components.public-link-security_password_copied'),
);
}} }}
/> />
)} )}
</div> </div>
<div className="flex items-center justify-center w-full h-10"> <div className="flex items-center justify-center w-full h-10">
<Checkbox <Checkbox
disabled={props.disabled} disabled={props.disabled}
onChange={s => { onChange={s => {
@@ -166,7 +176,9 @@ const PublicLinkOptions = (props: {
label={Languages.t('components.public-link-security_expired')} label={Languages.t('components.public-link-security_expired')}
/> />
{useExpiration && (expiration || 0) < Date.now() && ( {useExpiration && (expiration || 0) < Date.now() && (
<Info className="ml-2 text-red-500">({Languages.t('components.public-link-security_expired')})</Info> <Info className="ml-2 text-red-500">
({Languages.t('components.public-link-security_expired')})
</Info>
)} )}
{useExpiration && (expiration || 0) > Date.now() && ( {useExpiration && (expiration || 0) > Date.now() && (
<Info className="ml-2">({expirationDate(expiration)})</Info> <Info className="ml-2">({expirationDate(expiration)})</Info>