Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { useEnvironmentQuery } from "../../state/query";
import { serverEnvironment } from "../../state/server";
import { useEnvironmentsWithScope } from "../../state/session";
import { sourceControlEnvironment } from "../../state/sourceControl";
import { IssueAutomationSection } from "./components/IssueAutomationSection";
import {
IssueTrackerCredentialForm,
IssueTrackerProjectRows,
Expand Down Expand Up @@ -238,6 +239,16 @@ export function SettingsIssueTrackerRouteScreen({
/>
</SettingsSection>
) : null}
{/* Rules read statuses as the tracker's first account. */}
{usable &&
tracker.automationContainer !== undefined &&
route.params.accountId === undefined ? (
<IssueAutomationSection
environmentId={environmentId}
tracker={tracker.tracker}
container={tracker.automationContainer}
/>
) : null}
</>
)}
</DetailPage>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
import { useAtomValue } from "@effect/atom-react";
import type {
EnvironmentId,
IssueAutomationAction,
IssueAutomationRule,
IssueTrackerKind,
StandaloneIssueTrackerDescriptor,
} from "@t3tools/contracts";
import { useGT } from "gt-react-native";
import { useState } from "react";
import { Pressable, View } from "react-native";

import { AppText as Text } from "../../../components/ui/AppText";
import { SymbolView } from "../../../components/ui/AppSymbol";
import { Button } from "../../../components/ui/Button";
import { Chip } from "../../../components/ui/Chip";
import { Menu } from "../../../components/ui/Menu";
import { ThemedSwitch } from "../../../components/ui/ThemedSwitch";
import { uuidv4 } from "../../../lib/uuid";
import { issueTrackerEnvironment } from "../../../state/issueTrackers";
import { useEnvironmentQuery } from "../../../state/query";
import { useAtomCommand } from "../../../state/use-atom-command";
import { SettingsSection } from "./SettingsSection";

type Container = NonNullable<StandaloneIssueTrackerDescriptor["automationContainer"]>;

/**
* Issue status rules for one tracker: each team or board sorts its workflow statuses into Wake,
* Snooze, and Settle, and the server organizes every thread linked to one of its issues.
*/
export function IssueAutomationSection({
environmentId,
tracker,
container,
}: {
readonly environmentId: EnvironmentId;
readonly tracker: IssueTrackerKind;
readonly container: Container;
}) {
const gt = useGT();
const query = useEnvironmentQuery(
issueTrackerEnvironment.automation({ environmentId, input: {} }),
);
const catalog = useEnvironmentQuery(
issueTrackerEnvironment.automationCatalog({ environmentId, input: { tracker } }),
);
const allowed = useAtomValue(
issueTrackerEnvironment.saveAutomation.permissionAtom(environmentId),
);
const [draft, setDraft] = useState<IssueAutomationRule | null>(null);
const rules = (query.data?.rules ?? []).filter((rule) => rule.tracker === tracker);
const available = (catalog.data?.containers ?? []).filter(
(option) => !rules.some((rule) => rule.containerId === option.id),
);
const team = container === "team";
const addLabel = team ? gt("Add team") : gt("Add board");
return (
<SettingsSection
title={team ? gt("Teams") : gt("Boards")}
footer={gt("Wake, snooze, or settle threads when a linked issue's status changes.")}
trailing={
<Menu
title={addLabel}
actions={
available.length > 0
? available.map((option) => ({ id: option.id, title: option.name }))
: [
{
id: "retry",
title: catalog.isPending
? gt("Loading…")
: catalog.error
? gt("Retry")
: team
? gt("Every team already has a rule.")
: gt("Every board already has a rule."),
attributes: { disabled: catalog.error === null },
},
]
}
onPressAction={({ nativeEvent }) => {
const option = available.find((candidate) => candidate.id === nativeEvent.event);
if (option !== undefined && catalog.data !== null)
setDraft({
id: uuidv4(),
enabled: true,
tracker,
workspace: catalog.data.workspace,
containerId: option.id,
containerName: option.name,
mappings: {},
});
else if (nativeEvent.event === "retry") catalog.refresh();
}}
>
<Pressable
accessibilityRole="button"
accessibilityLabel={addLabel}
disabled={!allowed || draft !== null}
className="min-h-9 flex-row items-center gap-1 px-2 active:opacity-70"
>
<SymbolView name="plus" size={14} tintColorClassName="accent-primary-text" />
<Text className="text-sm text-primary-text">{addLabel}</Text>
</Pressable>
</Menu>
}
>
{query.error ? (
<Text accessibilityRole="alert" className="px-4 py-3 text-sm text-danger-foreground">
{query.error}
</Text>
) : null}
{rules.map((rule) => (
<RuleCard
// A save replaces the generation, which resets the card's unsaved edits.
key={`${rule.id}:${rule.generation ?? ""}`}
environmentId={environmentId}
rule={rule}
container={container}
lastReadFailed={
query.data?.status.find((status) => status.ruleId === rule.id)?.error != null
}
/>
))}
{draft ? (
<RuleCard
key={draft.id}
environmentId={environmentId}
rule={draft}
container={container}
lastReadFailed={false}
onDone={() => setDraft(null)}
/>
) : null}
{rules.length === 0 && draft === null && !query.isPending ? (
<Text className="px-4 py-3 text-sm text-foreground-muted">
{team
? gt("Add a team to organize the threads linked to its issues.")
: gt("Add a board to organize the threads linked to its issues.")}
</Text>
) : null}
</SettingsSection>
);
}

