buster/web/src/controllers/AppPasswordAccess.tsx

100 lines
2.9 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-02-01 06:21:50 +08:00
metricId?: string;
dashboardId?: string;
2025-02-01 02:04:49 +08:00
type: ShareAssetType;
children: React.ReactNode;
2025-02-01 06:21:50 +08:00
}> = React.memo(({ children, metricId, dashboardId, type }) => {
const getAssetPassword = useBusterAssetsContextSelector((state) => state.getAssetPassword);
2025-02-01 06:21:50 +08:00
const { password, error } = getAssetPassword(metricId || dashboardId || '');
if (password && !error) {
return <>{children}</>;
}
return (
<AppPasswordInputComponent
password={password}
error={error}
2025-02-01 06:21:50 +08:00
metricId={metricId}
dashboardId={dashboardId}
/>
);
});
AppPasswordAccess.displayName = 'AppPasswordAccess';
const AppPasswordInputComponent: React.FC<{
password: string | undefined;
error: string | null;
2025-02-01 06:21:50 +08:00
metricId?: string;
dashboardId?: string;
2025-02-01 06:21:50 +08:00
}> = ({ password, error, metricId, dashboardId }) => {
const setAssetPassword = useBusterAssetsContextSelector((state) => state.setAssetPassword);
2025-03-05 02:56:15 +08:00
const inputRef = useRef<HTMLInputElement>(null);
const onEnterPassword = useMemoizedFn((v: string) => {
2025-02-01 06:21:50 +08:00
setAssetPassword(metricId || dashboardId!, v);
});
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"
/>
{error ? (
<Text className="mb-1!" variant="danger">
{error}
</Text>
) : 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>
);
};