File size: 2,745 Bytes
dbcdc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import clsx from 'clsx';
import { Plus, Minus } from 'lucide-react';
import { useCallback } from 'react';
import loaderStore from '/src/utils/hooks/loader/useLoaderStore';
import { useOptions } from '/src/utils/optionsContext';

const Zoom = () => {
  const { tabs, zoomLevels, setZoom, resetZoom } = loaderStore();
  const activeFrameRef = loaderStore((state) => state.activeFrameRef);
  const { options } = useOptions();

  const actab = tabs.find((tab) => tab.active);
  const currentz = actab ? zoomLevels[actab.id] || 100 : 100;
  const isNewTab = actab?.url === 'tabs://new';
  const canZoom = actab && !isNewTab && activeFrameRef?.current;

  const zIn = useCallback(() => {
    canZoom && currentz < 200 && setZoom(actab.id, Math.min(currentz + 10, 200), activeFrameRef);
  }, [canZoom, actab, currentz, setZoom, activeFrameRef]);

  const zout = useCallback(() => {
    canZoom && currentz > 50 && setZoom(actab.id, Math.max(currentz - 10, 50), activeFrameRef);
  }, [canZoom, actab, currentz, setZoom, activeFrameRef]);

  const ressetLvl = useCallback(() => {
    canZoom && resetZoom(actab.id, activeFrameRef);
  }, [canZoom, actab, resetZoom, activeFrameRef]);

  return (
    <div className="w-full flex justify-between items-center px-3 py-2">
      <span className={clsx('text-[0.8rem]', !canZoom ? 'opacity-50' : undefined)}>Zoom</span>
      <div className="flex items-center gap-2">
        <button
          type="button"
          onClick={zout}
          disabled={!canZoom || currentz <= 50}
          className={clsx(
            'w-6 h-6 rounded flex items-center justify-center',
            options.type === 'light' ? 'hover:bg-gray-100' : 'hover:bg-[#ffffff0c]',
            !canZoom || currentz <= 50 ? 'opacity-50 cursor-not-allowed' : '',
          )}
        >
          <Minus size={14} />
        </button>
        <button
          type="button"
          onClick={ressetLvl}
          disabled={!canZoom}
          className={clsx(
            'text-[0.75rem] min-w-[3rem] text-center px-2 py-0.5 rounded',
            options.type === 'light' ? 'hover:bg-gray-100' : 'hover:bg-[#ffffff0c]',
            !canZoom ? 'opacity-50 cursor-not-allowed' : '',
          )}
        >
          {currentz}%
        </button>
        <button
          type="button"
          onClick={zIn}
          disabled={!canZoom || currentz >= 200}
          className={clsx(
            'w-6 h-6 rounded flex items-center justify-center',
            options.type === 'light' ? 'hover:bg-gray-100' : 'hover:bg-[#ffffff0c]',
            !canZoom || currentz >= 200 ? 'opacity-50 cursor-not-allowed' : '',
          )}
        >
          <Plus size={14} />
        </button>
      </div>
    </div>
  );
};

export default Zoom;