-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathOutputDependenciesTable.tsx
More file actions
109 lines (103 loc) · 3.39 KB
/
Copy pathOutputDependenciesTable.tsx
File metadata and controls
109 lines (103 loc) · 3.39 KB
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
107
108
109
import { ActionIcon, Group, Stack, Table, Text, Tooltip } from '@mantine/core';
import { IconCalculator, IconEye } from '@tabler/icons-react';
import { useMemo } from 'react';
import { Link } from 'react-router-dom';
import { PercentageFormatter } from '@/core/intl/PercentageFormatter';
import { useShallowStore } from '@/core/zustand';
import type { FactoryOutput } from '@/factories/Factory';
export interface IOutputDependenciesTableProps {
factoryId: string;
output: FactoryOutput;
}
export function OutputDependenciesTable(props: IOutputDependenciesTableProps) {
const { factoryId, output } = props;
const factoriesUsingOutput = useShallowStore(state =>
state.games.games[state.games.selected ?? '']?.factoriesIds
.map(id => state.factories.factories[id])
.filter(
factory =>
factory?.progress !== 'disabled' &&
factory?.inputs?.some(
i => i.resource === output.resource && i.factoryId === factoryId,
),
),
);
const dependencies = useMemo(() => {
return factoriesUsingOutput.flatMap(
source =>
source.inputs
.filter(
input =>
input.resource === output.resource &&
input.factoryId === factoryId,
)
.map(input => ({ source, input })) ?? [],
);
}, [factoriesUsingOutput, factoryId, output.resource]);
if (dependencies.length === 0)
return (
<div>
<Stack gap="xs" justify="center" align="center">
No dependencies found.
<Text size="xs" ta="center" c="dimmed">
Add a factory that uses this output as an input.
</Text>
</Stack>
</div>
);
return (
<div>
<Table>
<Table.Thead>
<Table.Tr>
<Table.Th>Source</Table.Th>
<Table.Th>Amount</Table.Th>
<Table.Th>Percentage of output</Table.Th>
</Table.Tr>
</Table.Thead>
{dependencies.map(({ source, input }) => (
<Table.Tr key={source.id}>
<Table.Td>
<Group gap="xs" wrap="nowrap">
<Text size="sm">{source.name}</Text>
<Tooltip label="Open factory" withArrow>
<ActionIcon
component={Link}
to={`/factories/${source.id}`}
size="sm"
variant="filled"
color="blue"
aria-label="Open factory"
>
<IconEye size={14} />
</ActionIcon>
</Tooltip>
<Tooltip label="Open calculator" withArrow>
<ActionIcon
component={Link}
to={`/factories/${source.id}/calculator`}
size="sm"
variant="filled"
color="cyan"
aria-label="Open calculator"
>
<IconCalculator size={14} />
</ActionIcon>
</Tooltip>
</Group>
</Table.Td>
<Table.Td>
{input.amount}
<small>/min</small>
</Table.Td>
<Table.Td>
{PercentageFormatter.format(
(input.amount ?? 0) / (output.amount ?? 1),
)}
</Table.Td>
</Table.Tr>
))}
</Table>
</div>
);
}