buster/web/src/controllers/AppPasswordAccess.tsx

95 lines
2.8 KiB
TypeScript
Raw Normal View History

'use client';
import { BusterLogo } from '@/assets/svg/BusterLogo';
import React, { useRef } from 'react';
2025-03-05 02:56:15 +08:00
import type { ShareAssetType } from '@/api/asset_interfaces';
import { useBusterAssetsContextSelector } from '@/context/Assets/BusterAssetsProvider';
2025-03-08 07:02:56 +08:00
import { useMemoizedFn } from '@/hooks';
2025-03-05 02:56:15 +08:00
import { Title, Text } from '@/components/ui/typography';
import { Button } from '@/components/ui/buttons';
import { Input } from '@/components/ui/inputs';
export const AppPasswordAccess: React.FC<{
2025-03-19 03:04:21 +08:00
assetId: string;
2025-02-01 02:04:49 +08:00
type: ShareAssetType;
children: React.ReactNode;
2025-04-11 06:05:25 +08:00
}> = React.memo(({ children, assetId, type }) => {
const getAssetPassword = useBusterAssetsContextSelector((state) => state.getAssetPassword);
2025-03-19 03:04:21 +08:00
const { password, error } = getAssetPassword(assetId);
if (password && !error) {
return <>{children}</>;
}
2025-04-11 06:05:25 +08:00
return (
<AppPasswordInputComponent password={password} error={error} assetId={assetId} type={type} />
);
});
AppPasswordAccess.displayName = 'AppPasswordAccess';
const AppPasswordInputComponent: React.FC<{
password: string | undefined;
error: string | null;
2025-03-19 03:04:21 +08:00
assetId: string;
2025-04-11 06:05:25 +08:00
type: ShareAssetType;
}> = ({ password, error, assetId, type }) => {
const onSetAssetPassword = useBusterAssetsContextSelector((state) => state.onSetAssetPassword);
2025-03-05 02:56:15 +08:00
const inputRef = useRef<HTMLInputElement>(null);
const onEnterPassword = useMemoizedFn((v: string) => {
2025-04-11 06:05:25 +08:00
onSetAssetPassword(assetId, v, type);
});
const onPressEnter = useMemoizedFn((v: React.KeyboardEvent<HTMLInputElement>) => {
onEnterPassword(v.currentTarget.value);
});
const onEnterButtonPress = useMemoizedFn(() => {
2025-03-05 02:56:15 +08:00
const value = inputRef.current?.value;
if (!value) return;
onEnterPassword(value || '');
});
return (
2025-03-05 02:56:15 +08:00
<div
className="flex h-full min-h-[100vh] w-full justify-center"
style={{
marginTop: '25vh'
}}>
2025-03-12 07:01:39 +08:00
<div className="flex max-w-[440px] flex-col items-center space-y-6">
2025-03-05 02:56:15 +08:00
<BusterLogo className="h-16 w-16" />
2025-03-05 02:56:15 +08:00
<div className="text-center">
<Title
as="h2"
className="text-center">{`To access this page, enter the password below`}</Title>
</div>
2025-03-05 02:56:15 +08:00
<div className="flex w-full flex-col space-y-2 space-x-0">
<div className="flex flex-col space-y-1">
<Input
ref={inputRef}
defaultValue={password}
onPressEnter={onPressEnter}
className="w-full"
placeholder="Enter password"
type="password"
2025-04-11 06:05:25 +08:00
autoFocus
2025-03-05 02:56:15 +08:00
/>
2025-04-11 06:05:25 +08:00
{/* {error ? (
2025-03-05 02:56:15 +08:00
<Text className="mb-1!" variant="danger">
{error}
</Text>
2025-04-11 06:05:25 +08:00
) : null} */}
</div>
2025-03-05 02:56:15 +08:00
<Button block variant="black" onClick={onEnterButtonPress}>
Submit
</Button>
</div>
</div>
2025-03-05 02:56:15 +08:00
</div>
);
};