summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/transitions.ts
blob: da1a45ac016048540c67129fc5805444f32982ab (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import TransitionRule, { ITransitionDescriptor, ITransitionFork } from './lib/transition-rule';

export interface ITransitionGroupProps {
  name: string;
  duration: number;
}

interface ITransitionMap {
  [name: string]: ITransitionFork;
}

/**
 * Transition descriptors
 */
const transitions: ITransitionMap = {
  slide: {
    forward: {
      name: 'slide-up',
      duration: 450,
    },
    backward: {
      name: 'slide-down',
      duration: 450,
    },
  },
  push: {
    forward: {
      name: 'push',
      duration: 450,
    },
    backward: {
      name: 'pop',
      duration: 450,
    },
  },
};

/**
 * Transition rules
 * (null) is used to indicate any route.
 */
const transitionRules = [
  r('/settings', '/settings/language', transitions.push),
  r('/settings', '/settings/account', transitions.push),
  r('/settings', '/settings/preferences', transitions.push),
  r('/settings', '/settings/advanced', transitions.push),
  r('/settings/advanced', '/settings/advanced/wireguard-keys', transitions.push),
  r('/settings/advanced', '/settings/advanced/linux-split-tunneling', transitions.push),
  r('/settings', '/settings/support', transitions.push),
  r(null, '/settings', transitions.slide),
  r(null, '/select-location', transitions.slide),
];

/**
 * Calculate TransitionGroup props.
 *
 * @param {string} [fromRoute] - source route
 * @param {string} toRoute     - target route
 */
export function getTransitionProps(
  fromRoute: string | null,
  toRoute: string,
): ITransitionGroupProps {
  // ignore initial transition and transition between the same routes
  if (!fromRoute || fromRoute === toRoute) {
    return noTransitionProps();
  }

  for (const rule of transitionRules) {
    const match = rule.match(fromRoute, toRoute);
    if (match) {
      return toTransitionGroupProps(match.descriptor);
    }
  }

  return noTransitionProps();
}

/**
 * Integrate ITransitionDescriptor into ITransitionGroupProps
 * @param {ITransitionDescriptor} descriptor
 */
function toTransitionGroupProps(descriptor: ITransitionDescriptor): ITransitionGroupProps {
  const { name, duration } = descriptor;
  return {
    name,
    duration,
  };
}

/**
 * Returns default props with no animation
 */
function noTransitionProps(): ITransitionGroupProps {
  return {
    name: '',
    duration: 0,
  };
}

/**
 * Shortcut to create TransitionRule
 */
function r(from: string | null, to: string, fork: ITransitionFork): TransitionRule {
  return new TransitionRule(from, to, fork);
}