function RuleCard({
environmentId,
rule,
container,
lastReadFailed,
onDone,
}: {
readonly environmentId: EnvironmentId;
readonly rule: IssueAutomationRule;
readonly container: Container;
readonly lastReadFailed: boolean;
/** Present for a rule not saved yet; called once it is saved or abandoned. */
readonly onDone?: () => void;
}) {
const gt = useGT();
const [mappings, setMappings] = useState(rule.mappings);
const [enabled, setEnabled] = useState(rule.enabled);
const [busy, setBusy] = useState(false);
const [failure, setFailure] = useState<string | null>(null);
const allowed = useAtomValue(
issueTrackerEnvironment.saveAutomation.permissionAtom(environmentId),
);
const save = useAtomCommand(issueTrackerEnvironment.saveAutomation, {
label: `save ${rule.containerName} status rule`,
});
const workflow = useEnvironmentQuery(
issueTrackerEnvironment.automationCatalog({
environmentId,
input: { tracker: rule.tracker, containerId: rule.containerId },
}),
);
const states = workflow.data?.states ?? [];
const unsorted = states.filter((state) => mappings[state.id] === undefined);
const otherWorkspace =
workflow.data !== null &&
workflow.data.workspace.toLowerCase() !== rule.workspace.toLowerCase();
const dirty =
onDone !== undefined ||
enabled !== rule.enabled ||
Object.keys(mappings).length !== Object.keys(rule.mappings).length ||
Object.entries(mappings).some(([id, action]) => rule.mappings[id] !== action);
const locked = !allowed || busy;
const team = container === "team";
const buckets: ReadonlyArray<{ readonly action: IssueAutomationAction; readonly label: string }> =
[
{ action: "wake", label: gt("Wake") },
{ action: "snooze", label: gt("Snooze") },
{ action: "settle", label: gt("Settle") },
];
const submit = async (remove: boolean) => {
setBusy(true);
setFailure(null);
const result = await save({
environmentId,
input: { id: rule.id, rule: remove ? null : { ...rule, enabled, mappings } },
});
setBusy(false);
if (result._tag === "Success") onDone?.();
else setFailure(gt("The rule could not be saved."));
};
const alert =
failure ??
workflow.error ??
(otherWorkspace
? gt("This rule belongs to another workspace. Add the team or board again to use it here.")
: lastReadFailed
? gt("The last status check failed. Threads were left as they are.")
: null);
return (
<View className="gap-3 border-t border-border-subtle p-3">
<View className="flex-row items-center gap-3">
<Text className="min-w-0 flex-1 text-base font-t3-medium text-foreground">
{rule.containerName}
</Text>
<ThemedSwitch
accessibilityLabel={gt("Enable rule")}
value={enabled}
onValueChange={setEnabled}
disabled={locked}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel={onDone ? gt("Cancel") : team ? gt("Remove team") : gt("Remove board")}
disabled={onDone ? busy : locked}
onPress={() => (onDone ? onDone() : void submit(true))}
className="min-h-9 min-w-9 items-center justify-center active:opacity-70"
>
<SymbolView
name={onDone ? "xmark" : "trash"}
size={16}
tintColorClassName="accent-icon-muted"
/>
</Pressable>
</View>
<Text className="text-xs text-foreground-muted">
{team
? gt("Applies to threads linked to this team's issues, in every project.")
: gt("Applies to threads linked to this board's issues, in every project.")}
</Text>
{buckets.map(({ action, label }) => (
<View key={action} className="gap-1 rounded-xl bg-subtle px-3 py-2">
<View className="flex-row items-center justify-between gap-2">
<Text className="text-sm font-t3-medium text-foreground">{label}</Text>
<Menu
title={gt("Add a status to {bucket}", { bucket: label })}
actions={
unsorted.length > 0
? unsorted.map((state) => ({ id: state.id, title: state.name }))
: [
{
id: "empty",
title: gt("Every status is in a bucket."),
attributes: { disabled: true },
},
]
}
onPressAction={({ nativeEvent }) => {
if (unsorted.some((state) => state.id === nativeEvent.event))
setMappings((current) => ({ ...current, [nativeEvent.event]: action }));
}}
>
<Pressable
accessibilityRole="button"
accessibilityLabel={gt("Add a status to {bucket}", { bucket: label })}
disabled={locked || workflow.data === null || otherWorkspace}
className="min-h-8 min-w-8 items-center justify-center active:opacity-70"
>
<SymbolView name="plus" size={16} tintColorClassName="accent-primary-text" />
</Pressable>
</Menu>
</View>
<View className="min-h-3 flex-row flex-wrap gap-1.5 pb-1">
{Object.entries(mappings)
.filter(([, value]) => value === action)
.map(([id]) => {
const name = states.find((state) => state.id === id)?.name ?? id;
return (
<Chip
key={id}
label={name}
trailingIcon="xmark"
size="small"
disabled={locked}
accessibilityLabel={gt("Remove {status}", { status: name })}
onPress={() =>
setMappings((current) =>
Object.fromEntries(Object.entries(current).filter(([key]) => key !== id)),
)
}
/>
);
})}
</View>
</View>
))}
{alert ? (
<Text accessibilityRole="alert" className="text-xs text-danger-foreground">
{alert}
</Text>
) : null}
{workflow.error ? (
<Button label={gt("Retry")} size="compact" tone="text" onPress={workflow.refresh} />
) : null}
{dirty ? (
<View className="flex-row gap-2">
<Button
label={gt("Save")}
tone="primary"
size="compact"
disabled={locked || (enabled && (workflow.data === null || otherWorkspace))}
onPress={() => void submit(false)}
/>
{onDone ? null : (
<Button
label={gt("Discard")}
size="compact"
disabled={busy}
onPress={() => {
setMappings(rule.mappings);
setEnabled(rule.enabled);
setFailure(null);
}}
/>
)}
</View>
) : null}
</View>
);
}
10 changes: 5 additions & 5 deletions apps/mobile/src/features/threads/threadListV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ export function buildThreadListV2ListItems(input: {
const queuedThreadKeys = input.queuedThreadKeys?.size ? input.queuedThreadKeys : undefined;
const threadItems = input.items.map((item): ThreadListV2ListItem => {
const snoozeWakeLabelText =
item.snoozed && item.thread.snoozedUntil != null && input.snoozeLabelNow !== undefined
item.snoozed && input.snoozeLabelNow !== undefined
? snoozeWakeLabel(item.thread.snoozedUntil, { now: input.snoozeLabelNow }, resolve)
: undefined;
// Put the clock on items so recycler equality limits ticks to rows displaying snooze preset times.
Expand Down Expand Up @@ -865,10 +865,10 @@ export function buildThreadListV2Items(input: {
? sortInboxThreadsByReturn(active, input.inboxReturnAt)
: applyPendingThreadOrder(sortThreadsForListV2(active), "active", pending);
const orderedWorking = sortWorkingThreadsBySend(working);
const orderedSnoozed = [...snoozed].sort(
(left, right) =>
parseTimestampMs(left.snoozedUntil ?? "") - parseTimestampMs(right.snoozedUntil ?? ""),
);
// A snooze with no wake time wakes last.
const wakeOrder = (thread: (typeof snoozed)[number]) =>
thread.snoozedUntil == null ? Number.POSITIVE_INFINITY : parseTimestampMs(thread.snoozedUntil);
const orderedSnoozed = [...snoozed].sort((left, right) => wakeOrder(left) - wakeOrder(right));
const selectedThreadKey = input.selectedThreadKey ?? null;
const visibleWorking =
input.workingShelfExpanded === true
Expand Down
Loading
Loading