Skip to content

Instantly share code, notes, and snippets.

@mittinatten
Last active December 11, 2020 11:32
Show Gist options
  • Save mittinatten/1ba3ba365eb1efc2c6aaca7038204af9 to your computer and use it in GitHub Desktop.
Save mittinatten/1ba3ba365eb1efc2c6aaca7038204af9 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
function invalidSearchParams(context) {
const numberSourceLabels = context.viewParams?.sourceLabels?.length ?? 0;
return numberSourceLabels === 0;
}
const dialogMachine = Machine({});
const machine = Machine(
{
id: "MonitorState",
context: {
viewParams: { sourceLabels: [ ] }
},
initial: "PageUnknown",
on: {
BROWSER_POP_STATE: {
target: "PageUnknown",
actions: [
assign({
viewParams: (context, event) => event.params,
}),
],
},
BACK_TO_HOME: "StartPage",
},
states: {
PageUnknown: {
always: [{ target: "StartPage", cond: invalidSearchParams }, "ContributionsPage"],
},
StartPage: {
entry: ["resetContext", "pushUrl"],
on: {
SELECT_SOURCE_LABELS: {
target: "ContributionsPage",
actions: ["setSourceLabelsInContext"],
},
},
},
ContributionsPage: {
type: "parallel",
states: {
Dialog: {
initial: "Closed",
states: {
Open: {
invoke: {
id: "dialog",
src: dialogMachine,
onDone: "Closed",
data: {
type: (_, event) => event.type,
selectedContributionIds: (context) =>
context.selectedContributionIds,
},
},
},
Closed: {
on: {
ADD_TO_LIST: "Open",
},
},
},
},
View: {
initial: "ViewUnknown",
states: {
ViewUnknown: {
always: [
{ target: "Detail", cond: (context) => context.viewParams?.view === "Detail" },
{ target: "Compact", cond: (context) => context.viewParams?.view === "Compact" },
{ target: "Grid" },
],
},
Grid: {
entry: ["setGridViewInContext", "pushUrl"],
on: {
SHOW_COMPACT: "Compact",
SHOW_DETAIL: "Detail",
},
},
Compact: {
entry: ["setCompactViewInContext", "pushUrl"],
on: {
SHOW_GRID: "Grid",
SHOW_DETAIL: "Detail",
},
},
Detail: {
initial: "ItemUnknown",
entry: ["setDetailViewInContext", "pushUrl"],
exit: "deselectDetailItem",
states: {
ItemUnknown: {
always: [
{
cond: (context) => Boolean(context.viewParams.item),
target: "ItemKnown",
},
],
on: {
SHOW_DETAIL: [
{
target: "ItemKnown",
cond: (context, event) => Boolean(event.contributionId),
actions: ["selectDetailItem", "replaceUrl"],
},
],
},
},
ItemKnown: {
entry: [
assign({
selectedContributionIds: (context) =>
context.viewParams.item ? [context.viewParams.item] : [],
}),
],
on: {
SHOW_DETAIL: {
target: "ItemKnown",
actions: ["selectDetailItem", "pushUrl"],
},
},
},
},
on: {
SHOW_GRID: "Grid",
SHOW_COMPACT: "Compact",
},
},
},
},
Filter: {
on: {
UPDATE_FILTER: {
actions: ["updateFiltersInContext", "updateUrl"],
},
SELECT_SOURCE_LABELS: {
actions: [
(context, { sourceLabels }) =>
send({
type: "UPDATE_FILTER",
params: { ...context.viewParams, sourceLabels },
}),
],
},
},
},
ContributionAddedSubscription: {
initial: "Listening",
states: {
Listening: {
entry: "startAddedSubscription",
exit: "stopSubscription",
on: {
UPDATE_FILTER: "Listening",
},
},
},
},
ContributionsAddedCount: {
initial: "Idle",
states: {
Idle: {
on: {
CONTRIBUTION_ADDED: "Counting",
},
},
Counting: {
entry: "fetchUpdatedCount",
on: {
CONTRIBUTIONS_ADDED_COUNT_RECEIVED: "Idle",
},
},
},
},
ContributionsCount: {
initial: "Counting",
states: {
Idle: {
on: {
UPDATE_FILTER: "Counting",
},
},
Counting: {
entry: "fetchCount",
on: {
CONTRIBUTIONS_COUNT_RECEIVED: "Idle",
},
},
},
},
Contributions: {
initial: "Fetching",
states: {
Idle: {
on: {
UPDATE_FILTER: "Fetching",
FETCH_CONTRIBUTIONS_FROM_TOP: "FetchingFromTop",
FETCH_MORE_CONTRIBUTIONS: "FetchingMore",
},
},
Fetching: {
entry: "fetchContributions",
on: {
CONTRIBUTIONS_RECEIVED: "Idle",
},
},
FetchingFromTop: {
entry: "fetchFromTop",
on: {
CONTRIBUTIONS_RECEIVED: "Idle",
},
},
FetchingMore: {
entry: "fetchMore",
on: {
CONTRIBUTIONS_RECEIVED: "Idle",
},
},
},
},
},
},
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment